entry

    project

    leJOS

    download

    installation

    - models -

    links

entry

Models: turtle

<<  back
 

Turtle.java

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());
    }
}


 WritingTurtle.java

import josx.platform.rcx.*;
import java.lang.String;


public class WritingTurtle extends Turtle
{

    WritingTurtle(double turnf, double drivef)
    {
        this.lMotor       = Motor.C;
        this.rMotor       = Motor.A;
        this.pencilMotor  = Motor.B;
        this.pencilSensor = Sensor.S2;
        this.turnfactor   = turnf;
        this.drivefactor  = drivef;
    }

    public void toNextChar() {
        TextLCD.print("next");
        if (isPencilDown()) pUp();
        if (cDir == NORTH) { turn(90); cDir = EAST; }
        drive(0.5); turn(-90);
        LCD.clear();
    }

    public void write(String string) {
        
        LCD.clear();
        char[] characters = string.toCharArray();
        
        for (int i=0; i<characters.length; i++) {
            writeChar(characters[i]);
            toNextChar();
        }
    }

    public void writeChar(char character) {
        
        TextLCD.printChar(character,4);
        LCD.refresh();
        Sound.beep();
        if (character == 'A') {
            turn(30); pDown(); drive(2.309); turn(-60); drive(-2.309);
            pUp(); drive(1.155); turn(-60); pDown(); drive(1.154);
            pUp(); drive(-1.154); turn(60); drive(-1.155); turn(120);
            cDir = EAST;
        } else
        if (character == 'B') {
            pDown(); drive(2); 
            turn(120); drive(1.155); turn(-60); drive(-1.155); 
            turn(60);  drive(1.155); turn(-60); drive(-1.155); pUp();
            turn(30);  drive(1); cDir = EAST;
        } else
        if (character == 'C') {
            pUp(); drive(2); turn(-90); drive(-1); pDown();
            drive(1); turn(-90); drive(2); turn(-90); drive(1); pUp();
            cDir = EAST;
        } else
        if (character == 'D') {
            pDown(); drive(2); turn(135); drive(1.414); turn(90);
            drive(1.414); pUp(); turn(45); drive(-1); turn(90);
            cDir = NORTH;
        } else
        if (character == 'E') {
            pDown(); drive(2); turn(90); drive(1); pUp(); turn(-90);
            drive(-1); turn(-90); pDown(); drive(1); pUp();
            turn(90); drive(-1); turn(90); pDown(); drive(1); pUp();
            cDir = EAST;
        } else
        if (character == 'I') {
            pDown(); drive(2); pUp(); drive(-2); cDir = NORTH;
        } else
        if (character == 'H') {
            pDown(); drive(2); pUp(); drive(-1); turn(90); pDown(); drive(1);
            pUp(); turn(-90); drive(1); pDown(); drive(-2); pUp(); cDir = NORTH;
        } else
        if (character == 'K') {
            pDown(); drive(2); pUp(); turn(90); drive(1); turn(135); pDown();
            drive(1.414); turn(-90); drive(1.414); pUp(); turn(-45);
            cDir = EAST;
        } else
        if (character == 'M') {
            pDown(); drive(2); turn(-45); drive(-1.414); turn(90); drive(1.414);
            turn(-45); drive(-2); pUp(); cDir = NORTH;
        } else
        if (character == 'P') {
            pDown(); 
            drive(2); turn(-60); drive(-1.155); turn(120); drive(-1.155); pUp();
            turn(-60); drive(-1); turn(90); drive(1); cDir = EAST;
        } else
        if (character == 'R') {
            pDown(); drive(2); turn(-60); drive(-1.155); turn(120); 
            drive(-1.155); turn(-105); drive(-1.414); pUp(); turn(45); 
            cDir = NORTH;
        } else
        if (character == 'T') {
            turn(90); drive(1); turn(-90); pDown(); drive(2); pUp(); 
            turn(-90); drive(1); pDown(); 
            drive(-2); pUp(); turn(90); drive(-2); cDir = NORTH;
        } else
        if (character == '+') {
            drive(0.5); pDown(); drive(1); pUp(); drive(-0.5); turn(-90); 
            drive(0.5); pDown(); drive(-1); pUp(); turn(90); 
            drive(-1); cDir = NORTH;
        }
    }
}
// end of program.


 WritingTest.java

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

    }
}