Programming Ideas Using The UKC API
(In Preparation)

Background

This page documents some simple programming exercises that make use of our ukcrobots API with the leJOS Java implementation.

A Main-Method Class

In our Java teaching we de-emphasise the main method through use of the BlueJ Interactive Java Environment. It is not possible to use this environment when developing programs for the RCX, so instead we tend to always use a standard main-method class of the following form.
/**
 * 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 First Exercise - Object Creation

Implement a model with two motors connected to the 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.

Solution.

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?

Using a Light Sensor

Build a simple model with two motors and a light sensor. This will serve as a basis for some of the exercises on looping that are described below. Write a program that activates the light sensor for a few seconds and then passivates it. See the documentation for the 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?

Finding the Brightest Reading

This exercise involves writing a loop. The task is to have a model rotate in place, taking a succession of light readings, in order to find the brightest reading; i.e., the highest value received by the light sensor in that position.

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)?


The work described here has no official connection with the LEGO company.
LEGO is a registered trademark of the LEGO company.
RCX and MINDSTORMS are trademarks of the LEGO company.
Java is a trademark of Sun Microsystems, Inc.
This document (http://www.cs.kent.ac.uk/~djb/rcx/programming.html) is maintained by: David Barnes, to whom any comments and corrections should be addressed.
© David J. Barnes
Last Updated: 21st March 2003
Created: 21st March 2003