Skip to main content

VEX V5 - Encoder Sensors (Smart Motors)


Driving and maneuvering using just time as the controlling variable can be unpredictable and lack in accuracy when precision is needed. This is where motor encoders come in. Encoders track speed and position of a motor shaft, which makes getting to specific targets and having accurate movement much easier if speed and position can now be tracked.

 LM.velocity(rpm);        //returns velocity of motor, can take rpm or pct

LM.rotation(rev);          //returns the amount of rotations completed, can take revolutions or degrees

Inch Drive

Inch Drive is a function that tells the robot to drive a certain distance, in inches, and stop. It uses the encoder position of the motor axle and some basic geometry. This is also the basic version of a proportional controller from a PID, though it is somewhat different in design.

In this function we are setting up a global variable dia (diameter) of the wheel which is 4 inches. Using the diameter of the wheel we could calculate how far a wheel rolls when the robot moves forward using circumference 

Circumference = dia * Pi 


After giving the function a name and setting its parameters, then we will set a local variable that will be used to calculate the circumference based on how many rotations the wheel has done. 

InchDrive Process

The process is first set all the variables and reset our encoder to zero then:

1. Move the robot a small amount

2. Measure the distance moved

3. Check if distance is still less than target

4. repeat the steps 1,2,3  until distance is = or > target then stop the robot


Using a while loop we could set a condition that will check how much we have turned compared to the target we are trying to reach and as long as X is less than the target we keep moving and as soon as the x becomes equal or bigger than the target we have set then it exits the while loop and the next line of code should stop the robot. 

Lastly you have to cal the function in the main part of our code and give it the target number which is how many inches you want to move forward and at what speed. The slower speed will give more time to the robot to come to a stop before we go past the target. 

 // GLOBAL Variables
float dia=4.0; //wheel diameter in inches

void inchDrive(float target, int speed)                 //takes target distance and speed as parameters
    float x=0;                                                         //variable that will be current distance
  while(x<=target)                                                //proportional control feedback loop for error
    x=LM.rotation(rev)*3.14*dia; // pi D        //distance =total rotations * circumference of 1 rotation
     LM.stop(brake);                                              //stops motors once target is reached and loop finishes
     RM.stop(brake);                                             //optional braking, will make motion more fluid

int main()
inchDrive(40,75);                                               // drive 40 inches at 75% speed