3.2.6
Driver Assist Vehicle
Driver Assist Vehicle
What to expect
Today, we will create a “smart” remote control car that has built-in “driver assistance”, incorporating some of the features that we have developed over the past several weeks. At the end, we will have a remote control vehicle that automatically senses obstacles and prevents the driver from hitting them, and also prevents the car from running off the road.
Connections
Many of today’s modern vehicles have advanced “driver-assist” safety features like automatic braking and lane-keeping assistance. These features make cars safer by helping drivers prevent collisions and accidents. In this activity, we will build some of those same features into the remote control MOVE vehicle by using some of the same built-in sensors and code that we have used in the past few activities. We will use the remote control vehicle code from Activity 2.2, the ultrasonic distance sensor used in Activity 2.4, and the contrast sensors from the line following car in Activity 2.5.
Materials
Kitronik MOVE vehicle with 4 AA batteries
Kitronik Arcade Controller with 3 AA batteries
(2) micro:bits - one for the MOVE vehicle, and one for the Arcade Controller
Large piece(s) of paper
Black electrical tape
Instructions
This activity has three parts. First, we will program the MOVE vehicle as a remote-control car using the same code as Activity 2.2. Then we will add automatic braking to prevent the vehicle from colliding with objects, and finally we will add line-sensing (i.e. “lane-keeping”) to keep it from running off the road.
Part I. Remote Control Vehicle (same as Activity 2.2)
To program the MOVE vehicle as a remote control vehicle, we will use the same micro:bit code as Activity 2.2.
For the Arcade controller, download this code to the micro:bit and insert the micro:bit into the Arcade controller with the array of LEDs facing up: https://makecode.com/_Fy8TAP02RHuX. Be sure to change the radio group number to a different number than your classmates. When asked to “Choose your hardware” while downloading the code, scroll down and select the “Kitronik Arcade for micro:bit” tile (not “Kitronik ARCADE”, which does not use a micro:bit).
Test the Arcade controller code by sliding the on/off switch to ON. You should see a vehicle sprite on the LCD screen, and the direction of the vehicle sprite should change when you push a different direction button.
For the MOVE vehicle, download this code to the micro:bit and insert the micro:bit into the MOVE vehicle with the array of LEDs facing forward: https://makecode.microbit.org/_RjjJJt8FPAvu
Test the remote control MOVE vehicle. Turn the power switches on the Arcade controller and the MOVE vehicle to ON, place the vehicle on the floor, and verify that you can drive the vehicle just like you did in Activity 2.2.
Part II. Add Automatic Braking
Next, we will add automatic braking to prevent the car from crashing into an obstacle. When the remote control MOVE vehicle is traveling forward and is about to run into an obstacle, we want the MOVE vehicle to detect the obstacle and stop before it collides with the object.
Most of the code will remain identical, but we need to edit the forever loop. Before the forever loop calls the Forward function, we need it to check the distance to the nearest obstacle. If the distance to the obstacle is less than a certain distance, the vehicle should stop by calling the Stop function. Otherwise, it should move forward by calling the Forward function.
Under the Variables menu, create a new variable and name it distance.
Figure 1. Remote control vehicle code with obstacle avoidance. Link to code: https://makecode.microbit.org/_08D01iJfKaF7
2. Edit the forever loop in your code so that it looks like Figure 1. The teal blocks are in the Logic menu, green blocks are in the MOVE Motor menu, and red blocks are in the Variables menu. To create the “call Stop” and “call Forward” blocks, right-click on the Stop and Forward functions in your code, and select “Create call Stop” and “Create call Forward”.
Download the code to your vehicle’s micro:bit.
Test your vehicle by attempting to drive it into a wall. If it is working correctly, it should stop automatically before it collides into the wall. Note: You might notice that it is still possible for the MOVE vehicle to collide with an obstacle if the vehicle is in reverse or turning. This is true because the MOVE vehicle’s ultrasonic sensor is fixed in the forward-looking direction.
Part III. Add automatic lane-keeping
The final addition to the smart remote control car is lane-keeping ability. When the remote control vehicle is driving forward, it should avoid crossing a line of electrical tape indicating the edge of the lane or road. Most of the vehicle’s code will remain identical, but we will edit the Forward function so that it will stay within its lane, which will be bordered by electrical tape as shown in Figure 3.
Figure 2. Revised Forward function to add lane-keeping ability. Link to the full code: https://makecode.microbit.org/_VVWPfpFvVCWs
3. While leaving the rest of the code unchanged, edit the Forward function so that it matches Figure 2. Be careful not to switch the “Right” and “Left” values. Also, note that this code is slightly different than the code in Activity 2.5. In 2.5, the vehicle was programmed to follow a single line, whereas now the vehicle is programmed to stay between two lines. For example: if the value read by the right contrast sensor is much lower than the value read by the left contrast sensor, then the right contrast sensor must be over the right-side lane boundary (black tape). Therefore, the car is too far right, and should turn left to compensate. To turn left, the right motor should be energized at a higher speed than the left motor.
Figure 3. Roadway bordered by electrical tape to test the car’s lane-keeping ability.
4. Download the code to the vehicle’s micro:bit.
5. To test the car’s lane-keeping ability, use two lines of electrical tape to create a “roadway” on a large piece of paper, similar to Figure 3. The roadway should be a bit wider than the car.
6. Turn the power switches on the MOVE vehicle and the Arcade controller to ON, then place the vehicle in the roadway. Place the vehicle at a slight angle to the roadway, so that it would run off the road if it went straight ahead.
7. Press the up arrow on the Arcade controller to make the vehicle move forward. When the vehicle is about to run off the road, it should automatically correct itself to stay on the roadway. Hint: The car’s sensors work best in brightly-lit areas. If the car’s lane-keeping or distance-sensing ability is poor, try moving to a brighter location. Or, use your phone’s flashlight to illuminate the area in front of and beneath the car. You can also adjust the values in the Forward function block of the code.
8. Congratulations, you have a fully-functional, “smart” remote control car with built-in driver assistance features! The car is fully remote controlled, but it will not allow itself to drive off the road or crash into something in front of it.
Extension
Create new, more complicated roadways with intersections, merges, and/or multiple lanes. Or, create a traffic pattern similar to the roads around your school. (Note: we will continue this theme in next week’s activity, so you can consider this a head start for next week)
Wrap-up
Today we converted the remote control vehicle from Activity 2.2 into a “smart” car with driver-assistance features.
What would happen if you attempted to drive off the road at a perpendicular (90 degree) angle to the edge of the road? Would your car prevent you from doing this?
Are there any other sensors that you could imagine using to improve the driver-assistance features of your smart car?
Do you think features like the ones we incorporated into the vehicle today are making modern cars safer? Or are they simply enabling drivers to become more distracted?
Next Activity
Next week is the final scheduled activity in Unit 2. We will make the roadways more realistic for our “smart” remote control vehicle, incorporating intersections and some of the devices from Unit 1 like traffic lights and railroad crossings. We will modify the code of the vehicle to build more driver assistance features, including the ability to communicate with the traffic lights and railroad crossings.