Repository navigation
Getting started
From pip install to a drive you can see on the bus, in a few minutes. Tested with
canopen-sim 0.1.1.
Python 3.10 or newer:
pip install canopen-sim
canopen-sim --version # canopen-sim 0.1.1
canopen-sim list # generic-drive, generic-encoder, generic-io| bus | where | who can see the frames |
|---|---|---|
virtual (default) |
Linux, Windows, macOS | only the process that runs the bench: pytest, a Bench in Python |
socketcan on vcan0
|
Linux | every process on the machine: candump, your master, a second canopen-sim
|
So for pytest you need nothing else. To talk to the simulator from another program, use SocketCAN.
Create the virtual interface once per boot (needs root):
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0Serve a drive on node 3 and watch the traffic in a second terminal (candump is in the
can-utils package):
$ canopen-sim serve --profile generic-drive --id 3 --interface socketcan --channel vcan0
generic-drive id 3 generic-drive vcan0
control 127.0.0.1:47100
$ candump vcan0
vcan0 703 [1] 00 # boot-up
vcan0 703 [1] 7F # heartbeat: pre-operationalNow any CANopen master on vcan0 can talk to node 3. With the
canopen library:
import canopen
network = canopen.Network()
network.connect(interface="socketcan", channel="vcan0")
drive = network.add_node(3, "drive.eds") # canopen-sim export-eds generic-drive --id 3 -o drive.eds
print(drive.sdo["Manufacturer device name"].raw)# bench.yaml
bus: {interface: socketcan, channel: vcan0}
nodes:
- {profile: generic-drive, id: 3, name: axis, heartbeat_ms: 500}
- {profile: generic-io, id: 5, name: io}
control: {port: 47100} # optional; always 127.0.0.1canopen-sim serve --bench bench.yaml--interface and --channel on the command line override the bus: of the file.
While serve runs, a second terminal talks to it over the control channel:
$ canopen-sim state
node id profile nmt_state faults errors
axis 3 generic-drive pre_operational - -
io 5 generic-io pre_operational - -
$ canopen-sim inject over_temperature --node axis
$ canopen-sim state
node id profile nmt_state faults errors
axis 3 generic-drive pre_operational over_temperature 0x4310
io 5 generic-io pre_operational - -
$ canopen-sim state --node axis
node id profile nmt_state faults errors
axis 3 generic-drive pre_operational over_temperature 0x4310
$ canopen-sim clear --node axisstate --json gives the same as JSON. inject and clear need --node as soon as a
bench has more than one node; for state it is optional and shows only that node (by
name or ID; since 0.1.1). The control channel has no authentication and binds to 127.0.0.1 only;
keep it that way. Which faults exist and what to do with them: Fault injection recipes.
There is no SocketCAN, so serve runs on the virtual bus and prints a note that other
processes cannot reach it. Use the simulator from Python or pytest instead
(see Testing in CI); that is the same bus the test suite uses on these systems.
canopen-sim · MIT licensed · CANopen® and CiA® are registered trademarks of CAN in Automation (CiA) e.V.; canopen-sim is not certified by or affiliated with CiA.