Session 8: Advanced Motor Control and Sensor-Based Data Analysis
Duration: 50-60 minutes.
Equipment Required:
- M3D Robotics kit (with line sensor and distance sensor)
- Laptops with Arduino IDE and Web BLE setup
- USB/OTG cables to connect the robot to the laptop
- Projector for live code demonstration
- Paper and pencils for students to record observations and sketch graphs
Learning Objectives:
- Introduce advanced motor control techniques, focusing on gradual acceleration and precise speed adjustment.
- Explore line sensor commands that distinguish between lighter and darker backgrounds.
- Understand the purpose and interpretation of graphs using real-time sensor plotting on Web BLE.
- Develop skills in using loops, motor control, and conditional logic to create dynamic robotic movements.
1. Introduction to Acceleration and Motor Control (10 minutes)
Discussion Starter:
Begin with a question to gauge students’ knowledge of acceleration:
“Can anyone tell me what they know about acceleration? How does it feel in a car when it starts moving from a stop? And how is it different from just moving at a steady speed?”
Encourage responses and build on their answers, connecting the concept of acceleration to their experiences.
Teacher Explanation:
Explain how the robot currently moves abruptly at a set speed:
“When we program the robot to move forward at 50% power, it instantly starts at that speed, which can feel sudden. But just like a car, we can control how quickly it reaches that speed by adjusting its acceleration.”
Demonstration of Acceleration Command:
Show how to program gradual acceleration using the go.forward() and go.reverse() commands with acceleration time:
go.forward(80, 2.5); // Moves forward at 80% power, accelerating over 2.5 seconds
go.reverse(50, 1.5); // Moves backward at 50% power, accelerating over 1.5 seconds
Teacher’s Role:
Guide students in testing the commands and observing the difference between abrupt and gradual acceleration.
2. Exploring Motor Synchronization with setMotors() (10 minutes)
Activity:
Introduce the go.setMotors() command to control both motors at different speeds using a single command:
go.setMotors(70, 50, 2); // Left motor at 70%, right motor at 50%, accelerating over 2 seconds
Explain how controlling each motor independently can make the robot drift or turn gradually:
“By setting different speeds for each motor, we can make the robot curve or ‘drift.’ This can be useful for smoother turns and more complex paths.”
Challenge Prompt:
Have students experiment with setting different motor speeds and observe how the robot’s path changes.
You may give them a challenge task such as:
Challenge: Smooth Start and Stop
- Task: Program the robot to accelerate forward to 60% speed over 3 seconds, move for 2 seconds, then decelerate to a stop over 3 seconds.
- Goal: Practice using gradual acceleration and deceleration.
Example Code:
go.forward(60, 3.0); // Accelerate to 60% over 3 seconds
go.delay(2000); // Move at steady speed for 2 seconds
go.stop(3.0); // Decelerate to stop over 3 seconds
**Teacher’s Role:**
Walk around and observe as students test various speed combinations, encouraging them to note the impact on movement.
---
### **3. Line Sensor Commands and Shade Detection Activity (15 minutes)**
**Activity - "Painter Robot":**
Explain the concept of using the robot as a "painter" that can distinguish between light and dark shades. Introduce the line sensor commands `getBackgroundIsLighter()` and `getBackgroundIsDarker()`:
```cpp
if (line.getBackgroundIsLighter()) {
go.forward(40, 1.0); // Move slowly forward on lighter surfaces
} else if (line.getBackgroundIsDarker()) {
go.reverse(30, 1.0); // Reverse slowly on darker surfaces
}
Discussion Prompt:
“Imagine a painter working on a canvas—how do you think this robot can ‘choose’ which areas to move forward on and which to avoid?”
Encourage students to hypothesize how the robot might behave on different surfaces.
You can give them a small task to code the robot such as:
Challenge: Painting Path Based on Shade
- Task: Make the robot move forward on light surfaces and reverse on dark surfaces.
- Goal: Use
getBackgroundIsLighter()andgetBackgroundIsDarker()to change movement based on detected shades.
Example Code:
if (line.getBackgroundIsLighter()) {
go.forward(50, 1.5); // Move forward on light areas
} else if (line.getBackgroundIsDarker()) {
go.reverse(30, 1.5); // Reverse on dark areas
}
- Encourage them to be creative and incorporate web BLE notifications as well as emojis into the robot’s behavior to make it feel more alive.
Teacher’s Role:
Guide students as they use these commands to make the robot react to various shades, noting the robot’s behavior as it transitions between lighter and darker backgrounds.
4. Plotting Sensor Data with Web BLE: LIDAR Simulation (15 minutes)
Activity - “LIDAR Simulation”:
Introduce the concept of using a loop to rotate the robot while plotting distance sensor readings against an angle, simulating LIDAR.
- You may ask the students if they know what a LIDAR is or if they have seen something similar on the internet or in real life.
Guide them through the steps and encourage them to think what the robot needs to do in order to detect objects in it’s surroundings and show on using the plot command on Web BLE:
for (int angle = 0; angle <= 360 ; angle += 10) {
indication1.plot(angle, range.get_cm()); // Plot distance against angle
go.spinClockwise(10, 0.1); // Rotate slightly to get the next angle
}
Discussion Prompt:
“Have you seen graphs in other areas, like sports, science experiments, or gaming scores? How do graphs help us see changes and patterns over time?”
Explaining Graph Purpose:
Discuss how graphs can visually show how things change and can be helpful in spotting patterns, trends, or unusual changes.
Challenge: Distance Travel Plotting
Task:
Program the robot to move forward in small increments and plot the distance measured by the rangefinder at each increment. The robot will gradually move, recording its surroundings as it travels forward.
Goal:
Practice using loops and plotting to understand how the robot perceives its environment as it moves.
Example Code:
#include <M3DGo.h>
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
int distanceTravelled = 0;
while (distanceTravelled <= 100) { // Move up to 100 cm
indication1.plot(distanceTravelled, range.get_cm()); // Plot rangefinder value vs. distance moved
go.travel_cm(5); // Move forward in 5 cm increments
distanceTravelled += 5; // Update distance travelled
go.delay(500); // Small delay to allow plotting
}
go.stop(); // Stop the robot after reaching the set distance
}
Teacher’s Role:
Explain how each plotted point gives a picture of the robot’s surroundings and guide students through running the code, observing the changing distance measurements on the Web BLE plot. Support students as they work through each challenge, helping them troubleshoot code, interpret sensor readings, and refine their graphs.
6. Wrap-Up and Reflection (5 minutes)
Reflection Questions:
- “What was the most surprising thing you noticed about how the robot handled gradual acceleration?”
- “How did plotting the sensor data help you ‘see’ the surroundings of the robot?”
- “Can you think of other uses for graphs, outside of robotics?”
Teacher’s Role:
Discuss how the session’s techniques are useful in real-world robotics, such as in self-driving cars or drones. Reinforce how graphs and data visualization can help analyze and understand complex data effectively.
Learning Outcome:
By the end of this session, students will understand advanced motor control techniques, use line sensors to detect shades, and visualize sensor data through plotting. This session strengthens their coding, debugging, and analytical skills, building a foundation for future sessions in robotics and data analysis.