CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

OpenMV related project discussion.
uzair mazhar awan
Posts: 7
Joined: Tue Jul 18, 2017 4:27 am

CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Postby uzair mazhar awan » Tue Aug 01, 2017 3:26 am

Hey!
I have to do CAN communication between OpenMV and AT90CAN128. Is there anyone who can help me out. Any sample code or built in commands.
User avatar
kwagyeman
Posts: 1175
Joined: Sun May 24, 2015 2:10 pm

Re: CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Postby kwagyeman » Tue Aug 01, 2017 12:33 pm

@Ibrahim? Was CAN enabled?
Nyamekye,
User avatar
iabdalkader
Posts: 363
Joined: Sun May 24, 2015 3:53 pm

Re: CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Postby iabdalkader » Thu Aug 03, 2017 8:47 pm

Yes CAN was enabled in 2.2, never tested though. We don't have examples but you can use MicroPython's examples.
Apollo22
Posts: 9
Joined: Fri Aug 11, 2017 5:14 am

Re: CAN communication between OPenMV M7 and AT90CAN128 (Atmel family)

Postby Apollo22 » Fri Aug 11, 2017 5:30 am

Here's an exemple working (don't forget to add a transceiver on an external PCB or protoboard).

Init

Code: Select all

import pyb

can = pyb.CAN(2)                        #The bus 2(PB12, PB13) is the only can bus on the Open MVCam

can.init(pyb.CAN.NORMAL, extframe=False, prescaler=18, sjw=1, bs1=8, bs2=3) #250kb/s
# APB1 = 54MHz, Prescaler 18 et 250kB/s --> 12 time quanta par bit (1 + 8 + 3 = 12)
can.setfilter(0, pyb.CAN.LIST16, 0, [0x700, 0x700, 0x701, 0x702], rtr = [0, 1, 0, 0])
Emission

Code: Select all

data = b'\x01\x50'
can.send(data, id = 0x702, timeout=0, rtr=False)
Be careful if you want to convert int to bytearray, the functions to convert from and to byte are set in little endian, even if you specify big in the function

Reception

Code: Select all

 while(can.any(0)):
        message = can.recv(0)
        # print("can : message received") # [Debug]
        if(message[0] == 0x701):
            manual_launch           = False
            cam_has_control         = False
            stop_linetracking       = False
            mother_error            = False
            if(message[3] == b'\x00'):
                cam_has_control     = True
            elif(message[3] == b'\x01'):
                stop_linetracking   = True
            elif(message[3] == b'\x02'):
                mother_error        = True
            elif(message[3] == b'\x03'):
                manual_launch       = True
        elif(message[0] == 0x700):
            if(message[1]):		#RTR bit active
                print('id sent')
            if(message[3] == b'\x01'):
                print('detected')
Be careful on the reception, you can't use interruption because can.recv() allocates memory, which is not currently allowed in an interruption.

Return to “Project Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest