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.