/CAN-PeCAN_device

Build of a CAN logger with raspberry pi

Primary LanguageShell

PeCAN - CAN Hardware Decoder

Description:

Build of a PeCAN, CAN decoder with a raspberry pi zero w and a CANHat.

List of material:

Set up:

1) Set up the raspberry pi:

  • Install Raspbian lite OS on the micro SD card thanks to the Raspberry Pi imager.
  • Set up the wifi and ssh connection parameters to be able to access the raspberry without monitor from your computer (see here)

Tips: Set up a hotspot from your computer to have a private subnet where you can connect your raspberry(see here).

2) Set up the CANHat:

You can follow this tutorial. Or in a nutshell:

  • Plug the Hat on your Raspberry
  • Modify config.txt:
    • Open config.txt file with sudo nano /boot/config.txt and allow SPI protocole by uncommenting the line dtparam=spi=on
    • Add the following line to the file: dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000 to load the mcp2515 (CANHat controller) driver and set communication parameters
  • Load the SocketCAN kernel modules:
    • Use the command sudo modprobe can and sudo modprobe can_raw
    • Verify the result with lsmod | grep "can"
  • Configure and bring up the SocketCAN network interface:
    • Configure with sudo ip link set can0 type can bitrate 500000 restart-ms 100
    • Bring up with sudo ip link set up can0
    • Verify the result with ip addr | grep "can"

Tips: For step 2) you can direclty replace the config.txt by this one and run socketcan_init.sh script to perform all the steps. Note that you need to perform those steps only once an for all, only the 'Configure and bring up the SocketCAN network interface' steps need to be perform at every Raspberry boot, please refer to next section.

3) Set up Socketcan interface on boot:

  • Add sockectcan_wakeup.sh on /bin/ directory
  • change access rights with sudo chmod +x /bin/socketcan_wakeup.sh
  • Add sockectcan_wapeup.service on /etc/systemd/system/
  • reload daemon with sudo systemctl daemon-reload
  • Enable service with sudo systemctl enable socketcan_wakeup.service
  • Start service with sudo systemctl start socketcan_wakeup.service
  • Check status with systemctl status socketcan_wakeup.service

4) Send / Receive messages:

  • Install package sudo apt install can-utils
  • To receive use candump -tz can0
  • To send use cansend can0 456#00FFAA5501020304 for example

Receiving / decoding strategy:

The PeCAN will be responsible for one specific CAN channel. It will be connect to it though a DB9 connector linked to the CANHat. A dbc file will be assign to the PeCAN. The package CAN-TcpCanStream will be used as a service on boot to receive TCP request from the router and execute the associated command. For more details please refer to the related repository.

References:

https://www.pragmaticlinux.com/2021/10/can-communication-on-the-raspberry-pi-with-socketcan/ https://www.waveshare.com/wiki/RS485_CAN_HAT https://wiki.seeedstudio.com/2-Channel-CAN-BUS-FD-Shield-for-Raspberry-Pi/ https://www.beyondlogic.org/adding-can-controller-area-network-to-the-raspberry-pi/ https://www.waveshare.com/wiki/2-CH_CAN_HAT https://circuitdigest.com/microcontroller-projects/rs485-serial-communication-between-arduino-and-raspberry-pi