Table of Contents
<texit info> author=Johnny Rieper, Bent Bisballe Nyeng and Kasper Sohn title=Marvin - The Balancing Robot. </texit>
Lab report 4 - Behaviour Control
Date: January 16nd 2009
Duration of activity: 8-16
Participants: Kasper, Bent and Johnny
Project Goal
Make the robot drive autonomously, avoiding obstacles by means of a behaviour model.
Plan
- Move balance related code to a thread.
- Make control parameters writeable from outside the motor thread to make it possible to drive around.
- Copy code from previous Behaviour project 1).
- Modify the code to make it fit Marvin.
Theory
The Ultrasonic Sensor
Ultrasonic sensors work on a principle similar to radar or sonar by emitting an impulse and interpreting the echoes from radio or sound waves respectively. Ultrasonic sensors generate high frequency sound waves and evaluate the echo which is received back by the sensor. The time interval between the emitted and received signal is then calculated in order to determine a distance to a given object. The ultrasonic sensors are also known as transducers when they both send and receive signals. The Ultrasonic Sensor2) measures distance in centimetres and in inches. It is able to measure distances from 0 to 255 centimetres with a precision of +/- 3 cm. Large sized objects with hard surfaces return the best readings. Objects made of soft fabric or that are curved (like a ball) or are very thin or small can be difficult for the sensor to detect. Note that two or more Ultrasonic Sensors operating in the same room may interrupt each other’s readings.
Knowledge Learned from Previous Lessons
Prior to using a sensor for a specific purpose it is important to investigate the main use for the sensor. For example in lab session 4 3) we used the light-sensor as an input for measuring tilt of the robot. Or in other words, it was not the primary use for the sensor we used it for. Another example is the ultrasonic sensor 4) which operates fairly precise when operated at an angle of 90 degrees to the wall, because is uses echo for measurement. When the angle was shifted to one side the sensor became increasingly inaccurate. Again you have to closely investigate the capabilities and limitations of the sensor one plans to use. As the ultrasonic sensor depends on sound, the primary limitations are echo delays and angle shifts. Therefore the best readings will be available when the sensor is positioned directly in in front of a wall. And the best readings will also be available when to sensor is close to the wall due to the fact that echoes will travel in different directions. These limitations will not be a problem in our project due to the fact that we only use the ultrasonic sensor to avoid that Marvin will hit any walls.
Implementation
The Behavior Class
A platform for a behaviour based system with a simple suppression mechanism. An actual behaviour is defined by extending the Behavior
class. Each behaviour is a single thread of control.
It has access to control the Marvin by means of the control methods (forward, backward, left and right).
However, access can be suppressed so that the motor commands will not be performed while the behaviour is suppressed.
Variable | Value |
---|---|
turnPower | 200 |
tiltPower | 0.06 |
public class Behavior extends Thread { private ArrayList behaviors;; private boolean suppressed; private Ctrl ctrl; public Behavior(ArrayList behaviors, Ctrl ctrl) { this.ctrl = ctrl; suppressed = false; this.setDaemon(true); // Clone the Behavior list. this.behaviors = new ArrayList(); for(int i = 0; i < behaviors.size(); i++) { Behavior b = (Behavior)behaviors.get(i); this.behaviors.add(b); } } public void suppressLower() { for(int i = 0; i < behaviors.size(); i++) { Behavior b = (Behavior)behaviors.get(i); b.setSuppress(true); } } . . . public synchronized boolean isSuppressed() { return suppressed; } public synchronized void setSuppress(boolean suppress) { suppressed = suppress; } public void forward(int period) { if(!isSuppressed()) { ctrl.setLeftMotorOffset(0); ctrl.setRightMotorOffset(0); for(int time = 0; time < period; time += 10) { try { Thread.sleep(10); } catch(InterruptedException e) { } ctrl.setTiltAngle(-tiltPower); } } } public void right(int period) { if(!isSuppressed()) { ctrl.setLeftMotorOffset(turnPower); ctrl.setRightMotorOffset(-turnPower); delay(period); } } . . . }
The Behavior
class has the following 3 subclasses:
The RandomDrive Class
This behaviour drives in one of four random directions for a random number of milliseconds. All of these numbers are computed using a random function from Java Math library.
The AvoidFront Class
Implemented using a ultrasonic sensor. The behaviour activates a backward motion and do this for a number of milliseconds to back away from the obstacle. Afterwards Marvin stops and turns right corresponding to an approximately 90 degree turn. The code for the essential parts of the AvoidFront
class can be seen below as an illustrative example of how behaviours are implemented in general.
public class AvoidFront extends Behavior { . . . public void run() { UltrasonicSensor us = new UltrasonicSensor(SensorPort.S2); int tooCloseThreshold = 20; // cm while (true) { int distance = us.getDistance(); while(distance > tooCloseThreshold) { distance = us.getDistance(); delay(100); } suppressLower(); backward(5000); right(1200); stop(2000); unsuppressLower(); } } }
The BTController Class
The thread handles bluetooth communication in order to control Marvin with a remote control. The arrow keys on the computer is used instead of a remote. This class will be covered in detail in the next lab report.
The full source code for these classes can be found in the files Behavior.java
, RandomDrive.java
, BTController.java
and AvoidWall.java
in the Marvin code tarball 5)