Wednesday 12 November 2014

Following a Line using a My Block

One of the challenges we've had with this year's competition has been following lines efficiently. The lines are different colors, sometimes we want to follow the left side of the line and sometimes the right, and we have different criteria for when to stop following.

Since we had this problem we assume others did as well, so here's our solution.

After programming a bunch of different missions and reproducing similar code several times we decided to try creating a My Block to do the work.

FollowLine My Block (click to enlarge)

This My Block takes four inputs: 
1) gain: the amount of correction to apply for a given size of error
2) power: the power applied to the driving wheels
3) light: the light level on the bright side of the line
4) dark: the light level on the dark side of the line

Using this My Block we can follow any color of line, along either side of the line.

We found some good advice online that said not to try to incorporate the end condition (when to stop following the line) in the My Block, and not to pass in the motor ports.  The end condition could be another light sensor detecting another color, a touch sensor being pressed, etc.  The way to accomplish this with this My Block is shown below:

In the example above, the robot drives with power of 40, using a gain of 1 to correct the steering, and stays between a white region of 40 and a dark region of 9, exiting the loop when the light sensor on port 3 sees black.  Note that changing the sign of the gain (-1 instead of +1) causes the robot to search to the left for the line instead of the right, allowing it to follow along the right side of a line instead of the left.  Also note that if you're using more than one light sensor you may want to add a "port" input specifying which sensor you want to use.

We hope this was helpful.  Good luck with your programs!

1 comment: