Change Simulink.Parameter ExportedGlobal from inside Simulink model

30 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 1 de Jul. de 2020
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 comentarios
Fangjun Jiang
Fangjun Jiang el 2 de Jul. de 2020
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
Jonas el 2 de Jul. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Event Functions en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by