Monday, October 04, 2004

Line Follower using Navigation

For the final version of the Line Follower control program, I used the Navigator API. The code is here. I like this approach: the code is much simpler and cleaner than the other versions, although it does rely on the calibration parameters being accurate.

One minor problem is this: when the robot needs to turn in order to find the line, it uses the navigator's rotate method. If the turn causes it to pass completely over the line from one side to the other, it will fail to notice that it has done so. I looked at the source code for rotate, and what it does is to calculate how long it needs to run the motors for in order to turn through the specified angle, and then sleeps (using Thread.sleep) until this time has elapsed. So I considered setting up a separate thread which watched the light sensor, and interrupting the sleep if this happens. If the sleep call in rotate is interrupted, it just stops the motors.

In the end, I decided this was too much hassle. However, it turns out there is another problem as well. The TimingNavigator class maintains an instance variable (called angle) containing the current angle. The rotate method updates this to the target value, i.e. the current value plus the rotation, before starting to run the motors. So if the thread is interrupted, angle will contain the wrong value.

A possible improvement to the class would be to update angle to the actual angle, based on when the thread was interrupted. Another change which might make life nicer would be to have a rotateUntil() method, which rotates through an angle or until some other condition is met. I would prefer this, as it gives you tighter control over how to interrupt the turn, and avoid the overhead of having a separate thread and catching the exception.

Sunday, October 03, 2004

Line Follower robot, version 2; The Calibrator

I recoded the line follower to use behaviors. It doesn't really seem to be an improvement in terms of clarity, though there may be better ways of coding it. Take a look here.

For the next step, I want to try out the Navigator API, in its TimingNavigator version. I will return to this in a later posting. The TimingNavigator takes parameters specifying the time to travel a standard linear distance, and to make a rotation. To help determine these, I wrote a class called the Calibrator. I was also finding myself spending some time setting the motor powers to get the robot to run straight, and finding the sensor thresholds for the light and dark levels. The class includes methods to help determine all of these. See the comments in the code for details. There is also a main method, which allows you to execute each of the tests in turn.