logo

OPENMV的CAN 通讯初始化问题

2021-04-14 17:23
M
_5ayx

历程上说CAN的初始化有两种办法。

第一种:can = CAN(2, CAN.NORMAL, baudrate=250_000, sampling_point=75

第二种:can.init(pyb.CAN.NORMAL, prescaler=16, sjw=2, bs1=7, bs2=3) #250kb/s

但是我用第一种方法进行初始化后可以正常通讯,用第二种办法的时候无法进行通讯,想问问是怎么回事呢?(因为第一种办法没办法设置波特率为800kBps,所以我需要第二种方法自己设置参数使得波特率为800

# 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 pyb
import time, omv
from pyb import CAN

# NOTE: Set to False on receiving node.
TRANSMITTER = False
#can = pyb.CAN(2) 
can = CAN(2, CAN.NORMAL, baudrate=250_000, sampling_point=75)
#can.init(pyb.CAN.NORMAL, prescaler=16, sjw=2, bs1=7, bs2=3) #250kb/s
# NOTE: uncomment to set bit timing manually, for example:
#can.init(CAN.NORMAL,prescaler=18, sjw=1, bs1=8, bs2=3)
can.restart()

if (TRANSMITTER):
    while (True):
        # Send message with id 1
        can.send('Hello', 1)
        time.sleep_ms(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, 1638))
    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.any(0))
        message =can.recv(0, timeout=10000)
        messagetex = message[3]
        if (messagetex[0] == 0x01 ):
            print(123)
        else:
            print(message)
       # print(can.any(0))

)

M
_5ayx
70993d54-aa0e-4f66-988c-8c0fecec9749.png

用第二种办法没有办法通讯的

2021-04-14 19:06
I
网红小智智 星瞳科技

can = CAN(2, CAN.NORMAL, baudrate=250_000, sampling_point=75

baudrate直接改为800_000不行吗?

M
_5ayx
a45919f5-4c74-4223-b2b3-4c3f16993b94.png
2021-04-14 19:17
I
网红小智智 星瞳科技

你用的是什么硬件?我没报错。

2021-04-14 19:24
M
_5ayx
52cb889f-8fff-4d50-a384-9e9964a8e263.jpg
M
_5ayx

@kidswong999 openmv4连接了一个CAN收发器,接到can转USB上

2021-04-15 12:59
I
网红小智智 星瞳科技

我使用OpenMV4 固件版本3.9.4也不会报错。