How do I set sample time for a CAN transmission?

17 views (last 30 days)
I am working on a test bench and want to measure a stress. I got two ESP32, one HX711 and two CAN modules. The hardware is working; and on Simulink I got one model to measure, convert and transmit data with CAN, and a second one to receive the information. The first one is build on one of the ESP32 and the other used in "Run and Monitor" mode on the other.
The HX711 works at 10Hz, so I though about putting the sample time of the simulation to 1/10 to be able to send data through the CAN module at a 10Hz rate. But this isn't working and I don't know why. When I set the sample time of the second model at 1/10, the CAN receives data one time and then it only displays zeros. My data has the good type: the int32 data gets transform into 4 uint8 that fill up the CAN Bus by half of its capacity. Then, I retransform them and convert them into voltage.
The thing is that there is no issue when I use the same programs on Arduino IDE. I put those codes into "IO Builder blocks": one for HX711, one CAN Receive and one CAN Transmit. Moreover, when I put the sending program on Simulink, build and run it on the first ESP32: I can read the data with Arduino IDE on the other ESP32. But that won't work with Simulink... I think it is a sampling time problem. Simulink seems to execute only once the CAN Receive function I created.
If you could help me with that project that would be awesome, thank you in advance!
  1 Comment
Mathurin
Mathurin on 4 Jun 2025
Sorry, I forgot to say that I found a solution, so there it is:
The HX711 is a blocking-reader sensor, which means that when it is doing something, the ESP32 only does the reading and "stop" everything else. Moreover, having multiple IO blocks in a Simulink model seems to always create issues. Simulink tries to access and launch a C code on the ESP by opening Serial communication (if XCP Serial is chosen of course), and when there is multiple blocks the task gets confusing and many times, the error : "XCP serial port error: serial port send error" will appear.
To avoid this from happening, the sample time can be set the same for the two IO blocks. But, and yes it's that simple, the HX711 one, which is supposed to be executed first, has to be manually set to priority "First". In that way, Simulink will open the connection to the sensor first, then open the CAN bus.
However, and I am currently on this issue, when there is a third IO Custom Block, for another sensor, this is not working anymore. The second sensor is using Serial(2) so it should be a different way of communication to the ESP. But I still get the XCP error even after changing the priorities. I think I will just create a new model and run it on a third ESP32.

Sign in to comment.

Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!