Spinning 3pi line following robot

This is newly written code for the students of YRC – run under Vidyasagar Academy, with the spinning ability while following the black line by the world’s fastest robot – POLOLU 3pi.

This code is written by our expert faculty, Prof. Dattaraj Vidyasagar, for the students. The code gives the ability to trace the direction i.e. bends of black line at every rotation and thus follows it.

This idea is taken from the spinning robot designed by “Byon”. This code is available freely for the students of YRC Akola. Those who want the code can contact us.

Watch this video of Spinning Pololu

Part of source code

#include <pololu/3pi.h>
#define   FORWARD_OFFSET   0xA0            // Offset (0..255) of forward from the the front line
#define   MAX_SPEED      255               // Maximum speed the wheels will go
#define   MIN_SPEED      200               // Minimum speed the wheels will go
void Spinning_Line_Follow( void )
{
   unsigned short phase_start = get_ms();   // Start time of this rotation
   unsigned short last_phase_len = 100;   // Duration of the last rotation
   char last_line_side = 0;            // which side was the line on?
   char line_count = 0;               // Is this the front or back line?
   char led_duration = 0;               // How much longer should the LED be on
   while ( 1 ) {
      unsigned short cur_time = get_ms();   // Grab the current time in ms
      unsigned int sensors[5];         // Is the line left or right?
      char line_side = (read_line(sensors,IR_EMITTERS_ON) < 2000);   
      left_led( 0 );                  // Turn off the "FRONT" LED
      if (line_side & !last_line_side) {   // If it just changed, 
         if ( ++line_count & 1 ) {      // and if this is the front line
            left_led( 1 );            // Turn on "FRONT" LED
            last_phase_len = cur_time - phase_start;// save the last rotation duration
            phase_start = cur_time;      // and start counting this rotation
         }
      }
      last_line_side = line_side;         // Remember where the line was

      unsigned short cur_phase = cur_time - phase_start;   // How far are we into the curent rotation?
      cur_phase <<= 8;               // Multipy by 256       cur_phase /= last_phase_len;      // based on the last rotation duration       cur_phase += FORWARD_OFFSET;      // offset by which direction is "FORWARD"       short left = cur_phase & 0xFF;      // Wrap back to 0 .. 255       if ( left >= 128 ) {            // Convert to 0 .. 127 .. 0
         left = 256 - left;
      }
      left = (((left * (MAX_SPEED - MIN_SPEED))>>7) + MIN_SPEED);   // Scale the wheel speed to be MIN at 0, MAX at 127
      short right = MAX_SPEED + MIN_SPEED - left;   // the right is 180 degress out of phase from the left
      set_motors(left, -right);         // and the right goes backwards
   }
}
Vidyasagar Academy, Akola is a trusted name in the field of education. If you liked our free downloads, please write Google Review about us or follow us on Google+.

About the author: My fields of interest are applied electronics, robotics & web designing. I’m an Engineer by education and a sincere student of science & technology. People call me teacher!

Comments on this entry are closed.