How to set the Raspberry Pi CAN receive block to receive CAN bus message

6 vues (au cours des 30 derniers jours)
Hello everyone I meet a problem when trying modeling in simulink to receive the CAN message by Raspberry Pi 4B and PiCAN3 hat shield.
I connected the Raspberry Pi to my computer then launched the raspberryPiResouceMonitor. It showed that my computer had get the sudo license so that I could open the terminal window in my computer. I am sure the Pi was succussed to hack into the CAN divice after this following:
Yes multiple messages include multiple signals.
Then I run the model (↓) I made previously by by click Monitor & Tune
but received the error message↓:
### Starting build procedure for: CANtest
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\test\CANtest_ert_rtw
### Invoking Target Language Compiler on CANtest.rtw
### Using System Target File: C:\Program Files\MATLAB\R2021a\rtw\c\ert\ert.tlc
### Loading TLC function libraries
.......
### Initial pass through model to cache user defined code
...
### Caching model source code
...............................................................................
.........
### Writing header file CANtest_types.h
### Writing header file CANtest.h
### Writing source file CANtest.c
### Writing header file CANtest_private.h
.
### Writing header file rtwtypes.h
### Writing header file multiword_types.h
### Writing source file CANtest_data.c
### Writing header file rtmodel.h
### Writing source file ert_main.c
.
### TLC code generation complete.
### Creating extmode_task_info.m.
### Creating external mode types file ext_mode_types.h
### Evaluating PostCodeGenCommand specified in the model
### Using toolchain: GNU GCC Embedded Linux
### 'C:\test\CANtest_ert_rtw\CANtest.mk' is up to date
### Building 'CANtest': make -f CANtest.mk all
### Successful completion of build procedure for: CANtest
### Simulink cache artifacts for 'CANtest' were created in 'C:\test\CANtest.slxc'.
Build process completed successfully
Build Summary 1
Elapsed: 0.113 sec
Top model targets built:
Model Action Rebuild Reason
================================================================================
CANtest Code generated and compiled Global variable raspiCANMsg has changed.
1 of 1 models built (0 models already up to date)
Build duration: 0h 1m 44.551s
A run-time error is encountered when running External mode simulation on the Raspberry Pi hardware. This usually occurs when a hardware resource, such as a web camera or an audio card, is not available or configured incorrectly. The log file, /home/pi/MATLAB_ws/R2021a/CANtest.log, storing model diagnostic information on the Raspberry Pi hardware has the following content: **** Starting the application ****
/dev/spidev0.0
SPI_open/open: No such file or directory
Error opening SPI bus.
open SPI Channel 0 in RW mode -> fd -1
and checked the file, the file has been updated to the latest correct code.
Here are the configuration parameters I set:

Réponse acceptée

Jeanyee. S
Jeanyee. S le 15 Oct 2021
the MATLAB technical support contacted me and here is what I did then:
I think not only me who followed the PiCAN3 installation guide adding 3-line coding into the config.txt would meet the same problem,
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay
but the simulink support package have made the things more simple,
so just try to delete the last 2 lines
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay
and then
sudo reboot
then every thing become sweet!
  1 commentaire
Marou AD
Marou AD le 28 Jan 2024
Modifié(e) : Marou AD le 11 Fév 2024
I faced the same problem.
The 'dtoverlay' configuration forces the CAN interface to be available on the Raspberry Pi as a network interface. If you remove the following lines:
"dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25" and "dtoverlay=spi-bcm2835-overlay"
You may encounter difficulties in sending messages from the Raspberry Pi to the Simulink block. The 'cansend' command on the Raspberry Pi will no longer work.
so how you send data from rasp to simunilk ?

Connectez-vous pour commenter.

Plus de réponses (1)

Sean Murray
Sean Murray le 22 Fév 2022
Be careful with RPI CAN support. We found a bug with r2021a where the interrupt pin on the RPI would stay low after the first message recieved despite everything configured correctly, and CAN data was not passed out of the recieve block.
Upgrading to r2021b and its now fixed. Also, the CAN interrupt pin is no longer set in the Hardware settings (target hardware resources) in matlab, but setting this on the RPI in the config file does the trick. I guess the 2x methods were conflicting somewere and mathworks have now deffered to the tried and tested RPI method which is good because CAN Utils will also now function.
The lines needed in the config are (sudo nano /boot/config.txt)
#dtparam=spi=on
#dtoverlay=mcp2525-can0, oscillator = 8000000, interrupt = 25
#dtoverlay=spi0-hw-cs
Replacing the ocillator frequency and interrupt (GPIO#) with the right values to suit your own CAN Board.
Hope this helps someone and saves many hours debugging!
  2 commentaires
Christof Hadamek
Christof Hadamek le 8 Avr 2022
Hey,
i have still problem with receiving multiply CAN Signals. I can see that the data will be shwon once. But after i change die Data from the CAN-Message (coming form a PEAK CAN Driver) there is noch change detecting anymore in my Model (Monitor & Tune). Its like frozen, i think that the interrupt Pin is still not working right.
First time i got sipport from Mathwork they told me, that the blocks not support more then one can message while receiving. is that still the problem? Because who need the CAN Package on Raspberry with only one CAN Signal?
Im working with the Simulink Raspberry Pi Support Package and the Vehicle Network Toolbox.
(Matlab r2021b, update3).
In my config.txt:
dtparam=spi=on
dtparam=i2c_arm=on
enable_uart=1
start_x=1
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25
dtoverlay=spi-bcm2835-overlay
dtoverlay=spi0-hw-cs
Transmitting works fine.
Sean Murray
Sean Murray le 8 Avr 2022
We had this trouble but after installing r2021b it worked okay, and we get multiple rx messages from multiple can IDs no problem.
Best to check it all works on the RPi first, and then with Matlab.
Install can utils (if you haven't already):
in a terminal on the RPi 'sudo apt install can-utils'
and then 'candump can0' , you should see the recieved messages displayed. 'cansend can0 123#00FF00AA00FF00CC' to send messages; 123# is the can id, followed by 8 hex bytes.
If thats not working then it could be 'buss off' or a problem with the interrupt pin etc. check the pin is pulsing to the correct levels on the DIO you are using with an oscilloscope.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Raspberry Pi Hardware dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by