Software _ CAN Bus Configuration - WARRExploration/Rover_II GitHub Wiki
Using a CAN bus in ROS
Initialize SocketCAN Interface
At the moment, there is no automatic way of initializing a SocketCAN interface, thus it has to be done manually (refer to section SocketCAN)
Start CAN communication from ROS
In the rover_control package the rover_control_node is used as a hardware interface sitting between the ROS controllers and the actual physical hardware. It reads values from cmd topic and writes, depending on the controller type, into the pos, vel, or effort topic (write functionality not implemented yet).
To start the node, you can use the control_test.launch file which sets up the required diffdrive controller, as well as the rqt_steering node to write into the cmd topic. Make sure that you have an available SCAN interface set up.
roslaunch rover_control control_test.launch
SocketCAN
SocketCAN is set of CAN drivers for linux where CAN controllers are treated as network devices. It incorporates native, SLCAN (serial CAN) and virtual interfaces.
Configure and enable a SocketCAN interface
Load required modules
Independent of the interface type you are using, you need to load these two modules first.
sudo modprobe can
sudo modprobe can-raw
Native CAN interface
When there is a physical CAN controller available, the interface is configured using the controller name canX
where X stands for the number of the controller. E.g
sudo ip link set can0 type can bitrate 500000
sudo ip link set up can0
Serial CAN interface using USB-to-CAN driver
Load the serial CAN module and check the corresponding device name after you plugged in the USB-to-CAN driver into a USB Port, e.g. using tail
. It should be similar to ttyACM0.
sudo modprobe slcan
tail /var/log/kern.log
...
kernel: [ ] cdc_acm 1-4.2.3:1.0: ttyACM0: USB ACM device
...
Using CAN utils (make sure it is installed globally) you can attach a SCAN interface to the USB device and configure it, e.g. slcan0 to ttyACM0.
sudo slcan_attach -f -s5 -o /dev/ttyACM0
# attached tty /dev/ttyACM0 to netdevice slcan0
sudo slcand ttyACM0 slcan0
sudo ip link set up slcan0
Virtual CAN interface
To enable a virtual interface, the command is similar but you need to load the vcan module (maybe also others, refer to this tutorial)
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
Check configuration
To list all available controller you can type
ip link show
To get some info about the interface (e.g. can0), you can use these commands:
ip addr ls dev can0
Or
ifconfig can0
Send/Receive data using a terminal
A powerful tool set is the can-utils package (refer to the github repo)
sudo apt install can-utils
For example sending data on an interface can be done by
cansend can0 123#1122334455667788
where the 0x123
is the identifier and [ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 ]
are the data bytes