CanBus_OpenMV_H7

Hello, dear collegues!
I use the OpenMV H7 board and want to work with CAN.
I try to work with the CAN example that is in examples menu but it does not work.

Then I try to write my prog and my first command is:
import time, omv
from pyb import CAN

I try to compile it and there is a message – “ImportError: cannot import name CAN”.

Where is mistake?
Can you help me with some small Tx/Rx example?

@iabdalkader

Is not working…

Now I work with the canbus, and its working in the Lopback mode:

import sensor, image, time, pyb, json, omv
from pyb import CAN
clock = time.clock()
can = CAN(2, CAN.LOOPBACK)
can.setfilter(0, CAN.RANGE, 0, (1, 4))

while(True):
clock.tick()
can.send(‘mege!’, 2)
time.sleep_ms(100)
print(can.recv(0))

Now I try to work in the Normal mode, but there is some problem.
Could you help me to to find the problem in my code, that is attached bellow?

import sensor, image, time, pyb, json, omv
from pyb import CAN
clock = time.clock()
#canbus communication config
can = pyb.CAN(2, CAN.NORMAL)
can.init(pyb.CAN.NORMAL, extframe=False, prescaler=18, sjw=1, bs1=8, bs2=3) #250kb/s

while(True):
clock.tick()
can.send(15, 0x123,timeout=1000, rtr=False) # send a message with id 123
time.sleep_ms(100)

Do you have a phy attached and another device sending data?

Yes, I have STM32 based controller, I try to send data from h7 to the controller and back. I use iso1050dub can transceiver.

Is somwhere the code example where the CAN bus with callback is used? The problem is: monitor what is in the CAN bus but not in the main program.

import sensor, image, time, array, pyb, omv
from pyb import LED
from pyb import CAN
sensor.reset()

sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 1000)
clock = time.clock()
can = CAN(2, CAN.NORMAL, baudrate=125_000, sample_point=75)
can.restart()
FLAG_Work = 100
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
ir_led = LED(4)
i=0
int_array1 = array.array(‘i’, [1, 2, 3])
int_array2 = array.array(‘i’, [1, 2, 3])

def cb0(bus, reason):
print(‘cb0’)
if reason == 0:
print(‘pending’)
FLAG_Work = 200
if reason == 1:
FLAG_Work = 200
print(‘full’)
if reason == 2:
print(‘overflow’)
FLAG_Work = 200
can.setfilter(0, CAN.RANGE, 0, (10, 30))
can.rxcallback(0, cb0)
while(True):
clock.tick()
img = sensor.snapshot()
print(can.rxcallback(0, fun))
#can.rxcallback(0, cb0)
if FLAG_Work == 100:
for c in img.find_circles(threshold = 3400, x_margin = 5, y_margin = 10, r_margin = 10, r_min = 6, r_max = 15, r_step = 2):
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
if i < len(int_array1):
int_array1[i] = c.x()
int_array2[i] = c.y()
i=i+1
#pyb.delay(500)
else:
i = 0
#print(c)
i = 0
print(“FPS %f” % clock.fps())

My code is here but callback does not work. I think that I do something wrong but i do not know where is mistake… Please, help with this problem…

Please format your code.

import sensor, image, time, array, pyb, omv
from pyb import LED
from pyb import CAN
sensor.reset()

sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 1000)
clock = time.clock()
can = CAN(2, CAN.NORMAL, baudrate=125_000, sample_point=75)
can.restart()
FLAG_Work = 100
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
ir_led = LED(4)
i=0
int_array1 = array.array(‘i’, [1, 2, 3])
int_array2 = array.array(‘i’, [1, 2, 3])

def cb0(bus, reason):
    print(‘cb0’)
    if reason == 0:
        print(‘pending’)
        FLAG_Work = 200
    if reason == 1:
        FLAG_Work = 200
        print(‘full’)
    if reason == 2:
        print(‘overflow’)
        FLAG_Work = 200
can.setfilter(0, CAN.RANGE, 0, (10, 30))
can.rxcallback(0, cb0)
while(True):
    clock.tick()
    img = sensor.snapshot()
    print(can.rxcallback(0, fun))
    #can.rxcallback(0, cb0)
    if FLAG_Work == 100:
        for c in img.find_circles(threshold = 3400, x_margin = 5, y_margin = 10, r_margin = 10, r_min = 6, r_max = 15, r_step = 2):
            img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
            if i < len(int_array1):
                int_array1[i] = c.x()
                int_array2[i] = c.y()
                i=i+1
                #pyb.delay(500)
                else:
                i = 0
                #print(c)
                i = 0
                print(“FPS %f” % clock.fps())

Formated it!

Can you verify the filter you set is actually working?

Now it works but when it is connected through the openmv ide. when I disconnect it, it does not work (supply is good). May be I forgot some part of code? The code is uploaded as main.py.

import sensor, image, time, array, pyb, omv
from pyb import CAN
sensor.reset()

sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 1000)
clock = time.clock()

can = CAN(2, CAN.NORMAL, baudrate=125_000, sample_point=75)

can.setfilter(0, CAN.RANGE, 0, (1, 3))
can.restart()
FLAG_Work = 200
FLAG_BIGMARK = 0
i=0
int_array1    = array.array('i', [1, 2, 3])
int_array2    = array.array('i', [1, 2, 3])
x_coordinates = array.array('i', [1, 2, 3])
y_coordinates = array.array('i', [1, 2, 3])
rmin = 6

while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(strength = 1, zoom = 1.0)
    while(can.any(0)):
        message = can.recv(0)
        print(message[0])
        if(message[0] == 0x1):
            FLAG_Work = 100
            print('YES')
        if(message[0] == 0x3):
            FLAG_Work = 200
            print('NOT')

    if FLAG_Work == 100:
        for c in img.find_circles(threshold = 3400, x_margin = 5, y_margin = 10, r_margin = 10, r_min = rmin, r_max = 20, r_step = 2):
            img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
            if c.r() > 10:
                rmin = 12
                x_coordinates[0] = 250
                x_coordinates[1] = c.x()
                x_coordinates[2] = 250
                y_coordinates[0] = 240
                y_coordinates[1] = c.y()
                y_coordinates[2] = 240
                FLAG_BIGMARK = 100
            else:
                if i < 3:
                    int_array1[i] = c.x()
                    int_array2[i] = c.y()
                    i=i+1
                    FLAG_BIGMARK = 200
                else:
                    i = 0
            i = 0
            if FLAG_BIGMARK == 200:
                x_coordinates = sorted(int_array1)
                y_coordinates = sorted(int_array2)
            can.send(bytes(x_coordinates+y_coordinates), 0x112)
            int_array1[0]=255
            int_array1[1]=255
            int_array1[2]=255
            int_array2[0]=254
            int_array2[1]=254
            int_array2[2]=254

Did you save the script to the OpenMV Cam and then safety eject the device?