Hi all,
I am working on estimating robot position and velocity by using its encoder here's the encoder properties:http://www.robotshop.com/en/12v-120-rpm-dc-motor-641.html
but always the velocity is zero , I do not have any idea what the problem is,Can anyone give me the solution?
Hereby, you will find an attached image, it is the wiring of the circuit.
// Define pin locations and delay time between each query for wheel encoding
#define RightEncoderPinA 4
#define RightEncoderPinB 2
#define LeftEncoderPinA 3
#define LeftEncoderPinB 6
// Motor pins
int leftMotor = 5;
int rightMotor = 9;
// Speed output variables
double leftspeedcmd = 0;
double rightspeedcmd = 0;
// Encoder counters
volatile unsigned int LeftEncoderPos = 0;
volatile unsigned int RightEncoderPos = 0;
// Sample time for velocity calculation. Lower resolution encoders need a longer sample time for an accurate reading
#define DELAY_TIME 100
// Define physical constants of the wheel for wheel encoding
#define WHEEL_CIRCUMFERENCE 0.15079 // In meters
#define WHEEL_TICKS 768 // The number of encoder 'ticks' for a full wheel cycle
// Velocity stuff
double leftvelocity = 0;
double rightvelocity = 0;
void setup()
{
InitializeHardware();
}
// Modify your target speeds here
void loop()
{
// these setpoints are in m/s
analogWrite (rightMotor,0);
analogWrite (leftMotor,255);
//Serial.print("LeftEncoder Ticks: ");
//Serial.print(LeftEncoderPos);
Serial.print("RightEncoderB Ticks: ");
Serial.print( RightEncoderPos);
Serial.print("RV: ");
Serial.print(rightvelocity);
Serial.print("\t");
//Serial.print(" LRevolutions: ");
//Serial.print(LeftEncoderPinA/320);
//Serial.print(LeftEncoderPinB/768);
// Serial.print(" NewRPos : ");
//Serial.print( NewRPos );
Serial.print("\n");
}
// Initialize all hardware components
void InitializeHardware()
{
// Set input pin state for the line sensors
Serial.begin(9600);
pinMode(LeftEncoderPinA, INPUT);
pinMode(LeftEncoderPinB, INPUT);
pinMode(RightEncoderPinA,INPUT_PULLUP);
pinMode(RightEncoderPinB, INPUT_PULLUP);
pinMode(leftMotor,OUTPUT);
pinMode(rightMotor,OUTPUT);
// This function sets the EncoderEvent() function to be called whenever a line detector detects a change
attachInterrupt(0, RightEncoderEvent, CHANGE);
attachInterrupt(1, LeftEncoderEvent, CHANGE);
}
// Release all hardware components
void ReleaseHardware()
{
// Release the interrupt registration
detachInterrupt(0);
detachInterrupt(1);
}
// Where the magic happens
// Return the real-world vehicle speed (in meters per second)
void GetSpeeds()
{
long NewLPos;
long NewRPos;
long OldLPos;
long OldRPos;
double LdVal = 0;
double RdVal = 0;
// Find the old and new encoder positions
OldLPos = LeftEncoderPos;
OldRPos = RightEncoderPos;
delay(DELAY_TIME);
NewLPos = LeftEncoderPos;
NewRPos = RightEncoderPos;
// If an integer overflow occurs, throw out the new value
if(abs(NewLPos - OldLPos) < 60000)
LdVal = ((double)(NewLPos - OldLPos) / (double)DELAY_TIME) * 1000.0;
if(abs(NewRPos - OldRPos) < 60000)
RdVal = ((double)(NewRPos - OldRPos) / (double)DELAY_TIME) * 1000.0;
// Find the linear velocity
double LangVel = LdVal / (double)WHEEL_TICKS;
leftvelocity = (LangVel * WHEEL_CIRCUMFERENCE);
double RangVel = RdVal / (double)WHEEL_TICKS;
rightvelocity = (RangVel * WHEEL_CIRCUMFERENCE);
}
// Encoder events for the interrupt calls
void LeftEncoderEvent()
{
if (digitalRead(LeftEncoderPinA) == HIGH)
{
if (digitalRead(LeftEncoderPinB) == LOW)
LeftEncoderPos++;
else
LeftEncoderPos--;
}
else
{
if (digitalRead(LeftEncoderPinB) == LOW)
LeftEncoderPos--;
else
LeftEncoderPos++;
}
}
void RightEncoderEvent()
{
if (digitalRead(RightEncoderPinA) == HIGH)
{
if (digitalRead(RightEncoderPinB) == LOW)
RightEncoderPos++;
else
RightEncoderPos--;
}
else
{
if (digitalRead(RightEncoderPinB) == LOW)
RightEncoderPos--;
else
RightEncoderPos++;
}
}
Maisa:
Hi all,
I am working on estimating robot position and velocity by using its encoder
Regards
Should always do the usual and get back to bare minimum basics. So start off with bare minimum code to test the most important things.... such as whether your arduino is receiving basic signals from the interrupt events (for rising edge signals etc).
Like, use 1 encoder only to begin with. Set up your pin and interrupt to detect signal changes for 1 channel (eg. channel A) of that encoder. Write code so that something gets printed onto the Serial Monitor whenever a rising edge is detected by the interrupt pin. Also, if you are using INTERNAL arduino pullup resistors for the interrupt pin, then use the "INPUT_PULLUP" codeword for the software setup of the interrupt input pin.
Manually rotate the encoder and watch the serial monitor. If you start seeing activity when you manually turn the encoder, then that would be a good sign.
Southpark:
Should always do the usual and get back to bare minimum basics. So start off with bare minimum code to test the most important things.... such as whether your arduino is receiving basic signals from the interrupt events (for rising edge signals etc).
Like, use 1 encoder only to begin with. Set up your pin and interrupt to detect signal changes for 1 channel (eg. channel A) of that encoder. Write code so that something gets printed onto the Serial Monitor whenever a rising edge is detected by the interrupt pin. Also, if you are using INTERNAL arduino pullup resistors for the interrupt pin, then use the "INPUT_PULLUP" codeword for the software setup of the interrupt input pin.
Manually rotate the encoder and watch the serial monitor. If you start seeing activity when you manually turn the encoder, then that would be a good sign.
I have done what you said,the encoder detects the edges and in the last code also detects,because it increments or decrease if it's detects any change if it's rising or falling.So the basics is right,where's the problem ??any suggests?
Maisa:
I have done what you said,the encoder detects the edges and in the last code also detects,because it increments or decrease if it's detects any change if it's rising or falling.So the basics is right,where's the problem ??any suggests?
Your function.... "GetSpeeds()"... it is a function in your code. But it never appears to be used (aka ..... is never 'called').
In other words, you forgot to use this function that is meant to calculate the value that you're interested in.