Shaft encoders to go in a perfect line

I am trying to use a shaft encoder on each side of my robot to run in a straight line. I know how to use a single shaft encoder at a time, but is there a way to run 2 sections of programming at the same time? This would make it so I could just test each side to make sure that it goes the exact amount of rotations, but I don’t know how to accomplish this. Thank you for the help!

You can run multiple encoders at a time. They are interrupt driven so they count based on change in state not based on where in the program they are called. Normally to make a robot drive strait you would import the drive library
and then do

Rotation = LeftEncoder - RightEncoder

I haven’t used the drive library before, for the encoders I have been using while loops. Is there a place where I can find out how to use the drive library? I have never used it before, but it sounds like that’s what I need to learn.

You import the library, a howto is in getting started in the help file.

Once the library is imported you call either TwoWheelDrive or FourWheelDrive once and specify the motor ports the library will use.

Then you call Drive() and give it a speed and heading using values from -127 to 127.


#include "Main.h"

void main ( void )
{
      long target = 0; // Specify the Distance in Clicks
      long leftEnc = 0; // Counts returned by the left Encoder
      long rightEnc = 0; // Counts returned by the right Encoder
      long heading = 0; // The heading the robot will maintain
      long speed = 0; // The velocity the robot will drive at

      TwoWheelDrive ( 1, 2 ) ; // Left Motor Port 1 , Right Motor Port 2
      StartQuadEncoder ( 1 , 5 , 0 ) ; // Start the Left Encoder
      StartQuadEncoder ( 2 , 6 , 1 ) ; // Start the Right Encoder
      target = 180 ; // go 180 encoder clicks or two revolutions
      while ( 1 ) // Loop forever
      {
            leftEnc = GetQuadEncoder ( 1 , 5 ) ; // Get left encoder count
            rightEnc = GetQuadEncoder ( 2 , 6 ) ; // Get right encoder count
            heading = leftEnc - rightEnc ; // subtract the left encoder count from the right encoder count to get heading
            speed = target - (leftEnc + rightEnc / 2) ; // Create a proptional loop to drive a certain distance
            Drive ( speed, heading ) ; // Call the drive command to write values to the motor
      }
}

I’ve attached the program, it will probably need some love but, it should get you started.
Encoder Drive.zip (2.95 KB)