# CanBus\_OpenMV\_H7

**URL:** https://forums.openmv.io/t/canbus-openmv-h7/5921
**Category:** OpenMV Boards
**Created:** [May 23, 2021, 6:46pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921 "2021-05-23T18:46:22Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [May 23, 2021, 6:46pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/1 "2021-05-23T18:46:22Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [May 24, 2021, 1:18am UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/2 "2021-05-24T01:18:29Z")

</div>

@iabdalkader

> <https://github.com/openmv/micropython/blob/80a3c5cdd6d14c35433c055074af0c2351944c1a/ports/stm32/pyb_can.c#L176>

Is not working…

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [December 17, 2021, 1:20pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/3 "2021-12-17T13:20:30Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [December 17, 2021, 6:18pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/4 "2021-12-17T18:18:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [December 17, 2021, 6:37pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/5 "2021-12-17T18:37:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [March 14, 2022, 7:09am UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/6 "2022-03-14T07:09:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [March 14, 2022, 10:28am UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/7 "2022-03-14T10:28:43Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [March 15, 2022, 12:57pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/8 "2022-03-15T12:57:42Z")

</div>

Please format your code.

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [March 15, 2022, 7:02pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/9 "2022-03-15T19:02:44Z")

</div>

```auto
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())

```

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [March 17, 2022, 1:59pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/10 "2022-03-17T13:59:02Z")

</div>

Formated it!

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [March 19, 2022, 9:18pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/11 "2022-03-19T21:18:09Z")

</div>

Can you verify the filter you set is actually working?

---

<div class="post-metadata">

### Author: ![vasja](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vasja](https://forums.openmv.io/u/vasja)
#### Post date: [May 11, 2022, 11:27am UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/12 "2022-05-11T11:27:44Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)
#### Post date: [May 12, 2022, 4:16pm UTC](https://forums.openmv.io/t/canbus-openmv-h7/5921/13 "2022-05-12T16:16:49Z")

</div>

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