1. Introduction

The digital EV3 Infrared Seeking Sensor detects proximity to the robot and reads signals emitted by the EV3 Infrared Beacon. Some features about this sensor:

  • Proximity measurement of approximately 50-70 cm

  • Working distance from the beacon of up to two meters

  • Supports four signal channels

  • Receives IR remote commands

image

2. How to use the sensor

2.1. Measure distances

package ev3dev.sensors.ev3;

import ev3dev.sensors.Battery;
import lejos.hardware.port.SensorPort;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class IRSensorDemo {

	private static EV3IRSensor ir1 = new EV3IRSensor(SensorPort.S1);

	private static int HALF_SECOND = 500;

	public static void main(String[] args) {

		System.out.println(Battery.getInstance().getVoltage());

		final SampleProvider sp = ir1.getDistanceMode();
		int distanceValue = 0;

		//Control loop
		final int iteration_threshold = 50;
		for(int i = 0; i <= iteration_threshold; i++) {

			float [] sample = new float[sp.sampleSize()];
		    sp.fetchSample(sample, 0);
		    distanceValue = (int)sample[0];

			System.out.println("Iteration: {}" + i);
			System.out.println("Distance: {}" + distanceValue);

		    Delay.msDelay(HALF_SECOND);
		}

		System.out.println(Battery.getInstance().getVoltage());


	}

}

2.2. Detect a beacon

package ev3dev.sensors.ev3;

import ev3dev.sensors.Battery;
import lejos.hardware.port.SensorPort;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class IRSensorDemo2 {

	//Robot Configuration
	private static EV3IRSensor ir1 = new EV3IRSensor(SensorPort.S1);

	//Configuration
	private static int HALF_SECOND = 500;

	public static void main(String[] args) {

		System.out.println(Battery.getInstance().getVoltage());

		final SampleProvider sp = ir1.getSeekMode();

		int beaconInfo1H = 0;
		int beaconInfo2H = 0;
		int beaconInfo3H = 0;
		int beaconInfo4H = 0;
		int beaconInfo1D = 0;
		int beaconInfo2D = 0;
		int beaconInfo3D = 0;
		int beaconInfo4D = 0;

		//Control loop
		final int iteration_threshold = 50;
		for(int i = 0; i <= iteration_threshold; i++) {

			float [] sample = new float[sp.sampleSize()];
		    sp.fetchSample(sample, 0);

		    beaconInfo1H = (int)sample[0];
			beaconInfo1D = (int)sample[1];

			beaconInfo2H = (int)sample[2];
			beaconInfo2D = (int)sample[3];

			beaconInfo3H = (int)sample[4];
			beaconInfo3D = (int)sample[5];

			beaconInfo4H = (int)sample[6];
			beaconInfo4D = (int)sample[7];

			System.out.println("Iteration: {}" + i);
			System.out.println("Beacon Channel 1: Heading: {}, Distance: {}" + beaconInfo1H + beaconInfo1D);
			System.out.println("Beacon Channel 2: Heading: {}, Distance: {}" + beaconInfo2H + beaconInfo2D);
			System.out.println("Beacon Channel 3: Heading: {}, Distance: {}" + beaconInfo3H + beaconInfo3D);
			System.out.println("Beacon Channel 4: Heading: {}, Distance: {}" + beaconInfo4H + beaconInfo4D);

		    Delay.msDelay(HALF_SECOND);
		}

		System.out.println(Battery.getInstance().getVoltage());

	}

}

2.3. Detect commands from a remote beacon

package ev3dev.sensors.ev3;

import lejos.hardware.port.SensorPort;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class IRSensorDemo3 {

	//Robot Configuration
	private static EV3IRSensor ir1 = new EV3IRSensor(SensorPort.S1);

	//Configuration
	private static int HALF_SECOND = 500;

	public static void main(String[] args) {

		final SampleProvider sp = ir1.getRemoteMode();

		int beaconInfo1 = 0;
		int beaconInfo2 = 0;
		int beaconInfo3 = 0;
		int beaconInfo4 = 0;

		//Control loop
		final int iteration_threshold = 50;
		for(int i = 0; i <= iteration_threshold; i++) {

			float [] sample = new float[sp.sampleSize()];
		    sp.fetchSample(sample, 0);

			beaconInfo1 = (int)sample[0];
			beaconInfo2 = (int)sample[1];
			beaconInfo3 = (int)sample[2];
			beaconInfo4 = (int)sample[3];

			System.out.println("Iteration: {}" + i);
			System.out.println("Beacon Channel 1: Remote: {}" + beaconInfo1);
			System.out.println("Beacon Channel 2: Remote: {}" + beaconInfo2);
			System.out.println("Beacon Channel 3: Remote: {}" + beaconInfo3);
			System.out.println("Beacon Channel 4: Remote: {}" + beaconInfo4);

		    Delay.msDelay(HALF_SECOND);
		}

	}

}