TimerFcn loop for arduino

3 visualizaciones (últimos 30 días)
john white
john white el 9 de Abr. de 2019
Editada: john white el 10 de Mayo de 2019
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 comentario
john white
john white el 9 de Abr. de 2019
actually my question is about writting the TimerFcn to do the loop function

Iniciar sesión para comentar.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 10 de Abr. de 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 comentarios
john white
john white el 21 de Abr. de 2019
Editada: john white el 10 de Mayo de 2019
and this is my full code:
function parentFunction
a1=arduino('com5','uno','Libraries','RotaryEncoder');
a2=arduino('com6','uno');
a3=arduino('com3','Due','Libraries','RotaryEncoder');
encoder1=rotaryEncoder(a3,'D2','D3',4000);
encoder2=rotaryEncoder(a3,'D4','D5',4000);
encoder3=rotaryEncoder(a1,'D2','D3',48);
t=timer('Period',0.01,'ExecutionMode','fixedRate');
t.TimerFcn=@function1;
t.TasksToExecute=3000;
start(t)
function function1(~,~)
R=5*diag([1/2 1/2 1/2]);
Q = zeros(9,9); q = 500;
Q(1) = q; Q(7) = -q; Q(55) = -q; Q(61) = q;
Q(11) = q; Q(17) = -q; Q(65) = -q; Q(71) = q;
Q(21) = q; Q(27) = -q; Q(75) = -q; Q(81) = q;
count1=readCount(encoder1);
degree1=(count1/(5*4000))*360;
theta1=degree1*pi/180;
speed1=readSpeed(encoder1);
rpm1=speed1/5;
theta_dot1=rpm1*2*pi/60;
count2=readCount(encoder2);
degree2=(count2/(5*4000))*360;
theta2=degree2*pi/180;
speed2=readSpeed(encoder2);
rpm2=speed2/5;
theta_dot2=rpm2*2*pi/60;
count3=readCount(encoder3);
degree3=(count3/(5*48))*360;
theta3=degree3*pi/180;
speed3=readSpeed(encoder3);
rpm3=speed3/5;
theta_dot3=rpm3*2*pi/60;
T1 = u(1,1);T2 = u(1,2);T3 = u(1,3);
if T1>2
T1=2;
end
if T1<-2
T1=-2;
end
if T2>2
T2=2;
end
if T2<-2
T2=-2;
end
if T3>2
T3=2;
end
if T3<-2
T3=-2;
end
Res=1.003;
kt=0.6835;
kto=Res/kt^2;
vol1=kt.*theta_dot1+kto.*T1;
vol2=kt.*theta_dot2+kto.*T2;
vol3=kt.*theta_dot3+kto.*T3;
pwdc1=vol1/12;
pwdc2=vol2/12;
pwdc3=vol3/12;
p1=abs(pwdc1);
p2=abs(pwdc2);
p3=abs(pwdc3);
if p1>1
p1=1;
end
if p2>1
p2=1;
end
if p3>1
p3=1;
end
writePWMDutyCycle(a1,'D5',p1);
writePWMDutyCycle(a1,'D6',p2);
writePWMDutyCycle(a2,'D5',p3);
if sign(pwdc1)==1
writeDigitalPin(a1,'D7',1);
writeDigitalPin(a1,'D8',0);
else if sign(pwdc1)==-1
writeDigitalPin(a1,'D7',0);
writeDigitalPin(a1,'D8',1);
else if sign(pwdc1)==0
writeDigitalPin(a1,'D7',0);
writeDigitalPin(a1,'D8',0);
end
end
end
if sign(pwdc2)==1
writeDigitalPin(a1,'D4',1);
writeDigitalPin(a1,'D9',0);
else if sign(pwdc2)==-1
writeDigitalPin(a1,'D4',0);
writeDigitalPin(a1,'D9',1);
else if sign(pwdc2)==0
writeDigitalPin(a1,'D4',0);
writeDigitalPin(a1,'D9',0);
end
end
end
if sign(pwdc3)==1
writeDigitalPin(a2,'D7',1);
writeDigitalPin(a2,'D8',0);
else if sign(pwdc3)==-1
writeDigitalPin(a2,'D7',0);
writeDigitalPin(a2,'D8',1);
else if sign(pwdc3)==0
writeDigitalPin(a2,'D7',0);
writeDigitalPin(a2,'D8',0);
end
end
end
end
end
Geoff Hayes
Geoff Hayes el 21 de Abr. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by