# CarTwin--Development of a Digital Twin for a Real-World In-Vehicle CAN Network
## 4 System Level Implementation Based on Simulink Models
In this way, the CAN bus is terminated with 2 \(\times\) 120 \(\Omega\) resistors between CANH and CANL. In order to use the CAN network from the vehicle wiring harness, we cut the CAN wires before the original vehicle connectors and soldered 2.54 mm headers on them. The headers were connected to the 2.54 mm male connectors, which were available on each development board as a CAN bus connector. The experimental setup that contains the in-vehicle ECU models and the VN5610A connected to the wiring harness is shown in Figure 13 ii. We now describe the software deployment for creating the digital twin on the automotive-grade controllers. For generating the Simulink models as C code, we used the Simulink Embedded Coder feature from MATLAB ([[https://www.mathworks.com/help/ecoder/](https://www.mathworks.com/help/ecoder/)]([https://www.mathworks.com/help/ecoder/](https://www.mathworks.com/help/ecoder/)), accessed on 26 November 2022). This feature allows the generation of C and C++ code using specific tool settings that we detail as follows. The settings we configured for each model are: (1) fixed step solver type with discrete states so we could configure the step time according to the task execution time on the embedded device, (2) step size was set to 20 milliseconds since the task cycle time configured in the AURIX software is of 20 ms and (3) device vendor was set to \"Infineon\" and device type set to \"TriCore\" so the variable types and endianes are generated as C code according to the hardware target capabilities. We had to perform one more step in Simulink for each model before we could generate and integrate the code on the embedded hardware target. Where there were any continuous-time blocks, we had to replace them with discrete-time blocks with the same functionality, e.g., continuous integrator blocks with discrete integrator blocks that use the configured step time. Figure 3:6 AURIX node and experimental setup for CarTwin. After the model was generated as C code, we integrated it in the embedded project from AURIX studio that we configured for the Infineon AURIX TC275 microcontroller. As model execution steps inside the software project we start with the initialization functions, i.e., in order to initialize the local variables and data structures according to the model settings. Then, we execute the model step function every 20 milliseconds to consume the input data received from CAN bus, i.e., from the tool or from other models. After executing the step function, the outputs from the model are transmitted as CAN bus signals that can be split into multiple CAN frames depending on the content. The ABS twin computes the valve status and slip for each wheel (front left, front right, rear left, rear right) and transmits the calculated vehicle speed on CAN based on the braking status information. The powertrain twin computes the vehicle acceleration, engine torque, etc., and transmits the engine speed and gear position on the bus. The power-steering twin computes and provides the steering offset of the vehicle considering the steering wheel angle value and also sends the X and Y position relative to the vehicle starting point. The restraint control module provides the airbag status, taking into account the buckle status received from the vehicle bus. The instrument panel cluster provides as outputs on the CAN bus the average vehicle speed and trip distance based on the received vehicle speed values and the buckle alert using the buckle status from the CAN tool frame. The remote function actuator modifies and transmits the door lock status, taking into account if the door lock button is pressed using a debounce time of 1 s. The accessory protocol interface module will activate the rear-view camera if the vehicle is driven backwards and will constantly provide the rear-camera video status on the vehicle bus.