Sensors.java
package prey;
 
import lejos.nxt.ColorSensor;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
import lejos.nxt.UltrasonicSensor;
import lejos.nxt.addon.IRSeekerV2;
import lejos.nxt.addon.IRSeekerV2.Mode;
import lejos.robotics.Color;
 
/**
 * @author HS
 *
 */
public class Sensors extends Thread {
    public static final int BORDERCOLOR = 1, EDGECOLOR = 2, FOODCOLOR = 3;
 
    private IRSeekerV2 irSeeker = new IRSeekerV2(SensorPort.S4, Mode.AC);
    private ColorSensor cSensor = new ColorSensor(SensorPort.S2);
    private UltrasonicSensor uSsensor = new UltrasonicSensor(SensorPort.S1);
    private TouchSensor tSensor = new TouchSensor(SensorPort.S3);
 
    private int lastColor = -1;
    private int numberOfSameColor = 0;
 
    private int currentColor = -1;
 
    private static final int edgeColorID = 0;
    private static final int borderColorID = 3;
    private static final int foodColorID = 2;
 
    private static int testColorID = -1;
 
    private static int CSValue = -1;
    private static int USDistance = 255;
    private static int IRDirection = 0;
    private static int[] IRValues = null;
    private static int IRAvr = 0;
    private static boolean TSValue = false;
    private static final int IRMeasureMargin = 10;
 
    /**
     * @throws InterruptedException 
     * 
     */
    public Sensors() throws InterruptedException {
        this.setDaemon(true);
        this.start();
    }
 
    /**
     * @return
     */
    public static int getUltraSonicDistance() {
        return USDistance;
    }
 
    /**
     * @return
     */
    public static int getColourSensorValue() {
        return CSValue;
    }
 
    /**
     * @return
     */
    public static int getInfraredDirection() {
        return IRDirection;
    }
 
    /**
     * @return
     */
    public static int[] getInfraredValues() {
        return IRValues;
    }
 
    /**
     * @return
     */
    public static int getAverageInfraredValues() {
        return IRAvr;
    }
 
    public static boolean getTouchSensorValue() {
        return TSValue;
    }
 
    private int getColorID(int colorID) {        
        testColorID = colorID;
        switch (colorID) {
        case edgeColorID: 
            return EDGECOLOR;
        case borderColorID: 
            return BORDERCOLOR;
        case foodColorID: 
            return FOODCOLOR;
        }
        return 0;
    }
 
    /* (non-Javadoc)
     * @see java.lang.Thread#run()
     */
    public void run() {
 
        while (true) {
 
            CSValue = getColorID(cSensor.getColorID());
            USDistance = uSsensor.getDistance();
            IRDirection = irSeeker.getDirection();
            IRValues = irSeeker.getSensorValues();
            TSValue = tSensor.isPressed();
 
            int[] sensorValues = IRValues;
            int sum = 0;
            int numOfActiveSensors = 0;
            for (int i=0; i < sensorValues.length; i++) {
                if (sensorValues[i] > IRMeasureMargin) {
                    sum += sensorValues[i];
                    numOfActiveSensors++;
                }
            }
            if (numOfActiveSensors > 0) {
                IRAvr = sum/numOfActiveSensors;
            } else {
                IRAvr = 0;
            }
        }
    }
 
    private int sameColorCounter(int colorID) {
        final int numberOfMatches = 3;
 
        if (colorID == lastColor) {
            numberOfSameColor++;
        } else {
            numberOfSameColor = 1;
        }
        if (numberOfSameColor >= numberOfMatches) {
            currentColor = colorID;
        }
        lastColor = colorID;
        return currentColor;
    }
 
    public static int getCSID() {
        return testColorID;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License