Fifth example: Trapped robot (NxtJLibA autonomous mode) Purpose: Use the NXT brick as an autonomous rover with a light sensor. Move the rover on a black floor. Whenever the rover leaves the floor, move it back, so that it remains trapped on the black area forever. The package NxtJLibA provides an addon to the leJOS NXJ library with a clean OOP design. Programs can be ported from the direct mode using the package NxtJLib with very little effort because the class design of NxJLibA and NxtJLib is the same, despite the implementations are very different: NxtJLibA uses the leJOS NXJ distribution extensively, while NxtJLib is independent of the leJOS iCommand distribution. The program makes use of the light sensor's event method bright() that is triggered when the reflected light level exceeds the given trigger level. When the rover quits the black area, it moves back and turns, then it moves forward again. // TrappedRobot.java Discussion: In order to prevent the program to terminate when the constructors comes to the end, we let it loop until the Escape button is hit. All work is done by the event method that runs in its own thread. If the program should be ported to the direct mode using NxtJLib, just replace while (!Button.ESCAPE.isPressed()) {} by while (!QuitPane.quit()) {} and change the related import from (In order to compile, download and run the program, the leJOS NXJ programming environment has to be installed. After this, download the NxtJLibA package and follow the installation instructions.)
Sixth example: Bluetooth communication (NxtJLibA autonomous mode) Purpose: A first NXT rover moves on a random path. It steers a second NXT rover that performs a similar movement. The package NxtJLibA contains a class Transceiver that implements a Bluetooth transmitter-receiver in order to exchange information as pairs of integers. When "switched on", the transceiver searches the recipient's transceiver to establish the connection. If the search fails, it enters a listening mode and waits for the recipient's transceiver to connect. With this peer-to-peer concept, it is of no importance which NXT is switched on first. The controlling robot is called master, the guided robot is called slave. The code for the slave is quite simple because the incoming data from the master triggers an event callback method received() that gets the pair of integer sent by the master. The callback method notifyConnection() is called when the connection is established or lost. The interface Command defines the communication protocol. // SlaveRobot.java After switching on its transceiver, the master sends the commands to the slave using the method send() that takes a pair of integers. The rover moves on a polygon with random angles. // MasterRobot.java
Discussion: The constructor's thread is put in a wait state by calling Tools.putSleep(). When the master closes the Bluetooth communication, notifyConnection(false) is invoked and Tools.wakeUp() notifies the waiting thread to continue. Do not forget to call exit() to release all resources. As stated before, you do not have to care whether the master or the slave program is started first. But when one of them is started, you must wait for about 5 seconds to start the other, because during this time each program searches mutually for a waiting recipient. | |||||||||