The final Simulink diagram showcases three main steps from left to right:
- CAN message reception and decoding.
- Control algorithm with signal conversion.
- CAN message encoding and transmission.
The CAN blocks in steps 1 and 2 are provided by the Vehicle Network Toolbox, requiring a CAN interface to communicate with the Rover’s CAN bus. We employ a Kvaser USBcan 2, although any device compatible with Simulink should suffice. This interface, along with the toolbox, enables sending, receiving, encoding, and decoding CAN messages within Simulink. Consequently, we can extract values from a CAN message and utilize them as input in our Simulink model, then use the model’s output to transmit a CAN message to the servo board.
In this example, we use two messages:
-
ID 0x30D, containing the actual servo position in degrees.
-
ID 0x100, where we send the output pulse width to the servo board.
The Vehicle Network Toolbox provides Simulink blocks facilitating the filtering of these messages and their decoding/encoding. The CAN configuration block configures the Kvaser CAN interface with the correct bitrate of 125 kbit/s. For message reception, the CAN Receive block connects to a CAN Unpack block to decode signals for Simulink use. For transmission, the CAN Pack block encodes signals in a CAN message, and the CAN Transmit block sends the message on the bus. These blocks are configurable with CAN Database (DBC) files, allowing message selection from a drop-down list, where we utilize the Rover’s DBC file.
The servo control logic sits between the CAN reception and transmission logic. We generate a reference signal for controlling the servo via a sine wave generator, moving the servo from left to right by 45 degrees in each direction with a frequency of 1.5 rad/s. To visualize the sine wave, we send it to a scope block named “Signal viewer” along with the position feedback obtained from the CAN bus. This way we can gauge the control system’s response. We calculate the error in degrees by subtracting the position signal from the reference signal, converting it to a pulse width in microseconds before inputting it to the PID controller.
The PID controller’s output signal is limited to a pulse width between -1000 and 1000 microseconds. Since digital servos operate within a pulse width range of 1000 to 2000, with 1500 representing a neutral position, we add an offset of 1500 microseconds to the PID controller’s output before sending the signal to the CAN message encoder. The PID controller is configured in discrete-time mode, and we obtain its parameters by manual tuning: P=0.1, I=3.9, and D=0.005.
To run the simulation on real hardware, initial setup of simulation pacing parameters is crucial. Refer to the below images for these settings.