Go Down

Topic: Wheel encoder interrupts (Read 3 times) previous topic - next topic

glenz

Hi,
       I am starting to put together my first robot (Boe Bot from Parallax) My microcontroller board is an Arduino Mega(ATmega1280)
I am having problems with the wheel encoders (Boe Bot digital encoder kit from Parallax). The encoder signal lines are connected to Arduino pins 2 & 3.
My problem is that I get interrupts much faster than the slots of the wheels pass the encoders. Also interrupts often continue to come in after the wheels have stopped.Here is my code:
Code: [Select]


#include <Servo.h>
Servo servoRight;
Servo servoLeft;
volatile int Rindex = 0;
volatile int Lindex = 0;

void setup()                                 
{
  attachInterrupt(0,rtEncoderInterrupt,RISING);
  attachInterrupt(1,ltEncoderInterrupt,RISING);
  Serial.begin(9600);
  servoRight.attach(12);
  servoLeft.attach(11);
  servoRight.writeMicroseconds(1700);
  servoLeft.writeMicroseconds(1300);
}
void loop()
{   
  // stop the servos after 16 interrupts from either  encoder
  if((Rindex == 16) || (Lindex == 16))
  {
    servoRight.writeMicroseconds(1500);
    servoLeft.writeMicroseconds(1500);
  }
  // hold execution here
   while((Rindex >= 16)||(Lindex >= 16)){}   
}

void rtEncoderInterrupt()
{
  Serial.println(Rindex);
  Rindex++;
}
void ltEncoderInterrupt()
{
  Serial.print("          ");
  Serial.println(Lindex);
  Lindex++;
}


From this code I expected  to see the Boe Bot wheels turn two reolutions (there are 8 slots in each wheel), and to see the following on the serial monitor:
1
       1
2
        2
3
etc.

Instead I saw the wheels turn 1/4 to 1/2 revolution at constant speed and this on the monitor:
1
2
3
       1
4
        2
        3
5
        6
etc.
the pattern being random. After one of the columns reached 16 ,and the motors were stopped, interrupts continued to occor sporadically.
I have been working at this little sketch for two days and I am at my wits end. Can anyone make a suggestion?

robtillaart


dont do serial prints in an interrupt. They should be as short as possible
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

some patches to your code
Code: [Select]

#include <Servo.h>
Servo servoRight;
Servo servoLeft;
volatile int Rindex = 0;
volatile int Lindex = 0;

void setup()                                 
{
  attachInterrupt(0, rtEncoderInterrupt,RISING);
  attachInterrupt(1, ltEncoderInterrupt,RISING);

  Serial.begin(115200); // why not faster ???

  servoRight.attach(12);
  servoLeft.attach(11);
  servoRight.writeMicroseconds(1700);
  servoLeft.writeMicroseconds(1300);
}
void loop()
{   
  Serial.print(Rindex); 
  Serial.print("          ");
  Serial.println(Lindex);

  // stop the servos after 16 interrupts from either  encoder
  if((Rindex >= 16) || (Lindex >= 16))  // you may just miss ==
  {
    servoRight.writeMicroseconds(1500);
    servoLeft.writeMicroseconds(1500);
  }
  // hold execution here
   while((Rindex >= 16)||(Lindex >= 16)){}   
}

void rtEncoderInterrupt()
{
  Rindex++;
}
void ltEncoderInterrupt()
{
  Lindex++;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

glenz

Thanks Rob,
     Both good suggestions, but alas, they made no improvement.  My ISRs are as brief as I can make them ( Rindex++ ).  I have tried eliminating the Serial.prints altogether and just observed the rotation of the Boe Bot wheels and they rotated randomly 1/4 to 1/2 revolution, sometimes running on indefinitely until I pulled the power.
Glen

robtillaart

if the ints overflow fast it can have side effects

change

volatile int Rindex = 0;
volatile int Lindex = 0;

in
volatile unsigned long Rindex = 0;
volatile unsigned long Lindex = 0;

then it will takes far longer before overflow ...

do you have pull-down resistors on the irq lines?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up