SinelaboreRT Header Logo

SinelaboreRT

Productivity for embedded software development

User Tools

Site Tools


wiki:examples:light_controller

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 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 automatic light controller zip files 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
  • t Ambient brightness low (twilight, night)
  • d Ambient brightness high (dawn, day)
  • a Auto mode
  • o Permanent on
  • x Permanent off
  • q Quit 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 machine1). 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 ZIP file contains an executable that was built with Cygwin on Windows (light.exe) and for OS X (light_osx). To change the state machine or to generate the code yourself download the demo version of the Sinelabore code generator.

Have fun!

1)
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.
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
wiki/examples/light_controller.txt · Last modified: 2019/12/09 19:00 by pmueller

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki