Session 5: Obstacle Detective: Avoiding Barriers
Session Architect: Muhammad Bilal
Duration: 50-60 minutes
Equipment Required:
- M3D Robotics kit with proximity sensor attached
- Laptops with Arduino IDE pre-installed
- USB/OTG cables to connect the robot to the laptop
- Paper and pencils for students to document their experiments
- Display screen for the robot (to display messages)
Learning Objectives:
- Understand the concept of obstacle detection using a proximity sensor.
- Learn to program the robot to take real-time actions based on sensor input.
- Reinforce the use of conditionals (if statements) to control the robot’s behavior.
- Experiment with displaying messages on the robot’s screen using
display.text().
1. Introduction to Obstacle Detection (5 minutes)
Activity:
Start with an engaging physical activity to help students relate their own senses to how robots detect and avoid obstacles. This will introduce the concept of how robots use sensors to sense objects in their surroundings.
Instructions:
-
Set the Stage:
- Begin by asking for two volunteers to come to the front of the class.
- Blindfold one student and place various objects (e.g., chairs, desks) in front of them, creating an obstacle course. Ask the other student to guide them through the obstacles by giving verbal cues, like “turn left,” “move forward,” or “stop.”
- Let the class observe the interaction for a few minutes.
-
Guided Discussion on Human Obstacle Detection:
After the activity, begin a discussion by asking the class:
“How did the blindfolded student know when to stop or turn?”
(Let students respond. Answers might include: “Someone told them,” or “They listened to instructions.”)
Then, follow up with:
“What helped them avoid the obstacles?”
Let students respond, mentioning things like listening or trusting the guide.
- Connecting to Human Senses:
- Build on their responses by explaining:
“As humans, we rely on our senses—like our eyes to see obstacles or our ears to hear instructions. Our brains process this information, allowing us to make decisions like when to stop or turn.”
- To get students thinking more deeply, ask:
“What would happen if we didn’t have our eyes to see obstacles? How would we avoid things in our way?”
- Let students share their ideas. Some might suggest using touch or listening carefully to others, while others might say it would be difficult.
- Relating to Robot Senses:
- Now, explain how robots function:
“Just like we use our eyes and ears, robots need a way to detect their surroundings. Since robots don’t have eyes, they use sensors to detect obstacles in front of them. These sensors, like the distance sensor on our robot, measure how close objects are, and based on this information, the robot can decide whether to stop, turn, or continue moving.”
- Interactive Discussion:
Engage students in a more interactive discussion:
“Can anyone think of real-world examples where robots or machines need to detect obstacles?”
(Let students share examples. They might mention self-driving cars, robotic vacuums, or automatic doors.)
- You can elaborate on their examples by saying:
“That’s right! Self-driving cars use sensors to detect other cars and pedestrians, just like how a robot vacuum senses furniture in your living room. In today’s lesson, we’ll make our robot detect obstacles and act based on what it senses.”
Teacher’s Role:
- Lead the activity with volunteers and guide the class discussion to help students draw connections between human senses and robot sensors.
- Make sure the students understand that robots rely on sensors (not eyes or ears) to detect their surroundings and avoid obstacles.
- Ask questions throughout the discussion to engage students and make them think critically about how robots make decisions.
- Transition the discussion smoothly into how the robot’s distance sensor works and how it will be used in the coding activity later in the session.
2. Making the Robot Talk: Displaying Text (5 minutes)
Activity:
Before starting the obstacle detection, students will play around with displaying messages on the robot’s screen using the display.text() command.
Instructions:
-
Connect the Display:
- Instruct students to connect the robot’s display.
- Once connected, demonstrate the command to display a message:
#include <M3DGo.h> void setup() { go.begin(); // Setuping up the robot display.text("Hello!"); } void loop(){ } -
Let Students Experiment:
- Encourage students to try displaying their own messages on the screen (e.g., their name, a greeting).
- Let them play with this for a few minutes to get comfortable.
Teacher’s Role:
- Show students how to use the
display.text()command. - Encourage them to experiment with different text messages to make the robot “talk.”
3. Introducing the Distance Sensor and Checking Connection (10 minutes)
Activity:
Introduce the distance sensor and show students how to check if it’s connected using the range.attached() command. Students will practice using conditionals to check whether the sensor is connected.
Instructions:
- Introduce the Sensor:
- Explain what the distance sensor is and how it works.
“The distance sensor helps the robot measure how far away an object is, just like we use our eyes to see how close something is.”
- Check Sensor Connection:
- Start a discussion with students on how we can ensure if the distance sensor is connected. You may say
“Have you connected the distance sensor?”
Wait for there responses and continue:
“Are you sure you have attached the sensor? Show me your bot and the sensor connected to it.”
When they show the sensor attached to the bot, you can ask:
“How about the robot? How does the robot know if the sensor is connected or not?”
-
Show them the “
range.attached();” instruction and how they can use it with the if-statement they learnt in the previous session and ask the robot if the sensor is connected. If it is, they can make the robot say on the screen “Distance Sensor Connected”. -
Assist them in writing the code. The final code should look like this:
if (range.attached()) { display.text("Distance Sensor Connected"); }- Explain the way this code works that it checks if the sensor is connected, and if it is, the robot displays a message on its screen.
- Test the Code:
- Ask students to write and upload this code to their robots and test if the display shows “Distance Sensor Connected”.
Teacher’s Role:
- Guide students in writing the code to check the sensor connection.
- Help troubleshoot any connection issues and ensure all students successfully upload their code.
4. Using the Distance Sensor to Measure Distance (5 minutes)
Activity:
Students will now experiment with the distance sensor, testing how it measures distances in different units and observing how the readings change.
Instructions:
-
Explain the Sensor Commands:
- Introduce the various commands to get the distance from the sensor:
range.get_cm(); // Gets distance in centimeters range.get_mm(); // Gets distance in millimeters range.get_inches(); // Gets distance in inches -
Test the Sensor:
- Ask students to experiment by covering the sensor, placing objects close and far away, and observing the distance values.
- Encourage them to record the distance readings in different scenarios and write down the values as they will be using them to write the obstacle detector code.
Teacher’s Role:
- Show students how to use the distance commands and help them interpret the readings.
- Encourage them to test the sensor under various conditions and take notes.
5. Obstacle Avoidance Using If Statements (15 minutes)
Activity:
Now students will use the distance sensor to make the robot take action when it detects an obstacle. They’ll program the robot to move forward, but if the sensor detects an object within a certain distance, it will stop or rotate.
Instructions:
- Introduce the Task:
“Now let’s make the robot move forward, but when it gets too close to an object, it will stop to avoid a collision.”
-
Example Code:
Show students a basic example of how they can use an if statement to stop the robot when an obstacle is detected within 10 cm:void setup() { go.begin(); // Initialize the robot } void loop() { if (range.get_cm() < 10) { // Check if obstacle is closer than 10 cm go.stop(); // Stop the robot if too close to an obstacle } else { go.forward(50); // Move forward at 50% speed if no obstacle } } -
Testing and Observing:
- Ask students to upload the code and test how the robot behaves when it detects an obstacle.
- Encourage them to adjust the distance value (e.g., 5 cm, 20 cm) and see how the robot responds to closer or farther obstacles.
Teacher’s Role:
- Guide students through writing the obstacle avoidance code.
- Encourage them to experiment with different distance values and observe the results.
- Provide support and troubleshooting as needed.
6. Challenges: Obstacle Avoidance with Sensor Data (15 minutes)
Activity:
After students have tested the basic code, challenge them with additional tasks that involve adjusting the robot’s behavior based on the distance sensor data.
Challenges:
Challenge 1: Stop and Wait with Warning Display
Objective: The robot will move forward, stop when an obstacle is detected, display “Obstacle Ahead” on the screen, and wait for 3 before moving back and saying “I’m coming back”. If there is no obstacle, the robot will say “All Clear!” and keep moving at 50% speed.
Example Code:
cpp
Copy code
void setup() {
go.begin(); // Initialize the robot
display.text("Ready to go!"); // Display initial message
}
void loop() {
if (range.get_cm() < 20) { // If an object is detected within 20 cm
go.stop(); // Stop the robot
display.text("Obstacle Ahead!"); // Display warning
delay(3000); // Wait for 3 seconds
display.text("I'm coming back!"); // Update the message
go.reverse (50); // Moving backward at 50% speed
} else {
display.text("All Clear"); // Display status when no obstacle
go.forward(50); // Keep moving forward if no obstacle
}
}
Challenge 2: Reverse and Turn with Display Updates
Objective: The robot will move forward until it detects an obstacle, then reverse, display “Backing Up,” spin 90 degrees, and display “Turning.”
Example Code:
cpp
Copy code
void setup() {
go.begin(); // Initialize the robot
display.text("Moving Forward"); // Display initial message
}
void loop() {
if (range.get_cm() < 15) { // If an obstacle is detected within 15 cm
display.text("Backing Up!"); // Display warning
go.reverse(50); // Reverse at 50% speed
delay(1000); // Reverse for 1 second
display.text("Turning Right"); // Display turning message
go.spinClockwise(90); // Spin 90 degrees clockwise
} else {
display.text("Moving Forward"); // Display status when no obstacle
go.forward(50); // Move forward at 50% speed if no obstacle
}
}
Challenge 3: Slow Down When Close with Display
Objective: The robot moves forward at full speed. When it detects an obstacle within 30 cm, it slows down and displays the distance. If the obstacle is within 10 cm, it stops and displays “Too Close.”
Example Code:
cpp
Copy code
void setup() {
go.begin(); // Initialize the robot
display.text("Moving Fast!"); // Display initial message
}
void loop() {
float distance = range.get_cm(); // Get the distance from the sensor
if (distance < 10) { // If the object is very close
go.stop(); // Stop the robot
display.text("Too Close!"); // Display warning
} else if (distance < 30) { // If the object is a little close
go.forward(30); // Slow down to 30% speed
display.text("Slowing Down"); // Display status update
} else {
go.forward(100); // Otherwise, move forward at full speed
display.text("Moving Fast!"); // Display status for full speed
}
}
Challenge 4: Turn Left if Blocked with Display
Objective: The robot will move forward and display “Moving Forward.” When it detects an obstacle within 15 cm, it will display “Obstacle Detected” and turn left.
Example Code:
cpp
Copy code
void setup() {
go.begin(); // Initialize the robot
display.text("Moving Forward"); // Display initial message
}
void loop() {
if (range.get_cm() < 15) { // If an obstacle is detected within 15 cm
display.text("Obstacle Detected!"); // Display obstacle warning
go.spinCounterClockwise(90); // Turn left by 90 degrees
delay(500); // Small delay to complete the turn
display.text("Turning Left"); // Update message during turn
} else {
display.text("Moving Forward"); // Display status when no obstacle
go.forward(50); // Move forward at 50% speed if no obstacle
}
}
7. Reflection and Wrap-Up (5 minutes)
Activity:
End the session with a brief discussion about the importance of sensors in robotics and how they enable robots to interact with their surroundings.
Instructions:
-
Ask Reflection Questions:
- “How did the robot know to stop when an obstacle was close?”
- “What was the most surprising or interesting part of today’s lesson?”
- “Can you think of real-world applications where robots use sensors like this?”
-
Summarize Key Learnings:
- Reinforce the concept of real-time feedback and decision-making based on sensor data.
- Highlight the use of conditional logic and how the robot makes decisions based on the distance it detects.
Teacher’s Role:
- Facilitate the reflection, helping students connect what they learned to real-world robotics.
- Encourage them to think creatively about how sensors could be used in different scenarios.