How to implement concurrent programming between simulink and matlab

7 views (last 30 days)
Hi everyone,
I need help for the following situation.
My project is to use simulink to simulate a robot.The output of the simulink model are robbot's position and torques at each timestep. My problem is on the data collection part. I plan to use a buffer to store the simulink output and use antoher matalb function to access the same buffer to get the data out of the buffer for online data analysis. The requirement is the simulink model and matlab data analysis function need to run simultaneously. And the matalb data analysis function decide when to get the data out of the buffer. This is like a producer-consumer problem, where the simulink is the producer and the data analysis matlab function acts as a consumer.
My question is how to protect the buffer for mutual exclusion. I do not want to use To workspace block, because it only updates data when simulink is paused or stopped. I do not find any smeaphore or mutex like structure provided by matalb or simulink. I have tried the following ways to solve the problem, but non of them works:
1. I have tried to use the queue and buffer block in DSP toolbox, this two blocks provides mutual exclusion, but the size of the output data is changed during the simulation. Basically when the matlab function collecting data, it takes all the data stored in the buffer at the moment. Then buffer block seems to output one by one at each simulink timestep.
2. I have tried to implement a queue by a persist variable in a embedded function. When the matalb function want to collect data, it flip a signal flag to tell the simulink to output the data into workspace. But in this method, the matalb function have to get the data by two calls. The 1st call to flip the flag and then return. In the next, the 2nd call is used to search the workspace to find the data outputted by simulink. This method is denied by my advisor, because it is not elegant.
I think RTW may solve this problem, but the simulink model and matalb analysis function code are often changed, so for debugging purpose, I plan to not change the simulink in to C/C++. But I wonder whether I can use C to implement a mutex and call by simulink and Matlab. If the answer is yes, then how to do this?
I really hope someone can help me out. Any suggestion is appreciated. By the way, I am using Linux system.

Answers (1)

Kaustubha Govind
Kaustubha Govind on 9 May 2012
I'm not entirely sure if this will help, but I can think of two possible solutions:
  1. Why not call your MATLAB code directly in the model using one of three available MATLAB Function blocks so that your data analysis done continuously in the model loop?
  2. You could set up your MATLAB code as an execution event listener on your model, so that your MATLAB code is invoked at every time step as soon as output values become available.
  2 Comments
Zhuofu Bai
Zhuofu Bai on 9 May 2012
Hi Kaustubha,
Thank you for your help. But unfortunately in my project, I have to use two threads (simulink and matlab) to run concurrently. Actually, my project is a java project and I am using simulink to do simulation. The DataAnalysis Matlab function is called by a java function(caller), so the return value of DataAnalysis function(callee) has to be returned to the caller java function. The execution event listener can solved my data collection problem, but it cannot return the value back to the java part. If I stored the return value of DataAnalysis function as temporary data in the disk and use a java function to get the data, then there is another concurrency problem between matalb function and java. I wonder if there exists a mechanism for simulink and matlab to use a shared memory to transfer dataset.
Kaustubha Govind
Kaustubha Govind on 11 May 2012
Could you use one of the MATLAB External Interfaces (http://www.mathworks.com/help/techdoc/matlab_external/bp_kqh7.html) to either return the value to Java, or write to a shared memory? If you can thinking of a way to write an external application (without MATLAB) to work with your Java application, then you can apply a similar technique to MATLAB.

Sign in to comment.

Categories

Find more on Get Started with Aerospace Blockset in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!