Behavior Programming Structured Programming A series of ifthens
Behavior Programming
Structured Programming • A series of if-thens • Easy to get started in and hardly requires any thought or design beforehand • But the code ends up as spaghetti code MMN Lab.
Behavior programming • Requires a little more planning before coding • Each behavior is nicely encapsulated within an easy to understand structure • Very easy to add or remove specific behaviors MMN Lab.
The Behavior API • Only one interface and one class – Behavior interface – Arbitrator class MMN Lab.
Behavior Interface • API – boolean take. Control() • Returns a boolean value to indicate if this behavior should become active – void action() • Initiates an action when the behavior becomes active – void suppress() • Immediately terminate the code running in the action() method MMN Lab.
Arbitrator • Constructor – public Arbitrator(Behavior [] behaviors) • Higher index has the higher priority • Method: – public void start(); MMN Lab.
Coding Behaviors MMN Lab.
Coding Behaviors cont’d MMN Lab.
Coding Behaviors cont’d MMN Lab.
Coding Behaviors cont’d • Add a new behavior MMN Lab.
Coding Behaviors cont’d • Add the new behavior to the Arbitrator MMN Lab.
Sensor Listener MMN Lab.
Lab 6 • Based on the sample code, design the following behaviors with proper priorities Low Priority High • • • Drive. Forward (included in sample code) Hit. Wall (included in sample code) Turn 360 degree by triggering ultrasonic sensor Play music by triggering sound sensor Do. Stop (included in sample code) MMN Lab.
Lab 6 • Hint: – You can use Sensor. Listener for more accurate sensor action – Add delay if the program starts in the unexpected way MMN Lab.
- Slides: 14