3.2.5
Line Follower Vehicle
Line Follower Vehicle
Figure 1. Line following robot in a manufacturing plant. Source: https://www.youtube.com/watch?v=peOM_Nk4AEY
What to expect
Today we will use another built-in capability of the MOVE motor vehicle: the ability to follow a line on the floor. It will do this by using a pair of sensors on the bottom of the vehicle that detect the amount of light received, then send that information to the micro:bit. Once you program this capability into the vehicle’s micro:bit, it will follow a line on the floor, wherever it may lead.
Connections
Line following robots are used in many industrial applications. For example, many modern manufacturing plants use autonomous line following robots to deliver materials from one part of the factory to another, as shown in Figure 1. In this activity, we will develop another way of making the MOVE motor vehicle drive autonomously, without any assistance from you after you program it and set it on a line. In future activities we will tie together these autonomous capabilities to make the car operate even more independently.
Materials
Black electrical tape
Scissors
Large sheet of light-colored paper (the floor itself may also work, especially if it is light-colored)
Kitronik MOVE motor car with micro:bit
USB cable
Computer
Instructions
To follow a line on the floor, line following vehicles use contrast sensors located on the bottom of the vehicle. This helps the vehicle tell the difference between the light-colored floor and the dark-colored line.
Figure 2. Contrast sensors on the line following board
On the MOVE motor vehicle, these contrast sensors are located on a board on the bottom of the vehicle, near the front as shown in Figure 2. The board has two contrast sensors: one on the right side of the vehicle labeled “R”, and one on the left side of the vehicle labeled “L”. These contrast sensors detect the amount of light received by the sensor, then send that data to the micro:bit.
The sensors on the MOVE vehicle give a low value if the surface they are sensing is dark (not reflective) and a high value if the surface is light (reflective). If the two sensors are at nearly the same value, then the car is likely on the line (or else it is completely off it). If the left sensor has a much lower value than the right sensor, then that means the left sensor is detecting the dark line, while the right sensor is detecting the light-colored background. Thus, the vehicle is too far to the right, and should turn left to get back to the center of the line. Conversely, if the right sensor has a much lower value than the left sensor, that means the right sensor is detecting the line, while the left sensor is detecting the background. Thus, the vehicle is too far left, and should turn right to get back to the center of the line.
Like previous weeks, this activity will have two parts. First, we will investigate the data that the contrast sensors provide. Then, we will use that information to program the micro:bit so that the vehicle will follow a line.
Part 1. View input from the contrast sensors
It is helpful to start by viewing the input data from the contrast sensors. We can do this by making a simple micro:bit program, then putting the MOVE vehicle at different points on or near a black line and reading the data.
Start by creating a new Micro:bit project (https://makecode.microbit.org/#editor). Name it Line Follower Calibration or something similar.
Figure 3. MOVE Motor extension
2. If you don’t already have the “MOVE Motor” extension in the MakeCode menu as shown below, add it by clicking on the gray Extensions button, then search for “MOVE Motor”, then click on the tile labeled “kitronik-move-motor”. You should now see the MOVE Motor extension in the menu as shown in Figure 3.
Figure 4. Code to view contrast sensor data. Link: https://makecode.microbit.org/_WRLK0M0YE1hg
3. Next, create a program that looks like the code in Figure 4 below. The purple blocks are from the Input menu, the blue blocks are from the Basic menu, and the green blocks are from the MOVE motor menu (under the Sensors sub-menu).
Figure 5. Straight line of dark-colored electrical tape on light-colored background
4. Connect your micro:bit to your computer and download the code onto your micro:bit.
Disconnect the micro:bit from the computer. If you haven’t already, insert your micro:bit into the MOVE motor car with the micro:bit’s A and B buttons facing forward.
Use the dark-colored electrical tape to create a straight line on a light-colored piece of paper, similar to Figure 5.
Figure 6. MOVE vehicle with the left contrast sensor directly on top of the line
5. Place the MOVE vehicle on the paper with the left contrast sensor directly on top of the line, as shown in Figure 6. Then turn the MOVE motor car’s power switch to “ON”.
Press the “A” button and make a note of the value shown on the LEDs for the right sensor. Then press the “B” button and make a note of the value for the left sensor. Repeat several times. You should see that the value for the left sensor is much lower than the value for the right sensor. Depending on the ambient light level in the room, you might find that the sensor over the black line gives a reading of about 100-400, while the sensor over the white paper gives a reading of about 500-800.
Figure 7. MOVE vehicle with the right contrast sensor directly on top of the line
6. Now place the MOVE vehicle on the paper with the right contrast sensor directly on top of the line, as shown in Figure 7.
Press the “A” button and make a note of the value shown on the LEDs. Then press the “B” button and make a note of this value. Repeat several times. You should see that the value for the right sensor is now much lower than the value for the left sensor.
Finally, place the MOVE vehicle in the center of the line, so that the contrast sensors are on either side of the line. Press the “A” and “B” buttons several times and take note of the values. You should see that the values for the right and left sensors are similar. When you are done, turn the vehicle’s power switch to “OFF”.
Part II. Program the Line Following Vehicle
Create a new MakeCode micro:bit project and name it Line Follower or similar. Add the MOVE motor extension if it’s not already there.
Figure 8. Code for Line Follower robot. Link: https://makecode.microbit.org/_6sadJuKUmAKs
2. Next, create a program that looks similar to the code in Figure 8 below. The blue blocks are from the Basic menu, teal blocks are from the Logic menu, dark purple blocks are from the Math menu, and green blocks are from the MOVE motor menu (under the Sensors and Motors sub-menus).
Note: the numbers in the code below are suggestions to start from. You can fine-tune the performance of your vehicle by using different values.
Explanation of the code: If the left contrast sensor reads a value that is at least 200 more than the right contrast sensor, the right contrast sensor must be on the dark line. Therefore, the car is too far left, and should turn right to get back to the center of the line. To turn right, the left motor is turned on at a higher speed than the right motor. Conversely, if the right contrast sensor reads a value that is at least 200 more than the left contrast sensor, the left sensor must be on the dark line. Therefore, the car is too far right, and should turn left to get back to the center of the line. To turn left, the right motor is turned on at a higher speed than the left motor. Finally, if the two contrast sensors detect similar values, the car is likely centered on the line (or else it is off the line completely), and should drive straight ahead.
3. Connect your micro:bit to your computer and download the code onto your micro:bit.
4. Disconnect the micro:bit from the computer. If it isn’t already, insert your micro:bit into the MOVE motor vehicle with the micro:bit’s A and B buttons facing forward.
5. Place your MOVE vehicle on the paper with the sensors centered over the straight line. Then turn the vehicle’s power switch to “ON”.
6. If everything is working correctly, your car should drive down the line. It may “bounce” from one side of the line to the other, but it should not veer off the line. If it does not stay on the line, check your code to make sure it matches Figure 8. You can also try adjusting the numerical values in the code. After you are done, turn the car’s power switch to “OFF”.
Figure 9. Example of a track with curves
7. If your MOVE vehicle has successfully followed a straight line, it is time to try something more challenging! Use the electrical tape to make a track with some twists and turns, like Figure 9.
Place your MOVE vehicle on the track with the sensors centered over the line. Then turn the vehicle’s power switch to “ON”.
If everything is working correctly, your car should drive around the track. It may “bounce” from one side of the line to the other, but it should not veer off the line. If it does not stay on the line, check your code to make sure it matches Figure 8. You can also try adjusting the numerical values in the code. After you are done, turn the car’s power switch to “OFF”.
Hint: if the track has lots of sharp turns, the car will be able to stay on the line more reliably if there is a significant difference between the left and right motor speeds when the car is turning. However, this also tends to make the car “bounce” from one side of the line to the other on the straightaway sections of track. Conversely, the car tends to bounce around the line less when the difference between left and right motor speeds during the turns is low. However, this causes the car to have more difficulty following the line on twisty tracks. Experiment with different motor speeds to see what works best for your car and your track.
Extension
If your car successfully stayed on the track in the previous section, try making a more complicated track! For example, try joining two pieces of paper together and make a larger track with more turns. Or a figure-eight track.
Alternatively, apply the electrical tape directly to the floor in your classroom and make a track around your classroom.
Wrap-up
Today we used the micro:bit and the MOVE motor vehicle to first view the input from the vehicle’s contrast sensors, and then program the vehicle to follow a curved dark-colored line on the ground.
Does the line need to be much darker than the surrounding background? What if you had a black background and white electrical tape?
Was the vehicle able to follow a line placed directly on the classroom floor?
What are some examples of applications where this technology could be used?
Next Activity
In the next activity we will begin creating our own city neighborhood using many of the tools and materials that you have used in the past few months: traffic lights, gate crossings, and autonomous vehicles. Over the next few weeks you will add more realistic and interactive features to your neighborhood to make it come alive!