Most efficient way to update 'constant' variables in RTW/EC (variable vargin lengths?)
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Given a simple example to explain what I'm asking:
-A control system is realized in Simulink.
-C code is generated by RTW/EC, and then compiled to work on an embedded system.
Everything works fine with the code handling the following equation:
y = k*x
where
y - output from main function call
x - input to the main function call
k - hardcoded or #defined constant
Now we want to be able to change some things occasionally during run time, namely x_hat and k from the following equation:
y = k*(x - x_hat)
So the easiest (smallest amount of change from the current design) to do it is to have x_hat and k passed from the wrapper into the Simulink code every time. This is despite the fact that they would be passed many times per second, and may only be changed every 10 minutes, if ever.
Any suggestions?
1 Kommentar
MarkB
am 15 Feb. 2011
It isn't clear from the example that you gave: are you changing "x" or "x_hat"'s dimensions as well?
Antworten (1)
Shreshth
am 20 Mai 2024
Hello Kenny,
To address your requirement of dynamically changing x_hat and k during runtime with minimal changes to the existing design, you can implement a strategy that involves using Simulink's capabilities for parameter tuning and external mode execution. This approach allows you to modify x_hat and k without having to regenerate and redeploy code every time you want to change these values. Here's a simplified step-by-step guide:1. Define Parameters in Simulink
- Parameterize x_hat and k: Instead of hardcoding x_hat and k in the Simulink model, define them as parameters. You can use Simulink.Parameter objects to do this. This makes it easy to change their values from outside the Simulink model.
2. External Mode Setup
- Enable External Mode: Configure your Simulink model to run in external mode. External mode allows you to run your model on your hardware and communicate with it from Simulink in real-time. This way, you can change x_hat and k from Simulink while the model is running on your embedded system.
3. Create a User Interface for Parameter Tuning
- Develop a UI for Changing Parameters: You can create a simple user interface (UI) in MATLAB that communicates with your Simulink model. This UI can have input fields for x_hat and k, and buttons to apply the changes. When you change the values in the UI and apply them, the new values are sent to the Simulink model running on your embedded system in real-time.
4. Implementing the Changes in Code
- Code Generation with Tunable Parameters: When you generate C code from your Simulink model, ensure that x_hat and k are marked as tunable parameters. This way, the generated code will include mechanisms to update these parameters during runtime.
5. Handling the Parameters in Embedded Code
- Update Mechanism: In your embedded system's main loop or control function, implement a mechanism to check for updated values of x_hat and k and apply them to the control algorithm. This can be done through direct memory access, using a communication protocol, or through an interrupt service routine, depending on your system's architecture and requirements.
Example Code Snippet for Embedded System
volatile float x_hat = INITIAL_X_HAT_VALUE; // Volatile if updated in an ISR
volatile float k = INITIAL_K_VALUE; // Volatile if updated in an ISR
void update_control_parameters(float new_x_hat, float new_k) {
x_hat = new_x_hat;
k = new_k;
}
void main_loop() {
while(1) {
// Your control algorithm here, using x_hat and k
float y = k * (x - x_hat);
// Rest of your loop code
}
}
Conclusion
This approach minimizes the changes needed in your current design and setup. It leverages the power of Simulink's external mode for real-time parameter tuning and keeps the embedded system's code straightforward and efficient. Remember, the specific details of implementing these steps can vary based on your exact model setup, target hardware, and Simulink version. Always refer to the latest Simulink documentation for the best practices and detailed instructions.
Thank You.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Deployment, Integration, and Supported Hardware finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!