Why using CANBus on the robots and how to implement it

Issues and context of the Eurobot ( 2018 )

Many technologies have been implemented on the robots for the 2018 edition of the EUROBOT.

Those technologies were a huge step forward in terms of quality, speed and ease of use.

However, unexpected downsides appeared during the implementation of the technologies on the robots.

Thoses downsides are of all type but those who are speaking about during this tutorial are *electronic and space.

alt text

Electronic problem

Once the boards are installend on the robots, they needed a lot of connections between them. The last picture shows the kind of result we got after connecting all the wires.

From that moment it is very complex to add or remove wires because of the big amount of cables.

It is also very complex for the debugging because it is hard to take measures and to find where the error is.

Space problem

Another problem wich appeared and that we didn't is the huge space taken by the wires.

During the conception the mistake we did is to disregard the size of the wires. In the future, some leeway should be taken on the size of the robot during the conception.

The huge number of wires had also an mechanical impact. It added constraint on the connexions wich often broke.

Le CANBus

Context

Those problems have already been met many years ago in the automotive. Indeed, more and more electronic devises were used in the cars for differents purposes :

  • Engine temperature, air...
  • RPM, speed...
  • accelerometer...
  • Safety, ABS, Airbag, opened doors, seatbelt...

Specifications

We can cleary see that with the number of wires increasing the wiring harness became a real problem.

To resolve this, BOSCH developped the CANBus with several caracteristics :

  • High speed communication up to 1 Megabit/s (see the table below for more information)
  • Real time ( wich makes CANBus better than TCP/IP)
  • Error detection, fast recovery and repair ( stil in real time)
  • Security and stability
  • Priorization of transmission
  • Based on a differiental signal, the CANBus is made for harsh environnements (External noises and fault tolerance)
  • It uses a twisted pair cable wich limits the noise emition

You can see below the speed transmission according to the distances

alt text

Topology

Finally, the biggest benefit of the CANBus is his one bus line topology which limits the number of cables to 4. (CAN HIGH, CAN LOW, VCC and GROUND).

This the reason why it is used on every modern car.

alt text

We can see on the last figure a system without the CANBus (left) and a system with the CANBus (right).

Every component, called a node, is connected to the bus line like the figure below :

alt text

By analogy, the CANBus can be represented by a number of people ( the nodes ) in a room ( the bus line ) where everybody screams his informations. Everyelse has the choice to listen or not. A priority system chooses who is going to speak when several people want to speak at the same time.

The frame

alt text

A data frame is made of different parts :

  • 1 dominant bit begins the frame
  • The ID of the message is made of 11 bits in the standart CAN or 29 bits in the extended CAN.
  • 6 bits which determine the lenght of the frame
  • The data made of 0...8 bytes
  • 15 bits of CRC to detect errors
  • ACK
  • End of frame bit

Priority of transmission

When differents nodes whant to talk at the same time on the bus line, it is the ID who is going to determine the priority.

It gives an important advantage to CANBus, it is possible to make priority of transmission by choosing the right ID. For example, in car the brake system is more important than the light system. The ID of the brake system will be lower ( more 0 ) than the light system.

Implementation

Hardware

alt text

The last figure shows the implementation of the CANBus. The implementation is quite easy because of the modules proposed by Microchip.

A node is made of a transceiver, a CAN controller and a microcontroller.

Transceiver

Microchip proposes a lot of solutions for the automotive including a transceiver.

It is the MCP2562. Formerly the MCP2551, is used to convert the TTL signal to a differential signal required by the CANBus.

Controller

Microchip also proposes the CAN controller MCP2515 which implements all the CAN 2.0 specifications. It is able to send and receive data and communicate it via SPI to a microcontroller.

Microcontroller.

Any microcontroller with an integrated SPI communication can be selected.

For example we can take the famous ATMEGA328p known for his arduino IDE.

Resistance

In every communication wire there is some reflexion that can corrupt the data. It can be avoided by using 120 Ohm resistor at the end of line.

PCB

Coming soon.

Software (Arduino)

There are many libraries that propose functions to interract with the CAN controller via SPI from the micrcontroller.

A very comlete and up to date library is the Seed studio lib : https://github.com/Seeed-Studio/CAN_BUS_Shield

Filters

Filters allow to choose wich messages will be sent to the microcontroller from the CAN controller.

The MCP2515 is able the define 6 filters. For example :

CAN.init_Filt(0, 0, 0x04);                          
CAN.init_Filt(1, 0, 0x05);                          

This code means that the CAN controller will only listen to the messages with an ID of 0x04 and 0x05.

Masks

The masks defines the bits that will be inspected to filter the coming ID. If the bit of the mask is 1, it means that the ID has to respect the bit of the filter.

For example with the following filter :

0100 1101

If the mask is :

1111 1111

Then it means that the accepted ID has to have exactly the same bits thant the filter because all the masks bits are 1. Only a message witch the next ID will be accepted :

0100 1101

Now, if the mask is this one :

1111 1110

It means that the filter will not inspect the last bit of the ID.

The next ID's will be accpeted

0100 1100
0100 1101

Usefull links