Using simulink runtime data in callback functions

1 visualización (últimos 30 días)
Kizito Amungwa Achembong
Kizito Amungwa Achembong el 19 de Jul. de 2021
Respondida: prabhat kumar sharma el 6 de Jun. de 2024
Hello, I am stressedup with App Designer. I have stateflow model in Simulink to be controlled from an app built in app designer. I am able to creat a Listener and a run time object in an updateGui helper function.
properties (Access = public)
status3; % Description
end
methods (Access = public)
function updateGUI(app, varargin)
% Create an object that gets the run-time value of the specified block
rto_2 = get_param('ChargingProcess/Display3','RuntimeObject');
rto1 = get_param('ChargingProcess/CarBatteryOut1','RuntimeObject');
% Update the GUI accordingly.
app.status3 = rto_2.InputPort(1).Data.char;
% app.EditField.Value = rto_2.InputPort(1).Data.char;
% Assign input from Car Battery Level(CarBatteryOut1) to Car Battery level 1 editfield
app.CarBatteryLevel1EditField.Value = rto1.InputPort(1).Data;
end
end
there are other objects which are not very necessary now
This works well and displays the information on the app as I want. Now, i need some of this information (status3) in a push button callback to control a a switch. First issue is that I cannot access the varriable status3 directly in a callback function. I treid to declear it as a property and call it in the button callback funtion. It is called but does not update as the simulation progresses.
The main idea is for the switch controlled by the button callback function to stay on '1' when the button is pressed to the point when status3 matches a particular string 'WaitAtStation' and a certain edith field vallue == 100. In this case, I used a while loop. I treid to output the edith field value (gotten from simulink too ) with the same callback funtion but it also does not update as the value in the edith field changes
striingValue = num2str(app.status3)
function Charge1ButtonPushed(app, event)
set_param('ChargingProcess/ChargeMyCar1','sw','1');
while (app.CarBatteryLevel1EditField.Value == 100 && strcmp(stringValue, ~'WaitAtStation'))
set_param('ChargingProcess/ChargeMyCar1','sw','0');
end
end
This either doesnt work or freezes my system when the button is pushed. What i actually need help is;
1) How to get status3 and the app.CarBatteryLevel1EditField.Value in my callback function that will update as the actuall vallues change in simulink
2) A better way represent the logic because the while loop appears not to be doing the job

Respuestas (1)

prabhat kumar sharma
prabhat kumar sharma el 6 de Jun. de 2024
Hi Kizito,
To ensure that status3 and CarBatteryLevel1EditField.Value update in real-time in your callback, you should avoid relying on a while loop that continuously checks these conditions. Instead, you should use a timer.
You can use a MATLAB timer object to periodically execute the updateGUI function, which updates status3 and other UI components. This ensures that your UI always reflects the current state of the Simulink model.
First, create and start a timer in your app's startupFcn:
function startupFcn(app)
% Create a timer that calls updateGUI every 0.5 seconds
app.updateTimer = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.5, ...
'TimerFcn', @(~,~) updateGUI(app));
start(app.updateTimer);
end
Make sure to add a property for the timer in your app:
properties (Access = private)
updateTimer % Timer for updating GUI
end
And don't forget to stop and delete the timer when closing the app:
function closeRequest(app)
stop(app.updateTimer);
delete(app.updateTimer);
delete(app);
end
Implementing the Logic Without Freezing the System
Instead of using a while loop within your button callback, consider implementing the logic to check status3 and CarBatteryLevel1EditField.Value within the timer callback (updateGUI).
function updateGUI(app, varargin)
% Existing code to update status3 and CarBatteryLevel1EditField.Value
% Logic to control the switch based on updated conditions
if app.CarBatteryLevel1EditField.Value == 100 && ...
strcmp(app.status3, 'WaitAtStation')
set_param('ChargingProcess/ChargeMyCar1', 'sw', '0');
else
% You can add additional conditions to turn the switch back on, if needed
end
end
Button Callback
function Charge1ButtonPushed(app, event)
set_param('ChargingProcess/ChargeMyCar1', 'sw', '1');
% The conditions are checked and handled in updateGUI
end
I hope it helps to resolve your issue.

Categorías

Más información sobre Create Apps to Control Simulations en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by