import josx.platform.rcx.*;
import java.lang.Math;
import java.lang.Thread;
/**
* A Turtle you can need like a LOGO-Turtle.
*
* Designer: Sven Windhorst, Tim Rinkens
* Author: Tim Rinkens
*/
public class Turtle
{
public static final int NORTH = 0;
public static final int EAST = 1;
public static final int SOUTH = 2;
public static final int WEST = 3;
public static int cDir;
public static Motor lMotor;
public static Motor rMotor;
public static Motor pencilMotor;
public static Sensor pencilSensor;
public static double turnfactor;
public static double drivefactor;
/**
*
* Robot with two independent drive wheels and one idler wheel at the end.
* The pencil is mounted between the two wheels (zero turning radius).
* The touchsensor is activated when the pencil is down (writing).
* The turnfactor represents the time interval needed for turning 1 degree.
*
*
* @param lMotor left Motor
* @param rMotor right Motor
* @param pencilSensor Touchsensor for pencil down
* @param turnfactor determines the time needed to turn for one degree.
* @param drivefactor determines the size of the figures.
*/
Turtle() { }
/**
* Causes the turtle to move straight for distance units.
* @param distance positive or negative value for moving
* forward or backward.
*/
public void drive(double distance)
{
/* go ahead */
if (distance >= 0.0) {
rMotor.backward();
lMotor.backward();
}
else {
rMotor.forward();
lMotor.forward();
}
wait((int)(Math.abs(drivefactor * distance)));
rMotor.stop();
lMotor.stop();
}
/**
* Causes the turtle to turn around in one place, for angle degrees.
* @param angle positive or negative value for turning clockwise or
* anti-clockwise.
*/
public void turn(double angle)
{
turnDir(angle);
wait((int)(Math.abs(angle) * turnfactor));
rMotor.stop();
lMotor.stop();
}
public void turnDir(double angle) {
// Check if angle is positive or negative for left (-) or right turn (+)
if(angle < 0.0)
{ // turn left (left motor forward, right motor backward)
rMotor.backward();
lMotor.forward();
} else
{ // turn right (left motor backward, right motor forward)
rMotor.forward();
lMotor.backward();
}
}
public void wait(int delay) {
try { Thread.sleep(delay); }
catch (InterruptedException ie) { }
}
/**
* Causes the pencil to move upward
*/
public void pUp() {
pencilMotor.setPower(4);
pencilMotor.forward();
while (isPencilDown()) {
}
try { Thread.sleep(1600); }
catch (InterruptedException ie) { }
pencilMotor.stop();
}
/**
* Causes the pencil to touch down
*/
public void pDown() {
pencilMotor.setPower(4);
pencilMotor.backward();
while (!isPencilDown()) {
}
pencilMotor.stop();
}
/**
* @return true if the pencil is currently down.
*/
public boolean isPencilDown() {
return (pencilSensor.readBooleanValue());
}
}
import josx.platform.rcx.*;
public class WritingTest
{
static WritingTurtle myTurtle;
public static void main (String[] aArg)
{
myTurtle = new WritingTurtle(32, 700.0);
myTurtle.write("MATH");
//myTurtle.turn(180.0);
}
}