TimerFcn loop for arduino
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
Réponses (1)
Geoff Hayes
le 10 Avr 2019
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 commentaires
Geoff Hayes
le 21 Avr 2019
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.
Voir également
Catégories
En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!