2012-05-18 51 views
1

我正在嘗試在matlab中創建一個定期檢查我的遊戲杆輸入狀態的gui。這個想法是採取這種輸入,並與它一起控制伺服。到目前爲止,我可以讓所有事情分別開展工作,但從未一起工作目前,微控制器只是設置爲查看串行線路並在此基礎上調整伺服(0到180之間)。這工作得很好。這裏是我到目前爲止,如何在MATLAB中定期捕獲遊戲杆輸入gui

function Foo 

h.fig = figure('position', [1100 30 210 60]); 
h.serial = serial('COM3'); 
fopen(h.serial); 
h.joy = vrjoystick(1); 
h.timerObject = timer('TimerFcn',@JoyInput,'ExecutionMode','fixedRate',... 
        'Period',1.0); 

h.buttonOne = uicontrol('style', 'pushbutton' ,... 
       'position', [10 10 100 40],... 
       'string' , 'Start'); 

set(h.buttonOne, 'callback', {@Start, h}) 

function h = Start(hObject, eventdata, h) 
h.buttonTwo = uicontrol('style', 'pushbutton' ,... 
       'position', [100 10 100 40],... 
       'string' , 'Stop'); 
set(h.buttonTwo, 'callback', {@Stop, h}); 
set(h.buttonOne, 'enable', 'off'); 
start(h.timerObject); 
%fprintf(h.serial,'150') This works as is 


function h = Stop(hObject, eventdata, h) 

delete(h.buttonTwo) 
h = rmfield(h, 'buttonTwo'); 
set(h.buttonOne, 'enable', 'on'); 
stop(h.timerObject); 
fclose(h.serial); 
delete(h.serial); 

function h=JoyInput(hObject, eventdata, h) 
fprintf(h.serial,'150') %doesn't work 
% a = 1 % this repetively outputs a=1 without the fprintf 

我得到一個錯誤說

??? Error while evaluating TimerFcn for timer 'timer-17' 

Input argument "h" is undefined. 

我非常新在Matlab使用GUI和我在上這是什麼意思時,我可以用h的損失其他回調函數中的.serial。感謝您的幫助!

回答