Date: 23-02-12
Duration: 3 hours
Group members attending: Tore, Troels
Goal
Todays goal was completing the lab exercises for this week, which was about using the light sensor to control a car. This was accomplished follow the below plan.
Plan
1. Use the test program (*1) to test the blackt/white light sensor
2. Test the program (*2) line follower with calibration
3. Make a color sensitive program using (*2) that can detect 3 colors: black, blue, white.
4. Use the above experience to make a program that has the car follow a black line and stop in a blue end zone
5. Make a PID line follower
Execution
1. We wrote this (*3) program to test the BlackWhiteSensor program. The sensor gives a black value of around 35, while white is 60. We also tested the blue color with the value 45. Based on this, it should be easy to make a program that recognizes three colors. By calibrating the colors before testing, we take into account the light conditions in the room, which in the end will make for a better linefollower program.
2. We tested the line follower with calibration program (*2). On the straight line it did well, however on the curved line it was not always able to follow. It seemed like the program slept for too long between each correction. We could also change the amount of power to the motors to make it go a little slower or turn less.
3. We just copied the program we wrote for part 1, but change it so we had two thresholds, instead of only the blackWhiteThreshold:
blackBlueThreshold = (black value + blue value) / 2
and
blueWhiteThreshold = (blue value + white value) / 2
Now we could use these two thresholds to dinstinguish the three different colors in the same manner as with only the black and white:
public boolean black() {
return (ls.readValue()< blackGreenThreshold);
}
public boolean white() {
return (ls.readValue()> greenWhiteThreshold);
}
public boolean green() {
return (ls.readValue() > blackGreenThreshold && ls.readValue() < greenWhiteThreshold);
}
4. We used the program from part 2, and added a stop condition to make it stop in the end zone. The control while loop now looks like this
if ( sensor.black() ) {
Car.forward(power, 0);}
else {
Car.forward(0, power);}
if (sensor.green() && sideSensor.green()) {
Car.stop();}
We also changed the power to 60. It didn’t work, the 60 power was almost too low to make the car move, and the thresholds did not seem to work correctly. After a few tests we found out that at the transition between the black tape and the white background, the sensor would sometimes measure a mean value, which would be in the blue interval and make the car stop.
Our solution was to use the extra light sensor we was given. We used the middle sensor to follow the line, i.e. test for black and white, and the side sensor to test for the blue color and make the car stop. The extra sensor was mounted like this:
5. The next step was to use the PID controller to make a program that uses one sensor, but which would stop half way through. We basically implemented the pseudo code given in the article (*4). We wrote this control loop:
final int kp = 1;
final int tp = 68;
int offset = sensor.getThreshold(); // The black and white threshold generated from calibration
while (! Button.ESCAPE.isPressed())
{
int lightValue = sensor.light();
int error = lightValue - offset;
int turn = kp * error;
Car.forward(tp + turn, tp - turn);
}
kp is the turn value, and tp is the basic speed.
Test 1: At the first try, we realized our kp value at 10 was much too high, and the car would just oscilate in place without moving forward. We then changed kp to 2.
Test 2: the tp value of 50 was too low, the car was not able to drive forward with a power to both wheels of only 50. We changed it to 75.
Test 3: tp of 75 was too much, the car turned too much, so we lowered tp as well as the sleep time from 10 to 5 ms, thus making the car react faster.
Test 4: Still too large turns, so we lowered kp and tp again.
…
Test X: We have settled on kp = 1, tp = 65 and 0 sleep time. If we increase tp just a little, the car becomes less reliable because it does not turn fast enough at the sharp turns, however it also have trouble driving straight. We think instead of having the turn mechanic follow a linear graph, one could use a quadratic, thus still making small corrections when driving straight but the car should also be able to make sharp turns.
Status
We almost completed todays exercises. We tested the light sensor with the given test programs combined with our own program (*3) and got the car to follow a line and stop in the blue end zone. This was a bit complicated and we did not know how to do this with only one black/white light sensor, but it was fixed with two sensors.
The PID program worked rather well, although we actually only implemented the "P" in PID, the "I" and "D" will be another day.
A quick thought about our PID implementation is that when the robot drives straight, it slows down to a speed of 65. When it turns, one of the wheels increases speed and the robot drives faster. If we add something like 1/error to the final speed, this could increase the speed while driving straight, but decrease when turning, so that the robot would still be able to follow the line. Or we could increase the tp to maybe 75, and then slow down the wheels instead of speeding up to turn.
We also thought about other ways to control the robot like the PID. One thing was using a quadratic equation as described earlier, instead of a linear. This, however, is a project for some other time.
Videos can be seen here (*5). The first shows the PID-car follow the curved line. In the second video the car follows the line on the landscape table. In the latter video, we see our point about the car not turning sharp enough: it almost drives out of the first turn, and does not complete the second.
The code for our two programs, PIDfollower and the sensortest program, can be seen here (*3).
(*3) and (*5) are actually just other posts on the blog.
Referenses
(*1) http://legolab.cs.au.dk/DigitalControl.dir/NXT/Lesson4.dir/BlackWhiteSensor.java
(*2) http://legolab.cs.au.dk/DigitalControl.dir/NXT/Lesson4.dir/LineFollowerCal.java
(*3) http://troelskristiantore.blogspot.com/2012/02/legolab-4-code.html
(*4) http://www.inpharmix.com/jps/PID_Controller_For_Lego_Mindstorms_Robots.html
(*5) http://troelskristiantore.blogspot.com/2012/02/blog-post.html
No comments:
Post a Comment