CAN Interface Examples?

I’m in search of examples of how to write an interface to a device on the CAN bus.

We would like to build an interface to our Raspberry Pi using CAN.

I’m digging through the WPILib code and any other examples I can get my hands on.

Any suggestions or pointers would be great!

Here’s marshall’s list of stuff from another CAN thread.

Thank you for the references. I’ll read closer, but I’m looking for source code examples. I found some of the code in wpilib.

I’ll dig more to workout how the code works for these different devices.

Generally, you don’t want to communicate with your coprocessor via CAN, for a few reasons.

The Raspberry Pi doesn’t natively support CAN, in fact, most processors don’t. CAN is generally a pain to bit-bang, so you’re going to want to use some extra circuitry to control and communicate with the CAN bus. This, combined with the lower speed and lower bandwidth, makes CAN less desirable over something more like ethernet.

If, however, you still want to pursue the project, you will have to use “HAL/CAN.h” header, or the “” file. These contain the message sending, receiving and (for c++) the packing / unpacking of data.


We’re using a card on the Pi to interface with CAN.

Thank you for the references! This is very helpful.

We’re programming in C++. How does come into play?

I was looking at CtreCanNode.h as base class, as that seems common for many of the CAN interfaces already available (PCM, PDP, CANTalon) is the CAN.h equivalent for the Java language, if you’re coding in C++ you won’t need it.

CtreCanNode.h should function just fine for your needs

I’m going to second the caution that CAN may not be what you’re looking for. Ethernet is very likely more robust and easier than CAN. I’d recommend taking a critical look at what your use case is and if there’s an easier or better way to achive it.

That being said, if you do decide to go with CAN, people would be able to help more if they knew which card you were using - there may be specific resources or advice for it.

I’m quite baffled by all of the caution around CAN. Why is there so much concern? We run the entire robot on CAN now.

I find the project interesting so far.

I’d like to understand why there would be support for a particular CAN interface. I would have expected CAN to be well defined by the standard.

Looking forward to learn more.

CAN does have some quarks (as well as some nice parts). You should make sure that you understand the tradeoffs before you settle on an interface.

I prefer ethernet/TCP (or UPD), because it’s more common, easier to debug (some CAN issues can be difficult to debug without some hardware designed for it), and has more throughput.

I’m not saying don’t use CAN - just make sure that you have solid reasoning as to why you’re doing it.

CAN is a very well defined format (Well, pretty well defined. There are actually a few different versions of CAN out there, but you mostly will never see the less-standard ones), but the way to interface with a microprocessor that implements CAN can vary depending on the microprocessor.

I was under the impression that you were asking about writing the Pi-side code. If that’s not the case, then the chip that you’re using is not very important. Sorry if you’re asking about the roborio side of the interface - if that’s the case then I’ve misunderstood your question.

The one that you linked to uses some GPIO pins on the Pi to control the CAN controller and transceiver. It looks like it does this over SPI, but I’m not entirely sure about that. They provide example C code which should be fairly easy to adapt to C++. The links that I provide below should actually apply for both the Pi-side and Rio side as well.

Regarding the RoboRIO side of the interface, there’s been some good advice about looking at WPILib. I would recommend looking at the linux kernel docs for SocketCAN (The Wikipedia page is also good, and has some example code). Looking at the can-utils source could also be helpful. This is lower-level than most of what’s in WPILib. I find that that makes it easier to understand, but YMMV.

Thank you for your help. We are building both sides of the interface. In this case, I was looking for references for the RoboRio and WPILib.

Looks like I have some good references to go through.

Your help is appreciated!