"Save" function doesn't work in a Simulink FMU - simulink

I'm creating a Simulink FMU for robot trajectories. I'm using MATLAB R2022a.
The input of my FMU is targetPos, i.e., the target point coordinates (x,y,z) of the robot. The outputs are:
ExitFlag - (not) reachable point
config - joint angles of the manipulator
info - IK solver details
I'm aiming to save the joint angles of all waypoints in the trajectory, i.e., config in all Simulink iterations. I prefer to do it in .csv for later compatibility, but .mat is also fine. I've included the Save block in Simulink directly connected to config.
I've exported the model as standalone FMU and it's generated successfully.
Check the Simulink model and traceback when saving as FMU in this screenshot.
Then, I've tested the FMU in ROS 2 with the fmi_adapter, but the .mat is not being generated.
ros2 launch fmi_adapter fmi_adapter_node.launch.py fmu_path:=FMI/Trajectory/RobotMARA_SimulinkFMU_v3.fmu
So, is there an incompatibility with the "Save" block in an FMU? Any work around to solve this problem is welcome.
Thank you,
Jon

Writing to a file system is not defined in the FMI API, see https://github.com/modelica/fmi-standard/issues/326
Is it possible to define local variables or output that you can access from the the importing tool via the FMI API?
In FMI 3.0 you could define a binary stream to output your data.

Related

Matlab Simulink and Reinforcement Learning Approach

I would like to create a project using a Matlab simulink environment model to find optimal parameters using reinforcement learning. Is it possible to export the environment data from matlab simulink and incorporate it into a python script or to create an interface to matlab simulink in order to train interactively with it? Or is there a better approach?
Unfortunately, this is rather vague, so I'm sorry, as I do not know what the matlab model will look like. It would also be helpful for me to see an example project or something along the lines of accessing Matlab Simulink data and reinforcement learning.
Thank you very much.
To create an interface with python script and the Simulink simulation, you can use MATLAB Engine for Python.
I could find a sample implementation in this blog. You can extend the simulate function in this example with an RL Agent to determine an action.
You can use simulink and generate coder from simulink. You must download simulink coder, matlab coder packages to generate code.

How to perform local sensitivity analysis in modelica

I want to do local senstivity analysis in Dymola to evaluate different parameters affecting the energy consumption in a building (for multi-familyhouse).
Can anyone give me some suggestions, how to do it in Dymola (Modelica) software?
You could export you model as FMU and then follow one of many the different paths for sensitivity analysis with FMUs:
PyFMI + SAlib Sensitivity Analysis using PyFMI - FMU in for-loop
FMI Toolbox for Excel by Modelon http://www.modelon.com/products/modelon-deployment-suite/fmi-add-in-for-excel/
openTURNS + FMI https://www.researchgate.net/publication/321624302_A_Probabilistic_take_on_system_modeling_with_Modelica_and_Python, https://github.com/openturns/otfmi, http://www.openturns.org/
...
Did you take a look at the "Design" library that comes with Dymola? It contains functions to sweep and perturb parameters.
You can load it in File -> Libraries -> Design. Some examples on how to apply the library can be found in the first chapers of the Dymola Manual 2. From the question I guess the Calibration package should be most useful for you.
One approach could be to do the modelling in Dymola (or OpenModelica, Simulation X, whatever), and then do the parameter sweep, pre- and post-processing, plotting, optimization and so on from Python. Dymola has a Python interface, so you can start simulation, read results, change parameters and so on from Python. Everything that can be done from the GUI is in theory also possible from the Python interface.
A Matlab interface and a Javascript interface are also available.

Loading Classification Model in Simulink

I've trained a classification model (Classification Tree) using Matlab's Classification Learner App. I've exported it to the workspace and also saved it as a .mat file.
I need to embed it in a Simulink model to make predictions at every time step during simulation. I've tried using a Matlab Function block and using "load" to load the classification tree from the .mat file and then using mdl.predictFcn(myInputData) to make a prediction on new data. However, when I try to run the simulink model I get the error:
Found unsupported class for variable using function 'load'. MATLAB
class 'function_handle' found at 'mdl.ClassifierTTC.predictFcn' is
unsupported.
Does this mean I cannot use my classification model in Simulink? Has anybody tried to do something like this already?
Thanks in advance.
There is a work around for this problem without needing to write any C Code. All necessary C Code can be generated with MATLAB's code generator.
After generating a classification model, export it from the Classification Learner App to the workspace and then save it using the saveCompactModel() function. Note that some classifier models are not supported even in the latest MATLAB versions (2017a).
Write an m-file that loads and passes unseen data to the classifier and uses the predict() function to get the classification label from the classifier. Use codegen() with the dll configuration to generate C Code from the m-file, but do not let it compile. This will generate all necessary header and source files.
Now, using legacy_code() generate an S-Function to import the C Code into Simulink. Link ALL generated header and source files to the S Function and generate the S Function block and a tlc file with legacy_code().
This produces an S Function block with the classifier and the predict function embedded in the block. The legacy_code() function generates a tlc for this S Function and allows the S Function block to be compiled.

Run a continuous Simulink model with real-time input?

I'm new to Matlab/Simulink but have a requirement to interface with a Simulink model from a piece of software.
At the moment I have created a simple Simulink function 'Inc' which has 1 input to an addition block with a constant 1 and an output. I have used To/From Workspace blocks and can run the simulation over a fixed time using a time/value vector input, such as simin = [0,0;5,0;5,1;10,1].
What I would like to do is run my model continuously? Reading Workspace values in real-time rather than a pre-defined time based vector?
I can't see how to set this up? Can Matlab/Simulink do this?
To summarise, I would like simin and simout to be single values i.e. simin = 1, then with the model running continuously(infinitely) at the next fixed step simout would update to simout = 2. simin changes would be made at the Workspace at varying intervals.
The short answer is you can't (easily) do that, that's not how Simulink works. The MATLAB workspace is read/accessed at the beginning of the simulation and passed to the Simulink engine, and even if the values in the workspace change before the simulation is finished, this is not taken into account by the Simulink model running.
There are ways to work around this, but if you are a novice to MATLAB & Simulink, be aware that these are fairly advanced techniques, and I would advise to familiarise yourself with Simulink first.
Have a look at these similar questions for suggestions of how to do what you want:
Stream data form MATLAB to Simulink
Problem of variable updating in workspace
How Do I Change a Block Parameter Based on the Output of Another Block?
Tuning block parameters at every time step in a simulation
Simulink Signal Viewing using Event Listeners and a MATLAB UI
Obviously, you need to change the simulation end time to Inf or some large number.

Use Sim() command without coder.extrinsic in Matlab

Is there an alternative function to the sim() command oder a direct way executing a Simulink Model / a compiled version of it from Matlab without setting coder.extrinsic?
I want to execute a simulink model inside of an iterative Matlab-function. (Hence, speed matters dramatically). The used Simulink model contains a Dymola interface and hence, I cannot model it directly in Matlab. Another main Simulink model starts and iterates the Matlab functions and hence, simulating it leads the coder trying to compile it efficiently. By setting coder.extrinsic, of course I can use the sim command, but it is way too slow for its purpose. I thought about compiling the Simulink model as an alternative, but do not know if this would be a good approach
The structure looks as follows:
Simulink main model -> matlab functions -> simulink model
sim command needs MATLAB. So you need coder.extrinsic. There is no direct alternate way without coder.extrinsic to simulate a model. You can generate code from the model and call the generate code using coder.ceval functions. But if your goal is only to get more speed instead of getting stand-alone code you can set your model to run in accelerator mode and see whether that improves speed.