LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms...

37
LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller Music Experiment LMHanoi - Tower of Hanoi with Lego Mindstorms Markus Flatz, Peter Palfrader, Andreas Rottmann 25.01.2011

Transcript of LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms...

Page 1: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

LMHanoi - Tower of Hanoi with LegoMindstorms

Markus Flatz, Peter Palfrader, Andreas Rottmann

25.01.2011

Page 2: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Overview

1 Basics

2 The robot

3 SoftwareInter-thread communicationApplication logicRobot controllerMusic

4 Experiment

Page 3: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

The puzzle

• Move a set of disks from one rod to another

• With moving only ever one disk

• No larger disk may rest on a smaller one

Page 4: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

The puzzle

• Move a set of disks from one rod to another

• With moving only ever one disk

• No larger disk may rest on a smaller one

Page 5: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

The puzzle

• Move a set of disks from one rod to another

• With moving only ever one disk

• No larger disk may rest on a smaller one

Page 6: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Lego Mindstorms

• Extends Lego with new building blocks:• Sensors• Motors• Programmable computer

• NXT 2.0: 32-bit ARM7 microprocessor, 64 kB RAM and256 kB Flash memory

• 4 input, 3 output ports

• Bluetooth, USB

• Sound

Page 7: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

ABuNQEUC1 NXCNot eXactly C

• Free compiler to build Lego Mindstorms executables fromsomething similar to C

• Somewhat documented

• Several limitations, e.g., no recursion, no passing ofmutexes as parameters

• Workarounds require abusing the preprocessor

1almost, but not quite, entirely unlike C

Page 8: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

The robot: Klaus

• Forklift

• Can move along three axes

• Movement limited bysensors for two of them

• Using touch and colorsensors

Page 9: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Page 10: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Software

App. Logic Controller

Music

MBox

GlobalVariable

Robot

Three threads

• Application logic (Tower of Hanoi algorithm)

• Robot controller

• Music playback

Page 11: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Inter-thread communication

NXC Primitives

• Mutex

Mailbox

0 654321

front

fill

• Circular buffer

• Mutex (protects fill count and front pointer)

• Two counting semaphores (full, empty)[1]

Page 12: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Application logic

• Determine movement sequence

Page 13: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Recursive algorithm

vo id hano i ( i n t n , rod source , rod he lp , rod de s t ) {i f ( n > 0) {

hano i ( n − 1 , source , dest , h e l p ) ;move d i sk ( source , d e s t ) ;hano i ( n − 1 , he lp , source , d e s t ) ;

}}

• 2n − 1 moves

• Optimal movement sequence

Page 14: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Recursive algorithm

vo id hano i ( i n t n , rod source , rod he lp , rod de s t ) {i f ( n > 0) {

hano i ( n − 1 , source , dest , h e l p ) ;move d i sk ( source , d e s t ) ;hano i ( n − 1 , he lp , source , d e s t ) ;

}}

• 2n − 1 moves

• Optimal movement sequence

Page 15: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Recursive algorithm

vo id hano i ( i n t n , rod source , rod he lp , rod de s t ) {i f ( n > 0) {

hano i ( n − 1 , source , dest , h e l p ) ;move d i sk ( source , d e s t ) ;hano i ( n − 1 , he lp , source , d e s t ) ;

}}

• 2n − 1 moves

• Optimal movement sequence

Page 16: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Iterative algorithm [2],[3]

• Rods source, help and destination on a circle

• Even number of disks: ordered clockwise

• Odd number of disks: ordered counterclockwise

wh i l e ( not a l l d i s k s on d e s t i n a t i o n rod ) {move the sm a l l e s t d i s k one rod c l o c kw i s e ;i f ( a d i s k o th e r than the sm a l l e s t can be moved ) {

move t h i s d i s k ;}

}

• Same movement sequence

Page 17: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Iterative algorithm [2],[3]

• Rods source, help and destination on a circle

• Even number of disks: ordered clockwise

• Odd number of disks: ordered counterclockwise

wh i l e ( not a l l d i s k s on d e s t i n a t i o n rod ) {move the sm a l l e s t d i s k one rod c l o c kw i s e ;i f ( a d i s k o th e r than the sm a l l e s t can be moved ) {

move t h i s d i s k ;}

}

• Same movement sequence

Page 18: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Iterative algorithm [2],[3]

• Rods source, help and destination on a circle

• Even number of disks: ordered clockwise

• Odd number of disks: ordered counterclockwise

wh i l e ( not a l l d i s k s on d e s t i n a t i o n rod ) {move the sm a l l e s t d i s k one rod c l o c kw i s e ;i f ( a d i s k o th e r than the sm a l l e s t can be moved ) {

move t h i s d i s k ;}

}

• Same movement sequence

Page 19: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Implementation

• No support for recursion

• Choose iterative algorithm

Page 20: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Naıve implementation

help dest. source . . . dest.disk 1 2 3 . . . n

• Store the current location of each disk

• Space complexity: O(n)

• Time complexity per move: O(n)

Page 21: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Naıve implementation

help dest. source . . . dest.disk 1 2 3 . . . n

• Store the current location of each disk

• Space complexity: O(n)

• Time complexity per move: O(n)

Page 22: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Naıve implementation

help dest. source . . . dest.disk 1 2 3 . . . n

• Store the current location of each disk

• Space complexity: O(n)

• Time complexity per move: O(n)

Page 23: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Refined implementation

source 3help 4 1

destination 5 2

height: 1 2 2source help dest.

• Store the disks on each rod

• Store the height of each rod’s stack

• Approximately three times as much memory

• Space complexity: O(n)

• Time complexity per move: O(1)

Page 24: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Refined implementation

source 3help 4 1

destination 5 2

height: 1 2 2source help dest.

• Store the disks on each rod

• Store the height of each rod’s stack

• Approximately three times as much memory

• Space complexity: O(n)

• Time complexity per move: O(1)

Page 25: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Refined implementation

source 3help 4 1

destination 5 2

height: 1 2 2source help dest.

• Store the disks on each rod

• Store the height of each rod’s stack

• Approximately three times as much memory

• Space complexity: O(n)

• Time complexity per move: O(1)

Page 26: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Refined implementation

source 3help 4 1

destination 5 2

height: 1 2 2source help dest.

• Store the disks on each rod

• Store the height of each rod’s stack

• Approximately three times as much memory

• Space complexity: O(n)

• Time complexity per move: O(1)

Page 27: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Refined implementation

source 3help 4 1

destination 5 2

height: 1 2 2source help dest.

• Store the disks on each rod

• Store the height of each rod’s stack

• Approximately three times as much memory

• Space complexity: O(n)

• Time complexity per move: O(1)

Page 28: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Robot controller (1)

General structure

• Get movements from mailbox and execute them

• Maintains current position

• Wagon position stops on color sensor readings

• Fork vertical movement stops on touch sensor

• Fork moves horizontally by fixed amount

Page 29: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Robot controller (2)

Specialties

• OvershootTarget Position

Sensor Path

• Use raw mode of color sensor to detect blue

Page 30: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Robot controller (2)

Specialties

• OvershootTarget Position

Sensor Path

• Use raw mode of color sensor to detect blue

Page 31: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Music

• Additional concurrency

• Situational

Page 32: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Music

• Additional concurrency

• Situational

Page 33: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Experimentduration(n) = (2n − 1) · d + c

0

100

200

300

400

500

0 1 2 3 4 5

dura

tion [s]

number of disks

’lmhanoi.dat’expreg(x)

expreg(x) = (e0.693·x − 1) · 23.2 + 12.7

Page 34: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Experimentduration(n) = (2n − 1) · d + c

0

100

200

300

400

500

0 1 2 3 4 5

dura

tion [s]

number of disks

’lmhanoi.dat’expreg(x)

expreg(x) = (e0.693·x − 1) · 23.2 + 12.7

Page 35: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Experimentduration(n) = (2n − 1) · d + c

0

100

200

300

400

500

0 1 2 3 4 5

dura

tion [s]

number of disks

’lmhanoi.dat’expreg(x)

expreg(x) = (e0.693·x − 1) · 23.2 + 12.7

Page 36: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

References

John A. Trono and William E. Taylor.Further comments on “a correct and unrestrictiveimplementation of general semaphores”.SIGOPS Oper. Syst. Rev., 34:5–10, July 2000.

Wikipedia.Tower of Hanoi – Wikipedia, The Free Encyclopedia, 2011.https://secure.wikimedia.org/wikipedia/en/w/

index.php?title=Tower_of_Hanoi&oldid=407310252.

Wikipedia.Turme von Hanoi – Wikipedia, Die freie Enzyklopadie,2011.https:

//secure.wikimedia.org/wikipedia/de/w/index.

php?title=T%C3%BCrme_von_Hanoi&oldid=83701385.

Page 37: LMHanoi - Basics LMHanoi - Tower of Hanoi with …...LMHanoi - Tower of Hanoi with Lego Mindstorms Basics The robot Software Inter-thread communication Application logic Robot controller

LMHanoi -Tower ofHanoi with

LegoMindstorms

Basics

The robot

Software

Inter-threadcommunication

Application logic

Robot controller

Music

Experiment

Thank you for your attention.