PDA

View Full Version : Am I making a 2, 8 or 16 state machine?


Mark Bresler
12-26-06, 09:53 PM
To follow up on my earlier question, I am trying to decide whether to port my earlier forth programming for a two motor wheelchair or to code it in machine state logic as a learning experience for the IsoPod. My initial application will be a single switch controlled toy. Eight lights in a circle will be scanned slowly one at a time, when the desired direction is lit, the user activates the switch and the toy goes in that direction for as long as the switch is activated, and then scanning resumes at 12 o'clock position. I am trying to conceptualize, whether this is a two state machine with different values loaded for the active states (active light, motor speeds and directions) or an 8 or 16 state machine.

Thanks,
Mark

Dave
12-27-06, 02:48 PM
Here is a possible IsoMax state machine implementation, using only 4 directions. Shown are 8 states to obtain 4 directions.

Assumptions : that the device uses DC geared motors, being driven through an h-bridge as a differential drive toy robot.
Also that there is no odometry on the toy robot, so a timed estimate was made of how long it takes the toy to turn to the direction selected. Then the toy moves along that direction as long as the switch is depressed. If press and release, then toy moves only a short duration, stops and goes back to scanning.

Added were 4 LEDs for indicating direction on some PA pins, and a switch to ground for selection. PWMAx outputs to drive dual h-bridge like NMIH-0010 or D-Rex.

Going to 8 directions should probably add 4 additional HEADx states and 4 additional TURNx states for a total of 16 using this possible implementation. I think it should be possible to implement with less states by using array variables, but have not tried that.

Procedural Forth sould work also, but have not tried that implementation either.




( TOY DIRECTION TRAVELER
( LED DIRECTION INDICATORS DRIVEN ON PA2, PA3, PA4, PA5 LIMITED UNDER 4 mA
( SWITCH NORMALLY OPEN TO GROUND, CONNECTED TO PA0
( DUAL H-BRIDGE TO DRIVE 2 MOTORS, DRIVEN BY PWMA0 PWMA1 PWMA2 PWMA3
( DIRECTION SELECTED WHEN PA0 DEPRESSED
( ESTIMATE OF TIME OF TURN IN TURN-DURx LOOPINDEICES


VARIABLE DIRECTION EEWORD

: HEAD0LED PA2 ; EEWORD
: HEAD1LED PA3 ; EEWORD
: HEAD2LED PA4 ; EEWORD
: HEAD3LED PA5 ; EEWORD

: TURN-RIGHT PWMA0 ON PWMA1 OFF PWMA2 OFF PWMA3 ON ; EEWORD
: FORWARD PWMA0 ON PWMA1 OFF PWMA2 ON PWMA3 OFF ; EEWORD
: STOP-MOTOR PWMA0 OFF PWMA1 OFF PWMA2 OFF PWMA3 OFF ; EEWORD

LOOPINDEX DELAY
100 DELAY END EEWORD

LOOPINDEX TURN-DUR1
100 TURN-DUR1 END EEWORD

LOOPINDEX TURN-DUR2
200 TURN-DUR2 END EEWORD

LOOPINDEX TURN-DUR3
300 TURN-DUR3 END EEWORD


MACHINE PICK-HEADING EEWORD
ON-MACHINE PICK-HEADING
APPEND-STATE HEAD0 EEWORD
APPEND-STATE HEAD1 EEWORD
APPEND-STATE HEAD2 EEWORD
APPEND-STATE HEAD3 EEWORD

APPEND-STATE TURN1 EEWORD
APPEND-STATE TURN2 EEWORD
APPEND-STATE TURN3 EEWORD
APPEND-STATE MOVE EEWORD

IN-STATE
HEAD0
CONDITION
DELAY COUNT PA0 OFF? AND
CAUSES
DELAY RESET
FORWARD
THEN-STATE
MOVE
TO-HAPPEN IN-EE

IN-STATE
HEAD0
CONDITION
DELAY COUNT PA0 ON? AND
CAUSES
HEAD0LED OFF
HEAD1LED ON
DELAY RESET
THEN-STATE
HEAD1
TO-HAPPEN IN-EE


IN-STATE
HEAD1
CONDITION
DELAY COUNT PA0 OFF? AND
CAUSES
DELAY RESET
TURN-RIGHT
THEN-STATE
TURN1
TO-HAPPEN IN-EE

IN-STATE
HEAD1
CONDITION
DELAY COUNT PA0 ON? AND
CAUSES
HEAD1LED OFF
HEAD2LED ON
DELAY RESET
THEN-STATE
HEAD2
TO-HAPPEN IN-EE


IN-STATE
HEAD2
CONDITION
DELAY COUNT PA0 OFF? AND
CAUSES
DELAY RESET
TURN-RIGHT
THEN-STATE
TURN2
TO-HAPPEN IN-EE

IN-STATE
HEAD2
CONDITION
DELAY COUNT PA0 ON? AND
CAUSES
HEAD2LED OFF
HEAD3LED ON
DELAY RESET
THEN-STATE
HEAD3
TO-HAPPEN IN-EE


IN-STATE
HEAD3
CONDITION
DELAY COUNT PA0 OFF? AND
CAUSES
DELAY RESET
TURN-RIGHT
THEN-STATE
TURN3
TO-HAPPEN IN-EE

IN-STATE
HEAD3
CONDITION
DELAY COUNT PA0 ON? AND
CAUSES
HEAD3LED OFF
HEAD0LED ON
DELAY RESET
THEN-STATE
HEAD0
TO-HAPPEN IN-EE


IN-STATE
TURN1
CONDITION
TURN-DUR1 COUNT
CAUSES
TURN-DUR1 RESET
FORWARD
THEN-STATE
MOVE
TO-HAPPEN IN-EE

IN-STATE
TURN2
CONDITION
TURN-DUR2 COUNT
CAUSES
TURN-DUR2 RESET
FORWARD
THEN-STATE
MOVE
TO-HAPPEN IN-EE

IN-STATE
TURN3
CONDITION
TURN-DUR3 COUNT
CAUSES
TURN-DUR3 RESET
FORWARD
THEN-STATE
MOVE
TO-HAPPEN IN-EE


IN-STATE
MOVE
CONDITION
DELAY COUNT PA0 OFF? AND
CAUSES
DELAY RESET
THEN-STATE
MOVE
TO-HAPPEN IN-EE

IN-STATE
MOVE
CONDITION
DELAY COUNT PA0 ON? AND
CAUSES
HEAD0LED ON
HEAD1LED OFF
HEAD2LED OFF
HEAD3LED OFF
DELAY RESET
STOP-MOTOR
THEN-STATE
HEAD0
TO-HAPPEN IN-EE


: INIT
INSTALL PICK-HEADING
HEAD0 SET-STATE
; EEWORD

Mark Bresler
12-28-06, 10:14 PM
Dave, Thank you so much for your example, it is much shorter than the Forth program I wrote, I guess that shows the power of ISoMax. I am planning to keep things simple for now and keep motor speed and distance open loop, the user will be in control of deciding when the toy has travelled or turned enough.

Thanks again,
Mark