You're starting on the Line Follower robot today. Students who would like a skeleton class should copy the code from the following two files into their project:
The LineFollower class sets up some helpful variables, and also outlines a basic flow for the program to try and follow a line. You are not required to use this exact set of methods. What I've provided is a suggestion, but you can solve the problem however you'd like. That said, if you're unsure about how to proceed, stick to what I've given you for now.
There are several methods in the skeleton, all well-commented. Some are complete and ready to use, while others will need to have code added by you.
To start, complete the turn
method. This method,
when given a number of degrees, runs the motors so that the robot
is facing that many degrees away from its center position. For
example, if you said turn(90, false);
the robot would
be facing 90 degrees to the right when done. If you said
turn(-360, false)
the robot would turn to the left in
a complete circle.
The turn()
method needs to have a way to convert
"robot degrees" into "motor degrees". If you measure how far
apart the wheels of your robot are, and you know the diameter of
the wheels, you should be able to generate a reasonable estimate
of the ratio from one to the other. You can use this
relationship to convert robot headings into motor rotations.
turn()
method already written.Your next assignment is to work on the findInitialLine()
method. It needs to take the following actions:
turn()
method, turn the robot in a full circle (or more).true
for the immediateReturn
parameter, it will cause your program to run other code while the turn is still running.)Optional hint: to help debug your program, have the robot make a noise every time it sees a new "brightest" reading. You can make a beep by including this line of code in your program:
brick.getAudio().playTone(440, 500);
It's now time to write the findLine()
and followLine()
methods.
The approach is similar to that of findInitialLine()
in that you're going to start the motors moving and then take samples from the light sensor and look for a certain condition. For findLine()
you're looking for a reading that says you're on the line (you should stop the motors and return true). For followLine()
, you're looking for a reading that says you're off the line (you should stop the motors and return).
Because each of these methods picks up where the other left off, they work together to accomplish the task. If you want to test them in isolation, start with followLine()
and just have findLine()
return true
. You can manually place your robot on the line and let followLine()
do its job.