Session 6: Advanced Line Following with Curves and Junctions
Session Architect: Muhammad Bilal
Duration: 50-60 minutes
Equipment Required:
- M3D Robotics kit with line sensor attached
- Laptops with Arduino IDE pre-installed
- USB/OTG cables to connect the robot to the laptop
- Black tape to create curves and junctions on a white surface
- Paper and pencils for students to document their experiments
Learning Objectives:
- Understand how to modify the line-following algorithm to handle curves and junctions.
- Learn how to use
line.leftOfLine()andline.rightOfLine()to navigate the robot around obstacles, curves, and junctions. - Apply nested if-else structures for decision-making based on sensor data.
- Experiment with various conditions to optimize line-following behavior.
1. Revision of Conditional Logic (10 minutes)
Activity:
Begin the session by revisiting conditional logic, focusing on if-else structures and introducing nested if-else logic.
Instructions:
- Recap If-Else Logic:
Start with a quick refresher on if-else conditions. Pose the following question to students:
“Who can explain what happens if the condition inside an if statement is true? What happens when it’s false?”
The goal is to engage students in a discussion that makes them recall what they learned in previous sessions about checking one condition to perform an action, or doing something different when the condition is not met.
- Introduce Nested If-Else:
Transition into explaining nested if-else structures:
“Sometimes we need to check more than one condition before making a decision. This is where we use nested if-else statements, which allow us to check multiple conditions one after the other.”
Use the following simple code example to demonstrate nested if-else logic:
int speed = 70;
void setup() {
go.begin();
}
void loop() {
if (speed > 50) {
if (go.onTheLine()) {
go.forward(70); // Move forward if the robot is on the line
} else {
go.stop(); // Stop if the robot isn't on the line
}
} else {
go.reverse(); // Move in reverse if speed is 50 or below
}
}
Ask students to predict the robot’s behavior based on this code. Questions to ask:
“What happens when the speed is above 50?”
“What does the robot do when it’s not on the line, even if the speed is greater than 50?”
Guide them in understanding how nested conditions help fine-tune control over the robot’s behavior, making it respond to multiple inputs simultaneously.
Teacher’s Role:
- Engage students with questions and guide them through understanding if-else and nested if-else logic.
- Ensure students can articulate how conditions lead to different outcomes in the robot’s behavior.
- Walk them through a simple coding challenge to check their understanding.
2. Introducing the Line Sensor for Curves and Junctions (10 minutes)
Activity:
Students will attach their line sensor and learn how to use line.leftOfLine() and line.rightOfLine() to navigate more complex curves and junctions.
Instructions:
- Engaging Discussion on Real-Life Scenarios:
Start by asking students:
“Imagine you’re walking along a path and need to take a turn or follow a curve—how do you know when you need to adjust your direction?”
“What happens if you drift too far to one side of the path?”
Guide them toward understanding that just as humans rely on their senses to adjust, the robot uses sensors to detect its position on a path.
- Explaining the Line Sensor:
“Our robot’s line sensor works like its eyes. If one part of the sensor detects the line but the other doesn’t, the robot knows it’s drifting and can adjust.”
Introduce the commands:
- line.leftOfLine(): This command returns true when the left sensor detects the line but the right sensor does not.
- line.rightOfLine(): This command returns true when the right sensor detects the line but the left one does not.
- “line.getLeft();” Gives the value of the left sensor.
- “line.getRight();” Gives the value of the right sensor
- Coding Example with Explanation:
Guide students through an example that shows how the robot corrects itself when it drifts off the line and how they will be checking multiple conditions again and again:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (line.onTheLine()) {
go.forward(50); // Move forward if on the line
} else if (line.leftOfLine()) {
go.spinClockwise(30); // Turn right to adjust
} else if (line.rightOfLine()) {
go.spinCounterClockwise(30); // Turn left to adjust
} else {
go.stop(); // Stop if completely off the line
}
}
“Can anyone explain what happens when the robot detects the line on one side only?”
“How does the robot decide whether to turn left or right?”
Encourage students to break down the logic and explain it in their own words. Guide them through the decision-making process the robot uses to adjust its movement.
Teacher’s Role:
- Explain how the line sensor detects the robot’s position relative to the line.
- Ensure students understand how line.leftOfLine() and line.rightOfLine() work by relating it to real-life scenarios of turning and adjusting.
- Help students implement the sensor code and test its functionality.
3. Experimenting with Curves and Junctions (15 minutes)
Activity:
Students will apply their understanding of the line sensor to follow more complex paths with curves and junctions. They will experiment with how the robot makes decisions when faced with different paths.
Instructions:
-
Creating a Path with Junctions:
Ask students to create a more challenging path using black tape on a white surface. This path should include curves and at least one junction. -
Programming and Testing:
Instruct students to use their code from the previous section to have the robot follow the curved path. Encourage them to make adjustments to their code, such as:
void loop() {
if (line.onTheLine()) {
go.forward(60); // Move forward faster if on the line
} else if (line.leftOfLine()) {
go.spinClockwise(40); // Turn right at a sharper angle for curves
} else if (line.rightOfLine()) {
go.spinCounterClockwise(40); // Turn left at a sharper angle
} else {
go.stop();
}
}
- Teacher’s Role:
Walk around and ask students questions like:
“What happens when the robot approaches a curve? How does it adjust?”
“What changes did you make to your code to handle a sharper turn?”
Encourage them to test different parameters, such as adjusting turn speed or adding short delays to smooth out the robot’s movement.
Teacher’s Role:
- Provide guidance on how to fine-tune code for better performance on curves and junctions.
- Assist students in troubleshooting issues and improving their algorithm for smoother navigation.
- Encourage experimentation with speed and turning angles.
4. Challenges for Line-Following (15 minutes)
Activity:
Students will be given challenges to reinforce their understanding of line-following and decision-making with multiple conditions.
Instructions:
Challenge 1:
Follow the Curved Path:
Program the robot to follow a curved path where the line continuously bends. Ensure the robot adjusts smoothly without overshooting the line.
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (line.onTheLine()) {
go.forward(60); // Move forward at 60% speed if on the line
} else if (line.leftOfLine()) {
go.spinCounterClockwise(30); // Turn left at 30% speed to adjust gently
} else if (line.rightOfLine()) {
go.spinClockwise(30); // Turn right at 30% speed to adjust gently
} else {
go.stop(); // Stop if not detecting the line
}
}
Explanation:
- This code makes the robot follow a curved path by checking whether it’s on the line or if it has drifted left or right. It adjusts smoothly by spinning at lower speeds when off the line to ensure the robot doesn’t overshoot.
Challenge 2:
Handle Junctions:
Create a T-junction where the robot must decide whether to turn left or right. Use the line sensor commands to navigate the junction effectively.
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (line.onTheLine()) {
go.forward(50); // Move forward if on the line
} else if (line.leftOfLine()) {
go.spinCounterClockwise(40); // Turn left if on the left side of the line
} else if (line.rightOfLine()) {
go.spinClockwise(40); // Turn right if on the right side of the line
} else if (line.notOnLine){ // At a junction, where the robot has crossed the line and now is not detecting anything, it decides which way to turn
if (line.getLeft() > line.getRight) { // Compare the two sensor values and choose left or right
go.spinCounterClockwise(50); // Turn left at the junction
} else {
go.spinClockwise(50); // Turn right at the junction
}
delay(500); // Wait for half a second after turning
}
}
Explanation:
- This code compares the values of the two sensors to make a decision at a T-junction. It turns left when the left sensors has a higher value and turns right if the right sensor has a higher value. The robot adjusts its direction and continues forward after making a decision.
Challenge 3:
Zig-Zag Path:
Ask students to create a zig-zag path. They must adjust their code so the robot follows the path without stopping, using both turning and forward movement commands efficiently.
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (line.onTheLine()) {
go.forward(70); // Move forward at a faster speed if on the line
} else if (line.leftOfLine()) {
go.spinCounterClockwise(50); // Quickly adjust left to stay on the zig-zag
} else if (line.rightOfLine()) {
go.spinClockwise(50); // Quickly adjust right to stay on the zig-zag
} else {
go.stop(); // Stop if no line is detected
}
}
Explanation:
- This code makes the robot react faster to zig-zag movements by increasing the spin speed to 50%. It adjusts the direction quickly to navigate the sharp turns in a zig-zag path without stopping or losing the line.
Teacher’s Role:
- Encourage students to experiment with more complex paths and push their understanding of conditional logic.
- Provide help with debugging and encourage reflection on why certain decisions work or don’t work.
- Motivate students to collaborate and share solutions to the challenges.
5. Combining Line Sensor with Display (5 Minutes)
Activity:
In this short segment, the teacher will encourage students to enhance their existing line-following code by using the display to show what action the robot is currently performing. This will make the robot more interactive and give students more insight into its decisions.
Instructions:
- Introduce Display Feedback:
Start by asking the students:
“How do you know what your robot is doing right now? Wouldn’t it be helpful if the robot could tell us what action it’s performing?”
Explain that they can make the robot display text to show what action it’s currently executing, such as moving forward, turning left, or turning right.
- Modify the Code with Display Text:
- Guide the students to modify the code they wrote for the challenges by adding display commands that show the current action on the robot’s screen.
- You can give a quick example of how they can add display.text() to their existing code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (line.onTheLine()) {
go.forward(70); // Move forward at 70% speed
display.text("Moving Forward"); // Show action on screen
} else if (line.leftOfLine()) {
go.spinCounterClockwise(50); // Turn left
display.text("Turning Left"); // Show action on screen
} else if (line.rightOfLine()) {
go.spinClockwise(50); // Turn right
display.text("Turning Right"); // Show action on screen
} else {
go.stop(); // Stop if no line is detected
display.text("Stopped"); // Show action on screen
}
}
- Encourage Experimentation:
- Let the students experiment with adding the display.text() function to their own challenge code. Encourage them to come up with creative text that reflects the robot’s current action.
- Ask them to test the code by running it on their robot and observing both the robot’s movements and the messages displayed on the screen.
Teacher’s Role:
- Guide students through modifying their code to include the display.
- Encourage creativity in the messages they choose to display.
- Offer help if needed as students test their robots to ensure the display feedback is working as expected.
6. Reflection and Wrap-Up (5 minutes)
Activity:
End the session by reflecting on the new skills students learned about handling curves and junctions.
Instructions:
-
Discussion Questions:
- “What was the hardest part of programming your robot to follow curves?”
- “How did the robot behave when it approached a junction, and how did you fix any issues?”
- “How did using if-else statements help your robot make decisions in real-time?”
-
Reinforce Learning:
Recap how the line.leftOfLine() and line.rightOfLine() commands allowed the robot to make better decisions, and how nested if-else conditions helped refine those decisions.
Teacher’s Role:
- Lead the reflection, helping students articulate what they learned.
- Reinforce the key takeaway that real-time decision-making is crucial for navigating complex paths.
- Encourage students to think of how they might apply similar logic in future projects.
Learning Outcome:
By the end of the session, students will have improved their line-following algorithm to navigate curves and junctions. They’ll understand how the line.leftOfLine() and line.rightOfLine() commands provide real-time feedback, allowing the robot to make smarter decisions while moving.