socketcan_dune_implementation

This is an old revision of the document!


SocketCAN DUNE implementation

CANbus support was not present in DUNE, and therefore had to be added. Since the Raspberry Pi on the Otter is using Linux, we can use the SocketCAN package, which comes with the Linux kernel by default.

Many DUNE tasks use an interface defined by IO::Handle, and so does this CAN implementation. Apart from the setup, using CAN thus becomes identical to using Serial or TCP in DUNE. This also enables use of the Poll::poll function, and makes it possible to only read the SocketCAN buffer when there is something in it. Reading it without checking will result in the task waiting until a CAN message is received. An example of reading with the DUNE socketCAN implementation is:

if (Poll::poll(*m_can, 0.01)) {
 m_can->readString(m_can_bfr, sizeof(m_can_bfr));  // Read from CAN bus
 id = m_can->getRXID();  //Address of sender
}

Here, m_can_bfr is a char array that is used to store the data read from CAN. Writing to the CAN bus can be done like this:

m_can_bfr[0] = 0x4f;
m_can_bfr[1] = 0x74;
m_can_bfr[2] = 0x74;
m_can_bfr[3] = 0x65;
m_can_bfr[4] = 0x72;

m_can->setTXID(0x0000feab);
m_can->write(m_can_bfr, 5);

Here, m_can_bfr still is a char array. Thats it!

  • socketcan_dune_implementation.1570805799.txt.gz
  • Last modified: 2022/09/19 11:33
  • (external edit)