CAN Bus Arduino communication

Hey guys,
I am trying to send data to my arduino over the CAN bus. Unfortunately I am not receiving any messages. According to the can stats most of the transmissions time out. I also ran a loopback test, which failed. Does anybody know why this is happening?

Here are the CAN logs:
can0_stats.txt (201 Bytes)
can_log.txt (342 Bytes)

and here are my CAN Parameters:

I am running out of ideas what the problem might be, so any help or input is appreciated:)
Thank you for your help!

Can you show snippet of Arduino code for the CAN?
I think that type 10 is for Lua Scripting within the Flight Controller
Like this example:

This is my lua script:
can_send_pos.lua (1.6 KB)

and this is (most of) the arduino script:

So I am using a lua script to send data to my arduino

I dont think that mcp2515 library is directly compatible, you need a dronecan (uavcan) implementation

https://ardupilot.org/dev/docs/can-bus.html

Oh okay, thank you very much.

Maybe you could try with CANTester as it is lower level

DroneCAN is not required when using the Lua bindings. You can (and must) process individual frames at a fairly low level when developing a CAN driver with ArduPilot Lua scripting. For example, I have a CANOpen driver for some industrial motor controllers that works quite well and is completely independent of DroneCAN. Unfortunately, I can’t share the source for that here.

Assuming the wiring is correct, the CAN_write example should send a valid frame that can be read/parsed by a connected Arduino device. I think the mcp2515 library should be sufficient for that.

ardupilot/CAN_write.lua at master · ArduPilot/ardupilot · GitHub

Your own code includes an extremely short timeout of 100 microseconds. The example uses 10000 microseconds - perhaps the message is timing out every time. I’m not sure how CAN message calls from Lua are scheduled, but I do know that scripting is prioritized fairly low in the grand scheme of things, and scripting runtimes are often on the order of milliseconds as a result. So, a timeout of under 1ms is extremely brief in ArduPilot scripting terms.

1 Like

Thank you for your hint. I increased the timeout, but it still does’nt work. I also tried the example script, but when I use CAN:get_device(20) (like in the example script) instead of CAN.get_device(20) I get the following error: “CAN:get_device expected 1 arguments got 2”.

Since the loopback test fails, I think it might be a wiring issue, but i really don’t know what could be wrong with only 3 wires ( High <-> High, Low <-> Low, GND <-> GND). The arduino is powered by my computer.

The “colon” notation is a sort of syntax sugar, where the colon referenced method receives a “self” object as the first argument to approximate object oriented behavior.

The master branch has a PR to enforce consistency in using the colon syntax across most, if not all bindings. 4.3 and earlier may encounter errors with some of the recent examples, as you discovered. Your fix is correct for the branch on which you are working.

Failing physical access to your hardware and an oscilloscope, it’s hard to diagnose the problem.

Hey, just wanted to let you know that everything is working now. The problem was a missing termination resistor and a different clock speed on the Arduino. Thank you for your help.

Best regards
Bjarne

3 Likes

Lol , funny sometimes how we overlook the basic stuff :slight_smile:

3 Likes

Hi guys - how send frame with standard format not extended with lua script?