Reader small image

You're reading from  Learning Lego Mindstorms EV3

Product typeBook
Published inJan 2015
Publisher
ISBN-139781783985029
Edition1st Edition
Right arrow
Author (1)
Gary Garber
Gary Garber
author image
Gary Garber

Gary Garber teaches physics, math, and engineering at Boston University Academy. Gary is the president of the New England Section of the American Association of Physics Teachers and has led dozens of professional development workshops in education at both the local and national levels. Gary runs the Boston University FIRST Robotics program. He has run and hosted numerous robotics workshops in VEX, Tetrix, and LEGO platforms. He has run dozens of 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 is the author of Instant LEGO MINDSTORMS EV3, Packt Publishing. He currently resides in Massachusetts, US. When he is not playing with LEGO, robots, or toy trains, he enjoys spending time with his wife, Catalina, and their two children, Alejandro and Leonardo.
Read more about Gary Garber

Right arrow

Chapter 8. Advanced Programming and Control

In this chapter, we will explore advanced controlling algorithms to use for sensor-based navigation and tracking. We will cover:

  • Proportional distance control with the Ultrasonic Sensor

  • Proportional distance control with the Infrared (IR) Sensor

  • Line following with the Color Sensor

  • Two-level control with the Color Sensor

  • Proportional control with the Color Sensor

  • Proportional integral derivative control

  • Precise turning and course correction with the Gyro Sensor

  • Beacon tracking with the IR sensor

  • Triangulation with two IR beacons

Distance controller


In this section, we will program the robot to gradually come to a stop using a proportional algorithm. In Chapter 4, Sensors and Control, we wrote a program where the robot would stop a set distance from a barrier using feedback from the distance sensors. These programs used a discrete reading where the robot would run at full speed until the sensor feedback triggered the robot to abruptly stop. In a proportional algorithm, the robot will gradually slow down as it approaches the desired stopping point.

Before we begin, we need to attach a distance sensor to our robot. If you have the Home Edition, you will be using the IR sensor, whereas if you have the Educational Edition, you will use the Ultrasonic Sensor. Because these sensors use reflected beams (infrared light or sound), they need to be placed unobstructed by the other parts of the robot. You could either place the sensor high above the robot or well out in front of many parts of the robot.

The design I have shown...

Infrared versus Ultrasonic


We are going to start out with a simple program and will gradually add complexity to it. If you are using the Ultrasonic Sensor, it should be plugged into port 4, and this program is on the top line. If you are using the IR sensor, it should be plugged into port 1 and this program is at the bottom line. In this program, the robot moves forward until the Wait block tells it to stop 25 units from a wall or other barrier. You will find that the Ultrasonic Sensor can be set to stop in units of inches or centimeters. The Ultrasonic Sensor emits high-frequency sound waves (above the range of human hearing) and measures the time delay between the emission of the sound waves and when the reflection off an object is measured by the sensor.

In everyday conditions, we can assume that the speed of sound is constant, and thus the Ultrasonic Sensor can give precise distance measurements to the nearest centimeter. As mentioned in Chapter 4, Sensors and Control, using presence...

Proportional algorithm


In the next set of program, we create a loop called Slow Down. Inside this loop, readings from the Ultrasonic or Infrared proximity sensor block are sent to a Math block (to take the negative of the position values so that the robot moves forward) and then sent to the power input of a Move Steering block. We can have the loop end when it reaches our desired stopping distance as shown in the following screenshot:

Instead of using the exact values of the output of the sensor block, we can use the difference between the actual position and the desired position to control the Move Steering block, as shown in the following screenshot. This difference is called the error. We call the desired position the setpoint. In the following screenshot, the setpoint is 20. The power is actually proportional to the error or the difference between the positions. When you execute this code, you will also find that if the robot is too close to the wall, it will run in reverse and back up...

Line following using the Color Sensor


When we are using the Color Sensor in Reflected Light Intensity mode, the sensor emits light and the robot measures the intensity of the reflected light. The brightness of the red LED in the sensor is a constant, but the intensity of the reflection will depend on the reflectivity of the surface, the angle of the sensor relative to the surface, and the distance of the sensor from the surface. If you shine the sensor at a surface, you will notice that a circle of light is generated. As you change the height of the sensor, the diameter of this circle will change because the light emitted from the LED diverges in a cone. As you increase the height, the size of the circle gets larger and the reflected intensity gets smaller. You might think you want the sensor to be as close as possible to the surface. Because there is a finite distance between the LED and the photo diode (which collects the light) of about 5.5 mm, it puts a constraint on the minimum diameter...

Setpoint for line tracking


We first need to determine the desired setpoint of the Light Sensor reading for the robot to track on. When using only one sensor, we don't want to track the middle of the line because small changes in position will not be sensed. If we track along the edge of a line, then a small change in position will substantially affect the size of the correction error in our algorithm. In this case, 50 percent of the circle of red light will have a high reflectivity and 50 percent will have a low reflectivity. Small deviations from the edge will result in huge changes in the measured intensity. Placing the middle of the circle of light exactly on the edge can be a challenge. Since we are trying to obtain a numerical value, it is easier to separately measure the high reflection intensity and low reflection intensity and find the average of those two values.

You will find that you will frequently determine a new setpoint value based on your environmental conditions. In addition...

Two-level or bang-bang controller


When we looked at the simplest of distance controllers, we simply used a Wait block to tell the robot to stop. However, as we are tracking a line, our conditions relative to the line are constantly changing as the line may move straight or curve. The simplest controller is called a two-level or bang-bang controller. If the sensor measures a reading greater than the setpoint, the robot will curve to the left. If the sensor measures a reading less than the setpoint, the robot will drive to the right.

When you execute the program in the following screenshot, you will notice that the robot will follow a zigzag type of motion along the line:

In the program itself, you may notice that we read the value of the setpoint from the Read File block. You can actually take data from an external source into the loop, and this is shown with the splice in the wire at the edge of the loop. Alternatively, you could store the information from the File block into a variable....

Proportional line follower


The proportional algorithm will make a correction to the motor speeds, which is proportional to the magnitude of the error between the setpoint and the sensor value. Each wheel has a given base speed, which we correct by either adding or subtracting a factor proportional to the error. If the right wheel is faster, the robot turns to the left. If the left wheel is faster, the robot will turn to the right. A critical number for efficient line tracking is the proportional gain constant. If the gain is too large, the robot will overcompensate for errors and wiggle back and forth. If the gain is too small, the robot will have trouble following tighter curves.

Entering gain and speed

You could enter the gain and speed directly by reprogramming the robot. However, to save ourselves time in adjusting the setpoint, gain, and speed of the robot, let's first write two more programs to enter the speed and gain factors. The program in the following screenshot will allow you to...

PID controller


One of the limitations of the proportional controller is that a value for the gain in your program might be ideal for only one type of curve. For instance, the parameters that work well for tracking a straight line might not work as well on sharp curves. In some instances, the correction might not be strong enough, and in others, your robot might overcorrect. The PID controller attempts to compensate for these environmental variations.

We have already discussed the ideas behind the proportional controller. The strength of the correction is proportional to the error of how far our sensor reading is from the setpoint. If we are consistently having to include a large and consistent correction (say we are always a bit too far off the edge of the line), then we must have needed a stronger gain. The way to account for this on the fly is to have the robot remember that it has been consistently off the line (or too far above/below the setpoint) for a period of time and increase the...

Gyro Sensor


In Chapter 4, Sensors and Control, when we introduced the Gyro Sensor, we wrote a program that stopped the rotation of the robot when the Gyro Sensor reached a certain value. As we noticed, the robot always overshot this value. In Chapter 7, Advanced Programming, we tried to compensate for the overshooting with a two-level controller. Here we show a program where the measurement from the Gyro Sensor goes into a proportional controller.

You will find that this method is significantly more precise and useful in making exact turns.

The preceding program asks the robot to rotate through a given angle and slow to a stop. If we used a smaller gain constant, we could avoid using a loop. However, this can take more time and runs the risk of never reaching your setpoint. This is similar to approaching a set distance from a wall using the motion or proximity sensors. Again, we start out by resetting the Gyro.

A more interesting use of the Gyro Sensor will be to drive in a straight line under...

IR sensor navigation and beacon tracking


One of the simplest uses of the IR sensor and the IR beacon is to have the robot seek out the beacon. The program to have the robot seek the beacon is simple and again uses a proportional controller. The sensor block values of heading for the beacon range from -25 to 25. In the following program, my correction is going into the steering plug of a Move Steering block. Since the steering input values of the Move Steering block range from -100 to 100, you want to have a gain of 4 in the program. Make sure your beacon is set to the same channel as your sensor block, which in this case is channel 1.

If your robot turns in the wrong direction, you can either swap which cable the motors are connected to or change the gain constant to -4.

Navigating a field with the IR sensor and the beacon may not be as obvious as using the Ultrasonic motion sensor and the Gyro Sensor. However, if you read about approach paths used in aviation using non-directional beacons...

Tracking a circle


Using the beacon and the IR sensor, you can program your robot to drive in a circle while maintaining a constant radius relative to the beacon. To do this, the IR sensor should be mounted on the robot pointed to the side as shown in the following screenshot. The robot should be set up to drive a circumference of a circle.

Remember, the IR beacon has a limited cone of emission, so you will need to start the robot within about a 45 degree cone of the front of the beacon. This program will work better for larger distances (more than a meter). Your gain constant values will vary depending on the type of robot you use. If you were to set the beacon standing on its end, you can actually track a wider circle. However, the radius will be smaller when the robot travels around the back of the beacon since there is opaque plastic blocking some of the signal. To do this, you should use a smaller radius of about half a meter. The program starts out by sending the proximity to the beacon...

Triangulation


In aviation, pilots can use two non-directional beacons to triangulate their position. The instrument that tells us the heading of the beacons on an airplane is called the Automatic Direction Finder (ADF). If you have two IR beacons, you can do a similar thing with just one IR sensor mounted on your robot. One disadvantage of the heading indicator from the beacon is that the values are not in the degrees that the Gyro Sensor uses. Still, with some effort you can use two beacons to do some basic triangulation.

In the following program, we use two IR beacons. Each beacon is set to a different channel. Set the first beacon to channel 1. Set the second beacon to channel 2. The IR sensor should be on the front of your robot. Choose a location that you want the robot to navigate back to and place your robot at that point. It is helpful to angle the beacons towards the robot. When you run the program, the robot will first turn towards beacon 1 and take a proximity measurement. Then...

Summary


In this chapter, we explored advanced methods of navigations. We used both the Ultrasonic Sensor and the Infrared sensor to measure distance with a proportional algorithm. We used the Color Sensor with a two-level, proportional, and PID algorithm. We used the Gyro Sensor for a proportional algorithm and course corrections. Finally, we used the IR sensor with the IR beacon to navigate with several advanced techniques.

In the next chapter, you will learn about data logging and recording experimental work using the graphing features of the Educational Edition of the software.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Lego Mindstorms EV3
Published in: Jan 2015Publisher: ISBN-13: 9781783985029
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gary Garber

Gary Garber teaches physics, math, and engineering at Boston University Academy. Gary is the president of the New England Section of the American Association of Physics Teachers and has led dozens of professional development workshops in education at both the local and national levels. Gary runs the Boston University FIRST Robotics program. He has run and hosted numerous robotics workshops in VEX, Tetrix, and LEGO platforms. He has run dozens of 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 is the author of Instant LEGO MINDSTORMS EV3, Packt Publishing. He currently resides in Massachusetts, US. When he is not playing with LEGO, robots, or toy trains, he enjoys spending time with his wife, Catalina, and their two children, Alejandro and Leonardo.
Read more about Gary Garber