Skip to main content

VEX V5 - Encoder Sensors (Smart Motors)

Encoders

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.

//motors declared in the config.cpp file

brain Brain;
motor LM = motor(PORT1,false);
motor RM = motor(PORT2,true);

 // 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
  {
    LM.spin(forward,speed,pct);
    RM.spin(forward,speed,pct);
    wait(10,msec);
    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
}