This sample runs a CiA 402 drive, node 6 at 500 kbit/s, on the board’s CAN bus, with a simulated motor: its velocity follows the target with a 200 ms time constant while the drive is operation enabled, coasts down once disabled and ramps down faster on a quick stop, which it then reports done. The model is the control loop: it runs on its own every millisecond, in the tick interrupt, and shares only atomics with the node, so it never waits for the stack. In profile velocity, the write callback hands it a new target as soon as it is written, and the main thread reports its velocity every 10 ms. In cyclic synchronous velocity, the SYNC callback hands it the target RPDO 2 applies and reports its velocity, which TPDO 2 then sends with the statusword. TPDO 1 sends the statusword and the mode on every change.
The board needs a transceiver on its CAN pins and a CANopen master on the
bus, terminated at both ends. master.py makes a host with a CAN adapter that
master: it resets and starts the node, enables the drive in cyclic synchronous
velocity with SYNC at 100 Hz, checks that the velocity follows two targets,
quick stops it, then enables it in profile velocity and waits for the target
to be reached. It needs python-can and
canopen, with the interface and channel of
the adapter and the EDS file the build generates:
pip install python-can canopen
samples/subsys/canopen/drive/master.py --interface socketcan --channel can0 \
--eds build/canopen/od.eds
pbl samples samples/subsys/canopen/drive
every board
device:
type: 0x00020192
name: PebbleOS CANopen drive sample
product_code: 2
revision: 1
heartbeat:
producer: 100
cia402:
modes: [pv, csv]
rpdos:
- mapping: [controlword, modes_of_operation]
# Applied on SYNC
- transmission: 1
mapping: [target_velocity]
tpdos:
- transmission: 254
inhibit: 10
mapping: [statusword, modes_of_operation_display]
# Sampled on SYNC
- transmission: 1
mapping: [statusword, velocity_actual_value]
#include "od.h"
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pbl/canopen/canopen.h>
#include <pbl/canopen/cia402.h>
#include <pbl/drivers/can.h>
#include <pbl/kernel/thread.h>
#include <pbl/kernel/time.h>
#include <pbl/kernel/timer.h>
#define NODE_ID 6U
#define BITRATE 500000U
/* Control loop period, and how often the main thread reports what it does */
#define LOOP_PERIOD_MS 1
#define REPORT_PERIOD_MS 10
/* Time constant of the simulated motor, and of its quick stop ramp */
#define TAU_MS 200
#define QS_TAU_MS 50
/* Target reached within this many units per second */
#define WINDOW 10
/* Fractional bits of the model's velocity */
#define Q 256
enum command {
CMD_COAST,
CMD_RUN,
CMD_QUICK_STOP,
};
static struct pbl_canopen s_co;
static struct pbl_cia402 s_drive;
static struct pbl_timer s_loop;
/*
* What the node and the control loop share. The loop never waits for the node: it only loads and
* stores lock-free atomics, so it may as well run in a zero-latency interrupt.
*/
static atomic_int_least32_t s_target;
static atomic_int_least32_t s_velocity;
static atomic_uint_least8_t s_command;
/* The control loop, the simulated motor here, in the tick interrupt. */
static void prv_loop(struct pbl_timer *timer) {
static int32_t velocity_q;
enum command command = atomic_load(&s_command);
int32_t target = command == CMD_RUN ? atomic_load(&s_target) : 0;
int32_t tau = command == CMD_QUICK_STOP ? QS_TAU_MS : TAU_MS;
int32_t step;
pbl_timer_start(timer, PBL_MSEC(LOOP_PERIOD_MS));
step = (target * Q - velocity_q) * LOOP_PERIOD_MS / tau;
velocity_q = step == 0 ? target * Q : velocity_q + step;
atomic_store(&s_velocity, velocity_q / Q);
}
/* Report the loop's velocity and what follows from it. */
static void prv_report(void) {
int32_t velocity = atomic_load(&s_velocity);
uint8_t command = atomic_load(&s_command);
int32_t target = command == CMD_RUN ? atomic_load(&s_target) : 0;
(void)pbl_canopen_od_write(&s_co, OD_VELOCITY_ACTUAL_VALUE, 0U, &velocity, sizeof(velocity));
pbl_cia402_set_status(&s_drive, PBL_CIA402_SW_TARGET_REACHED,
abs(target - velocity) <= WINDOW ? PBL_CIA402_SW_TARGET_REACHED : 0U);
if (command == CMD_QUICK_STOP && velocity == 0 &&
atomic_compare_exchange_strong(&s_command, &command, CMD_COAST)) {
pbl_cia402_quick_stop_done(&s_drive);
}
}
static int prv_switch_on(struct pbl_cia402 *drive, void *arg) {
printf("drive: power stage on\n");
return 0;
}
static void prv_switch_off(struct pbl_cia402 *drive, void *arg) {
printf("drive: power stage off\n");
}
static int prv_enable(struct pbl_cia402 *drive, void *arg) {
int32_t target = 0;
printf("drive: enabled\n");
(void)pbl_canopen_od_read(&s_co, OD_TARGET_VELOCITY, 0U, &target, sizeof(target));
atomic_store(&s_target, target);
atomic_store(&s_command, CMD_RUN);
return 0;
}
/* The motor coasts to a stop. */
static void prv_disable(struct pbl_cia402 *drive, void *arg) {
printf("drive: disabled\n");
atomic_store(&s_command, CMD_COAST);
}
/* The loop ramps the motor down, and prv_report() tells the drive once it stands still. */
static void prv_quick_stop(struct pbl_cia402 *drive, void *arg) {
printf("drive: quick stop\n");
atomic_store(&s_command, CMD_QUICK_STOP);
}
static int prv_set_mode(struct pbl_cia402 *drive, enum pbl_cia402_mode mode, void *arg) {
printf("drive: mode %d\n", mode);
return 0;
}
static const struct pbl_cia402_ops s_ops = {
.switch_on = prv_switch_on,
.switch_off = prv_switch_off,
.enable = prv_enable,
.disable = prv_disable,
.quick_stop = prv_quick_stop,
.set_mode = prv_set_mode,
};
/* Profile velocity: a new target goes to the loop as soon as it is written. */
static int prv_write(struct pbl_canopen *co, uint16_t index, uint8_t subindex, const void *data,
size_t len, void *arg) {
if (index == OD_TARGET_VELOCITY &&
pbl_cia402_mode(&s_drive) == PBL_CIA402_MODE_PROFILE_VELOCITY) {
int32_t target;
memcpy(&target, data, sizeof(target));
atomic_store(&s_target, target);
}
return 0;
}
/*
* Cyclic synchronous velocity: the target RPDO 2 applied goes to the loop, and TPDO 2 samples the
* velocity reported here.
*/
static void prv_sync(struct pbl_canopen *co, uint8_t counter, uint32_t cycles, void *arg) {
int32_t target;
if (pbl_cia402_mode(&s_drive) != PBL_CIA402_MODE_CYCLIC_VELOCITY) {
return;
}
if (pbl_canopen_od_read(&s_co, OD_TARGET_VELOCITY, 0U, &target, sizeof(target)) ==
(int)sizeof(target)) {
atomic_store(&s_target, target);
}
prv_report();
}
static void prv_nmt(struct pbl_canopen *co, enum pbl_canopen_nmt_state state, void *arg) {
printf("drive: NMT %02x\n", state);
}
static const struct pbl_canopen_callbacks s_cb = {
.nmt = prv_nmt,
.write = prv_write,
.sync = prv_sync,
};
int main(void) {
const struct pbl_canopen_config cfg = {
.can = CAN_BUS,
.od = &od,
.node_id = NODE_ID,
.bitrate = BITRATE,
.cb = &s_cb,
};
int ret;
ret = pbl_canopen_init(&s_co, &cfg);
if (ret != 0) {
fprintf(stderr, "drive: node init failed (%d)\n", ret);
return ret;
}
ret = pbl_cia402_init(&s_drive, &s_co, &s_ops, nullptr);
if (ret != 0) {
fprintf(stderr, "drive: drive init failed (%d)\n", ret);
return ret;
}
ret = pbl_canopen_start(&s_co);
if (ret != 0) {
fprintf(stderr, "drive: start failed (%d)\n", ret);
return ret;
}
printf("drive: node %u at %u bit/s\n", NODE_ID, BITRATE);
pbl_timer_init(&s_loop, prv_loop);
pbl_timer_start(&s_loop, PBL_MSEC(LOOP_PERIOD_MS));
for (;;) {
pbl_thread_sleep(PBL_MSEC(REPORT_PERIOD_MS));
prv_report();
}
return 0;
}
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 Core Devices LLC
# SPDX-License-Identifier: Apache-2.0
"""CANopen master for the drive sample.
Resets the drive, starts it, runs it in cyclic synchronous velocity mode with
SYNC and a target velocity in RPDO 2, quick stops it, then runs it in profile
velocity mode and disables it.
"""
import argparse
import sys
import time
import canopen
NODE_ID = 6
SW_STATE = 0x6F
SW_SWITCH_ON_DISABLED = 0x40
SW_OPERATION_ENABLED = 0x27
SW_TARGET_REACHED = 1 << 10
MODE_PV = 3
MODE_CSV = 9
def wait_status(node, mask, value, timeout=2):
end = time.monotonic() + timeout
while time.monotonic() < end:
sw = node.sdo["statusword"].raw
if sw & mask == value:
return sw
time.sleep(0.02)
sys.exit(f"statusword 0x{sw:04x}, expected 0x{value:04x} in 0x{mask:04x}")
def enable(node, mode):
node.sdo["modes_of_operation"].raw = mode
node.sdo["controlword"].raw = 0x06
node.sdo["controlword"].raw = 0x0F
wait_status(node, SW_STATE, SW_OPERATION_ENABLED)
print(f"mode {node.sdo['modes_of_operation_display'].raw}: operation enabled")
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--interface", required=True, help="python-can interface")
parser.add_argument("--channel", required=True, help="python-can channel")
parser.add_argument("--bitrate", type=int, default=500000)
parser.add_argument("--eds", required=True, help="od.eds from the build directory")
parser.add_argument("--sync", type=float, default=100, help="SYNC rate in Hz")
args = parser.parse_args()
network = canopen.Network()
network.connect(
interface=args.interface, channel=args.channel, bitrate=args.bitrate
)
node = canopen.RemoteNode(NODE_ID, args.eds)
network.add_node(node)
try:
node.nmt.state = "RESET"
node.nmt.wait_for_bootup(5)
node.tpdo.read()
node.rpdo.read()
node.nmt.state = "OPERATIONAL"
wait_status(node, SW_STATE, SW_SWITCH_ON_DISABLED)
network.sync.start(1 / args.sync)
enable(node, MODE_CSV)
for target in (1000, -500):
node.rpdo[2]["target_velocity"].raw = target
node.rpdo[2].start(1 / args.sync)
time.sleep(1.5)
node.tpdo[2].wait_for_reception(1)
actual = node.tpdo[2]["velocity_actual_value"].raw
print(f"target {target}: actual {actual}")
if abs(actual - target) > 10:
sys.exit("velocity not reached")
node.rpdo[2].stop()
node.sdo["controlword"].raw = 0x0B
wait_status(node, SW_STATE, SW_SWITCH_ON_DISABLED)
print(f"quick stop: actual {node.sdo['velocity_actual_value'].raw}")
enable(node, MODE_PV)
node.sdo["target_velocity"].raw = 2000
wait_status(node, SW_TARGET_REACHED, SW_TARGET_REACHED, timeout=3)
print(f"target reached: actual {node.sdo['velocity_actual_value'].raw}")
node.sdo["controlword"].raw = 0x06
network.sync.stop()
node.nmt.state = "PRE-OPERATIONAL"
print("done")
finally:
network.disconnect()
if __name__ == "__main__":
main()
drive: node 6 at 500000 bit/s
Generated from PebbleOS 7c542d7a9. This page is maintained in the pebbleos repository: docs/samples/subsys/canopen/drive.md.