我正在尝试将 arduino 代码转换为 matlab 代码。该代码适用于使用四个 LDR 的太阳能跟踪器,这些 LDR 的连接方式是伺服电机将移动,因此 LDR 上的平均读数为零。我在这里找到它:http: //www.instructables.com/id/Arduino-Solar-Tracker/
我将它用于我们的项目,但我们需要使用 matlab 并有一个 GUI。
Arduino代码:
#include <Servo.h> // include Servo library
Servo horizontal; // horizontal servo
int servoh = 90; // stand horizontal servo
Servo vertical; // vertical servo
int servov = 90; // stand vertical servo
// LDR pin connections
// name = analogpin;
int ldrlt = A0; //LDR top left
int ldrrt = A1; //LDR top rigt
int ldrld = A2; //LDR down left
int ldrrd = A3; //ldr down rigt
int spd = A4; //speed
int tole = A5; //tolerance
void setup()
{
Serial.begin(9600);
// servo connections
// name.attacht(pin);
horizontal.attach(9);
vertical.attach(10);
}
void loop()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(spd)/20; // read potentiometers
int tol = analogRead(tole)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the difference of up and down
int dhoriz = avl - avr;// check the diffirence of left and right
if (-1*tol > dvert || dvert > tol) // check if the difference is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = ++servov;
if (servov > 180)
{
servov = 180;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl = avr)
{
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
}
到目前为止,这是我能够锻炼的 MATLAB 代码:
clear all;clc;
c=arduino('COM31'); %Create Arduino Object
servoAttach(c,9); %Horizontal Servo
servoAttach(c,10); %Vertical Servo
servoStatus(c,9);
servoStatus(c,10);
servoh=90; %Horizontal Servo initial position
servov=90; %Vertical Servo initial position
tic
while toc < 60
lt=c.analogRead(0)
rt=c.analogRead(1)
ld=c.analogRead(2)
rd=c.analogRead(3)
spd=c.analogRead(4)/20
tol=c.analogRead(5)/4
avt = (lt + rt) / 2; % average value top
avd = (ld + rd) / 2; % average value down
avl = (lt + ld) / 2; % average value left
avr = (rt + rd) / 2; % average value right
dvert = avt - avd; % check the diffirence of up and down
dhoriz = avl - avr; % check the diffirence of left and right
%check if the diffirence is in the tolerance else change vertical angle
if (-1*tol > dvert || dvert > tol)
if (avt > avd)
servov = servov+1;
if servov > 180;
servov = 180;
end
servoWrite(c,10,servov);
end
else if (avt < avd)
servov = servov-1;
if (servov < 0)
servov = 0;
end
servoWrite(c,10,servov);
end
%servoWrite(c,10,servov);
end
%check if the diffirence is in the tolerance else change horizontal angle
if (-1*tol > dhoriz || dhoriz > tol)
if (avl > avr)
servoh = servoh-1;
if (servoh < 0)
servoh = 0;
end
servoWrite(c,9,servoh);
end
else if (avl < avr)
servoh = servoh+1;
if (servoh > 180)
servoh = 180;
end
servoWrite(c,9,servoh);
end
%servoWrite(c,9,servoh);
end
end
我的问题是伺服系统在 matlab 中移动非常缓慢。我不确定这是否是因为代码有问题。我什至删除了暂停代码,但它没有帮助。另外,我只使用 tic toc 进行测试,因为我不知道用什么来替换“无效循环”。对于 GUI,我希望程序持续运行直到按下停止按钮,并且伺服的延迟(速度)和 LDR 的容差将使用滑块改变。我在将滑块的值返回到主程序时遇到问题。