ev3dev-lang-java / ev3dev-lang-java

A project to learn Java and create software for Mindstorms Robots using hardware supported by EV3Dev & the LeJOS way.
http://ev3dev-lang-java.github.io/
MIT License
96 stars 42 forks source link

Review IR Sensor to detect a Beacon #168

Closed jabrena closed 7 years ago

jabrena commented 7 years ago

http://www.lejos.org/ev3/docs/lejos/hardware/sensor/EV3IRSensor.html http://www.ev3dev.org/docs/sensors/lego-ev3-infrared-sensor/

http://thetechnicgear.com/2014/03/beacon-tracking-lego-mindstorms-ev3-ir-sensor/ https://sourceforge.net/p/lejos/wiki/Sample%20Programs/ https://github.com/TarkanAl-Kazily/EV3-Java/blob/master/src/org/lejos/ev3/sample/followbeacon/Follow.java http://www.rapidpm.org/2014/01/27/lego-mindstorms-ev3-components--infrar.html

jabrena commented 7 years ago

Example:

package examples.sensors.ev3;

import ev3dev.sensors.ev3.EV3IRSensor;
import lejos.hardware.port.SensorPort;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;
import lombok.extern.slf4j.Slf4j;

public @Slf4j 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) {

        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 = 100;
        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];

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

            Delay.msDelay(HALF_SECOND);
        }

    }

}