3.2.7
Driver Assist Vehicle Part 2
Driver Assist Vehicle Part 2
What to expect
Today, we will add a final “smart” feature to the remote control MOVE vehicle. We will add the ability for the car to communicate with the STOP:bit (stoplight), detect when the light is red, and stop automatically. We will do this by revising the micro:bit code for both the STOP:bit and the MOVE vehicle.
Connections
There are a few self-driving cars on the market today (e.g. Tesla) that use cameras to view the car’s surroundings and stop automatically at a red stoplight. Our MOVE vehicle does not have a camera, so it cannot “see” a red light, but it does have a wireless receiver. If we program the STOP:bit to send a wireless signal when the light turns red, the MOVE vehicle will receive the signal and “know” that the light is red. We can program the car to stop when it receives this signal.
In this activity we will begin by modifying the code for the STOP:bit to send a signal when the light is red. Then we will modify the code for the MOVE vehicle. We will start with the code that we developed last week in Activity 2.6, and we will modify it to receive and respond to a signal sent by the STOP:bit.
Materials
Kitronik MOVE vehicle
Kitronik Arcade controller
KitroniK STOP:bit (stoplight)
3 micro:bits: one for the STOP:bit, one for the MOVE vehicle, and one for the Arcade controller
USB connection cable for micro:bit
Large piece(s) of paper (for the roadway)
Black tape (for roads and lane boundaries)
Batteries for the STOP:bit, MOVE vehicle, and Arcade controller
Instructions
This activity will have two parts. First, we will program the STOP:bit to send a wireless signal when the stoplight is red, and a different wireless signal when the stoplight is green or yellow. Next, we will modify the code for the MOVE vehicle so that it stops automatically when the stoplight is red and the vehicle is near the stoplight.
Part I. Programming the STOP:bit
Open the Makecode website on your computer and create a new project. Name it Smart Stoplight or something similar.
Figure 1. Makecode menus and Kitronik STOP:bit extension tile
2. Click on the gray “+ Extensions” tab on the left side of your screen, search for “stop:bit,” and click on the “kitronik-stopbit” tile to add it to your code as shown in Figure 1. You should see the green “Kitronik STOP:bit” menu on the left side of your screen.
3. Under the Variables menu, create a variable called light. This variable will keep track of the status of the stoplight.
4. Create the code shown below in Figure 2. However, adjust the radio group number in the “radio set group” block to a unique value that no one else in your classroom is using. Note that blue blocks are under the Basic menu, pink blocks are under the Radio menu, red blocks are in the Variables menu, green blocks are in the STOP:bit menu, and teal blocks are in the Logic menu.
Explanation of the code:
When the program begins, it sets the radio group to 1 (you should change this to a unique value that no one else is using) and the light variable to 0.
The first forever loop causes the stoplight to continuously cycle through the traffic light colors. When the traffic light state is Stop, the stoplight’s red LED will be lit, and the variable light is set to 0. The stoplight remains in this state for 3 seconds. Then it switches to Go, where the green light will be lit, the variable light is set to 1, and it remains in this state for 3 seconds. Finally, it switches to Ready to Stop. In this state the yellow LED will be lit, and it remains in this state for 3 seconds (note that the variable light does not change in this state, so it is still equal to 1).
The second forever loop uses the value of the light variable to determine what radio signal to send. If the value of the light variable is 1 (which occurs when the stoplight is green or yellow), the micro:bit sends the value 8. If the value of the light variable is not equal to 1 (which occurs when the stoplight is red), the micro:bit sends the value 7.
Figure 2. Code for STOP:bit. Link to code: https://makecode.microbit.org/_WvYJkWbda77g
5. Download the code to the vehicle’s micro:bit.
6. 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.
Part II. Programming the MOVE vehicle
We will begin with the same smart driver-assist remote control vehicle code that we used last week in Activity 2.6. However, we will add a feature that allows the car to drive if it receives radio number 8, indicating the traffic light is green or yellow. If the car receives radio number 7, the traffic light is red, and the car will not be able to drive.
Return to the same code that you used in Activity 2.6. Remember, this code functioned as a “smart” remote control car. It is controlled like a normal remote control car, except it will not allow the car to collide with an obstacle in front of it, and it will not allow the vehicle to leave the roadway marked by black tape.
Under the Variables menu, create a variable called stoplight. This variable will keep track of the status of the STOP:bit traffic signal.
Add a new block of code to the program like the code in Figure 3. Note that pink blocks are from the Radio menu, teal blocks are from the Logic menu, and red blocks are from the Variables menu.
Explanation of the code:
From the previous section, we know that the STOP:bit will send a radio signal of 7 when the traffic light is red. If the number received by the vehicle is equal to 7 and the strength of the signal is greater than or equal to -45, the variable stoplight is set to 0. Otherwise, it is set to 1. The signal strength of the signal is a number between -128 (very weak signal) and -28 (strong signal). We will use the signal strength to determine how far away the MOVE vehicle is from the traffic light. If the MOVE vehicle is far away from the STOP:bit, the signal strength received by the vehicle will be low, and the car will not respond to the traffic lights. If the signal strength is very weak, the vehicle is probably far away from the traffic light, and it does not need to obey the traffic light. You can adjust this number to change the distance at which the vehicle responds to the traffic light. In another part of the code, we will use the variable stoplight to enable or disable the movement of the car.
Figure 3. Additional code to communicate with the STOP:bit. Link to full code: https://makecode.microbit.org/_TwbYLPW7TFa2
4. The final modification to the code is to use the stoplight variable to enable or disable the movement of the car. If the value of stoplight is 0, the car should stop. If the value of stoplight is 1, the car should continue to operate normally as it did in Activity 2.6.
5. To complete this, we only need to add an “if - else” block to the forever loop, and put the code that was already in the forever loop inside this new “if-else” block. Edit your forever loop so that it looks like Figure 4. Notice that most of the code is identical to the previous code, but it is now wrapped in an “if - else” statement that checks the value of the stoplight variable. Note: you do not need to re-create the code that you already had in the forever loop! Simply click and drag the contents of the forever loop aside, insert an “if - else” block, and drag the previous contents back into the “if” portion of the “if-else” block.
Figure 4. Modified forever loop so that the vehicle will only move if the value of the stoplight variable is 1. Link to full code: https://makecode.microbit.org/_TwbYLPW7TFa2
6. Download the code to the vehicle’s micro:bit and insert the micro:bit into the vehicle.
7. Turn the power switches on the MOVE vehicle and the Arcade controller to ON and connect the batteries to the STOP:bit. Then place the vehicle on the floor. Place the traffic light nearby, and ensure the traffic light is cycling between red, green, and yellow lights.
8. Drive the vehicle in the vicinity of the traffic light. When the traffic light is green or yellow, the car should operate just like it did last week. When the traffic light is red and the car is close to the traffic light, the car should stop automatically. Note: if the traffic light is directly in front of the vehicle, the vehicle may see the traffic light as an obstacle and stop to avoid running into it. To avoid this, keep the traffic light off to the side off the vehicle.
9. Use the large pieces of paper and black electrical tape to create intersections, networks of roads, or any other traffic pattern that you can imagine.
Extension
Now that you have included the STOP:bit into the control of the MOVE vehicle, try to incorporate the ACCESS:bit into the vehicle’s control code as well. Try to program the vehicle so that the vehicle will not move when the access gate is lowered.
or
Try to make a system of roads with paper and black tape that matches the roads outside of your school.
Wrap-up
In this activity we created a stoplight that communicates with the MOVE vehicle so that the vehicle stops automatically when the stoplight is red, then tested its operation on a roadway made from paper and electrical tape. Someday this safety feature could be implemented into real cars on the roads, and it could improve safety for drivers, passengers, and pedestrians.
How close did the MOVE vehicle need to be before it responded to the red stoplight? Could you adjust it?
Besides stoplights, are there other objects on real-world roadways that could communicate with vehicles, like stop signs, pedestrian crossings, etc.? Can you think of any examples between your home and school?
Do you think a system like this should be implemented in the real world?
Next Activity
This concludes Unit 2. In the next unit, you will begin building a life-size electric vehicle - an off-road, all-terrain electric scooter!