behavior programming

14
Behavior Programming

Upload: chaeli

Post on 14-Jan-2016

49 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Behavior Programming

Behavior Programming

Page 2: 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.

Page 3: Behavior Programming

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.

Page 4: Behavior Programming

The Behavior API

• Only one interface and one class– Behavior interface– Arbitrator class

MMN Lab.

Page 5: Behavior Programming

Behavior Interface

• API– boolean takeControl()

• Returns a boolean value to indicate if this behavior should become active

– void action()• Initiates an action when the behavior becomes acti

ve

– void suppress()• Immediately terminate the code running in the acti

on() method

MMN Lab.

Page 6: Behavior Programming

Arbitrator

• Constructor– public Arbitrator(Behavior [] behaviors)

• Higher index has the higher priority

• Method:– public void start();

MMN Lab.

Page 7: Behavior Programming

Coding Behaviors

MMN Lab.

Page 8: Behavior Programming

Coding Behaviors cont’d

MMN Lab.

Page 9: Behavior Programming

Coding Behaviors cont’d

MMN Lab.

Page 10: Behavior Programming

Coding Behaviors cont’d

• Add a new behavior

MMN Lab.

Page 11: Behavior Programming

Coding Behaviors cont’d

• Add the new behavior to the Arbitrator

MMN Lab.

Page 12: Behavior Programming

Sensor Listener

MMN Lab.

Page 13: Behavior Programming

Lab6

• Based on the sample code, design the following behaviors with proper priorities

• DriveForward (included in sample code)• HitWall (included in sample code)• Turn 360 degree by triggering ultrasonic sensor• Play music by triggering sound sensor• DoStop (included in sample code)

MMN Lab.

Low

High

Priorit

y

Page 14: Behavior Programming

Lab6

• Hint:– You can use SensorListener for more accurat

e sensor action – Add delay if the program starts in the unexpec

ted way

MMN Lab.