Week 8 Lesson 7 - JakJak7/Lego2014 GitHub Wiki

Date: 3/4 2014
Duration: 10-15
Group members: Jakob, Jesper, Henrik

LEGO vehicle that exhibits a single behavior

Goals:

  • Observe how the robot behaves when running AvoidFigure9_3.java.
  • Get the robot to drive backwards and spin around when all sensors are below threshold.

Plan:

  • Download, install and run the supplied program.
  • ???
  • Profit

Results:
We observed that the robot, upon seeing an obstacle, stops and looks left and right, and continues in whichever direction was free. It does have some trouble when driving into a wall, dead end, etc, as the robot can't just turn around.
Video of robot exhibiting a single behavior
To get the robot to turn around in this situation, we added the following code:

		if (leftDistance < stopThreshold && rightDistance < stopThreshold && frontDistance < stopThreshold){
			car.backward(power,0);
			Delay.msDelay(ms);
			car.stop();
			car.backward(power,power);
			Delay.msDelay(1000);
			car.forward(power,-power);
			Delay.msDelay(2100);
		}

First the robot corrects its orientation after looking to the right, then it drives backwards for a fixed amount of time, and finally it rotates for a fixed amount of time. We found that 2100 ms worked fine, although this is relative to current battery power etc. A better solution would be to use the tacho counter.
Video of robot turning 180 degrees
Just running the robot into a wall didn't work, as it'd turn either left or right, so we had to set up a dead end to show off the new behavior.

Behaviors as concurrent threads

Goals:

  • Download the program and describe the behavior.

Plan:

  • Run the program first with avoid and follow disabled, then twice more with avoid and follow disabled respectively.

Results:
Running with only cruise just makes the robot drive forward.
Enabling follow makes the robot check for light left and right once light above the threshold has been seen, and then follow the light.
Enabling avoid makes the robot avoid obstacles just like in the previous exercise.
The way the triggers are implemented is by having a noCommand loop that is broken out of once the trigger condition is met. The way the robot follows light and avoids obstacles is just like the way it avoided obstacles in the previous exercise.

Add an Escape behavior

Goal:
Get the robot to react when touch sensors are pressed.
Plan:

  • Add touch sensors to robot.
  • Implement escape behavior as described in [1].

Results:
The book says that if either the left or the right sensor is pressed, the robot should turn the opposite direction. With our robot, we had to get it to first back off before turning, or it'll just keep running into the obstacle.
In addition, we very rarely got the system to register both sensors being pressed, as it'd most often register one of them and start turning immediately. To trigger the behavior when both sensors are pressed, we simply got it to behave as such whenever the left sensor is pressed.

    public void run() {		       
        while (true) {
			boolean left = touchLeft.isPressed();
			boolean right = touchRight.isPressed();
			if (left || right) {
			car.backward(power,power);
			Delay.msDelay(200);
			if(!left)
				car.forward(power,-power);
			else
				car.forward(-power,power);
			Delay.msDelay(400);
			}
			else
			car.noCommand();
		}
    }

With this approach, we added the escape behavior to the other behaviors, and got it to overrule them.
Video of robot turning away when bumping into obstacles

Add a third motor to turn the light sensor

Goal:
Replace the lightsensor with one that has a motor rotate it, so the robot itself doesn't have to turn in order to follow light.
Plan:

  • Add a third horizontal motor to the robot.
  • Move the light sensor on top of the motor.
  • Modify the Follow class to use this motor rather than moving the whole robot.

Results:
Video of robot turning away when bumping into obstacles
We used the tacho counter to rotate the light sensor.

	    // Follow light as long as the light level is above the threshold
	    while (frontLight > lightThreshold) {

		// Get the light to the left
		Motor.A.rotateTo(-45);
		leftLight = light.getLightValue();
		// Get the light to the right
		Motor.A.rotateTo(45);
		rightLight = light.getLightValue();
		Motor.A.rotateTo(0);

		// Follow light for a while
		delta = leftLight-rightLight;
		
		car.forward(power-delta, power+delta);
		Delay.msDelay(ms);
    		
		frontLight = light.getLightValue();
	    }

When running the robot for longer periods, the light sensor starts getting slightly rotated from its starting position, due to inaccuracies of the motor.

###The classes SharedCar and Arbiter The implementation in the book is described by the behaviors having a flag describing how the vehicle should behave. The vehicle is told how to behave using these. There's no way to define motor power etc.
In our implementation it's possible to define power for each motor.

Conclusion

We managed to get the system to react to different environmental changes, and we got it to check for light without rotating the whole robot. This is desirable in more enclosed areas.

References