Hi!
I'm trying to do odometry with my robot. The motor encoders are correctly installed and working (moving the wheels manually Arduino generates the desired interrupts) however when i give some power to the wheels the motor encoders start going crazy.
If i move just one wheel both of them start generating interrupts!! The pull-up resistors are on and i checked everything a thousand times.
What is really driving me crazy is that this behavior is not happening when i move the wheels manually so i guess it's not an autoinduction problem.
I'll post my code in case it helps somehow.
// H-Bridge setup
#define port2 5
#define port7 6
#define port10 9
#define port15 10
// IR ranger port
#define IRPort A5
#define IRCtrl 7
// Encoder pins
#define REncoder 2
#define LEncoder 3
int REncoderCount = 0;
int LEncoderCount = 0;
/************************************************
SETUP
***********************************************/
void setup()
{
// Setup the H-Bridge
pinMode(port2, OUTPUT);
pinMode(port7, OUTPUT);
pinMode(port10, OUTPUT);
pinMode(port15, OUTPUT);
// Setup the encoders
pinMode(REncoder, INPUT);
digitalWrite(REncoder, HIGH); // turn on pullup resistor
pinMode(LEncoder, INPUT);
digitalWrite(LEncoder, HIGH); // turn on pullup resistor
attachInterrupt(0, doREncoder, CHANGE);
attachInterrupt(1, doLEncoder, CHANGE);
// Setup the IR sensor
pinMode(IRCtrl, OUTPUT);
digitalWrite(IRCtrl, LOW); // Disable the sensor initially
// Set up the serial communications
Serial.begin(9600); // send and receive at 9600 baud
}
/************************************************
LOOP
***********************************************/
void loop()
{
rightWheelForward(100);
// delay(1000);
// parada();
// delay(5000);
// leftWheelForward(100);
// delay(1000);
// parada();
// delay(5000);
}
/************************************************
ROUTINES
***********************************************/
void rotate(int dir, int vel)
/*
Routine for rotating the robot
dir:
0 -> rotate clockwise
1 -> rotate counterclockwise
vel: velocity given to the motors PWM, 0-255
*/
{
if(dir == 0)
{
// Left wheel forward
leftWheelForward(vel);
rightWheelBackward(vel);
Serial.println("Rotating clockwise...");
} else
{
// rotate counterclockwise
rightWheelForward(vel);
leftWheelBackward(vel);
Serial.println("Rotating counterclockwise...");
}
}
void leftWheelForward(int vel)
{
analogWrite(port2, 0);
analogWrite(port7, vel);
}
void leftWheelBackward(int vel)
{
analogWrite(port2, vel);
analogWrite(port7, 0);
}
void rightWheelForward(int vel)
{
analogWrite(port10, 0);
analogWrite(port15, vel);
}
void rightWheelBackward(int vel)
{
analogWrite(port10, vel);
analogWrite(port15, 0);
}
void forward(int vel)
{
leftWheelForward(vel);
rightWheelForward(vel);
Serial.print("Going forward with vel: ");
Serial.println(vel);
}
void backward(int vel)
{
leftWheelBackward(vel);
rightWheelBackward(vel);
Serial.print("Going backward with vel: ");
Serial.println(vel);
}
void doREncoder() {
/*
Routine activated by the interrupt 0, counts the amount moved by the right wheel
*/
REncoderCount++;
Serial.print("Right encoder: ");
Serial.println(REncoderCount, DEC);
}
void doLEncoder() {
/*
Routine activated by the interrupt 1, counts the amount moved by the right wheel
*/
LEncoderCount++;
Serial.print("Left encoder: ");
Serial.println(LEncoderCount, DEC);
}
void parada()
{
rightWheelForward(0);
leftWheelBackward(0);
}
int irRead()
/*
Returns the distance measured from the IR sensor (0-1024).
Check the datasheet to see how the values are mapped
*/
{
int distance = 0;
int averaging = 0;
// turn on, wait 250 ms to completely stabilize TEST THIS
digitalWrite(IRCtrl,HIGH);
delay(250);
// Get a sampling of 5 readings from sensor and average them
for (int i=0;i<5 ; i++){
distance = analogRead(IRPort);
averaging+=distance;
delay(55);
}
distance = averaging/5; // Average of all the measurements
digitalWrite(IRCtrl, LOW); // Turn down the IR Sensor
return(distance);
}
Any advice will be appreciated.
Thanks,
Víctor