openmv/scripts/examples/02-Board-Control/can.py
2019-11-01 17:34:47 +01:00

43 lines
1.3 KiB
Python

# CAN Shield Example
#
# This example demonstrates CAN communications between two cameras.
# NOTE: you need two CAN transceiver shields and DB9 cable to run this example.
import time, omv
from pyb import CAN
# NOTE: Set to False on receiving node.
TRANSMITTER = True
can = CAN(2, CAN.NORMAL)
# Set a different baudrate (default is 125Kbps)
# NOTE: The following parameters are for the H7 only.
#
# can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3) # 125Kbps
# can.init(CAN.NORMAL, prescaler=16, sjw=1, bs1=8, bs2=3) # 250Kbps
# can.init(CAN.NORMAL, prescaler=8, sjw=1, bs1=8, bs2=3) # 500Kbps
# can.init(CAN.NORMAL, prescaler=4, sjw=1, bs1=8, bs2=3) # 1000Kbps
can.restart()
if (TRANSMITTER):
while (True):
# Send message with id 1
can.send('Hello', 1)
time.sleep(1000)
else:
# Runs on the receiving node.
if (omv.board_type() == 'H7'): # FDCAN
# Set a filter to receive messages with id=1 -> 4
# Filter index, mode (RANGE, DUAL or MASK), FIFO (0 or 1), params
can.setfilter(0, CAN.RANGE, 0, (1, 4))
else:
# Set a filter to receive messages with id=1, 2, 3 and 4
# Filter index, mode (LIST16, etc..), FIFO (0 or 1), params
can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
while (True):
# Receive messages on FIFO 0
print(can.recv(0, timeout=10000))