Apply Persistent Variables in Real-Time Applications
This example shows how to apply persistent variables in real-time applications.
In a model, you save variables on target computer whose values persist when the real-time application stops and even when the target computer is shut down by using Persistent Variable Write blocks. At real-time application start, you direct the real-time application to read these persistent variables by using Persistent Variable Read blocks.
In MATLAB®, you can get or set the values of persistent variables on the target computer by using the getPersistentVariables
function and the setPersistentVariables
function.
Examine Persistent Variables in the Model
The model slrt_ex_persist
computes the final position by using information from the speed
input and initial position
input. The Persistent Variable Read block variable position
provides the initial value for the first run of the application from variable default value when the variable position
does not exist on target computer. When the application stops, the Persistent Variable Write block variable position
stores the final position from the run. The real-time application uses this value as the initial position for the next run.
Create Target Object and Connect
Create a Target object for the default target computer and connect to the target computer. In the Command Window, type:
tg = slrealtime; connect(tg);
Build Real-Time Application
To open the model and examine the Persistent Variable block values, in the Command Window, type:
model = 'slrt_ex_persist'; open_system(model); modelSTF = getSTFName(tg); set_param(model,"SystemTargetFile",modelSTF)
Build and Load the Real-Time Application
To build and load the real-time application, in the Command Window, type:
evalc('slbuild(model)');
load(tg,model);
Initial Value of Persistent Variable
The initial value of the persistent variable position
when it does not exist on target computer is 0.
The value is set by the Default value parameter of the Persistent Variable Read block.
Observe How Persistent Variable Changes in First Run
To run the real-time application, in the Command Window, type:
start(tg);
Check the value of the persistent variable after the stop time expires and the real-time application stops.
pause(5); myPersistVars = getPersistentVariables(tg)
myPersistVars = struct with fields: position: 20
Observe How Persistent Variable Changes in Second Run
To run the real-time application, in the Command Window, type:
load(tg,model); start(tg);
Check the value of the persistent variable after the stop time expires and the real-time application stops.
pause(5); myPersistVars = getPersistentVariables(tg)
myPersistVars = struct with fields: position: 40
Clear the Persistent Variable Values
Because the persistent variable values remain on the target computer after the real-time application stops, you must clear the retained values if the retained values are not needed. These steps show a way to clear the position
persistent variable values.
myNewPersistVars = rmfield(myPersistVars,'position');
setPersistentVariables(tg,myNewPersistVars);
myPersistVars = getPersistentVariables(tg)
myPersistVars = []
You can also remove all persistent variable values by using this command.
setPersistentVariables(tg, []);
Preserve Persistent Variable Data by Safe Shutdown of Target Computer
The previous steps demonstrate how Persistent Variable values are stored when the real-time application stops and are reloaded when the real-time application starts. These variables are also retained when the target computer is shut down.
Target computers can handle being shut down by turning off power to the computer, but using this approach is not the best practice for the target computer. Also, if you just turn off the target computer while the real-time application is running, you can lose the last few seconds of data for the Persistent Variables.
To preserve all persistent variable data and safely shut down the target computer:
1. On the target computer, stop the real-time application (for example, stop(tg)
). The values for persistent variables are stored.
2. Open a system terminal window.
3. On the development computer, for user slrt
and target computer IP address 192.168.7.5
, type command: ssh slrt@192.168.7.5
4. Complete the login with password: slrt
5. At the target computer system prompt that appears in the terminal window, shut down the target computer by using QNX Neutrino command: shutdown -S system
6. After the shutdown
command runs, you can safely turn off power to the target computer.
For more shutdown
command info, see shutdown in the QNX Neutrino documentation.
Close All Files
bdclose(model);