Neercs
Posts: 13
Joined: Mon Oct 05, 2015 1:47 pm

Finite State Machine with multitasking threads

Sat Nov 21, 2015 11:53 pm

Hi everybody,

I am working on my first project with Raspberry Pi and Python for more than 2 months now and I really struggle with my python code. The hardware part of the project is almost operative but the software part... I have recently discovered threads and FSM
and I am not sure about how to structure the codes...

The box I am building, is intended to be placed in a rack to control the pressure of a vacuum chamber, and is composed of:

Inside
- A microprocessor / microcontroller Raspberry Pi B +
- 4 relays to control two valves (VV, HVGV) and two pumps (PP and TMP)
- 3 sensors (P1, P2, P3)
- And other electronic components, wires ....

Outside on a front panel
- A button with 2 states to define the mode of control (local control = actuators, relays are controlled with manual switch buttons directly on the front panel, remote control = actuators are controlled with Labview GUI buttons that send commands to the controller or other software)
- 4 buttons to individually control the states ON / OFF of the 2 valves and 2 pumps
- Fuses, LEDs to indicate status, a start button and stop ...
- a 16 * 2 LCD display

Once I would have a working python code, I would also like the code to be automatically run on Raspi when the power supply is turned on. No need to enter command in the terminal (sudo etc) ... i.e. to autorun the Python script after initialization of the OS Raspbian. I found several things on the internet for that, so normally no big deal.

After having defined all GPIOs for the screen, relays, sensors (3 inputs through SPI with ADC ADS1118)... in my main .py I called it CODE.py I run several threads.
The system can be in 5 different states:
State 0: Vented chamber
State 1: Establish vacuum
State 2: Maintain vacuum
State 3: Ventilating chamber
State 4: Safe State
And under certain conditions the system can move from one state to another.

I defined different threads to :
- Acquire continuously my way of local or remote operation
- Acquire continuously 3 sensor values
- Display continuously information on the screen
- Establish a TCP / IP connection with the Labview GUI (GUI Receive orders "Establish the vacuum", "Vent the cahmber" or "STOP" and transmit from Raspi the states of the pumps and valves and sensors)
- I would like to write continously (every 1 sec) the states of the physical data (pumps, valves, sensors values​​...) in a log file
- Also write the various control changes executed in another log file (each time a new command is sent)
- Do a continuous backup of the files (I found an example for that but not sure I brought the right way ...)
- Continuously measure the size of the files and create a new one when this size go over a certain value
- Manages different types of possible errors (here I am a bit lost ...). I have written in different .txt files Errors that may occur (Error 5501, Emergency, Stop loop interrupted,Press emergency,Critical ... for example) with the severity level (information, critical ...), the possible cause, and a message. Under certain conditions in my 5 different states, these errors are introduced and must be recorded in an error log.

My issues relate to the global variables defined in these threads. I am not sure whether the right values ​​are passed between my threads, managing the backup, the log files and the error log...

If someone can find the time to advise me regarding the best way to structure my code, I would really appreciate.

Please find attached to the link below what I have already written as well as the organigram that rawly structure my codes.

http://getwebb.org/v/HqGkxuQe


Thank you for your time and consideration.

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 3:32 am

threads are fun, but you might be able to get away with something simpler:

main loop that waits on commands from TCP/IP using select

Code: Select all

while 1:
    (r,w,x) = select([port],(),(),1)
    // handle the input (if any), check the state of the sensors, log to disk, actuate outputs if needed...
Simple class to
- open a file
- provide a "write line" function
- count lines and re-open file as needed in "write line"
should take care of logging and rolling over

Functions to set the state of the various controls which call the log function first (i.e. takes care of logging all commands)

cron-job which rsync's your log directory periodically for log-file backup

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 9:40 am

Hello,
huge system.
When using threads, I usually establish an event queue as an input for the state machine. In a very simple implementation, just write 'commands' as strings. This allows also for an easy test of the state machine, as the events can be provided by a test driver.
In a more complex implementation, write a 'eventHandler' class, using queue under the hood, but also providing some error messages if queue size too long and logging for the actions.
For the state machine, there is a great statemachine compiler (http://smc.sourceforge.net/). Could be useful to implement timeout-events for the states: when machine is longer than N secs in state, then timeout is fired. For technical systems, this allows easy error detection.
The backup of files is usually a task outside of the application code. Cron jobs as already proposed are a good choice.
Regards,
Gerhard

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 12:28 pm

The odd thing to me is the choice of Python over C.

Neercs
Posts: 13
Joined: Mon Oct 05, 2015 1:47 pm

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 1:45 pm

Hi everybody and thank you for your answers,

As I wrote in my first post I am totally new with Python and Raspberry Pi (and also not very good with coding techniques), so there are several programming concepts you mentioned with which I am totally ignorant (cron jobs, queues...)

I got a look at cron jobs/tabs for the backup files, I think I understand how to implement it now, thanks for the tip.

Concerning the global structure of my code :

For the state machine, I am trying to execute the .jar file from the statemachine compiler (http://smc.sourceforge.net/) you sent me ghp, but nothing pop up... The java tool kit is installed on my Windows PC... I even tried to convert the .jar file into an .exe using launch4j but I have the same result, nothing happened when I double-click. I also tried to run it from the Command prompt window, but I didn't manage to do it :oops: :cry: :roll: ... I found others State Machine Compiler (Qfsm for example) that works on my pc but they do not enable to compile in Python... I think I don't understand how the SMC compiler works...
The odd thing to me is the choice of Python over C.
joan, I decided to choose Python over C because I wanted to use RPi.GPIO library but clearly when I made this choice I did not know which language would best suit for my project... What is the reasons why you would have chosen C and not Python ?

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 2:12 pm

Hello,
just tried to download the smc compiler and found that the 'root' element is an ant wrapper. Download the zip file smc_6_6_0.zip instead and extract. In the bin folder, there is smc_6_6_0\bin\Smc.jar.
A sample command line is:
java -jar smc_6_6_0\bin\Smc.jar -python -d . smc_6_6_0\examples\Python\EX3\AppClass.sm
Works also on jessie.
(The ant wrapper is useful if you use ant to build and deploy your applications. I develop all my apps in eclipse with pydev and then build and deploy them using ant.)

Good luck,
Gerhard

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 2:27 pm

Neercs wrote: ...
The odd thing to me is the choice of Python over C.
joan, I decided to choose Python over C because I wanted to use RPi.GPIO library but clearly when I made this choice I did not know which language would best suit for my project... What is the reasons why you would have chosen C and not Python ?
I expect the customer would want a robust, reliable, testable program. They are not attributes I would personally associate with Python.

robbes
Posts: 145
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Finite State Machine with multitasking threads

Sun Nov 22, 2015 7:01 pm

As another option, you might look at http://oosmos.com/
This gives you an FSM in C/C++ with threadless concurrency.

Neercs
Posts: 13
Joined: Mon Oct 05, 2015 1:47 pm

Re: Finite State Machine with multitasking threads

Thu Nov 26, 2015 6:11 am

Hi everybody,

Can someone help me regarding my issues with multitasking and FSM, I really struggle with these notions in Python. I do not have the programming skills to implement this and lost a lot of time and hairs trying things that doesn't work...

http://getwebb.org/v/rMyV3xUT

I would like to Skype with the kind person who would accept to help me....

robinson69
Posts: 1
Joined: Thu Nov 26, 2015 3:46 pm

Re: Finite State Machine with multitasking threads

Thu Nov 26, 2015 4:09 pm

Hello,

I reply in french because it is a little bit off-topic, sorry.

Bonjour,

J'ai vu tes demandes sur codeur.com. Je t'envoie juste un petit message amical pour te dire que ce n'est pas une heure de travail qui serait nécessaire, mais beaucoup plus. En effet, tu travailles sur un projet de grande ampleur et malheureusement, en matière de développement, il faut passer au moins autant de temps pour mettre au point le projet que pour faire le plus gros du développement.
D'ailleurs, on se rapproche souvent de la règle des 80-20: il faut 20% du temps pour faire 80% des fonctionnalités et 80% du temps pour faire les 20% restant (en gros, la mise au point).
Tu risques d'avoir du mal à trouve de l'aide sur codeur.com, mais j'espère que je me trompe.

Bon courage,
Cordialement,

Nicolas

Neercs
Posts: 13
Joined: Mon Oct 05, 2015 1:47 pm

Re: Finite State Machine with multitasking threads

Mon Dec 21, 2015 11:51 pm

Hi everybody,

I have continued with my code (on the base of a code sent to me by ghp) and as I am still struggling a lot I am looking for someone to help me to build, debug, improve... what is already done : http://getwebb.org/v/wvOAPj5B

I am especially wondering why when I run the code the print lines I have inserted in different threads running at the same time are displayed on the same line in the command prompt window.
How to properly convert data from my ADC...
Why when I display data on my screen and kill the code the data are still displayed, and when I run the code again I got some weird characters that stay until the Raspberry pi is unplugged...

If someone has the patience to help regarding this I would really appreciate.

Yours sincerely

Return to “Python”