State Machine Example 3

 
 

This examples shows the state diagram of a truck’s fright lift. The ramp is operated by a control panel with three control levers. To open or close the ramp area the middle control lever must be used. A sensor measures the angle between 90° ... -10°. In open position the angle is 5°. In closed position the angle is 90°. The left control lever allows to drive the ramp up or down. A position sensor is used to calculate the position (pos) as a value between 0 ... 100%. If the lift is up the position is 100%. If the lift is down the position is 0%. It is only possible to operate the lift upwards / downwards if the door is fully opened.  If the door is open the tilt can be adjusted between +5° and -10° with the right control lever. Adjusting the tilt is only possible if the door is fully open.


The diagram below shows the state diagram for the fright ramp. This is not the only possible solution. As you can see an evStop event is going from every state to the Stopped state. A possible design alternative would be to create a composite state ‘Moving’ with all the other states (beside Stopped) as children and model one transition from Moving to Stopped.


The diagram above was created with Enterprise Architect. To generate code export  the model into an XMI file. The following command then generates C code from the state machine.


java -jar codegen.jar  -p ea -l c -t "Model:Class Model:test" -o ramp ramp.xml


The flag -p selects the used parser. In this case ea selects the parser for Enterprise Architect. Other parsers for UModel, Cadifra or Magic Draw are also available. The next flag -l selects the target language. In this case C is selected. The flag -t defines where the state diagram can be found in the XMI file. This path corresponds to the package structure in your model file. The flag -o defines the filenames of the generated files and finally the input file must be specified.


The following files are generated:

ramp.c    -> contains state machine function

ramp.h     -> header file for ramp.c

ramp_dbg.h  -> contains helper functions useful for debugging purposes

ramp_ext.h -> the event definitions


Here you can download the generated files.


To test the state machine you can start the code generator in simulation mode by adding the command line switch -S:


java -jar codegen.jar -S -p ea -l c -t "Model:Class Model:test" -o ramp ramp.xml


A window pops up that shows the state diagram and the events accepted by the machine in the presently active state. In the bottom area of the window the fired event and the executed code as reaction to this event is shown. You can click on events to trigger a transition. It is also possible to send events from a real target device e.g. via a serial link. This allows to visually monitor a live target. See the manual for more info.


The diagram below shows the simulation window:



Summary

This example has shown how simple it is to transform a UML state diagram model into C code and interactively test it. The generated code is Lint clean and requires no framework. SinelaboreRT does not enforce a specific execution framework or system design like other tools do.

 

A Truck’s Freight Ramp

Homehttp://www.sinelabore.comhttp://www.sinelabore.comshapeimage_3_link_0

Copyright © 2009 Peter Mueller. All Rights reserved. See Trademarks and Terms of Use.