Fixed a bug in obstacle avoidance.

This commit is contained in:
Miguel Angel Astor Romero
2016-05-25 11:28:11 -04:30
parent c76636717a
commit 010b5ad544
4 changed files with 69 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package ve.ucv.ciens.cicore.icaro.ryabi; package ve.ucv.ciens.cicore.icaro.ryabi;
import lejos.nxt.Button; import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor; import lejos.nxt.LightSensor;
import lejos.nxt.Motor; import lejos.nxt.Motor;
import lejos.nxt.SensorPort; import lejos.nxt.SensorPort;
@@ -43,19 +44,22 @@ public class RyABI {
private static FeatureDetectorsHandler detectorHandler; private static FeatureDetectorsHandler detectorHandler;
public static void main(String[] args) { public static void main(String[] args) {
boolean invertLightDetector;
/* Create the sensors. */ /* Create the sensors. */
sonar = new UltrasonicSensor(SensorPort.S1); sonar = new UltrasonicSensor(SensorPort.S1);
touch = new TouchSensor(SensorPort.S2); touch = new TouchSensor(SensorPort.S2);
compass = new CompassHTSensor(SensorPort.S3); compass = new CompassHTSensor(SensorPort.S3);
light = new LightSensor(SensorPort.S4); light = new LightSensor(SensorPort.S4);
invertLightDetector = invertLightDetector();
/* Create the pilot. */ /* Create the pilot. */
pilot = new CompassPilot(compass, WHEEL_DIAMETER, TRACK_WIDTH, Motor.A, Motor.C); pilot = new CompassPilot(compass, WHEEL_DIAMETER, TRACK_WIDTH, Motor.A, Motor.C);
/* Create the feature detectors. */ /* Create the feature detectors. */
rangeDetector = new RangeFeatureDetector(sonar, 20, 200); rangeDetector = new RangeFeatureDetector(sonar, 20, 200);
touchDetector = new TouchFeatureDetector(touch); touchDetector = new TouchFeatureDetector(touch);
lightDetector = new LightFeatureDetector(light); lightDetector = new LightFeatureDetector(light, invertLightDetector);
detectorHandler = FeatureDetectorsHandler.getInstance(); detectorHandler = FeatureDetectorsHandler.getInstance();
detectorHandler.setRangeDetector(rangeDetector); detectorHandler.setRangeDetector(rangeDetector);
@@ -71,17 +75,60 @@ public class RyABI {
/* Register escape button for forced exit. */ /* Register escape button for forced exit. */
Button.ESCAPE.addButtonListener(new QuitButtonListener()); Button.ESCAPE.addButtonListener(new QuitButtonListener());
/* Start the sensor calibration. */
behaviors = new Behavior[1];
behaviors[0] = new SensorCalibrationBehavior(sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH);
arbitrator = new Arbitrator(behaviors, true);
arbitrator.start();
/* Wait for the user to press the center button. */
System.out.println("Sensors ready.");
System.out.println("Press ENTER");
System.out.println("to start.");
Button.ENTER.waitForPress();
/* Create the behaviors. */ /* Create the behaviors. */
behaviors = new Behavior[6]; behaviors = new Behavior[5];
behaviors[0] = new WanderBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH); behaviors[0] = new WanderBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH);
behaviors[1] = new VictoryBehavior(); behaviors[1] = new VictoryBehavior();
behaviors[2] = new SearchBoxBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH); behaviors[2] = new SearchBoxBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH);
behaviors[3] = new CatchBallBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH); behaviors[3] = new CatchBallBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH);
behaviors[4] = new AvoidObstaclesBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH); behaviors[4] = new AvoidObstaclesBehavior(pilot, sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH);
behaviors[5] = new SensorCalibrationBehavior(sonar, touch, light, compass, WHEEL_DIAMETER, TRACK_WIDTH);
/* Start the program. */ /* Start the program. */
arbitrator = new Arbitrator(behaviors, true); arbitrator = new Arbitrator(behaviors, true);
arbitrator.start(); arbitrator.start();
} }
private static boolean invertLightDetector() {
int btnID;
boolean invertSensor = false, done = false;
while(!done) {
LCD.clear();
System.out.println("Invert l. sens:");
if(invertSensor)
System.out.println("<< YES >>");
else
System.out.println("<< NO >>");
System.out.println("Press ENTER");
System.out.println("to set.");
btnID = Button.waitForAnyPress();
switch(btnID) {
case Button.ID_LEFT:
case Button.ID_RIGHT:
invertSensor = !invertSensor;
break;
case Button.ID_ENTER:
done = true;
break;
default:
System.exit(1);
}
}
return invertSensor;
}
} }

View File

@@ -23,13 +23,14 @@ public class AvoidObstaclesBehavior extends BaseBehavior {
@Override @Override
public boolean takeControl() { public boolean takeControl() {
return state.getState() == States.WANDER && queue.hasNextTouchSensorEvent(); return queue.hasNextTouchSensorEvent() && state.getState() == States.WANDER;
} }
@Override @Override
public void action() { public void action() {
SensorEvent event = queue.getNextTouchSensorEvent(); SensorEvent event = queue.getNextTouchSensorEvent();
event.detector.enableDetection(false);
pilot.stop(); pilot.stop();
pilot.travel(-100); pilot.travel(-100);
Rotations.rotate90(compass, pilot); Rotations.rotate90(compass, pilot);
@@ -37,8 +38,10 @@ public class AvoidObstaclesBehavior extends BaseBehavior {
Rotations.rotateM90(compass, pilot); Rotations.rotateM90(compass, pilot);
pilot.stop(); pilot.stop();
if(!queue.hasNextTouchSensorEvent()) while(queue.hasNextTouchSensorEvent())
event.detector.enableDetection(true); event = queue.getNextTouchSensorEvent();
event.detector.enableDetection(true);
} }
@Override @Override

View File

@@ -54,6 +54,7 @@ public class SearchBoxBehavior extends BaseBehavior {
@Override @Override
public void action() { public void action() {
if(queue.hasNextRangeSensorEvent()) { if(queue.hasNextRangeSensorEvent()) {
if(pilot.isMoving()) if(pilot.isMoving())
pilot.stop(); pilot.stop();
ballFound = true; ballFound = true;

View File

@@ -46,7 +46,7 @@ public class LightFeatureDetector extends FeatureDetectorAdapter {
this.threshold = threshold; this.threshold = threshold;
this.invert = invert; this.invert = invert;
// Calculate angle a distance of bumper from center: // Calculate angle and distance of light sensor from center:
Point robot_center = new Point(0, 0); Point robot_center = new Point(0, 0);
Point bumper_p = new Point((float)xOffset, (float)yOffset); Point bumper_p = new Point((float)xOffset, (float)yOffset);
range = (float)robot_center.distance(xOffset, yOffset); range = (float)robot_center.distance(xOffset, yOffset);
@@ -56,9 +56,16 @@ public class LightFeatureDetector extends FeatureDetectorAdapter {
@Override @Override
public Feature scan() { public Feature scan() {
RangeFeature rf = null; RangeFeature rf = null;
if(lightSensor.getLightValue() <= threshold) { if(invert) {
RangeReading rr = new RangeReading(angle, range); if(lightSensor.getLightValue() >= threshold) {
rf = new RangeFeature(rr); RangeReading rr = new RangeReading(angle, range);
rf = new RangeFeature(rr);
}
} else {
if(lightSensor.getLightValue() < threshold) {
RangeReading rr = new RangeReading(angle, range);
rf = new RangeFeature(rr);
}
} }
return rf; return rf;
} }
@@ -70,7 +77,7 @@ public class LightFeatureDetector extends FeatureDetectorAdapter {
if(invert) { if(invert) {
while(lightSensor.getLightValue() >= threshold); while(lightSensor.getLightValue() >= threshold);
} else { } else {
while(lightSensor.getLightValue() <= threshold); while(lightSensor.getLightValue() < threshold);
} }
} }
} }