Line following works when the values used in setting the motor's power or robot's steering are set correctly. This overview will contain instructions you can use to easily configure the line following program for your robot. Line following programs use reflected light. Light emitted from the sensor is reflected off of the surface and returned back to the sensor. The amount of light reflected is measured by the sensor. Areas of the surface which are lighter reflect more light. Darker areas of the surface absorb light. Therefore, the amount of reflected light can be used as a measure indicating to the robot if the sensor is over a light or dark area.
To follow a line, you want to try to get the robot to find the midpoint between the light and dark. In other words, the robot should follow the edge of the line. If the robot sees too much light, then it should turn toward the line. If the robot sees too much dark, then it should turn away from the line.
A variable will be used to indicate to the robot, what the midpoint should be between light and dark. This variable will be called the 'target'. The difference between the lightSensorPct() and the target will be used for steering. If the robot does not turn enough to follow curves in the line, then you may need to multiply the difference between the lightSensorPct() and the target by a value called the 'gain'.
Tuning your Line Following Function
For optimal performance, the values for 'target' and 'gain' should be tuned for the particular line you are following and the environment the robot is operating in. The ambient light in a room can have an effect on the performance. A darker room will require a lower target value than a lighter room. The best way to set these values is to initially set the 'target' and then set the 'gain'.
The best way to set the target is to start with a gain set to 1. Then set the power in the syncMotors function to zero as in: gain=1 syncMotors(B, C, 0, gain*(lightSensorPct()-target)) Now look at the display as you push the robot across the line. You should see positive and negative numbers. The target will need adjusting if you see more positive than negative numbers (or see more negative numbers than positive). Decrease the target if you see more positive than negative numbers. Increase the target if you see more negative than positive numbers. For example, if you see +45 and -10, you will need to decrease the target by about 20. If you see +20 and -30, you will need to increase the target by about 5. If you see +80 and -20, you will need to decrease the target by 30. If you see +50 and -30, you will need to decrease the target by 10.
The target is set correctly once you have about an equal value between positive and negative numbers (e.g., +30 and -30)
To set the gain, restore power to the syncMotors function and let the robot follow the line. If there are curves in the line, does the robot follow them? Increase the gain if the robot is not turning enough to follow the curves in the line.
If the robot wiggles back and forth as it follows the line, try decreasing the amount of sleep. Instead of sleep(100), try sleep(20).
Line Following Example 1: One Sensor Steering with Constant Speed
Line Following Example 2: If-Block Based Steering
Line Following Example 3: Stepped Switching
The basic idea in this line following program is to increase the number of steps of steering. The typical implementation will have steps such as:
Turn Sharply to the Left (Motor B 60% power; Motor C Stopped)
Turn Gradually to the Left (Motor B 60% power; Motor 30% power)
Go Straight (Motors B and C 60% power)
Turn Gradually to the Right (Motor B 30% power; Motor 60% power)
Turn Sharply to the Right (Motor B Stopped; Motor C 60% power)
The range of values returned by a calibrated light sensor is 100. Divide this by 20 to get the five steps needed for the switch.
Line Following Example 4: One Sensor Proportional Power Level
The basic idea in this line following program is to increase the power proportionally based on the values from the Light Sensor.
Since the light range for a Light Sensor is 100, we can divide the current light sensor percent by 100 to convert it to a portion to be multiplied by the Default Power level. This becomes the power level for Motor B. The difference between the Default Power Level and the power for Motor B becomes the power for Motor C.
You can change which side of the line the robot follows by changing which motor gets the initial power.
Line Following Example 5: Proportional Power Level With Constant
The basic idea in this line following program is to increase the speed of the robot by adding a constant level of power to each motor in addition to the proportionally adjusted power from the Light Sensor.
Tip: Try to keep the robot moving forward by adding a constant power to both motors.
Since the light range for a Light Sensor is 100, we can divide the current light sensor percent by 100 to convert it to a portion to be multiplied by the Default Power level. Then add the Constant power. This becomes the power level for Motor B. The difference between the Default Power Level and the product of the Default power and the light sensor becomes the power for Motor C. Don't forget to add the Constant power to Motor C as well.
In summary, both motors get the Constant power. But one motor gets more, or less, of the variable power depending on the value obtained from the light sensor.
Tuning
Increase or decrease the Constant power to make the robot move faster or slower.
Increase or decrease the Variable power to help the robot to make wider or narrower turns.
If you want to change which side of the line the robot follows, change whether Motor B or Motor C is shown first in the program.
Troubleshooting
If the robot moves away from the line, place the robot on the other side of the line.
Line Following Example 6: Proportional Power (PID Minus the ID)
The basic idea in this line following program is to adjust the speed of each motor of the robot by adding (or subtracting) a variable level of power from a constant level of power.
Background: This method is based on a part of the classic PID controller. PID is short for Proportional, Integral, Derivative. A PID controller calculates an "error" value as the difference between a measured light value and the desired value (usually 50% if following a line). The controller attempts to minimize the error through the use of a formula. The PID controller formula involves three separate parameters: the proportional, the integral and derivative. Using a NXT robot, we can obtain excellent line following using just the "P" part of the formula. The Integral and Derivative are not needed at this level.
Since the light range for a Light Sensor is 100, we can divide the current light sensor percent by 100 to convert it to a portion to be multiplied by the Default Power level. This becomes the power level for Motor B. The difference between the Default Power Level and the power for Motor B becomes the power for Motor C.
Tuning
Increase or decrease the Constant power to make the robot move faster or slower.
Increase or decrease the Variable power to help the robot to make wider or narrower turns.
If you want to change which side of the line the robot follows, change whether Motor B or Motor C is shown first in the program.
If the robot shakes rapidly while following the line, reduce the amount of variable power.
If the robot fails to make turns, increase the amount of variable power.
Troubleshooting
If the robot moves backwards along the line, try multiplying the power for each motor by -1.
In Robot JavaScript, if you want to change the direction, you would need to reverse the sign of the power being supplied to the motor statement.
If the robot moves away from the line, place the robot on the other side of the line. Remember, all line following robots try to steer toward the edge of the line.
The program tells them whether to look for the left side or the right side of the line.
If the robot moves back and forth without making progress, adjust the value of variablePower (see "Tuning" above).
Line Following Example 7: Proportional Power with Two Sensors
The basic idea in this line following program is to get the robot to follow the line using a light sensor on each side of the line. Optimally, the two light sensors should each see gray (half over the line and half over the white background). This program will subtract the value from one sensor from the value of the other sensor (i.e., it will calculate the difference between the two sensors). If the difference between the sensors is zero (i.e., each sensor is receiving the same amount of light), then the robot will apply 50% power to each motor (driving the robot straight). If one sensor sees more light than the other, then the difference between them will not be zero. The robot will then apply a greater percentage of power to one motor than to the other.
The function getLightDiffPortion() is created here to calculate a portion of the default power that should be applied to motor B. The function will return the ratio of the numerator divided by 200. This will result in a real number from 0.00 to 1.00. This portion will be multiplied by the default power to determine the power for Motor B. Motor C will then receive the difference between the default power and the power given to Motor B.
Line Following Example 8: Steering Controlled By Two Sensors
Steering is a numeric value that ranges from -200 to +200. To drive straight, the steering value is zero. In between -200 and 0 and +200 and 0 are different combinations of power applied to the two motors. Values of steering that are between -99 and +99 result in the motors turning in the same direction. Values between -200 to -101 and +200 to +101 result in the motors turning in opposite directions. Steering that has a value of either +100 or -100 will result in a swing turn in which only one motor is turning.
To control steering using two light sensors, just multiply the difference between the light sensors by a constant called gain (0.5 to 2). If both light sensors are the same, then the difference between them would be zero and the product of this difference times the constant would also be zero resulting in the robot diving straight.
If one sensor has a value higher than the other sensor, then the difference between them times the constant would enable the robot to turn toward the center of the line.
Adjusting the Gain: If the line you are following is relatively straight, you can reduce the gain to reduce excess turning by the robot. If the line you are following has turns, you can increase the gain to help the robot turn more as needed.
Line Following Example 9: Steering Controlled by a Light Sensor Event Listener
What are Events?: Events are triggered by changes in the sensor values. When a sensor detects a change in the environment, it will send a message indicating that a change has been detected. You can create functions to monitor (i.e., listen) for these changes.
JavaScript allows you to handle various events that the computer (or robot in this language) may encounter.
In RobotJavaScript, you can handle events that occur in the sensors, motor encoders, and buttons on the robot.
For example, a touch sensor event occurs when the touch sensor is pressed. A touch sensor event also occurs when the depressed touch sensor is subsequently released. A light sensor event occurs whenever there is a change in the light sensor percent. A gyro sensor event occurs whenever the gyro sensor detects rotation. An ultrasonic sensor event occurs whenever the ultrasonic sensor detects a change in distance. A motor encoder event occurs whenever the motor turns.
A function will handle the changes in the light sensor. Those changes will be converted into steering that will control the robot.