Milestone 1: Basic Movement

The goal of this lab is to make the robot to follow the line and traverse in a figure 8. The robot is controlled using the reading from line sensor in order to make decisions regarding following lines and turning. We first created a simpler iteration that followed straight lines, and after a successful implementation we modified the code so the robot could follow turns, and ultimately traverse the figure 8.

Following a Straight Line

There are four line sensors attached to the front of the robot. Two line sensors are in the middle and two line sensors are on the outer side. For this part, we only need the middle ones for controlling the robot. The middle line sensors are on top of a white tape for following the straight line and making corrections. If either of them detects a value above the white tape threshold value, it means that the the robot is leaning towards one side. In this situation, the robot will make a slight correction by slowing one of the servo by how much the robot is out of bound. The magnitude of how much the robot is out of bound is determined by the difference between the right middle sensor and the left middle sensor.

This piece of code is placed in the function go_straight() for future use.


Figure 8 Traversal

The robot traverses the 8 by making a total of 8 turns, 4 left and 4 right. In the very beginning of the program, we wait for one second to start the program and have the robot go straight. Then it will loop in the turns forever. This part is demonstrated in the main function. The delay function is added for buffering, it is not a timing constraint for robot control.

while(i <4){
                move(left);
                i++;
              } 
              while(i < 8){
                move(right);
                i++;
                if (i == 7){
                  i = 0;
                }
              }
              

The move method takes in one parameter, direction of type int. If the robot is requested to turn, the robot will begin to turn and stops turning only when both middle sensors detects a white line. However, we can only implement this stop detection method after the middle two sensors are off the white line. This is implemented by forcing the robot to turn without stop detection for th first 0.3 seconds. This delay time is experimentally determined. It is possible to further improve the accuracy of this by first setting a while loop for an off-white line detection, which allows the robot to turn no matter how wide the white tape is, how narrow the angle is or how fast the robot goes. However, this would be unnecessary for this milestone because the white taped line is always the same width and the turn degree is wide enough for a timing approximation.

if (direction != forward){
                left_servo.write(servo_turn_value[direction]);
                right_servo.write(servo_turn_value[direction]);
                delay(300); //force turn over line
            
                while(1){
                  read_pid();
                  if (left_pid_val < WHITE && right_pid_val < WHITE){
                    break; //found white line
                  }
                } //end while, done turning
              } 
            

Once the robot is done turning, it will start to go in a straight line until it finds the next intersection. The finding intersection condition is used as the while loop condition, which breaks when both of the outer sensors are out of the dark area. After it finds the intersection, it goes on to execute the next function in the main loop.

//look for intersection
                while(1){
                  read_turn();
                  if (left_turn_val < WHITE && right_turn_val < WHITE){
                    break; //both turn sensors are on white line
                  }
                  go_straight();
                  delay(50);
                }//close while; found intersection
              

void loop() {
  Serial.println("hi");
  move(left);
  delay(50);
  move(left);
  delay(50);
  move(left);
  delay(50);
  move(left);
  delay(50);
  
  move(right);
  delay(50);
  move(right);
  delay(50);
  move(right);
  delay(50);
  move(right);
  delay(50);
}

The move method takes in one parameter, direction of type int. Our original design was that if the robot is requested to turn, the robot will begin to turn and stops turning only when both middle sensors detects a white line. However, we ran out of analog sensors. When trying to set up the digital lines sensors, we figured they have different responses (50 for white and 130 for black; 1800 for white and 2700 for black). One of the error range was too small. While it can still be implemented, I am not as confident in using a sensor with such a small detecting range.

From there I resovled to a different method: using only two line sensors that are just right by the edge of the line. When both sensors detect a white, that means it encounters a horizontla line and therefore should make a turn.

//look for intersection
  while(1){
    Serial.println("looking");
    read_turn();
    if (left_turn_val < WHITE && right_turn_val < WHITE){
      i = 0;
      Serial.println("found intersection");
      while(i < 30){
        go_straight();
        i++;
      }
      Serial.println("delay");
      break; //both turn sensors are on white line
    }
    go_straight();
    delay(10);
  }//close while; found intersection

However, since we don't have the middle sensors to detect when the robot is on the white line again, we wouldn't know when to stop. To solve this, we don't make the robot turn immediately. Instead, the robot will travel a little bit further straght after it detects a white by using a counter for delaying. The delay function could not be used because the robot need to still be doing straight line correction. Then, the robot will begin to turn. For each direction, the respective inner sensor will detect a dark first and then the white line the robot needs to be on, and then after it go over the line the robot can travel straight again with straght line correction.

while(analogRead(right_turn) > WHITE);
      while(analogRead(right_turn) < WHITE);
      Serial.println("done turn left");

Work Distribution

The Milestone 1 Work Distribution is as follows:
  - On 9/7, Joyce, Nathalia, Priya, and Vini worked on in lab rebuild the robot.
  - On 9/10, Joyce, Nathalia, and Vini worked on rebuilding the robot and started the code.
  - On 9/10, Nathalia went to open office hours and finished rebuilding the robot.
  - On 9/14, Joyce, Nathalia, Priya, and Vini worked on in lab to complete the milestone.
  - Joyce completed the final finish of the figure 8 using line sensor and turning capabilities.
The Lab Report Work Distribution is as follows:
  - Joyce: Introduction and Code Explanations
  - Priya: Code Section Write Up Drafts
  - Nathalia: Pictures and Videos
The website work distribution is as follows:
  - Nathalia: Website Milestone 1 Set Up and Maintenance