TimerFcn loop for arduino
显示 更早的评论
hi
I want to read some data from arduino with timer object and I want to read these data each 0.01 second as input datas and calculate datas and give output datas
the code that I want to use is:
t=timer('Period',0.01,'ExecutionMode','fixedRate');
t.TimerFcn=@(function1);
start(t)
function [T]=function1(x)
x=readCount(encoder);
k=5*x;
T=k+3;
end
is it correct?
回答(1 个)
Geoff Hayes
2019-4-10
john - is the above code nested within another function so that the function1 has access to the encoder variable which (presumably) has been initialized in that other/parent function? Note that there is no output parameter for a timer function callback, and the default parameters (of which there are generally two) correspond to the handle of the timer object and some event data (if it exists). So the above might not work as you intend. An example might be
function timerTest
k = 1;
T = [];
t=timer('Period',0.01,'ExecutionMode','fixedRate');
t.TimerFcn=@function1;
start(t)
function function1(~,~)
T(k) = randi(256,1,1);
k = k + 1;
end
end
Note how we must define the nested variables that the function1 callback will access before we assign the timer callback. And instead of returning an output parameter from the callback - which you cannot do - we store the result in an array (or you could do something with that value (i.e. update a plot) from within the callback.
19 个评论
john white
2019-4-10
Geoff Hayes
2019-4-10
do this in a loop...do what in a loop? until the result be what I want...what is the condition then for stopping then?
john white
2019-4-10
编辑:john white
2019-4-10
Geoff Hayes
2019-4-10
so by loop you just mean the timer firing every 0.01 seconds. So to stop you just compare T against the special value to end the timer?
function function1(~, ~)
x=readCount(encoder);
k=5*x;
T=k+3;
if T == specialValue
stop(t);
end
end
john white
2019-4-11
john white
2019-4-12
Geoff Hayes
2019-4-13
John - if you have a timer, then you won't need a loop since the timer will fire every 0.01 seconds (or whatever you choose) which behaves much like a loop.
john white
2019-4-14
Geoff Hayes
2019-4-14
Yes, the timer will execute 1000 times and then will stop (you haven't defined a StopFcn. But your TimerFcn is still incorrectly defined (unless something has changed in a later version of MATLAB). You cannot return an output parameter and there are generally two inputs to your timer function - see Timer Callback Functions for details. Your function needs to look more like
function function1(~,~)
x=readCount(encoder);
k=5*x;
T=k+3;
end
where T has already been defined in the parent function (assuming that you are using nested functions like I showed above). Your StopFcn, which executes when the timer stops, could then use T to do "something".
john white
2019-4-19
Geoff Hayes
2019-4-20
john - for your first set of code, are you nesting the callback within the parent function? You need to do something like
function parentFunction
a1=arduino('com3','uno','Libraries','RotaryEncoder');
encoder1=rotaryEncoder(a1,'D2','D3',4000);
t=timer('Period',0.01,'ExecutionMode','fixedRate');
t.TimerFcn=@function1;
t.TasksToExecute=3000;
start(t)
function function1(~,~)
x=readCount(encoder1);
k=5*x;
T=k+3
end
end
where the above code is saved to a parentFunction.m file. Your error message is suggesting that you aren't doing this and so the callback cannot find the encoder1 variable.
As for your second set of code, I don't think that you want to be opening a connection to the arduino device every 0.01 seconds. The error message references a variable named a but I don't see that in your code....
john white
2019-4-20
Geoff Hayes
2019-4-20
try writing with the parent function.
john white
2019-4-21
Geoff Hayes
2019-4-21
john - i feel that you are not showing all of your code because nowhere in your timer callback do i see any of the three encoders being used. please attach your code rather than showing a partial screenshot.
john white
2019-4-21
john white
2019-4-21
john white
2019-4-21
编辑:john white
2019-5-10
Geoff Hayes
2019-4-21
John - so looking closer at the error message
The value of the "a" property must be a numeric array without any Inf's or NaN's.
it isn't actually referring to a variable (in your code) named a. It is more likely referring to a parameter that is being passed into a function whose input parameter name (as defined in the function signature) is named a. The problem is that I don't know which of the functions might be the problem. It could be lqr since I've seen other posts (that have this same error message) with other functions from the Control System Toolbox. You may need to reduce your code to the smallest working example that generates this error so that you can pinpoint which function call is responsible for it.
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
