VBCores Docs
HomeGitHub
  • VBCores
  • Hardware
    • VBCore VB32G4
    • VB STM32 Programmer
    • BLDC motor driver 30A
    • DC motor driver 15A
    • Stepper motor driver 10A
    • Ethernet - CAN-FD
    • CAN-FD - Raspberry PI
      • Setting up CAN on Raspberry Pi
      • Troubleshooting
      • Работа с CAN FD через Python
    • PowerBoard 30A
    • SBus-HID
    • DC-DC 12V
    • USB 2.0-HUB
    • IMU BNO055
    • IMU BHI360
    • T-encoder
  • Software
    • Arduino IDE
      • Arduino IDE setup
      • Selecting MCU
      • Optional hardware preparation
      • Prepairing a sketch
      • Libraries
      • Examples
        • Все переписать нахрен
        • Работа с I2C
        • I2C detect
        • Датчик BNO055 / I2C
        • Датчик AS5047P / SPI
        • Датчик AS5600 / I2C
        • Работа с бесколлекторными двигателями
          • Simple FOC. Управление скоростью. Нахождение количества пар полюсов.
          • Simple FOC. Управление моментом
          • Чтение данных с датчика тока
        • Работа с коллекторным двигателем
          • Вращение DC мотором
          • Чтение угла по энкодеру. Управление DC мотором по углу
          • Чтение скорости вращения мотора по энкодеру
        • Работа с шаговым двигателем
          • Вращение шагового двигателя.
          • Контроль двигателя по интерфейсу SPI
    • STM32 CUBE IDE
      • Типовые настройки
      • Подсказки начинающим
        • Cube IDE для начинающих
        • Clock configuration
        • Таймеры - прерывания
        • Таймеры - ШИМ
        • Отладка программ
        • Коммуникации - FDCAN
        • Управление DC-мотором
        • Backup программы
  • Cyphal CAN
    • Cyphal CAN
    • PyCyphal
    • Yakut
    • Cyphal Arduino
      • Отправка и получение сообщений по cyphal
  • Работа с ROS
    • Установка Ubuntu, ROS и Arduino
    • ROS_LIB
    • Возможные ошибки
    • Примеры
      • Publisher. Hello World!
      • Publisher with Subscriber
      • Rotation by DC motor
  • Example projects
    • Motor-wheel upgrade
Powered by GitBook
On this page
  1. Hardware
  2. CAN-FD - Raspberry PI

Setting up CAN on Raspberry Pi

Last updated 1 month ago

The easiest way to work with CAN bus in Linux is the program set. Install it with the following command:

sudo apt-get install can-utils 

Can-utils consists of the following tools:

  • candump - allows to print, filter and record all data that is being received by a CAN interface

  • canplayer - playbacks recorded frames

  • cansend - transmits CAN frame

  • cangen - generates random CAN frames

  • canbusload - displays current CAN bus load

We will mainly use the candump and cansend utilities.

To start the CAN interface, enter the command

sudo ip link set can0 up txqueuelen 65535 type can bitrate 1000000

The bitrate field specifies interface speed, 1000000 bit/s in this example.

In case of "Cannot find device "can0"" error, do the following:

sudo nano /boot/config.txt

Append the string

dtoverlay=seeed-can-fd-hat-v2

Save (Ctrl+S) and close (Ctrl+X) the file. Reboot the raspberry:

sudo reboot

To view frames on the CAN0 bus use the candump command

candump can0

This will print all frames. In case you want to see frames with specific ID, use the command candump can0,<ID>:7ff. It uses hexadecimal numerals. ID=100 decimal will be ID=0x64 hexadecimal (100=0х64). For this ID the command takes the following form:

candump can0,064:7ff

To send frames to the bus use cansend. Sending 4 bytes (0xDE 0xAD 0xBE 0xEF) with message ID=100(0x64) looks like this:

cansend can0 00000064#DEADBEEF

To disable the interface use

sudo ifconfig can0 down

CAN-FD

CAN FD is the second generation of CAN protocol developed by Bosch. The basic idea is to overclock part of the frame and to oversize the payload. Developed in 2011 and released in 2012 by Bosch, CAN FD was developed to meet the need to increase the data transfer rate up to 5 times faster and with larger frame/message sizes. To configure CAN interface with FD mode on, use the following command:

sudo ip link set can0 up txqueuelen 65535 type can bitrate 1000000 dbitrate 8000000 fd on

In addition to bitrate there's dbitrate which specifies the speed of data segment in frame.

From here on we will use FD mode by default.

Let's set up automatic CAN bus configuration when Raspberry starts. To do so we will write a little script canup.sh and place it in home directory

sudo nano canup.sh
#!/bin/sh
sudo ip link set can0 up txqueuelen 65535 type can bitrate 1000000 dbitrate 8000000 fd on

To make the file executable:

sudo chmod +x canup.sh

To launch application when Raspberry starts you have to specify it in /etc/rc.local

sudo nano /etc/rc.local

In the nano editor window you will see lines of code. Find the exit 0 line and put the following command before it:

sudo bash /home/pi/canup.sh

Save (Ctrl + S) and close (Ctrl + X) the editor. Reboot the Raspberry

sudo reboot

It is useful to know the command

ip -details link show can0

This command allows you to get information about the CAN0 interface. Note that if there are no errors when sending or receiving messages, you will see 0 in the error counter, as shown below

can-utils