Clear Filters
Clear Filters

How to update constant value in Simulink while it is running?

15 views (last 30 days)
Hello
I am currently working on AR.Drone in MATLAB. I was able to run the script to make Drone to move through the script file. However, i don't know how I can control it in real-time like keyboard pressing.
In my simulink, it has "waypoints" constant block that refer from the one of my script file. However, if I want to implement that constant block or script to control by keyboard or real time input, I wasn't able to change my waypoints during I am running my simulink to control AR.Drone.
Does anyone have code to control AR.Drone 2.0 in keyboard or any other real-time interrupting inputs?

Answers (1)

Nihar Deodhar
Nihar Deodhar on 28 Jun 2016
The easiest way to update simulink block parameters in real time (while the model is running) is through a GUI (graphic user interface). Read matlab documentation on GUIDE functionality.
A simple illustration to create a GUI is shown below:
On the matlab command window type guide
select create a new GUI.
for our application, a simple GUI design similar to the one shown should work just fine:
It consists of a text to type in and a pushbutton to update.
Then go to view in the menu tab: > view callbacks:> CreateFcn
This will open up a matlab code that executes the GUI;
for every set of pushbutton and text block that you have edit this portion in the m file corresponding to the respective callbacks:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
A = get(handles.edit1, 'string');
set_param('simulink_file_name/subsystem_name_if_a_subsystem_exists/block_name', 'Value', A);
% those are the two main functions get and set in any GUI application.
save the code and GUI fig in the same folder as that of the simulink model.
Make sure you run the m file before starting the simulink model. There after just type in the value and hit update, the block value should change.
One important note: sometime you might have to use
set_param('simulink_file_name/subsystem_name_if_a_subsystem_exists/block_name', 'Value', num2str(A));
instead of just
set_param('simulink_file_name/subsystem_name_if_a_subsystem_exists/block_name', 'Value', A); as the constant block only accepts string inputs.

Categories

Find more on Simulation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!