Assignin not working in a while loop within a GUI callback?
3 views (last 30 days)
Show older comments
I have been working on a design project where we have a GUI that feeds values to a Simulink Model running on a PandaBoard. I am currently trying to interface a joystick controller for a more accessible way of controlling the values, but I have been running into problems getting the values from the joystick callback function to feed into the workspace. All of the callbacks, including the joystick callback, have been using assignin(base,'ValueName',Value), and they all work as they should except for the joystick callback. The relevant code is below, with some lines removed for readability:
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
CB = get(handles.checkbox2,'Value');
%%%call global variables here
START = 1;
while(CB&START)
axes = axis(joy);
buttons = button(joy);
%%%Determine S1, S2, L1, L2, and G from the button and axis inputs
%%%All 5 variables are doubles.
assignin('base','S1',S1);
assignin('base','S2',S2);
assignin('base','L1',L1);
assignin('base','L2',L2);
assignin('base','G',G);
set_param(MDLNAME,'SimulationCommand','Update');
if buttons(8)
START = 0;
end
CB = get(handles.checkbox2,'Value');
end
For this function, S1, S2, L1, L2, and G are not reaching the Simulink model running on the PandaBoard; however, the same calls to assignin and set_param work in other callback functions for the same GUI. If I comment out the while loop, the function works as it should, but I need the loop to continuously pull the joystick values as long as the checkbox is checked. Is there a better way to assign those values into the workspace? Or did I just make a silly mistake somewhere?
0 Comments
Answers (1)
Geoff Hayes
on 30 Sep 2014
Michael - you've said that other callbacks that make use of assignin work, as does this one if you comment out the while loop. Does that mean that this is the only callback that makes use of assigin from within a while (or any kind of) loop?
It may be, because of the "tight" loop, that no other function has a chance to interrupt this one and perform any of its actions. So the S1, S2, L1, L2, and G may be being updated in the base workspace, but the Simulink model cannot do its "job" with them.
Try adding a pause of 100 milliseconds within your while loop as
while(CB && START)
axes = axis(joy);
buttons = button(joy);
% etc.
CB = get(handles.checkbox2,'Value');
% pause here to allow interruption
pause(0.01);
end
3 Comments
Geoff Hayes
on 30 Sep 2014
Michael - I was going to ask something like "If we ignore Simulink (for the moment) can you observe the changing variables (S1, S2, L1, L2, and G) in the Workspace window as the joystick moves?" but figured I could just try this myself with a simple function and a while loop. However, I couldn't see the variables being updated in the base workspace. They would appear when the function stopped, but not until then.
But just because I can't see them in the workspace browser, doesn't mean that they aren't there. I created the attached simple GUI with a checkbox to start the while loop OR to start a timer (which answers your questions of whether there is an alternative to a while loop). There is a push button that, when pressed, grabs the latest value of a base variable z and displays it in an edit box.
Running through it, with both the while loop or timer option, allows the variable to be obtained from the workspace. So it is there and is being updated.
So how does the Simulink model get that data from the workspace? What is the code for that?
See Also
Categories
Find more on Startup and Shutdown 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!