所以我有这个旧的电动轮椅,我想把它改装成机器人。我用 2x12 的 sabertooth 替换了原来的电机驱动器,我正在使用 Arduino micro 与它交谈。电机轴一直抛出,所以我在背面连接了一个磁铁和一个霍尔效应传感器,作为旋转编码器。我目前的目标是能够告诉机器人向前移动一定量的脚然后停止。我写了一些代码来线性地做到这一点,但这并没有那么好。然后我了解了中断,这听起来正是我所需要的。所以我尝试了这一点,但在几个不同的层面上都出了问题。
第一级:我似乎从来没有能够正确地驱动电机,就像任何时候我在循环内发出命令来打开它们,或者如果语句他们决定做他们想做的事情并且偶尔和不可预测地移动
第二级:我觉得中断正在中断自己,我设置的东西是阻止轮子向前移动,因为我可以告诉它向前移动 14 个旋转编码器咔嗒声,一个轮子将继续移动超过 1000 次咔嗒声,而另一个停止
第三级:有几次我想我把中断放错了,因为当我上传代码时,窗口将停止识别 Arduino,我的驱动程序会中断,直到我在按下复位按钮后上传闪烁草图,该按钮也重新加载并修复了我的驱动程序。然后,如果我删除了其中一个中断,它将正常上传。
第四级:当电机打开时,我的霍尔效应传感器似乎无法正常工作。他们往往会在几秒钟内从 1 次点击跳到 200 次点击。这反过来又淹没了我的串行端口并使 Arduino ide 崩溃。
所以你可以看到系统中的某个地方存在一些缺陷,无论是我不知道的硬件还是软件。我是否以正确的方式接近这一点,还是有一些我不知道的 Arduino 秘密会让我的生活更轻松?如果我接近这个权利,你可以看看我下面的代码,看看我做错了什么。
#include <Servo.h>//the motor driver uses this library
Servo LEFT, RIGHT;//left wheel right wheel
int RclickNum=0;//used for the rotory encoder
int LclickNum=0;//these are the number of "clicks" each wheel has moved
int D =115;//Drive
int R =70;//Reverse
int B =90;//Break
int Linterrupt = 1;//these are the interrupt numbers. 0 = pin 3 and 1 = pin 2
int Rinterrupt = 0;
int clickConvert = 7;// how many rotery encoder clicks equal a foot
void setup()
{
Serial.begin(9600); //starting serial communication
LEFT.attach( 9, 1000, 2000);//attaching the motor controller that is acting like a servo
RIGHT.attach(10, 1000, 2000);
attachInterrupt(Linterrupt, LclickCounter, FALLING);//attaching the rotory encoders as interrupts that will
attachInterrupt(Rinterrupt, RclickCounter, FALLING);//trip when the encoder pins go from high to low
}
void loop()
{//This is for controling the robot using the standard wasd format
int input= Serial.read();
if(input == 'a')
left(2);
if(input == 'd')
right(2);
if(input == 'w')
forward(2);
if(input == 's')
backward(2);
if(input == 'e')
STOP();
}
void forward(int feet)//this is called when w is sent threw the serial port and is where i am testing all of my code.
{
interrupts(); //turn on the interrupts
while(RclickNum < feet * clickConvert || LclickNum < feet * clickConvert)// while either the left or right wheel hasnt made it to the desired distance
{
if(RclickNum < feet * clickConvert)//check if the right wheel has gone the distance
RIGHT.write(D); //make the right wheel move
else
RIGHT.write(B);//stop the right wheel
if(LclickNum < feet * clickConvert)
LEFT.write(D);
else
LEFT.write(B);
}
noInterrupts();//stop the interrupts
resetCount();//set the click counters back to zero
}
//once i have the forward function working i will implament it through out the other functions
//----------------------------------------------------------------------
void backward(int feet)
{
RIGHT.write(R);
LEFT.write(R);
}
void left(int feet)
{
RIGHT.write(D);
LEFT.write(R);
}
void right(int feet)
{
RIGHT.write(R);
LEFT.write(D);
}
void STOP()
{
resetCount();
RIGHT.write(B);
LEFT.write(B);
}
void LclickCounter()//this is called by the left encoder interrupt
{
LclickNum++;
Serial.print("L");
Serial.println(LclickNum);
}
void RclickCounter()//this is called by the right encoder interrupt
{
RclickNum++;
M Serial.print("R");
Serial.println(RclickNum);
}
void resetCount()
{
RclickNum=0;
LclickNum=0;
}