Automatic Light Controller


State machine example

Everybody knows outdoor automatic light switches. They offer the comfort to automatically switch on/off the outside light. This way they even help to save energy. Other names for such a device are: motion-activated (or motion-sensing) porchlight, dusk-to-dawn sensor porchlight


The following figure shows a simplified state machine of such a device.

 
 

Functional Requirements

The device has three modes which can be changed using a simple switch. It has the positions OFF, Auto and ON. The switch is realized in a way that the user can only go from OFF to AUTO to ON and vice versa.


In ON mode the light is permanently switched on. In OFF mode the light is permanently switched off. In auto mode the light is automatically switched on and off as described in the next section.


When its becoming dark and the auto mode is active the device automatically switches on the light if a person is detected. If the person walks away the light is switched off again. The light is on for at least a configurable time (e.g. 60s). If the person is still nearby after that time the light is kept on as long as no person can be detected anymore.


The light should not be switched on during daylight. Therefore the device permanently measures the ambient brightness. Only if the measured value is below an adjustable threshold the device switches on the light automatically.


State Machine Design

The three modes of the device can be easily transferred into states. The auto state is a hierarchical state which realizes the night/day behavior when a person was detected. The design was created using the  SinelaboreRT state machine editor. The diagram is shown in the figure above.


The following zip file contains the fully worked out example with a simple text based command interface. Start the executable and type in one of the supported letters to trigger the machine.


Available commands:

+There is a person

- There is no person

tAmbient brightness low (twilight, night)

dAmbient brightness high (dawn, day)

aAuto mode

oPermanent on

xPermanent off

qQuit the program


The files gui.c/h realizes the keyboard handling. In the file  light_trace.c a simple trace function was realized to trace the event flow. Main.c calls the state machine and checks the keyboard for new events. All other files are automatically generated from the state machine as shown above.


To test the machine type in e.g.:   o   x   a   t  +  ... (repeat 20 times) +  -


Graphical Trace

The sinelaboreRT codegen tool is able to display state diagrams based on an automatic layout engine. In simulation mode trace data can be received via a UDP connection. The received evens are then used to trigger the same state changes as in the compiled C program. This makes it possible to follow the state changes of the light.exe program while we stimulate it with the keyboard events.


The prepared example already sends the trace data via UDP (see light_trace.c).  To display the state machine start the editor in simulation mode as follows:


$ java -jar codegen.jar -p ssc -S -o automatic_light.xml automatic_light.xml


This command line brings up a window showing the state machine*). Start the light executable again and type in commands. You can now follow the current state of the state machine and the C-code executed as reaction to your keyboard input. The video below shows a simulation recording.




The ZIP file contains an executable which was built with Cygwin on Windows (light.exe) and for OS X (light_osx). To change the state machine and generate the code yourself or run the simulation from above yourself download a demo version of the
sinelaboreRT code generator and a ZIP file containing the
example right here.


Have fun!






*) Make sure graphviz is installed on your system and the correct path to the dot.exe (layout engine) is defined in the codegen.cfg file. Otherwise you will get an error message when starting the codegen in simulation mode.




Copyright © 2008-2010 Peter Mueller. All Rights reserved. See Trademarks and Terms of Use.