Skip to content

Software

mstemmer edited this page Jan 3, 2021 · 14 revisions

Overview

Jump to:
Sensors PID Control
Status LEDs
Plotly Dash
Multiple Processes

The idea of the software was to keep it as modular as possible for future extensions and keep it as basic as possible, since it is an ongoing Python learning project.
For this reason I decided to spawn distinct processes:

  1. The Controller (controller.py); only dedicated to measure Temperature & Humidity, adjusting the heater with a software PID controller to the set points and adjusting the status LEDs and buzzer.
  2. The Brood Lord (brood_lord.py); reads the incubation program and schedules the set point changes; it also schedules the egg turning
  3. The Output (output.py); collects all the data from the controller via a process queue and writes it into a csv file.

I also wanted to be able to resume the incubation program, in case the incubator has to be switched off in between or some problems arise. Therefore the `--init` flag writes the start time into time_init.json. When the flag is not used, the program resumes from that time point.

All the variables needed for hatchling are stored in json files. So there is no need to tweak the python code right away. `settings.json` contains all the hardware related info. The GPIO pins (BCM format), the names of the the data folder and output csv file, the LED/buzzer status modes, PID parameters and number of steps the motor should turn. The LED status section defines when the LEDs should switch from green to blue to red to red + buzzer. They work on either too high or too low values. E.g.: green=0.25; LED=green, if temperature between +- 0.25°C around set point.

Sensors

I use two DHT22 sensors. This type of sensor needs a 2 second sleep time, so in order to increase speed, I poll them alternately. So, I get values every second. The sensors sometime dont read correctly and output values that dont make any sense. So I am filtering the far off values. Sometimes there are also read failures due to the hardware. I tried to catch all these errors in the python script in order to ensure smooth running. It is important that this incubator runs for 3 weeks without interruption.

PID control

This was probably the most tricky part. It took me quite long the implement the PID controller, because I didn't just want to buy a hardware solution. I tried a few different packages, but then found simple-pid. It works nicely and comes with great options. Check out their page!

PID Tuning

This is the actual difficult and bit annoying part. I had no idea how a PID actually works and what values I need for my box, but then stumbled over Emile's Home Brewing Project. He has great documentation and the idea is not too far away from what I was building. So have a look at that page!

To start off, you should start warming up the incubator without any PID control and just let it settle (no more heating up due to cooling from outside). This can take a while depending on your system. Since my heater is actually too powerful, I let it run at 25% power. It then settles at around 40°C. You find a little python script under docs/ to help you with calculating the PID values from your data. These values worked pretty well for me, but these are for sure very system related. Still, there is room of improvement. E.g: the temperature fluctuates around the set point by +-0.2°C. I think that's pretty good, but bothers me that it is not perfect...However this might also be coupled to the relatively slow sensor reading (1Hz). The PID controller would work much better with a higher sampling rate. Using faster temperature sensors could do the trick in the future.

Status LEDs

The LEDs and buzzer are controlled via a 74HC595 shift register. The update of the shift register uses a decimal system, which encodes the different modes. Find the encoding table under the docs/ and here.

Plotly Dash

This is just a super helpful resource to easily create beautiful web interfaces. I just scratched the surface so far, but I already like it a lot. Just so much simpler than creating a GUI with Qt or something similar and it is already running in the browser, so easily accessible from basically everywhere. Look here for more info.
I found it easier to run this server independently to hatchling, so there are no conflicts. Also it is not needed, but highly recommended. However, hatchling runs just fine without dash and there are still the LEDs and the command line terminal to see all the values.
So, Dash is setup to monitor the hatchling output csv file calls its values. I changed the default IP of the server to 0.0.0.0 in order to spawn the server as local host. This way one can access the page easily from another device using the IP of the RaspberryPi and port 8050.
The server continuously updates and so data can be monitored live.

Multiple Processes

Personally, I run my RaspberryPi only in CLI mode and access it via ssh from another Linux machine. This way I dont need a monitor or keyboard attached to it and just need a power plug to run the incubator. To make sure hatchling keeps on running when disconnecting, it should be run in the background. I am a big fan of tmux to manage various processes. So you can then start hatchling in one and the dash monitor in another tmux window. After that you can detach from the session and reconnected whenever needed.