Main Content

Configure Model for XCP on CAN External Mode

R2026b

The XCP on CAN external mode enables you to monitor signals and tune parameters in a Simulink® model deployed on Raspberry Pi® using a CAN bus connection. With Simulink as the host interface, you can use the Monitor & Tune workflow to interact with the running model directly from Simulink. Use this workflow to prepare signals and parameters, deploy the model to Raspberry Pi hardware, and monitor and tune parameters in real time over CAN.

Prerequisites

Before you begin, make sure

Example Model

This example uses a simple model that generates a sine wave, applies a tunable gain, and outputs the results to a PWM pin on Raspberry Pi. You can monitor the sine wave signal and tune the gain parameter in real time over XCP on CAN.

Simulink model with a Sine Wave block connected to a Gain block, which connects to a PWM block. The Sine Wave block generates a 1 Hz signal with amplitude 1. The Gain block has a tunable parameter K set to 0.5. The PWM block outputs to GPIO Pin 18 on Raspberry Pi.

  1. Open Simulink model and create a blank model.

  2. Add the following blocks and connect them:

    • Sine Wave (Simulink) — Generates the input signal.

    • Gain (Simulink) — Tunable parameter for real-time adjustment.

    • PWM — Outputs the signal to a GPIO pin.

  3. Configure the Sine Wave (Simulink) block.

    • Set amplitude to 1.

    • Set frequency to 2*pi.

    • Set sample time to 0.01 seconds.

  4. Configure the Gain (Simulink) block.

    • Set gain to 0.5.

  5. Configure the PWM block.

    • Set pin number to 18 or any other available GPIO pin.

  6. In the Configuration Parameters dialog box, configure these parameters in the model solver:

    • Set Type to Fixed-step.

    • Set Solver to auto (Automatic solver selction).

    • Set Fixed-step size to 0.01.

Enable Signal Logging

Enable logging for the signals you want to monitor during external mode simulation.

  1. In the Simulink model, select the signal line you want to monitor (for example, the output of the Sine Wave (Simulink) block).

  2. On the Simulation tab of the model, click Log Signals. A logging badge appears on the signal line, indicating the signal will be captured during external mode execution.

  3. Repeat for any additional signals you want to monitor.

Select Hardware

  1. In the Configuration Parameters dialog box, select Hardware Implementation.

  2. Set Hardware board to Raspberry Pi (64bit).

Configure CAN Baud Rate on Target

  1. In the Configuration Parameters dialog box, navigate to Hardware board settings>Target hardware resources>CAN.

  2. Set the Bus speed (kbps) to match the baud rate configured on the host CAN hardware. The host and target CAN interfaces must use the same baud rate. A baud rate mismatch prevents communication.

Configure External Mode Communication

  1. In the Configuration Parameters dialog box, navigate to Hardware board settings>Target hardware resources>External mode.

  2. Set Communication interface to XCP on CAN.

  3. Set Host interface to Simulink.

  4. Configure the host-side CAN hardware parameters.

    1. CAN Vendor — Select the vendor of the CAN hardware connected to the host computer (Vector, PEAK-System, or Kvaser). This list is populated from the CAN hardware detected by the canChannelList (Vehicle Network Toolbox) function.

    2. CAN Device — Select the CAN device from the list of detected devices for the selected vendor.

    3. CAN Channel — Select the CAN channel on the selected device.

  5. Configure the target-side CAN parameter.

  6. Configure CAN identifier parameters.

    1. Extended CAN ID — Select if your CAN network uses extended 29-bit identifiers instead of standard 11-bit identifiers.

    2. CAN ID command — CAN identifier for command messages from host to target.

    3. CAN ID response — CAN identifier for response messages from target to host.

  7. Configure polling time.

    1. Set XCP target polling time — Set to Manually if you need to override the automatic polling time.

    2. Polling time (in seconds) — Specify a custom polling time.

  8. Click Apply.

Deploy Model and Monitor Signals

  1. On the Hardware tab of the model, click Monitor & Tune. Simulink builds the model, deploys it to Raspberry Pi, and establishes the XCP on CAN connection.

  2. Once the connection is established, open Simulation Data Inspector to view logged signals in real time. Modify tunable parameters directly in the block dialog or MATLAB® Command Window to observe the effect on the deployed model.

  3. To stop the external mode session, on the Hardware tab, click Stop.

Troubleshooting

Connection timeout in external mode

  1. Run canChannelList (Vehicle Network Toolbox) in the MATLAB Command Window to verify the CAN hardware is detected.

  2. If the hardware is not listed, see the Troubleshooting steps.

  3. Close and reopen the Configuration Parameters dialog box after connecting new hardware.

Insufficient logging buffer memory

If you encounter a logging buffer memory overflow error:

Data logging gaps or dropped packets

  • If model execution consumes most of the available CPU time, the external mode task may not receive sufficient execution time. To run the external mode task in a separate thread, select Run external mode in a background.

See Also

| (Vehicle Network Toolbox) | | | |

Topics