lecture 17: finite state machines, behavior-based ... › engi128 › handouts ›...

26
1 ENGI 128 INTRODUCTION TO ENGINEERING SYSTEMS Lecture 17: Finite State Machines, Behavior-Based Programming, Final Design Challenge “Understand Your Technical World”

Upload: others

Post on 30-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

1

ENGI 128 INTRODUCTION TO ENGINEERING SYSTEMS

Lecture 17: Finite State Machines,

Behavior-Based Programming, Final Design Challenge

“Understand Your Technical World”

Page 2: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

2

Finite-State Machines (Discrete Finite Automata)

Page 3: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

3

Finite-State Machines (Discrete Finite Automata)

It sounds complicated, but you’ve been using them for a while:

Sample FSM diagram:

Q: What does this program do?

waiting blink blue

lights

no button press

button press

reset

reset transition

transition

state

transition condition

Page 4: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

4

An Example FSM: Wall detection

[Demo dark avoid code on robot]

Page 5: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

5

An Example FSM: Wall detection

reset move

forward

Page 6: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

6

An Example FSM: Wall detection

reset move

forward

?

?

Page 7: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

7

An Example FSM: Wall detection

reset move

forward

rotate left

rotate right

?

?

?

Page 8: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

8

An Example FSM: Wall detection

no wall

reset move

forward

rotate left

rotate right

right sensor detect wall

left sensor detect wall

Q: Note that the left sensor triggers a right rotate. Does this make sense?

Page 9: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

9

An Example FSM: Wall detection

reset

? move

forward

rotate left

rotate right

?

?

?

no wall

right sensor detect wall

left sensor detect wall

Page 10: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

10

An Example FSM: Wall detection

reset

|angle| < p/2

move forward

rotate left

rotate right

|angle| < p/2

|angle| ≥ p/2

|angle| ≥ p/2

[Review dark avoid code in editor]

no wall

right sensor detect wall

left sensor detect wall

Page 11: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

11

Finite-State Machines (Quidditch Version)

Page 12: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

For example

Let’s say you are programming a “Seeker” robot…

reset move

around

Page 13: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

For example

Let’s say you are programming a “Seeker” robot…

no event

wall detector reset

wait for

timer timer done

move around

avoid wall

Page 14: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

For example

Let’s say you are programming a “Seeker” robot…

no event

wall detector reset

wait for

timer timer done

detect snitch

no snitch

move around

avoid wall

move to snitch

Page 15: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

For example

Let’s say you are programming a “Seeker” robot…

no event

wall detector reset

wait for

timer timer done

detect snitch

capture snitch

no snitch

move around

avoid wall

move to snitch

score!

Page 16: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

Yikes!

Don’t worry, it looks simpler in code…

Page 17: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

17

Behavior-Based Robot Programming

Page 18: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

18

Behavior-Based Control

A behavior is a small program (or finite-state machine) that reads the sensors and controls the robot

• Each behavior only does one simple thing • Each behavior has access to all the sensors of the robot and

produces motor outputs (tv, rv, active)

Only one behavior can be active at a time • There is a prioritization of behaviors • More important ones override, or subsume, less important ones

Page 19: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

19

An Example Behavior-Based Program

Follow a robot • Sensor: IR Communications • Behavior: Follow another robot

Avoid Obstacles • Sensors: Bump sensor to detect wall • Behavior: Move away from wall

Wander • Sensor: Encoders • Behavior: Move forward and turn

Q: Which behavior should have the highest priority?

Q: Which behavior should have the lowest priority?

Page 20: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

Combining Behaviors

We combine behaviors by overriding, or subsuming lower-priority behaviors if a higher-priority behavior becomes active

Wander

Behavior: move and turn

Avoid Obstacle

Behavior: avoid walls

Follow a robot

Behavior: Follow another robot Senso

rs

Actu

ato

rs

S

S

subsume operator

highest priority

lowest priority

Page 21: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

Genghis

The behavior-based poster child

Really simple hardware • 6 legs, 2 motors per leg

• a-motor for forward/back, b-motor for up/down

• 2 bump sensors (feelers) • 2 ground detection sensors (switches) • 6 heat sensors (but they weren’t used for walking)

R. Brooks. “A Robot that Walks; Emergent Behavior from a Carefully Evolved Network”, ICRA 1989

Page 22: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

Genghis in Action

Page 23: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

Subsumption Architecture

R. Brooks. “A robust layered control system for a mobile robot”, ICRA 1986

Page 24: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

Combining Behaviors

We combine behaviors by overriding, or subsuming lower-priority behaviors if a higher-priority behavior becomes active

Wander

Behavior: move and turn

Follow a robot

Behavior: Follow another robot

Avoid Dark

Behavior: avoid shadow at walls Senso

rs

Actu

ato

rs

S

S

subsume operator

highest priority

lowest priority

Page 25: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

25

Great, but how to you program this?

You can abstract this a bit more: • Write each behavior as a function that returns a tuple of

(tv, rv, active) • write a beh_subsume(high_priority, low_priority) function that

returns the high_priority or low_priority output, depending on which has active == True

• If neither behavior is active, it returns the INACTIVE_BEH output

INACTIVE_BEH = (False, 0.0, 0.0)

wander_out = wander(foo, bar)

gps_out = gps_navigation(bang, zoom)

obstacle_out = obstacle_avoidance(bif, bop)

beh = beh_subsume(gps_out, wander_out)

beh = beh_subsume(obstacle_out, beh)

velocity.set_tvrv(beh_get_tv(beh), beh_get_rv(beh))

Page 26: Lecture 17: Finite State Machines, Behavior-Based ... › engi128 › Handouts › Lec17-Robotics.pdfthe sensors and controls the robot •Each behavior only does one simple thing

26

Great, but how to you program this?

There are several ways: • Write each behavior as a function that returns a tuple of

(active, tv, rv) • Write getters to extract the different parts of the tuple • Use if statements to overwrite the outputs of lower-priority

behaviors

INACTIVE_BEH = (False, 0.0, 0.0)

wander_out = wander(foo, bar)

gps_navigation_out = gps_navigation(bang, zoom)

obstacle_avoidance_out = obstacle_avoidance(bif, bop)

beh = INACTIVE_BEH

if beh_get_active(wander_out):

beh = wander_out

if beh_get_active(gps_navigation_out ):

beh = gps_navigation_out

if beh_get_active(obstacle_avoidance_out):

beh = obstacle_avoidance_out

velocity.set_tvrv(beh_get_tv(beh), beh_get_rv(beh))