EETimes

Embedded Systems December 2000 Vol13_13

Issue link: http://dc.ee.ubm-us.com/i/71850

Contents of this Issue

Navigation

Page 49 of 197

The beauty of coding even simple algorithms as state machines is that the test plan almost writes itself. All you have to do is to go through every state transition. Notice that the code for RaisingGearO attempts to mirror the two row in the state transition table for the Raising Gear state. One guideli ne to keep in mind is "Avoid hidden states." A hidden state is what you get (or more honestly, what I got) when, out of laziness, you try to add a conditional substate rather than explicitly add a state. For instance, if your state function has code that says "Do this in one mode but do that in another mode," I'd start to wonder if that state shouldn' t be split into two. This choice would depend on how much stuff was con- tained in the conditional. If your code is handling the same input in different ways (that is, trigge ring dif- ferent state transitions) depending on a mode flag, then that's a hidden state. To implement it that way reduces the advantage that the state machine conveys. As an exercise, you may want to void RaisingGear() { /* Once all 3 legs are ~, go to the GEAlLUP state.*/ if ┬źnosegear_is_~ == MADE) && (Leftgear_is_~ == MADE┬╗ { pump-PDtor = OFF; gear_l ights = EXTINGUISH; curr_state = GEA~UP; }; /* If the pilot changes his mind, start lowering the gear.*/ if (gear_lever = DOWN) { pump_direction = DOWN; curr_state = GEAR_LOWERING; }. , } Testing The beauty of coding even simple algorithms as state machines is th at the test plan almost writes itself. All you have to do is to go through every state tran ition . I usually do it with a high lighter in hand, crossing off the arrows on the state transition diagram as they successfully pass theil- tests. This is a good reason to avoid "hidden states"- they' re more likely to escape testing than explicit tates. Until you can use the "real" hardware to induce state changes, e ither do it with a TAlLE 1 Current State 3 are u Gear down Waiting for takeoff Waiting for takeoff Raising gear Raising gear Gear up Lowering gear Lowering gear X X X Made X X X X NC ; No change in output X X X X X Made X ; Don't care input GND X X X "- X X X X NC DN ON UP NC == MADE) && (rtgear_is_~ expand the state mach ine we've described to add a timeout to the extension or retraction cycle, because our mechanical engineer doesn 't want the hydraulic pump to run for more than 60 seconds. If the cycle times out, the pilot should be ale rted byalternat- ing green and red lights, and he should be able to cycle the lever to try again. Another feature to exe rcise your skills would be to ask our hypo- the tical mechanical engineer "Does the pump suffer from having the direction reversed while it's running? We do it in the two cases where the pilot changes his mind." He' ll say "yes," of course. How would you modi- fy the state machine to stop the pump briefly when the direction is forced to reverse? In uts Timer Dir Out uts Timer Start stop Lights RED NC Restart NC NC NC lit NC NC OFF NC REO NC Next State Waiting for takeoff Raising gear Gear down Gear up Lowering gear Lowering gear Raising gear GREEN Gear down 48 DECEMBER 2000 Embedded Systems Programming

Articles in this issue

Archives of this issue

view archives of EETimes - Embedded Systems December 2000 Vol13_13