# CarTwin--Development of a Digital Twin for a Real-World In-Vehicle CAN Network
## 4 System Level Implementation Based on Simulink Models  
In this section, we describe the Simulink models, which implement the functionalities of the seven ECUs from our network. Based on these models, we generated the C code, which was flashed on the controllers to obtain the corresponding functionalities. Design and Validation of Models  
In Figure 5, we show an overview of the system model and the signal flow between the ECUs. In what follows, we discuss the Simulink model for each ECU, i.e., PSCM, ABS, PCM, IPC, RCM, APIM and RFA. Additionally, in this figure, we depict the RestBus simulation tool from which the models acquire their inputs (marked with orange), i.e., direction, brake command, buckle status, steering wheel and door-lock button. A Restbus  
Figure 4: Wire and stub lengths for the in-vehicle high-speed CAN bus from vehicle harnesses. Figure 3: Schematic view of the in-vehicle high-speed CAN bus that we use for the digital twin. simulation is generally used to provide the inputs required to validate ECU functionalities ([[https://www.ni.com/ro-ro/innovations/white-papers/12/the-fundamentals-of-restbus-simulation.html](https://www.ni.com/ro-ro/innovations/white-papers/12/the-fundamentals-of-restbus-simulation.html)]([https://www.ni.com/ro-ro/innovations/white-papers/12/the-fundamentals-of-restbus-simulation.html](https://www.ni.com/ro-ro/innovations/white-papers/12/the-fundamentals-of-restbus-simulation.html)), accessed on 26 November 2022). The vehicle speed signal (marked with green) is computed by the ABS ECU and used by other ECUs, e.g., PSCM, PCM. 1. **Power Steering Control Module (PSCM):** In Figure 6, we illustrate the implementation of the steering controller, which computes the steering command based on the steering wheel angle from the driver and the vehicle speed. To implement the steering controller, we use the Mapped Steering block from the Simulink library. This block computes the left and right wheel angles based on the steering wheel angle and vehicle speed using interpolation tables. Using the right wheel angle, we then compute the vehicle trajectory, i.e., \(x\), \(y\), as shown in Equations (1) and (2) and rotation angle, i.e., \(\psi\), as depicted in Equation (3): \[x=\int\cos\psi\times v\;dt\] (1) \[y=\int\sin\psi\times v\;dt\] (2) \[\psi=\int\tan\left(AngR\right)\times\frac{v}{3}\;dt\] (3)  
Figure 5: Overview of the Simulink model including the seven Electronic Control Units (ECUs). In Equation (3), _AngR_ is the right wheel angle, while in Equations (1) to (3), \(v\) is the vehicle speed. In the same equation, vehicle speed is divided by 3 in the calculation of \(\psi\) because the distance between the axle wheels is considered of 3 m.
2. **Anti-lock Brake System (ABS):** In Figure 7i, we model the estimation of the speed for each wheel based on vehicle speed and brake information (when the brake is pressed, the speed of each wheel is decreased using an integral controller). In Figure 7 ii, we depict the calculation of the brake command for the front-left wheel. In Figure 7 iii, we depict the calculation of the vehicle speed after braking using a proportional controller, which uses as a limit the preset vehicle speed target when the ABS is not braking on any wheel or zero when the ABS is braking on at least one wheel. The ABS ECU computes the slip for each wheel as shown in Equation (4): \[s=\frac{v_{x}-v}{v}\] (4) In this equation, \(s\) is the slip of a wheel, \(v_{x}\) is the speed of the wheel and \(v\) is the vehicle speed. The state of the brake command is computed based on the slip of the wheel and vehicle speed, i.e., _apply_ (input valve is open and output valve is closed, the pressure goes to the wheel), _hold_ (wheel is locked, input valve is closed to prevent more pressure to the wheel) and _release_ (the output valve is open, the pressure is released and the wheel can rotate). The state of the brake commands is used to control the valves, i.e., to open or close the valves. The same functionally is implemented on all vehicle wheels. 3.