public FilteredColorPoller(SensorModes colorSensor, int ReadingsToMedian) { SampleProvider colorReading = colorSensor.getMode("ColorID"); // Stack a filter which takes average readings SampleProvider colorMedianSource = new MedianFilter(colorReading, ReadingsToMedian); // The final, filtered data from the us sensor is stored in colorFilteredSource this.colorFilteredSource = colorMedianSource; // initialize an array of floats for fetching samples this.colorData = new float[colorFilteredSource.sampleSize()]; }
public boolean pressed() { SampleProvider touchProvider = ((EV3TouchSensor) tsensor).getTouchMode(); tsample = new float[touchProvider.sampleSize()]; touchProvider.fetchSample(tsample, 0); if (tsample[0] > 0.5) { return true; } return false; }
public static void main(String[] args) throws Exception { DifferentialPilot pilot = new DifferentialPilot(56f, 126f, Motor.D, Motor.A); pilot.setTravelSpeed(150); Brick brick = BrickFinder.getDefault(); Port s1 = brick.getPort("S1"); // Port s4 = brick.getPort("S4"); // TextLCD lcd = brick.getTextLCD(); NXTLightSensor lysSensor = new NXTLightSensor(s1); // NXT LYS EV3ColorSensor fargeSensor = new EV3ColorSensor(s4); // EV3 LYS // COLOR----------------------------------------------------------------- SampleProvider fargeSample = fargeSensor.getColorIDMode(); float[] colorVerdi = new float[fargeSample.sampleSize()]; // ---------------------------------------------------------------------- // LYS------------------------------------------------------------------- SampleProvider lysSample = lysSensor; float[] lysVerdi = new float[lysSample.sampleSize()]; // ---------------------------------------------------------------------- Boolean move = true; // Sett til false hvis roboten ikke skal kjøre videre // System.out.println("start lokke"); while (!Button.ESCAPE.isDown()) { // Kjører så lenge vi ikke trykker på exit-knappen // Kjør framover hvis vi ikke allerede gjør det if (!pilot.isMoving() && move) { pilot.forward(); } lysSensor.fetchSample(lysVerdi, 0); // LYS float lys = lysVerdi[0]; fargeSample.fetchSample(colorVerdi, 0); // COLOR // float color = colorVerdi[0]; int color = fargeSensor.getColorID(); // Fungerer dette? lcd.clear(); lcd.drawString("NXT " + lys, 0, 0); lcd.drawString("EV3 " + color, 0, 1); if (color != Color.BLACK) { pilot.rotate(-10); // VENSTRE } else if (lys < 48) { // pilot.arcForward(-150); pilot.rotate(10); } // Elsen under trengs ikke, da forward kjøres på starten uansett else { // pilot.forward(); if (!pilot.isMoving()) { pilot.forward(); } } } // while System.out.println("AVSLUTTET"); } // main
public static void main(String[] args) { int buttonChoice; // setup the odometer and display Odometer odo = new Odometer(leftMotor, rightMotor, 30, true); final TextLCD t = LocalEV3.get().getTextLCD(); do { // clear the display t.clear(); // ask the user whether he wants to detect blocks or search blocks t.drawString("< Left |Right >", 0, 0); t.drawString(" | ", 0, 1); t.drawString("Detect|Search ", 0, 2); buttonChoice = Button.waitForAnyPress(); while (buttonChoice != Button.ID_LEFT && buttonChoice != Button.ID_RIGHT && buttonChoice != Button.ID_ESCAPE) { /* * These two if statements is to make the motor attached to the USsensor rotate * 90 degrees before the main methods are launched */ if (buttonChoice == Button.ID_UP) { Scan scan = new Scan(usMotor); scan.usMotorSpeed(50); scan.turnSensor(90); buttonChoice = Button.waitForAnyPress(); } if (buttonChoice == Button.ID_DOWN) { Scan scan = new Scan(usMotor); scan.usMotorSpeed(50); scan.turnSensor(-90); buttonChoice = Button.waitForAnyPress(); } } } while (buttonChoice != Button.ID_LEFT && buttonChoice != Button.ID_RIGHT && buttonChoice != Button.ID_ESCAPE); if (buttonChoice == Button.ID_ESCAPE) { System.exit(0); } SensorModes usSensor = new EV3UltrasonicSensor(usPort); SampleProvider usValue = usSensor.getMode("Distance"); // colorValue provides samples from this instance float[] usData = new float[usValue.sampleSize()]; // colorData is the buffer in which data are returned SensorModes colorSensor = new EV3ColorSensor(colorPort); SampleProvider colorValue = colorSensor.getMode("ColorID"); // colorValue provides samples from this instance float[] colorData = new float[colorValue.sampleSize()]; // colorData is the buffer in which data are returned // The following start the PartA of the Lab when the right button is pressed, afterwards press // escape to exit program while (buttonChoice != Button.ID_RIGHT && buttonChoice != Button.ID_ESCAPE) { if (buttonChoice == Button.ID_LEFT) { ObjectDetection od = new ObjectDetection(colorValue, colorData, usValue, usData); od.run(); LCD.drawString("< Left |Right >", 0, 0); LCD.drawString(" | ", 0, 1); LCD.drawString("Detect|Search ", 0, 2); } buttonChoice = Button.waitForAnyPress(); } if (buttonChoice == Button.ID_ESCAPE) { System.exit(0); } // If the left button is pressed, the robot will start partB of the lab which is localize, and // start scanning the field odo.start(); final USLocalizer usl = new USLocalizer( odo, usSensor, usData, USLocalizer.LocalizationType.FALLING_EDGE, leftMotor, rightMotor, WHEEL_RADIUS, TRACK); final Scan scan = new Scan(usValue, usData, colorValue, colorData, odo, leftMotor, rightMotor, usMotor); br = new BlockRecognition(odo, usSensor, usData, colorValue, colorData, rightMotor, leftMotor); new LCDInfo(odo, usSensor, usData, colorSensor, colorData); // begin the threads (we launch a thread to be able to exit it whenever we want using escape) (new Thread() { public void run() { br.start(); scan.start(); usl.doLocalization(); scan.startRun(); } }) .start(); while (Button.waitForAnyPress() != Button.ID_ESCAPE) ; System.exit(0); }