MATLAB: Change Simulink.Parameter ExportedGlobal from inside Simulink model

argumentEmbedded Coderexportedglobalmodel referenceparameter writersimulinksimulink.parameter

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,
Jonas

Best Answer

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.