Milestone 1
Line Following & Figure Eight
Read the Report


Milestone 1


Integrate line sensors with the robot so that it can follow a line. Then, program the robot to follow a figure eight pattern along a rectangular grid in a robust manner.


      unsigned int * checkSensors() {
        static unsigned int sensorValues[NUM_SENSORS]; //declare output array
        sensorValues[0] = analogRead(A0);
        sensorValues[1] = analogRead(A1);
        sensorValues[2] = analogRead(A2);

        for (unsigned int i = 0; i < NUM_SENSORS; i++) {
          if (sensorValues[i] < THRESHOLD)
          sensorValues[i] = 0; //Sees Bright Line
        else
          sensorValues[i] = 1; //Sees Dark Background
        }
        return sensorValues;
      }
      
  • To track the line, we poll the sensors using checkSensors()
  • If the robot is leaning left, then the first value in the array will be 0
  • If that is the case, we call our adjustRight(servo_L, servo_R, amount) function to turn the robot slightly right as it moves forward
  • We can tune the strength of this adjustment by changing the amount value, which can range from 0-90
  • Similarly, if the robot is leaning right, based on reading the last value of the sensorStatus array, we call adjustLeft(servo_L, servo_R, amount)
  • Otherwise, if we have the desired [1,0,1] reading, we continue going forward

Line sensor algorithm

Demo:




      void stopMotors(Servo servo_L, Servo servo_R){
        servo_R.write(90);
        servo_L.write(90);
      }

      void moveForward(Servo servo_L, Servo servo_R){
        servo_R.write(180);
        servo_L.write(30); //set to 30 instead of 0 to account for differences in wheel geometries
      }

      void adjustRight(Servo servo_L, Servo servo_R, int amount){
        servo_R.write(180 - amount);
        servo_L.write(0);
      }

      void turnRight(Servo servo_L, Servo servo_R){
        servo_R.write(180);
        servo_L.write(180);
        delay(550);
        stopMotors(servo_L, servo_R);
      }

      void turnRightIntersection(Servo servo_L, Servo servo_R){
        moveForward(servo_L, servo_R);
        delay(350);
        stopMotors(servo_L, servo_R);
        turnRight(servo_L, servo_R);
      }
      

Demo: