2 Setting up the CAN Bus module - PolytechAngersMecatroniqueClub/Tutorials GitHub Wiki
All modules of the IstiaBot communicate using a CAN network. Here are the steps to configure the CAN network of the Raspberry.
First you need to configure the interfaces file. To do that, we will use the nano terminal text editor
$ sudo nano /etc/network/interfaces
the password is “tobi”, and you should have this screen:
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
# The loopback network interface
auto lo
iface lo inet loopback
modify this file to have
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
# The loopback network interface
auto lo
iface lo inet loopback
auto can0
iface can0 inet manual
pre-up /sbin/ip link set can0 type can bitrate 500000
up /sbin/ifconfig can0 up
down /sbin/ifconfig can0 down
to exit and save the modification use: the modification, use
Ctrl + X > Y > enter
You can use the command
$ cat /etc/network/interfaces
to check if the modification has been done (the file content should be displayed).
To edit the config.txt file, use the command:
$ sudo nano /boot/config.txt
I should display a “big” file containing the settings of your raspberry. Go to the end of the file and add:
# Additional overlays and parameters are documented /boot/overlays/README
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi1-1cs
Ctrl+X > Y > Enter, to exit and save the modification.
By doing the previous modification, the Raspberry CAN interface should be OK.
Once this is done, reboot the raspberry so the new configuration can be loaded:
$ sudo reboot
To check if the CAN interface is enable enter the command
$ ifconfig
and you should have a line can0 in the result, as
$ ifconfig
can0 Link encap:UNSPEC HWaddr ...
-00
UP RUNNING NAORP ...
RX packets: ...
TX packets: ...
…
if you do not have the can0 interface, check
- the interfaces file
- the config.txt file
- your CAN bus network
It can happen that the can interface fails. To reset the interface without rebooting the Pi, use the command
$ sudo ip link set can0 down && sudo ip link set can0 up
To test CAN communication you can install the can-utils software by doing:
$ sudo apt-get update
$ sudo apt-get install can-utils
This should install the software, otherwise, check your internet connection.
Can utils software provide at least two useful commands: cansend and candump. You can use them to check if the CAN network is OK. Note: be sure to have a CAN network, you have to connect at least 2 CAN components together to have a CAN network. If you only have the raspberry and its CAN shield, it will not work!
First you can use candump command to see what is happening over the CAN bus. Start a new Terminal and use the commands
$ candump can0
This should display nothing more. Do not close this terminal for the next command!
Open a new Terminal and use cansend to send a CAN message by doing
$ cansend can0 123#45
This does nothing to this terminal but if you check the terminal with the candump command (hopping that you did not closed it…) you should have:
$ candump can0
can0 123 [1] 45
Which means that over the can0 interface (your CAN network), a message occurred with the identifier 123 (hexa value) and 1 byte of data with the value 45 (hexa).
To use cansend, here is the syntax: <can_id>#{R|data}. Note that each value correspond to an hexa value. If I want to send a data message with the identifier 0xC0 and the data 0x785, I use the command:
$ cansend can0 0C0#0785
Note that the identifier should be three digits (for standard frame) or 8 digits for extended format message. The data should be an even number of digit between 0 and 16 digits. To send a remote request, you have to add the R flag after the #. For instance a request with no data and 0xAB for identifier can be send with the command
$ cansend can0 0AB#R
The candump command should then display
$ candump
can0 0AB [0] remote request
meaning that a CAN remote request message occurred with the identifier 0AB (hexa) and 0 data.