Program your first simulation.

Turn the new project into a small working simulation. The exact example, code and verification screenshots will be developed in this chapter.

  • Introduce the smallest useful simulation example.
  • Show how to verify that the program is running.

Simulation walkthrough

Use the maintained `pyCANdo/example_rbs_simple` project in code-server. Both CAN channels load the same DBC, and the Python simulation sends the database-backed `Control_01` message every second.

  1. Review the second CAN channel

    Open `config/can2.toml`. The example assigns PCAN hardware channel 2 to `CAN2` and loads `rbs_simple.dbc`, which makes messages such as `Control_01` available by name.

    code-server showing the copied and updated config/can2.toml Channel 2 definition
    Copy can2.toml
  2. Send Control_01 every second

    Open `rbs/main.py`. The simulation resolves `CAN2.Control_01` from the DBC, starts a one-second timer automatically and sends the message whenever the timer elapses. The message callback prints the raw `Activate` signal value when the frame is received.

    code-server showing the one-second Control_01 sender in rbs/main.py
    Copy main.py
  3. Change a signal on CAN1 and receive it on CAN2

    Open `rbs/examples/signal/main.py`. This extended example leaves the default `rbs/main.py` untouched. With CAN1 and CAN2 connected to the same CAN bus, it accesses the DBC-backed `Target_Speed` signal on CAN1, changes its physical rpm value, sends `Control_01`, and reads the received signal from the same message on CAN2.

    code-server showing the CAN1 to CAN2 Target_Speed signal example
    Copy main.py
  4. Create a log file

    Open `rbs/examples/logging/main.py`. This example creates a timestamped MF4 trace in the project's `default` trace folder. It registers a pyRBS `BusLogger` for all available CAN channels, starts recording when the simulation starts, and stops the logger during shutdown so the log file is exported correctly.

    code-server showing the timestamped MF4 bus logging example
    Copy main.py