Shared memory (read and write) in Simulink model on Raspberry Pi

5 views (last 30 days)
Hello everyone,
I would like to access shared memory in a Simulink model that runs on a Raspberry Pi 3 B+. I want to exchange data with another process running on RPi. I couldn't find anything applicable. Can anyone help me? Thank you!

Answers (1)

Ayush
Ayush on 3 Nov 2023
Dear Raphael,
I understand that you are working with Simulink and Raspberry Pi 3B+ and you want to exchange data with another process running on the Raspberry Pi.
To access shared memory in a Simulink model on a Raspberry Pi, you can write a C code that implements the steps mentioned below. Then, you can use the Simulink Coder Support Package for Raspberry Pi to generate and deploy a Simulink model that calls this code. You can integrate your C code with your Simulink model using either the MATLAB Function block or the S-Function block.
Here are the steps to create the C code:
  • Create a shared memory segment using the "shmget" function in C. You need to specify a unique key, the size of the segment, and the permission flags.
  • Attach the shared memory segment to your process using the "shmat" function. This will return a pointer to the shared memory location.
  • Use the pointer to read or write data to the shared memory segment. You can use any data type or structure that fits within the segment size.
  • Detach the shared memory segment from your process using the "shmdt" function when you are done.
  • Delete the shared memory segment using the "shmctl" function with the "IPC_RMID" command when it is no longer needed.
For more detailed guidance on writing the C code and deploying your application on Raspberry Pi with Simulink, please refer to the following links:
  1. Shared memory in C: https://raspberry-projects.com/pi/programming-in-c/memory/shared-memory
  2. Simulink deployment on Raspberry Pi: https://in.mathworks.com/help/supportpkg/raspberrypi/ug/create-and-run-an-application-on-raspberry_pi-hardware.html
  3. MATLAB Function Block: https://in.mathworks.com/help/simulink/what-is-a-matlab-function-block.html
  4. MATLAB S-Function block: https://in.mathworks.com/help/simulink/matlab-s-functions-1.html
Hope this helps!
Best regards,
Ayush Goyal

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!