Trouble getting Redbot to turn using encoders

I am trying to turn a redbot precisely (relative) 90° using the encoders but I cannot figure out how to do this using the encoder tutorial. Any thoughts on how to do this?

I think i am getting lost in the serial side of things. Pretty much i want the robot to drive straight turn 90° drive the same disrance back and end up back at the start. 

https://learn.sparkfun.com/tutorials/ex ... ncoder-sik

 

thanks!

Sparkfun wheel encoders

Which wheel encoders do you have? Neither the Sparkfun or Dagu are very precise when used on the wheels. For more accurate results, you probably want quadrature encoders directly on the motor shaft.

The theory is simple, the

The theory is simple, the implementation might be a bit harder.  

Just glancing through the tutorial quickly, I don’t see where this is covered.  

This is the basic algorithm though:  

Measure the distance between your drive wheels.  Let’s say it is 10 cm.  From that geometry class you and I didn’t pay much attention in:

2 * pie * r = circumference of circle

6.28 * 10cm diameter/2 = 31.4 cm

So, for you to turn 90 degrees, each wheel has to turn 31.4/4 = 7.85 cm (one wheel goes backwards, one wheel goes forward).  Assume that your wheel is 3.0 cm diameter,

6.28 * 3.0 cm diameter/2 = 9.42 cm per wheel turn / 192 ticks per wheel turn (found that in the tutorial) = 0.0490625 cm / tick

7.85 cm = x number of ticks * 0.0490625 cm / tick = ~ 160 ticks 

The goal is then to have each wheel spin 160 ticks, one in one direction and the other in the other direction.  If you measure accurately the distance between the wheels and the wheel size, you will get very accurate results.

I hope this helps.

Regards,

Bill 

 

 

 

 

 

 

 

Yes i understand how to do

Yes i understand how to do it but i cannot make the code work at all, maybe i sould have gone into computer science and not mechanical ; )

When i do get the program to compile, the robot just drives in a straight line. Let me post the code up.

/

/

   2PiR

   wheel distance = 157.48mm

   2Pi(157.48)=989.47

   1/4 turn = 989.47/4 = 247.37

   247counts for 1/4 turn or 120 per encoder to turn in place

 ****************************/

#include <RedBot.h>

RedBotMotors motors;

 

RedBotEncoder encoder = RedBotEncoder(A2, 10);

int buttonPin = 12;

int countsPerRev = 192;   // 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev

 

float wheelDiam = 2.56;  // diam = 65mm / 25.4 mm/in

float wheelCirc = PI * wheelDiam; // Redbot wheel circumference = pi*D

 

void setup()

{

  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(9600);

}

 

void loop(void)

{

  // set the power for left & right motors on button press

  if (digitalRead(buttonPin) == LOW)

  {

    turn();

  }

}

 

void turn()

{

  long lCount = 0;

  long rCount = 0;

  long targetCount;

  float numRev;

  motors.leftMotor(-80);

  motors.rightMotor(0);

 

  // variables for tracking the left and right encoder counts

  long prevlCount, prevrCount;

 

  long lDiff, rDiff;  // diff between current encoder count and previous count

 

 

 

  encoder.clearEnc(BOTH);    // clear the encoder count

  delay(100);  // short delay before starting the motors.

 

 

  while (rCount < 240)

  {

    // while the right encoder is less than the target count – debug print

    // the encoder values and wait – this is a holding loop.

    lCount = encoder.getTicks(LEFT);

    rCount = encoder.getTicks(RIGHT);

    Serial.print(lCount);

    Serial.print("\t");

    Serial.print(rCount);

    Serial.print("\t");

    Serial.println(targetCount);

 

 

    // calculate the rotation “speed” as a difference in the count from previous cycle.

    lDiff = (lCount - prevlCount);

    rDiff = (rCount - prevrCount);

 

    // store the current count as the “previous” count for the next cycle.

    prevlCount = lCount;

    prevrCount = rCount;

 

    }

    delay(50);  // short delay to give motors a chance to respond.

  }

 

Code

Couple of observations:

  1. Where to do you specify which I/O pins are used for the motors?
  2. You are starting the left motor in reverse and waiting for the right motor to count.

 

you dont specify pins since

you dont specify pins since the redbot uses its own board. 

 

https://www.sparkfun.com/products/12097?gclid=CInJp-rmysoCFQ8vaQodL-oMHA

 

GOT IT!I just need to change

GOT IT!

I just need to change the “while (rCount < 240)” to lCount and add motors.brake(); to the end.