How can I prevent or recover from a "joystick device error" during timer callback?

1 visualización (últimos 30 días)
I have a script that repeatedly reads the position of a joystick initiated by vrjoystick() using read(). The repetition is currently handled by a timer() callback. This works quite well... most of the time. However, the timer will occasionally crash and return the following error:
"Joystick device error: Failed to read joystick status."
Below is a minimum working example. Unfortunately you will need a joystick to run it. It will often run successfully for a few minutes before crashing, so please wait a bit before responding that it works.
Thank you very much for any help you can provide, even just brainstorming.
function MWE
figure('CloseRequestFcn','delete(timerfind); delete(gcf);');
a = axes('XLim',[-1,1],'YLim',[-1,1],'NextPlot','replacechildren');
j = vrjoystick(1);
t = timer('ExecutionMode','fixedRate','Period',0.01,'TimerFcn',@t_Callback);
start(t);
function t_Callback(~,~)
[position,~,~] = read(j);
plot(a,position(1),position(2),'ko');
end
end

Respuestas (1)

Jeffrey Girard
Jeffrey Girard el 1 de Nov. de 2014
Ok, success! Recovery can be solved by restarting the joystick within the catch-statement of a try-catch block. Does anyone have insight into why this is happening and/or how to prevent it?
function MWE
figure('CloseRequestFcn','delete(timerfind); delete(gcf);');
a = axes('XLim',[-1,1],'YLim',[-1,1],'NextPlot','replacechildren');
j = vrjoystick(1);
t = timer('ExecutionMode','fixedRate','Period',0.01,'TimerFcn',@t_Callback);
start(t);
function t_Callback(~,~)
try
[position,~,~] = read(j);
catch
j = vrjoystick(1);
return;
end
plot(a,position(1),position(2),'ko');
end
end

Categorías

Más información sobre Simulink Supported Hardware en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by