Instant LEGO MINDSTORMS EV3 [Instant] — Save 50%
Your guide to building and programming your very own advanced robot using LEGO MINDSTORMS EV3 with this book and ebook
In this article, written by Gary Garber, the author of Instant LEGO Mindstorm EV3, you will make a robot that will track a line quickly. The robot will be able to follow a line which makes corners sharper than 90 degrees using a proportional line follower.
(For more resources related to this topic, see here.)
First, you will need to build an attachment to hold the color sensor onto the robot.
Insert an axle that is five modules long into the color sensor. Place bushings onto the axle on either side of the sensor. This is illustrated in the following figure:
Attach the two-pin one-axle cross blocks onto the axle outside the bushings. This is illustrated in the following figure:
Insert 3-module pins into the cross blocks as shown in the following figure:
The pins will attach to the robot just in front of the castor. The bottom of the color sensor should be approximately leveled with the plastic part of the castor holder. If you are on a flat hard surface, your light sensor will be half a centimeter above the ground. If you are on a soft surface, you may need to add a spacer to raise up the sensor. This is illustrated in the following figure:
How to do it...
We are going to write a proportional line following code similar to the code used for the ultrasonic motion sensor.
- We will write the following code:
- This program contains a loop so the robot will track a line for 30 seconds.
- The base speed of the robot is controlled by a constant value which in this case, is 15.
- You will need to determine a desired light sensor value for the robot to track on. You can either read the light sensor reading directly on your EV3 brick, or look at the panel on the lower-right hand corner of your screen. You should see all of the motors and sensors that are currently plugged into your brick. In the following screenshot, the current light sensor reading is 16.
- When tracking a line, you actually want to track on the edge of a line. Our code is designed to track on the right edge of a black line on a white surface. The line doesn't have to be black (or a white surface), but the stronger the contrast the better. One way to determine the desired light sensor value would be to place the light sensor on the edge of the line.
- Alternatively, you could take two separate readings on the bright surface and the dark surface and take the average value.
- In the code we discussed, the average value is 40, but you will have to determine the values which work in your own environment. Not only will the surfaces affect the value, but ambient room light can alter this value.
- The code next finds the difference between the desired value and the sensor reading.
- This difference is multiplied by a gain factor, which for the optical proportional line follower will probably be between 0 and 1. In this program, I chose a gain of 0.7.
- The result is added to the base speed of one motor and subtracted from the based speed of the other motor:
MotorBPower = Speed - Gain * (LightSensor - DesiredValue)
MotorCPower = Speed + Gain * (LightSensor - DesiredValue)
- After taking the light sensor readings, practice with several numbers to figure out the best speeds and proportionality constants to make your robot follow a line.
How it works...
This algorithm will make corrections to the path of the robot based on how far off from the line the robot is. It determines this by calculating the difference between the light sensor reading and the value of the light sensor reading on the edge. Each wheel of the robot rotates at a different speed proportional to how far from the line it is. There is a base speed for each wheel and then they will go either slower or faster for a smooth turning. You will find that a large gain value will be needed for sharp turns, but the robot will tend to overcorrect and wobble when it is following a straight line. A smaller gain and higher speed can work effectively when the line is relatively straight or follows a gradual curve. The most important factor to determine is the desired light sensor value.
Although your color sensor can detect several colors, we will not be using that feature in this program. The color sensor included in your kit emits red light and we are measuring the reflection of that reflected light. The height of the sensor above the floor is critical, and there is a sweet spot for line tracking at about half a centimetre above the floor. The light comes out of the sensor in a cone. You want the light reflected into the sensor to be as bright as possible, so if your sensor is too high, the reflected intensity will be weaker. Assuming your color sensor is pointing straight down at the floor (as it is in our robot design), then you will see a circular red spot on the floor. Because the distance between the detector and the light emitter is about 5 to 6 mm, the diameter of this circle should be about 11 mm across. If the circle is large, then your color sensor is too high and the intensity will weaken. If the circle is smaller than this, then the sensor will not pick up the emitted light.
The color sensor in the LEGO MINDSTORMS EV3 kit is different from the optical sensors included in the earlier LEGO NXT kits. Depending on your application, you might want to pick up some of the older NXT lights and color sensors. The light sensor in the NXT 1.0 kit could not detect color and only measured reflected intensity of a red LED. What is good about this sensor is that it will actually work flush against the surface and saves the need to calibrate changes due to the ambient lighting conditions. The color sensor in the NXT 2.0 kit actually emitted colored lights and contained a general photo detector. However, it did not directly measure color, but measured the reflection of colored light, which it would emit. This actually allowed you to track along different colored lines, but it was also slower. The new EV3 sensor detects colors directly, works quickly, and emits only red light.
This article taught us to alter our robot, so it can track a line using an optical sensor. We used a proportional algorithm and adjusted the parameters for optimum tracking. Finally, we also wrote a program allowing the robot to be calibrated without the use of a computer.
Resources for Article :
- Playing with Max 6 Framework [Article]
- Panda3D Game Development: Scene Effects and Shaders [Article]
- Our First Project – A Basic Thermometer [Article]
|Your guide to building and programming your very own advanced robot using LEGO MINDSTORMS EV3 with this book and ebook|
eBook Price: $9.99
About the Author :
Gary Garber teaches physics, math, and engineering at Boston University Academy. Gary is the Past-President of the New England Section of the American Association of Physics Teachers, and has led dozens of professional development workshops in education, on both the local and national level. Gary runs the Boston University FIRST Robotics program. He has run and hosted numerous robotics workshops in VEX, Tetrix, and LEGO platforms. He has also run several LEGO robotics tournaments and spoken on robotics education at both local and national conferences. His robotics team has worked with Engineers Without Borders, NASA, and the National Science Teachers Association on a variety of engineering and education projects. He is currently an educational consultant, working to develop new software tools for the classroom, at the Tufts Center for Engineering Education and Outreach, which is a pioneer in LEGO Robotics Education. He currently resides in Massachusetts, US and when he is not playing with LEGOS, robots, or toy trains, he enjoys spending time with his wife Catalina, and their two children, Alejandro and Leonardo.