Robotics - Lecture 5
Robotics - Lecture 5
1
Behaviours, and Project Extras
First - more about good programming!
1
Feedback
2
Feedback
3
It’s Final
Naming Numbers
• Its nice to use words instead of having to type numbers.
• We should use Math.PI instead of remembering 3.141592654...
• Its better to use a name that you define once (like WHEEL_SIZE).
• When you change the wheels, you only have to change one line of code.
• Define constants just like other fields, but in UPPER CASE.
• Use Comments to help the reader (you in a week).
• Do not use static (global) objects!
Create them in a short main
Pass objects to the constructor of other classes.
4
Behaviours
The Rodney Brooks subsumption architecture
We have to manage complex robot behaviour
• We managed robot movement using the navigation stack
• Now we need to combine many simple tasks
• To make a robot that appears intelligent
• To make a robot that plans to achieve its goals
• Better than Threads: Who’s watching that Battery Voltage?
5
Subsumption
6
Subsumption architecture
7
Subsumption
Do you remember the last time conditions made you touch your face/hair.
8
The Behaviour Stack: Subsumption in leJOS
An Arbitrator manages a list of Behavior objects.
• Loops forever asking each Behavior object whether it wants to takeControl.
• Runs the highest priority action method in its own thread.
WHILE TRUE:
// Build a list of waiting Behaviour actions
ASK each Behavior if it wants to takeControl.
The suppress method can be called any number of times (including none).
• If a Behavior has a short action then it can ignore suppress calls.
The action will finish in good time anyway.
• A long running action method must notice suppress.
The action method should finish early and exit gracefully. 10
Subsumption in leJOS: The Behavior interface
11
Understanding a Behavior
leJOS lets us create many Behavior objects and think about them separately
• We let an Arbitrator object organise them for us.
• Each Behavior takes control of the robot when it is active.
• It should respond nicely when the Arbitrator tells it to stop. . .
• . . . because another (more important) Behavior needs a turn.
12
Building a Behavior
13
Building a Behavior continued
14
An example - Behaviour controlled driving
16
An Example Behavior - Get Me Out of Here
18
Sharing the State object: What sort of state am I in?
State is a kind of internal sensor - looking at the state of the robot
• Create a State class and then a State instance state in main.
• The state will record what the robot is trying to do.
• Share state with any Behaviour that depends on, or alters, the robot state.
• The State class can have a single char field with getters and setters.
// wait for a loud sound after a quiet one public class WFFall implements Behavior {
public class WFRise implements Behavior { private State sharedState;
private State sharedState; private float[] sound = new float[1];
private float[] sound = new float[1]; public boolean takeControl() {
public boolean takeControl() { if (sharedState.isWaitingForRising()) {
if (!sharedState.isWaitingForRising()) return false; return false;
sp.fetchSample(sound, 0); }
return (sound[0] > 0.5f); sp.fetchSample(sound, 0);
} return (sound[0] < 0.2f);
public void suppress() {} }
public void action(){ public void suppress() {}
// Do the "Heard a new clap" action public void action() {
sharedState.setWaitingForRising(false); sharedState.setWaitingForRising(true);
} // constructor and sp def'n not shown } // constructor and sp def'n not shown 20
States and Personality
21
Worrying Pitfalls
Fast Methods
• If action never takes a long time then suppress can just return.
• suppress should always return quickly.
• suppress will be called multiple times if action takes a while to finish.
22
Extra Sensors, Extra Programs
Extra Sensors
23
Self Test
Questions
1. Are you ready?
24