MATLAB Answers

Change Simulink.Parameter ExportedGlobal from inside Simulink model

9 ビュー (過去 30 日間)
Jonas 2020 年 7 月 1 日
コメント済み: Jonas 2020 年 7 月 2 日
Dear all
I am developing a controller model which I have put inside a Model Reference. I store all my Simulink.Parameter and Simulink.Signal objects inside a Data Dictionary. The storage class of these objects is set to ExportedGlobal.
The code that I am generating with Embedded Coder, interfaces with external code to run on hardware. This external code is able to change Simulink.Parameter values based on their name, and it can read the Simulink.Signal values also by using their name.
How it works is that the external code can change the value of a Simulink.Parameter (since it is an ExportedGlobal) and in the next execution time step the generated Simulink code uses the new value of that parameter.
My question now is, can I also change the parameter value from inside my Simulink model, with blocks?
The practical use-case is that an external event (such as a CAN message, or Ethernet message) can change a Simulink.Parameter value. But I want to place some value check algorithm inside the model to check if the new parameter value is allowed, e.g. is not above an allowed threshold. If it is above a threshold, I want to overwrite that value change by the threshold value but from inside Simulink.
I have investigated the Parameter Writer block and moving the Simulink.Parameters from the Data Dictionary to the Model Workspace. Such that the Parameter Writer block could change the arguments of the model reference. But this is only possible within an Initialize, Reset or Terminate block and it cannot be done on-command.
With kind regards,

  0 件のコメント



Fangjun Jiang
Fangjun Jiang 2020 年 7 月 1 日
The reason that the external code can modify that parameter is because
  1. You made it ExportedGlobal
  2. It is C code
  3. The C code basically reads and writes a memory location assigned to that parameter
To access the same memory location for that parameter in your Simulink model, you could use the Data Store Memory/Read/Write block, but that will basically add a Simulink.Signal. So
  1. If allowed, change the Simulink.Parameter to Simulink.Signal, because that IS a time varying signal according to Simulink.
  2. Otherwise, keep it a Simulink.Parameter. Declare a Data Store Memory, everywhere to access it through Data Store Read or Data Store Write.

  3 件のコメント

Jonas 2020 年 7 月 2 日
Thank you for your reply Fangjun Jian.
I believe I can change the Simulink.Parameter to Simulink.Signal in the Data Dictionary, and then use it inside my model by using the Data Store Read and resolve it to the Simulink.Signal. In Stateflow I can use 'Scope: Data Store Memory' to directly use it in my chart. Whenever I want to write to the variable from inside my model I use a Data Store Write, and also the external code can write to the variable.
One side-effect however, is when I run the model in Simulink Real-time, I currently use the command setparam to directly change the value of a Simulink.Parameter to change my model. How can I change the value of a Simulink.Signal in this case with Simulink Real-time?
I guess I need to trigger a Data Store Write to be able to change the parameter, but how can I do it? Create a separate Simulink.Parameter which I can change with Simulink Real-time, which is directly connected to a Data Store Write maybe? But then I need to define all my Simulink.Signals another time as a Simulink.Parameter. It is doable but a bit redundant I feel.
Or is an S-function a more fitting solution, which contains the assignment of the new value to the variable?
Fangjun Jiang
Fangjun Jiang 2020 年 7 月 2 日
If that is the case, I would suggest not bothering with parameter or signal. Keep your existing Simulink.Parameter and its ExportedGlobal storage class, but inside your Simulink model, you process the parameter (check boundary and overwrite value) and create an internal or temparary signal. Anywhere else, you can decide whether you want to use the orignal parameter or processed parameter which is now a signal.
Keep in mind, a parameter doesn't change value during run time. One exception might be calibration, which is update value directly at the memory location. This is what your external code does here.
For anything else that needs to be time varying, no matter every step, or asynchronize trigger or interupt, it is a signal by definition.
Jonas 2020 年 7 月 2 日
I don't really need to make a differentiation between the original parameter and the processed one. Because if the external code changes the variable in an illegal way, I want the model to react to this (don't accept it) and also overwrite the variable value which the external code had set. So I only want to use the 'processed parameter' value as you say.
I have done some tests with Data Store Write and Data Store Read and in fact it is quite easy. I can even use it outside the boundary of my Referenced Model.
For the Simulink Real-time behaviour, I guess I can make separate Simulink.Parameters to link it to Data Store Writes. It is in fact emulating the external code in changing variable values.


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by