Go Down

Topic: Problem with encoder readings. Using Arduino Uno and Motor Shield v2.3 (Read 1 time) previous topic - next topic

jesus151089

Hello,

I have this http://robotshop.com/en/dfrobot-wheel-encoders-dfrobot-3pa-4wd-rovers-2pk.html sensor attached to my mobile robot http://www.dfrobot.com/index.php?route=product/product&path=37_111&product_id=65 and using Arduino Uno along with Motor Shield v2.3.

The problem is that if one of the wheels is not turning and the other is, I get some values from the encoder attached to the wheel that is not turning due to the motion of the other wheel that is turning.


I have tried also to connect the Motor shield to the Arduino using 4 pins (+5V, GND, A5, A4) instead of putting it on top of the Arduino, but this gives the same result


However I disconnected the Motor Shield from the Uno and directly supplied voltage to the motors and
then I was getting good readings from the encoders, i.e one wheel turning did not affect the values given by the encoder from the other wheel (no coupling).


What do you  think might be causing this?

Here is the code that I am using:

Code: [Select]

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *M1 = AFMS.getMotor(1);
Adafruit_DCMotor *M2 = AFMS.getMotor(2);
 
// Loop time for calculating velocity
#define DELAY_TIME 10 // In ms
 
// Define physical constants of the wheel for wheel encoding
#define WHEEL_TICKS 20          // The number of 'ticks' for a full wheel cycle
 
// Variables for storing the calculated velocity
double RangVel;
double LangVel;

 
// Variables for storing position info
long OldLPos;
long NewLPos;
long OldRPos;
long NewRPos;
 
// Volatile variables that can be changed inside of interrupt functions
volatile unsigned int RightEncoderPos = 0;
volatile unsigned int LeftEncoderPos = 0;
double LdVal = 0;
double RdVal = 0;
 
void setup()
{
  attachInterrupt(0, LeftEncoderEvent, CHANGE);  //INT0, pin 2
  attachInterrupt(1, RightEncoderEvent, CHANGE); //INT1, pin 3
 
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
 
  AFMS.begin();
 
  M1->setSpeed(250);
  M2->setSpeed(0);
  M1->run(FORWARD);
  M2->run(FORWARD);
 
  Serial.begin(9600);
}

 
void loop()
{
    GetSpeeds();
 
    Serial.print("Left Velocity: ");
    Serial.print(LeftEncoderPos);
    Serial.print("Right Velocity: ");
    Serial.println(RightEncoderPos);
 
 
}
 

void GetSpeeds()
{
OldLPos = LeftEncoderPos;
        OldRPos = RightEncoderPos;
delay(DELAY_TIME);
NewLPos = LeftEncoderPos;
        NewRPos = RightEncoderPos;
 
LdVal = ((double)(NewLPos - OldLPos) / (double)DELAY_TIME) * 1000.0;
RdVal = ((double)(NewRPos - OldRPos) / (double)DELAY_TIME) * 1000.0;
 
LangVel = LdVal*2*3.14 / (double)WHEEL_TICKS;
 
        RangVel = RdVal*2*3.14 / (double)WHEEL_TICKS;
}
 
 
void RightEncoderEvent()
{
      RightEncoderPos++;
}

 

void LeftEncoderEvent()
{   
      LeftEncoderPos++;   
}





Thanks in advance

Jshield

I would suggest you to double check the wiring and make sure no interference is affecting the sensors.
Have you tried switching the working motor with the 'not-working' motor?

MarkT

The interference, if it is interference, will be from the motors and motor drivers,
so make sure to run encoder cables well away from the motors/drivers.

Add 2k2 pull up resistors to each encoder pin (the internal pullups are too weak
for a noisy environment like a robot).

[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up