// SOME LEJOS JAVA EXAMPLE PROGRAMS
// PlaySounds.java
// Play tones of increasing frequency & display values on LCD
// import java.lang.System;
import josx.platform.rcx.*;
class PlaySounds
{
public static void main (String[] args)
{
for(int i=16;i<=32000;i=i*2)
{
Sound.playTone(i,30);
LCD.showNumber(i);
LCD.refresh();
try{Thread.sleep(500);}
catch(Exception e) {}
}
}
}
// SoundSensor1 -- Uses Sensor1 press to trigger playing a sound
// Illustrates the polling technique
import josx.platform.rcx.*;
class SoundSensor1 implements SensorConstants
{
public static void main (String[] args)
{
Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH,SENSOR_MODE_BOOL);
while (true)
{
if(Sensor.S1.readBooleanValue())
{
Sound.playTone(220,10);
try{Thread.sleep(30);}
catch(Exception e) {}
}
}
}
}
// SoundSensor1 -- Uses Sensor1 press to trigger playing a sound
// Illustrates the polling technique
import josx.platform.rcx.*;
class SoundSensor1 implements SensorConstants
{
public static void main (String[] args)
{
Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH,SENSOR_MODE_BOOL);
while (true)
{
if(Sensor.S1.readBooleanValue())
{
Sound.playTone(220,10);
try{Thread.sleep(30);}
catch(Exception e) {}
}
}
}
}
// SoundSensorListen1SepClass -- Uses Sensor1 press to trigger playing a sound, separate classes
// Illustrates the use of a SensorListener
import josx.platform.rcx.*;
class SoundSensorListen1SepClass implements SensorConstants
{
public static void main (String[] args)
{
Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH,SENSOR_MODE_BOOL);
Sensor.S1.addSensorListener (new PlayTone());
while (true);
}
}
//use in conjunction with SoundSensorListen1SepClass.java
import josx.platform.rcx.*;
class PlayTone implements SensorListener
{
public void stateChanged (Sensor src, int oldValue, int newValue)
{
Sound.playTone(220,10);
try{Thread.sleep(30);}
catch(Exception e) {}
}
}
// SoundSensorListen1 -- Uses Sensor1 press to trigger playing a sound
// Listener and main function both contained in one class
// Run button press/release to terminate
// Illustrates the use of a SensorListener
import josx.platform.rcx.*;
class SoundSensorListen1 implements SensorConstants, SensorListener
{
public static void main (String[] args)
{
Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH,SENSOR_MODE_BOOL);
Sensor.S1.addSensorListener (new SoundSensorListen1());
try {Button.RUN.waitForPressAndRelease();}
catch(Exception e) {}
}
public void stateChanged (Sensor src, int oldValue, int newValue)
{
Sound.playTone(220,10);
try{Thread.sleep(30);}
catch(Exception e) {}
}
}
// MotorSensorListen -- Robot goes forward until sensor 1 pressed
// Then backs away, turns and goes forward again
// Run button press/release to terminate
// Illustrates the use of a SensorListener & motor control
import josx.platform.rcx.*;
class MotorSensorListen implements SensorConstants, SensorListener
{
public static void main (String[] args)
{
Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH,SENSOR_MODE_BOOL);
Sensor.S1.addSensorListener (new MotorSensorListen());
Motor.A.forward();
Motor.C.forward();
//while(true);
try {Button.RUN.waitForPressAndRelease();}
catch(Exception e) {}
}
public void stateChanged (Sensor src, int oldValue, int newValue)
{
boolean pressed = src.readBooleanValue();
if (pressed)
{
Motor.A.backward();
Motor.C.backward();
try {Thread.sleep(500);}
catch(Exception e) {};
Motor.A.forward();
try {Thread.sleep(500);}
catch(Exception e) {};
Motor.C.forward();
try {Thread.sleep(500);}
catch(Exception e) {};
}
}
}
// THE FOLLOWING THREE APPS ILLUSTRATE THE USE OF RCX BEHAVIORS IN JAVA
// BehaviorTest.java -- creates class BumperCar
// Implements a behavior using the josx.robotics interfaces and classes
// Simple Behaviors: DriveForward and HitWall
// BumperClass defined here instantiates each behavior, puts them in an array
// and starts the Arbitrator of those two behaviors
// The behaviors are defined in the DriveForward and HitWall classes
import josx.robotics.*;
class BumperCar
{
public static void main(String [] args)
{
Behavior b1 = new DriveForward();
Behavior b2 = new HitWall();
// NOTE: low level behaviors should have lower index number
// in the array i.e. b2 is higher level than b1.
Behavior [] bArray = {b1, b2};
Arbitrator arby = new Arbitrator(bArray);
arby.start();
}
}
// DriveForward.java -- Defines the DriveForward behavior class
// Used with the BumperCar application
import josx.robotics.*;
import josx.platform.rcx.*;
public class DriveForward implements Behavior
{
public boolean takeControl()
{
return true;
}
public void suppress()
{
Motor.A.stop();
Motor.C.stop();
}
public void action()
{
Motor.A.forward();
Motor.C.forward();
}
}
// HitWall.java -- Defines the HitWall behavior class
// Used with the BumperCar application
import josx.robotics.*;
import josx.platform.rcx.*;
public class HitWall implements Behavior
{
public boolean takeControl()
{
return Sensor.S1.readBooleanValue();
}
public void suppress()
{
Motor.A.stop();
Motor.C.stop();
}
public void action()
{
// Back up:
Motor.A.backward();
Motor.C.backward();
try{Thread.sleep(1000);}catch(Exception e) {}
// Rotate by causing only one wheel to stop:
Motor.A.stop();
try{Thread.sleep(300);}catch(Exception e) {}
Motor.C.stop();
}
}
//THE FOLLOWING EXAMPLE ILLUSTRATES THE USE OF THE TimingNavigator CLASS
// Navigation -- go to a specified point (70,90) and return
import josx.platform.rcx.*;
import josx.robotics.*;
class goto7090
{
public static void main (String [] args)
{
Motor.A.setPower(4);
Motor.C.setPower(7);
TimingNavigator tn = new TimingNavigator(Motor.C, Motor.A, 4.2f, 3.6f);
tn.gotoPoint(70,90);
tn.gotoPoint(0,0);
}
}