HitWall.java
package Caprani;
import lejos.nxt.*;
import lejos.robotics.subsumption.*;
 
public class HitWall implements Behavior {
    private TouchSensor touch;
    private UltrasonicSensor sonar;
    private boolean suppressed = false;
 
    private static int distance = 255;
 
    public HitWall(SensorPort sonarPort, SensorPort touchPort) {
 
        SonicThread sensor = new SonicThread(sonarPort);
        sensor.setDaemon(true);
        sensor.start();
        touch = new TouchSensor(touchPort);
    }
 
    public boolean takeControl() {
        return touch.isPressed() || distance < 25;
    }
 
    public void suppress() {
        suppressed = true;
    }
 
    public void action() {
        suppressed = false;
 
        Motor.A.backward();
        Motor.C.backward();
        try {
            int i = 0;
            while (i < 1000 && !suppressed) {
                Thread.sleep(1);
                Thread.yield();
                i++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        Motor.C.rotate(-180, true);
//        Motor.C.rotate(-360, true);
 
        while(Motor.C.isMoving() && !suppressed) {
            Thread.yield();
        }
 
        Motor.A.stop();
        Motor.C.stop();
    }
 
    private class SonicThread extends Thread {
 
        public SonicThread(SensorPort port) {
 
            sonar = new UltrasonicSensor(port);
        }
 
        public void run() {
            while (true) {
                HitWall.distance = sonar.getDistance();
                LCD.drawString("distance: " + distance + "  ", 0, 4);
                Thread.yield();
            }
         }
     }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License