7.5 KiB
Line Detection
Design ways to improve driving safety by helping to prevent drivers from falling asleep and causing an accident.
Connect
Think about:
- How can autonomous cars react to different traffic light signals?
- What can happen if a driver falls asleep while driving?
- How can we detect when a driver is falling asleep?
Construct
Build
Build a @boardname@ vehicle that can help prevent drivers from falling asleep and causing an accident. Start by constructing this model:
Build red and green “lights” for your robot to detect. You can use LEGO bricks, colored tape, or marker on white paper. Building instructions:
~hint
If clicking the above images doesn't open the instructions, right-click on the image and choose "Save link as..." to download the PDF.
~
Check
Before you program, check:
- Are all the wires correctly connected from the motors to ports B and C?
- Are the wheels correctly installed?
- Are the wheels rotating freely?
- Are the wires connected from the Color Sensor to port 3?
Program
Autonomous cars need to recognize and respond to traffic lights automatically. Create a program that will make your robot stop at red lights. Make sure your robot is only responding to the color red. Once you have succeeded, program your robot to drive forward again when the light changes from red to green.
Before you program, think about:
- How will you program the robot to detect a color?
- How will you program the robot to stop at a color?
- Which programming blocks will you use?
~ hint
Consider using these blocks in your solution:
loops.forever(function () {
})
motors.largeBC.steer(0, 50)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
motors.stopAll()
~
Sample Solution - Red light detection
- Loop forever.
- Start motors
B
andC
(drive forward). - Wait for the color sensor to detect the color red.
- Stop all motors.
loops.forever(function () {
motors.largeBC.steer(0, 50)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
motors.stopAll()
})
Download and test
Click Download and follow the instructions to get your code onto your EV3 Brick.
Congratulations! Your robot can stop at a red light.
Now add to your program and have your robot to drive forward again when the light changes from red to green.
Sample Solution - Red and green light detection in a loop
- Start motors
B
andC
(drive forward). - Wait for the color sensor to detect the color red.
- Stop all motors.
- Wait for the color sensor to detect the color green.
- Loop forever.
loops.forever(function () {
motors.largeBC.steer(0, 50)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
motors.stopAll()
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Green)
})
Download and test
Click Download and follow the instructions to get your code onto your EV3 Brick.
Contemplate
To simulate what could happen if a driver falls asleep while driving, your robot could sound an alarm signal when it crosses the line. This feature is often available in new cars.
Program your robot to perform this function.
Draw a dark line with tape or marker for your robot to cross.
~hint
Consider using these blocks in your solution:
motors.largeBC.steer(0, 50)
music.playSoundEffect(sounds.systemGeneralAlert)
~
Sample Solution - Line detection in a loop
- Start motors
B
andC
(drive forward with a curve toward the line). - Wait for the color sensor to detect the color black.
- Play sound effect
system general alert
. - Start motors
B
andC
(drive forward with a curve away from the line). - Wait for the color sensor to detect the color white.
- Loop forever.
loops.forever(function () {
motors.largeBC.steer(-30, 20)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Black)
music.playSoundEffect(sounds.systemGeneralAlert)
motors.largeBC.steer(30, 20)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.White)
})
Download and test
Click Download and follow the instructions to get your code onto your EV3 Brick.
Differentiation
Program your robot to drive on “autopilot” along a given route. You will need to create a program that recognizes and responds to a dark line (or white line). You will create a line-following program and your robot will need to travel along the line without losing contact with it.
You will need to constantly debug your program in order to make your robot travel as smoothly as possible along the line.
~hint
Consider using these blocks in your solution:
while (true) {
}
motors.largeBC.steer(0, 50)
- OR -
if (true) {
} else {
}
~
Sample Solutions - Line Following in Loop
Method 1
- While the Color Sensor detects the color black, start motors
B
andC
(drive forward with a curve toward the line). - While the Color Sensor detects the color white, start motors
B
andC
(drive forward with a curve away from the line). - Loop forever.
forever(function () {
while (true) {
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Black)
motors.largeBC.steer(-30, 50)
}
while (true) {
sensors.color3.pauseUntilColorDetected(ColorSensorColor.White)
motors.largeBC.steer(30, 50)
}
})
Method 2
- If the Color Sensor detects the color black, start motors
B
andC
(drive forward with a curve toward the line). Else the Color Sensor detects the color white, start motorsB
andC
(drive forward with a curve away from the line). - Loop forever.
forever(function () {
if (true) {
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Black)
motors.largeBC.steer(-30, 50)
} else {
sensors.color3.pauseUntilColorDetected(ColorSensorColor.White)
motors.largeBC.steer(30, 50)
}
})
Download and test
Click Download and follow the instructions to get your code onto your EV3 Brick.
Share
Think about:
- What challenged you?
- Were there any surprises?
- How can you improve your program?
- Can your program be more streamlined? Have you used too many blocks?
- Is there a more efficient way to build your program?
- How can your program be used in real-world scenarios?
Personalize:
- Click on the JavaScript tab and experiment with changing the values in the code.
- Add a custom image or sounds from the
||brick:Brick||
or||music:Music||
menus. - Create a video of your project, especially your final presentation and your robot’s performance. Explain some important features of your software program.
- Include an image of your program with comments.
- Add a team photograph!
Congratulations! What will you design next?