14) Implementing the prey robot

4) Implementing the Prey Robot

Date: 24/05 2012
Group members participating: Falk, Jeppe and Jakob
Duration of activity: 6 hours

4.1) Goal

Implement the prey robot and test it on a simple track.

4.2) Plan

  • Use exceptions to interrupt the currently running behavior at any time.
  • Test the color sensor.
  • Run the robot on a simple test track.

4.3) Results

4.3.1) Prey Behavior Architecture

Exceptions are now used to interrupt behaviors. This allows to stop running behaviors at any time without waiting for running methods of the behavior to be finished. For example the RunAway or AvoidObstacle behavior should be able to interrupt other behaviors or even itself if the predator appears or the robot runs into something. By throwing a InterruptBehaviorException it is now possible to instantly interrupt a behavior.

The run method of the Behavior class.

public void run() {
    while (true) {
        if (isRunning()) {
            try {
            } catch (InterruptBehaviorException e) {}

Additionally a behavior can now dynamically change if it is self-interruptible or not while it is performing the action. If the robot drives to the border of the track and the AvoidBorder is self-interruptible the robot appeared to be standing still because it was constantly interrupting itself. When the robot had left the border and was still performing the action it should be self-interruptible in case it came to the border again (e.g. if it is in a corner). Therefore the AvoidBorder is only self-interruptible once it is off the border.

4.3.2) Test on a Track

The robot was tested on a simple track of light tape on a dark ground. Using the light sensor the robot is able to "see" the border and avoid leaving the track while wandering around. A problem is that the robot can get over the border while it is backing off trying to avoid something. So the robot has to "know" if it is approaching the border forwards or backwards.

ColorID Color
0 red
1 green
2 blue
3 yellow
4 magenta
5 orange
6 white
7 black
8 pink
9 gray
10 light gray
11 dark gray
12 cyan

4.3.3) Color Sensor

The color sensor can be accessed using the ColorSensor class [Bibliography item colorsensor not found.]. The method getColorID gives the ID of of the measured color (see table to the right).

Using the color sensor a behavior EatFood is implemented. There are colored spots on the track represening food. While the robot is wandering around, its hunger increases and when it happens to be on food, the hunger defines the motivation to stop and eat. While it is eating, the hunger decreases, so at some point, the robot will move away from the food again.

4.4) Conclusion

After this session all the behaviors of the prey worked. There need to be some fine tuning on the "real" track, but right now the robot is able to stay on the track, wander around to eat food and avoids obstacles and if it sees the predator it is scared as hell and runs for its life.

We still need to build the other three preys as well as the predator.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License