Simulink MATLAB integration problem
Show older comments
I have a question regarding Simulink MATLAB integration. I am currently developing a Reinforcement Learning controller which learns from interaction with a system. This system is currently described in MATLAB with a state-space model, which is very easy to implement in a MATLAB code. However, now I want to use a model that is only in Simulink. My program currently interacts with the system (vehicle dynamics simulation) by providing a number of control inputs (1) to the system, a timestep (2), a initial condition (3) (or the state from the previous timestep) after which the system calculates the next state and returns this to the controller. I would like to use the Simulink model in (roughly) the same topology: provide it with control input, timestep, initial state and have it calculate the next state. I’ve thought of several ways to do it:
1. Convert my MATLAB program to Simulink using MATLAB function blocks. I do not prefer this option, it feels somewhat cumbersome and I might have a lot of work redoing some of the functionality I created.
2. Convert the Simulink file to C/C++ code and mex it in MATLAB so I can use it in my MATLAB code. I have little experience doing this. Currently the Simulink model allows itself to be converted to C/C++ code, however mex’ing the code produces errors (have to look again what the exact error was). I think this would be the preferable approach since it is robust and computationally most efficient.
3. Call the Simulink file every timestep. I have tried this using the sim command. But that is not very well suited as it has to prepare the Simulink file for every timestep, which takes 100-1000x longer than actually calculating the timestep. However there might be ways to do this more efficient, either by pausing the Simulink simulation after every timestep or keep it initialized between timesteps. This approach might not be as fast as approach 2 but the Simulink simulation remains more ‘observable’
What would be the best approach? I have tried to build the C/C++ code from approach 2 using the Simulink coder and by building the program. I guess if you run Simulink in Rapid Accelerator mode, it also uses compiled C/C++ code (it runs fine in Rapid Accelerator mode). Also, what advice can you give on the best approach or on other approaches?
Answers (0)
Categories
Find more on Simulink 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!