ukcrobots
API with the leJOS Java
implementation.
/** * A main-method class for the functionality defined in the * Model class. * Simply create an instance of Model and call its run method. */ public class ModelMain { public static void main(String[] args) { Model model = new Model(); model.run(); } }
Using this common outline allows students to focus on the features required of the Model class.
Each Model class to solve a particular problem
can set up its required device configuration within its
constructor and initiate its core functionality from the run
method. (Note that this does not imply that the Model class implements the
Runnable
interface or runs as a separate Thread
.)
Each has for the following form, therefore:
import ukcrobots.core.*; /** * Outline of a standard model class. * Flesh out the fields, constructor and run method * as required. */ class Model { // Device fields defined here ... /** * Initialise the fields by creating any required device objects. */ public Model() { // Device fields initialised here ... } /** * Implement the overall functionality of the model here. */ public void run() { // Model functionality operated from here ... } // Define any further (private) methods here ... }
A
and C
ports. Have the model move forward for
five seconds and then stop. Use the Motor
class
from the ukcrobots.core
package for the motors
and use the letTimePass
method
of a ukcrobots.util.Sleeper
object
as a way of letting time pass as the model moves.
As a variation, have the model reverse for a few seconds after it has moved forward. If it runs backwards for the same time as it ran forwards, does it always end back exactly where it started? Try this out several times, varying the number of seconds the model moves forwards and backwards for. If the model sometimes does not end up back where it started, why do you think this might be?
ukcrobots.core.LightSensor
for how to do this.
Modify your program so that the value of the light sensor is read
immediately after it has been activated, and display that value
on the RCX's screen
using an instance of the Display
class.
Add further statements to your program's run
method
so that the model moves for a few seconds after the light sensor has
been activated.
After that interval, stop the motors, take another light reading and
display its value. Let a few seconds pass before passivating the
light sensor and finishing the program. Are there any differences in
the two values that were displayed? Re-run the program somewhere else
in the room and compare the values displayed this time. Is there much
variation? What sort of values to you get in a dark area of the room or
near a window?
You can use the following
LightSearcherMain
program outline as the basis for your solution.
Only the findBrightest
method needs to be completed.
That will free you to concentrate on how to code the loop to perform the
repetative task of reading light values.
What sort of loop is best for repeating some statements a known number
of times (numberOfReadings
)?