The encoders detect how much the wheels have turned. We can use this information to figure out how far the robot has driven.

Sample code

#include <dragonfly_lib.h>
#include <encoders.h>
 * Drives the robot forward for 1500 encoder distance units
void encoders_sample(void)
  encoders_init(); // must be called before using any encoder code
  motors_init(); // must be called before using any motors code

  motor_l_set(FORWARD, 500); // turn on the left motor
  motor_r_set(FORWARD, 500); // turn on the right motor

  int distance = 0; // initialize distance variable
  while(distance < 1500) // while we have traveled less than 1500 distance units, do the following:
    distance = encoder_get_x(LEFT); // get the newest encoder value from the left encoder
                                    // the right encoder value should be about the same since we are driving straight
  // we won't get out of the while loop until we have traveled 1500 distance units
  // since we got here, we have driven far enough
  motors_off(); // stop the motors

Header File

For a full list of functions, see the encoders code header file.


Things to check/maintain:
  • Check battery
  • wheel slop (make sure wheels are not loose)
  • encoder board should have 2mm spacing (2 #4 lock washers)
  • check wiring on the underside. The wires have gotten pulled out from replacing batteries.


NOTE: stuff has to be wired up on the board to work. This has something to do with the chip select pins. See some of the robots to get this working

encoders.png View (302 KB) Emily Hart, 09/16/2009 08:50 PM

encoder_schematic.png View (27.4 KB) Emily Hart, 09/16/2009 09:49 PM