uRobot – The Line Following race

In a summer night in the early 21st century some Luxembourgish hackers came together to launch a non-disclosed project with the aim to combine their knowledge on artificial intelligence to make mankind’s life easier in the future. To reach this goal they carefully planned how to gain access to World’s most confidential micro-performance computer technology at that time, years later known as Arduino. They broke in into a secret fabrication laboratory somewhere in the Southern of Europe and brought back this technology home to Luxembourg. During an underground hacktivity soon later they started to analyze and design some little robots called uRobot. After a short time they were able to let uRobot follow autonomously a 50 mm thick black line through Syn2…, … through their underground hack center. After this success they went further and built a human-like machine – in the belief that this technology will ever be used for good purposes, at a time where hackers were good thinking people. Many years later they should find out that they were wrong. The machine called uTerminate fell into the wrong hands and destructive hacktivities began …

“uRobot vs. uTerminate” coming out in late 2012

Now the short story:

Bored and sitting around in the syn2cat hackerspace on a Saturday night I felt driven to assemble a robot kit and let it follow a line.

The robot consists of a 2WD robot frame with a Romeo-All in one Controller.

The code got written quick and dirty but it works good enough for a demo. The digital pins 4 to 7 are used to control the onboard L298 motor driver whereas the analog pins A0 and A1 are used for the optical sensors.


/*
 If the constants are defined with the define macro
 then a boolean expression in a if statement fails with the message:
 
 move_through_the_space.cpp: In function 'void setup()':
 move_through_the_space:68: error: expected primary-expression before '=' token
 move_through_the_space:68: error: expected `)' before ';' token
 move_through_the_space:68: error: expected primary-expression before ')' token
 move_through_the_space:68: error: expected `;' before ')' token
 
 */
const int LeftLineSensor = A0;
const int RightLineSensor = A1;

int E1 = 5;     //M1 Speed Control
int E2 = 6;     //M2 Speed Control
int M1 = 4;     //M1 Direction Control
int M2 = 7;     //M1 Direction Control

int leftSpeed = 255;
int rightSpeed = 235;

const int NoneOut = 0;
const int LeftOut = 1;
const int RightOut = 2;

int firstOut = NoneOut;

/**********************************/

void stop(void)
{
  digitalWrite(E1,LOW);   
  digitalWrite(E2,LOW);      
}

void backwards(char a,char b)
{
  analogWrite (E1,b);
  digitalWrite(M1,HIGH);    
  analogWrite (E2,a);    
  digitalWrite(M2,HIGH);
}

void forwards(char a,char b)
{
  analogWrite (E1,b);
  digitalWrite(M1,LOW);   
  analogWrite (E2,a);    
  digitalWrite(M2,LOW);
}

void left(char a,char b)
{
  analogWrite (E1,b);
  digitalWrite(M1,LOW);    
  analogWrite (E2,a);    
  digitalWrite(M2,HIGH);
}

void right(char a,char b)
{
  analogWrite (E1,b);
  digitalWrite(M1,HIGH);    
  analogWrite (E2,a);    
  digitalWrite(M2,LOW);
}

boolean onLine(int pin)
{
  int apin = analogRead(pin);
  if( apin  > 750 )
    return true;
  else
    return false;
}


/**********************************/

void setup(void) 
{ 
  // do nothing if the robot is not starting with both sensors
  // over the guide line.
  if( !onLine(LeftLineSensor) && !onLine(RightLineSensor) ) 
    exit(0);

  int i;
  for(i=4;i<=7;i++)
    pinMode(i, OUTPUT); 

  Serial.begin(9600);  
}

void loop(void) 
{

  if(!onLine(LeftLineSensor) && !onLine(RightLineSensor))
  {
    if(firstOut == LeftOut)
    {
      stop();
      right(255, 255);
    } 
    else
    {
      stop();
      left(255, 255);
    }  
  }

  if(!onLine(LeftLineSensor) && firstOut == NoneOut)
  {
    rightSpeed -= 80;
    firstOut = LeftOut;
  }

  if(!onLine(RightLineSensor) && firstOut == NoneOut)
  {
    leftSpeed -= 80;
    firstOut = RightOut;
  }

  if( onLine(LeftLineSensor) && onLine(RightLineSensor) )
  {
    leftSpeed = 255;
    rightSpeed = 245;
    firstOut = NoneOut;
  }

  forwards(leftSpeed, rightSpeed);

  delay(5);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>