主要内容

Configure Model for XCP on CAN External Mode

R2026b

Use XCP on CAN external mode to monitor signals and tune parameters in a Simulink® model deployed on Arduino® 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 Arduino hardware, and monitor and tune parameters in real time over CAN.

Prerequisites

Before you begin, make sure:

Example Model

In this example, you create a simple model that generates a sine wave, applies a tunable gain, and outputs the results to a PWM pin on Arduino. 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 1. The PWM block outputs to GPIO Pin 5 on Arduino Due board.

  1. Open Simulink and create a blank model.

  2. Add and connect these blocks:

    • Sine Wave — Generates the input signal

    • Gain — Tunable parameter for real-time adjustment

    • PWM — Outputs the signal to a GPIO pin

  3. Configure the Sine Wave block.

    • Set amplitude to 1.

    • Set frequency to 2*pi.

    • Set sample time to 0.01 seconds.

  4. Configure the Gain block.

    • Set gain to 1.

  5. Configure the PWM block.

    • Set pin number to 5 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 selection).

    • 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 block.

  2. On the Simulink Toolstrip, on the Simulation tab, 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

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

    Set Hardware board to any one of these supported Arduino boards:

    • Arduino Due

    • Arduino Uno R4 Wi-Fi®

    • Arduino Uno R4 Minima

    • Arduino Nano R4

    • Teensy 4.0 (Arduino compatible)

    • Teensy 4.1 (Arduino compatible)

Configure CAN Baud Rate on Target

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

  2. Set the CAN Bus Speed (kBits/s) to match the baud rate configured in the third-party calibration tool. 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. Navigate to Hardware board settings>Target hardware resources>Host-board connection>External mode over CAN and 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 module.

    • CAN module — Select the On-board CAN controller module for XCP on CAN.

  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 — If you need to override the automatic polling time, set to Manually.

    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 Arduino, 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. To verify the CAN hardware is detected, run canChannelList (Vehicle Network Toolbox) in the MATLAB Command Window.

  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:

See Also

(Vehicle Network Toolbox) | | | | |