Control application: Difference between revisions

From Aquarium-Control
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 22: Line 22:
* Schedule checks to limit the hour of day of operating certain actuators
* Schedule checks to limit the hour of day of operating certain actuators
* [[Communication with HW Watchdog]]
* [[Communication with HW Watchdog]]
* [[Temperature gradient calculation]]
The application is written in Rust and is [[automatic start by using systemd| automatically started using systemd]].
The application is written in Rust and is [[automatic start by using systemd| automatically started using systemd]].


Line 30: Line 31:
There is a [[Release procedure|release procedure]] which is highly recommended to be followed also by anyone developing the SW further.
There is a [[Release procedure|release procedure]] which is highly recommended to be followed also by anyone developing the SW further.


== Architecture ==
= Architecture =
=== Startup of the application ===
== Startup of the application ==
The startup uses a two-layer approach:
The startup uses a two-layer approach:
# main() - Simple error handler wrapper that calls run() and exits with code 1 on errors
# main() - Simple error handler wrapper that calls run() and exits with code 1 on errors
# run() - The actual initialization orchestrator that performs the major steps
# run() - The actual initialization orchestrator that performs the major steps


==== Phase 1: Command Line & Configuration ====
=== Phase 1: Command Line & Configuration ===
* Parses command line arguments ("help", "version", config file)
* Parses command line arguments ("help", "version", config file)
* Sets the default config file: <code>/etc/aquarium_control/aquarium_control.toml</code>
* Sets the default config file: <code>/etc/aquarium_control/aquarium_control.toml</code>
* Loads configuration and creates <code>ExecutionConfig</code> (boolean flags determining which modules to run)
* Loads configuration and creates <code>ExecutionConfig</code> (boolean flags determining which modules to run)


==== Phase 2: System Setup ====
=== Phase 2: System Setup ===
* Initializes the logging system
* Initializes the logging system
* Logs version information and executable hash
* Logs version information and executable hash
Line 47: Line 48:
* Publishes process ID to file for client communication
* Publishes process ID to file for client communication


==== Phase 3: Database & Hardware ====
=== Phase 3: Database & Hardware ===
* Connects to database (MariaDB)
* Connects to database (MariaDB)
* Creates component-specific SQL interfaces (<code>Schedule</code>, <code>Balling</code>, <code>Refill</code>, <code>Heating</code>, <code>Feed</code>, <code>Data</code>)
* Creates component-specific SQL interfaces (<code>Schedule</code>, <code>Balling</code>, <code>Refill</code>, <code>Heating</code>, <code>Feed</code>, <code>Data</code>)
Line 60: Line 61:
** Relay actuator (Controllino)
** Relay actuator (Controllino)


==== Phase 4: Communication Channels ====
=== Phase 4: Communication Channels ===
* Creates the <code>Channels</code> struct containing ~30+ inter-thread communication channels
* Creates the <code>Channels</code> struct containing ~30+ inter-thread communication channels
* Uses custom <code>AquaSender</code>/<code>AquaReceiver</code> wrappers for MPSC channels
* Uses custom <code>AquaSender</code>/<code>AquaReceiver</code> wrappers for MPSC channels
* Establishes bidirectional and unidirectional message paths between all modules
* Establishes bidirectional and unidirectional message paths between all modules


==== Phase 5: Component Initialization ====
=== Phase 5: Component Initialization ===
Instantiates all high-level control components:
Instantiates all high-level control components:
* data logger
* data logger
Line 78: Line 79:
* watchdog
* watchdog
* schedule checker
* schedule checker
* temperature gradient calculation
* IPC messaging system
* IPC messaging system


==== Phase 6: Thread Spawning ====
=== Phase 6: Thread Spawning ===
* Uses <code>std::thread::scope</code> for guaranteed cleanup
* Uses <code>std::thread::scope</code> for guaranteed cleanup
* Spawns threads conditionally based on <code>ExecutionConfig</code> flags
* Spawns threads conditionally based on <code>ExecutionConfig</code> flags
* Critical: A 3-second startup delay allows sensors to acquire first data before control loops activate
* Critical: A 3-second startup delay allows sensors to acquire first data before control loops activate


==== Architecture of thread configuration ====
=== Architecture of thread configuration ===
* The <code>Channels</code> module (<code>src/launch/channels.rs</code>) acts as a central wiring diagram, with the Signal Handler serving as the main event coordinator.
* The <code>Channels</code> module (<code>src/launch/channels.rs</code>) acts as a central wiring diagram, with the Signal Handler serving as the main event coordinator.
* The <code>ExecutionConfig</code> decouples configuration from execution - threads only receive flags about which modules are active, not the full config - this also avoids ownership issues.
* The <code>ExecutionConfig</code> decouples configuration from execution - threads only receive flags about which modules are active, not the full config - this also avoids ownership issues.


=== Threads ===
== Threads ==
The application spawns the following threads:
The application spawns the following threads:
{| class="wikitable" style="margin:auto"
{| class="wikitable" style="margin:auto"
Line 135: Line 137:
|-
|-
| publish_pid || Writing PID to lock file
| publish_pid || Writing PID to lock file
|-
| temperature_gradient || [[Temperature gradient calculation]]


|}
|}


== Measurement using Atlas Scientific circuits ==
= Measurement using Atlas Scientific circuits =
1. Core Abstraction (`AtlasScientificDriver` Trait)
The core Abstraction (<code>AtlasScientificDriver</code> Trait) is defined in <code>src/sensors/atlas_scientific_driver.rs</code>, this trait serves as the common interface for all sensor types. It enforces three key behaviors:
      Defined in src/sensors/atlas_scientific_driver.rs, this trait serves as the common interface for all sensor types. It enforces three key behaviors:
* <code>send_request_to_i2c_interface</code>: Encapsulates how to trigger a measurement (e.g., constructing specific I2C commands).
      * send_request_to_i2c_interface: Encapsulates how to trigger a measurement (e.g., constructing specific I2C commands).
* <code>receive_response_from_i2c_interface</code>: Handles parsing the raw byte response from the I2C bus into a normalized floating-point value.
      * receive_response_from_i2c_interface: Handles parsing the raw byte response from the I2C bus into a normalized floating-point value.
* <code>device_init</code>: Performs hardware-specific setup (crucial for OEM type sensor circuit).
      * device_init: Performs hardware-specific setup (crucial for OEM sensors).
 
  2. Specific Implementations (The Strategies)
 
      * OEM Driver (`AtlasScientificOem`):
          * Protocol: Uses a binary, register-based protocol.
          * Initialization: Requires an explicit "Wake Up" command (writing to REG_WAKEUP) and verifies the hardware by reading the REG_DEVICE_TYPE.
          * Read Cycle: Writes a register address (e.g., REG_DATA_MSB_PH), waits for a short processing time, and reads 4 bytes.
          * Parsing: Decodes the 4-byte response as a Big Endian unsigned integer and divides it by a specific scale factor (e.g., 1000.0) to get the float value.


      * EZO Driver (`AtlasScientificEzo`):
Circuit-type-specific implementations
          * Protocol: Uses a text-based (ASCII) command protocol.
* OEM Driver (<code>AtlasScientificOem</code>):
          * Initialization: Minimal or no-op (these devices are typically always ready or auto-on).
** Protocol: Uses a binary, register-based protocol.
          * Read Cycle: Sends the ASCII character 'R', waits for a longer processing time, and reads a fixed 8-byte response.
** Initialization: Requires an explicit "Wake Up" command (writing to <code>REG_WAKEUP</code>) and verifies the hardware by reading the <code>REG_DEVICE_TYPE</code>.
          * Parsing: Validates "magic" start/end bytes (1 and 0), converts the inner ASCII string (e.g., "23.50") to a float.
** Read Cycle: Writes a register address (e.g., <code>REG_DATA_MSB_PH</code>), waits for a short processing time, and reads 4 bytes.
** Parsing: Decodes the 4-byte response as a Big Endian unsigned integer and divides it by a specific scale factor (e.g., 1000.0) to get the float value.
* EZO Driver (<code>AtlasScientificEzo</code>):
** Protocol: Uses a text-based (ASCII) command protocol.
** Initialization: Minimal or no-op (these devices are typically always ready or auto-on).
** Read Cycle: Sends the ASCII character 'R', waits for a longer processing time, and reads a fixed 8-byte response.
** Parsing: Validates "magic" start/end bytes (1 and 0), converts the inner ASCII string (e.g., "23.50") to a float.


  3. Context & Factory (`AtlasScientific` Struct)
Context and factory (<code>AtlasScientific</code> struct)
      * The main AtlasScientific struct acts as the "Context". It holds a HashMap<AquariumSignal, Box<dyn AtlasScientificDriver>>.
* The main <code>AtlasScientific</code> struct acts as the context. It holds a <code>HashMap<AquariumSignal</code>, <code>Box<dyn AtlasScientificDriver>></code>.
      * Factory Logic: The create_drivers method functions as a factory. It reads the aquarium_control.toml configuration (specifically fields like sensor_type_ph = "oem" or "ezo") and
* Factory Logic: The <code>create_drivers</code> method functions as a factory. It reads the <code>aquarium_control.toml</code> configuration (specifically fields like <code>sensor_type_ph = "oem"</code> or <code>"ezo"</code>) and
        instantiates the correct driver implementation for each sensor (Temperature, pH, Conductivity).
instantiates the correct driver implementation for each sensor (temperature, pH, conductivity).
      * Execution: The main thread loop simply calls driver.send_request... and driver.receive_response... on the active driver, unaware of whether it is communicating via binary registers
* Execution: The main thread loop calls <code>driver.send_request...</code> and <code>driver.receive_response...</code> on the active driver, unaware of whether it is communicating via binary registers or ASCII commands.
        or ASCII commands.


  Communication Flow
Communication Flow


  The architecture completely decouples protocol logic from bus I/O:
The architecture decouples protocol logic from bus I/O:
  1. Driver creates an abstraction-specific I2cRequest (containing address, write buffer, read length, delay).
# Driver creates an abstraction-specific <code>I2cRequest</code> (containing address, write buffer, read length, delay).
  2. AtlasScientific Thread sends this request via a channel to the I2cInterface Thread.
# <code>AtlasScientific</code> thread sends this request via a channel to the <code>I2cInterface</code> thread.
  3. I2cInterface Thread performs the physical I2C transaction.
# <code>I2cInterface</code> thread performs the physical I2C transaction.
  4. AtlasScientific Thread receives the raw bytes and passes them back to the Driver for parsing.
# <code>AtlasScientific</code> thread receives the raw bytes and passes them back to the driver for parsing.
# Additionally, <code>AtlasScientific</code> performs measurement value checks informing deviations to <code>Monitors</code>.  


  This design enables the system to mix and match sensor types (e.g., an OEM pH sensor with an EZO temperature sensor) purely through configuration changes.
This design enables the system to mix and match sensor types (e.g., an OEM type pH sensor circuit with an EZO type temperature sensor circuit) purely through configuration changes.


== Monitors ==
= Monitors =
The Monitors module (<code>src/watchmen/monitors.rs</code>) is a dedicated thread that aggregates and stores historical system state information.
The Monitors module (<code>src/watchmen/monitors.rs</code>) is a dedicated thread that aggregates and stores historical system state information.
It interacts with the following modules:
It interacts with the following modules:
Line 189: Line 190:


The number of samples is also limited (rolling window), to avoid overflow.
The number of samples is also limited (rolling window), to avoid overflow.
= Commissioning =
For the [[commissioning software|commissioning of the aquarium control]], there is a separate binary which resides in <code>src/bin/commissioning.rs</code>.

Latest revision as of 20:17, 30 January 2026

The main control application of Aquarium control implements the following features:

The control of the relays for operating the devices can use two different modes:

Additionally, the main control application implements the following supporting features:

The application is written in Rust and is automatically started using systemd.

The documentation of the software is available here.

Development for the main control application requires the setup of the development environment.

There is a release procedure which is highly recommended to be followed also by anyone developing the SW further.

Architecture

Startup of the application

The startup uses a two-layer approach:

  1. main() - Simple error handler wrapper that calls run() and exits with code 1 on errors
  2. run() - The actual initialization orchestrator that performs the major steps

Phase 1: Command Line & Configuration

  • Parses command line arguments ("help", "version", config file)
  • Sets the default config file: /etc/aquarium_control/aquarium_control.toml
  • Loads configuration and creates ExecutionConfig (boolean flags determining which modules to run)

Phase 2: System Setup

  • Initializes the logging system
  • Logs version information and executable hash
  • Verifies root privileges (required for hardware access)
  • Publishes process ID to file for client communication

Phase 3: Database & Hardware

  • Connects to database (MariaDB)
  • Creates component-specific SQL interfaces (Schedule, Balling, Refill, Heating, Feed, Data)
  • Validates database schema and version compatibility
  • Initializes hardware components if configured:
    • RGB LEDs
    • GPIO handlers
    • Tank level switch
    • I2C interface
    • Atlas Scientific sensors
    • DHT temperature/humidity sensors
    • Relay actuator (Controllino)

Phase 4: Communication Channels

  • Creates the Channels struct containing ~30+ inter-thread communication channels
  • Uses custom AquaSender/AquaReceiver wrappers for MPSC channels
  • Establishes bidirectional and unidirectional message paths between all modules

Phase 5: Component Initialization

Instantiates all high-level control components:

  • data logger
  • sensor manager
  • relay manager
  • food injection
  • mineral dosing (balling)
  • water injection/refill
  • heating control
  • ventilation control
  • monitors
  • watchdog
  • schedule checker
  • temperature gradient calculation
  • IPC messaging system

Phase 6: Thread Spawning

  • Uses std::thread::scope for guaranteed cleanup
  • Spawns threads conditionally based on ExecutionConfig flags
  • Critical: A 3-second startup delay allows sensors to acquire first data before control loops activate

Architecture of thread configuration

  • The Channels module (src/launch/channels.rs) acts as a central wiring diagram, with the Signal Handler serving as the main event coordinator.
  • The ExecutionConfig decouples configuration from execution - threads only receive flags about which modules are active, not the full config - this also avoids ownership issues.

Threads

The application spawns the following threads:

Overview of threads
Thread name Purpose
signal_handler Receiving signals from the operating system
schedule_check Polling the database to capture changes of the actuator channel. Informing other threads about it.
sensor_manager Data acquisition
relay_manager Actuation of relays
data_logger Recording of data
tank_level_switch Postprocessing of tank level switch position signal
refill Fresh water refill control
heating Heating control
ventilation Ventilation control
balling Balling mineral dosing control
watchdog Sending alive signal to Raspberry Pi hardware watchdog
monitors Diagnostics (not implemented as of January, 2026)
messaging Receiving communication via message queue
memory Monitoring memory utilization
ds18b20 Recording data from DS18B20 temperature sensor
feed Feed control
i2c_interface Communication via I2C
atlas_scientific Recording data from Atlas Scientific temperature sensor
dht Recording data from DHT sensor
publish_pid Writing PID to lock file
temperature_gradient Temperature gradient calculation

Measurement using Atlas Scientific circuits

The core Abstraction (AtlasScientificDriver Trait) is defined in src/sensors/atlas_scientific_driver.rs, this trait serves as the common interface for all sensor types. It enforces three key behaviors:

  • send_request_to_i2c_interface: Encapsulates how to trigger a measurement (e.g., constructing specific I2C commands).
  • receive_response_from_i2c_interface: Handles parsing the raw byte response from the I2C bus into a normalized floating-point value.
  • device_init: Performs hardware-specific setup (crucial for OEM type sensor circuit).

Circuit-type-specific implementations

  • OEM Driver (AtlasScientificOem):
    • Protocol: Uses a binary, register-based protocol.
    • Initialization: Requires an explicit "Wake Up" command (writing to REG_WAKEUP) and verifies the hardware by reading the REG_DEVICE_TYPE.
    • Read Cycle: Writes a register address (e.g., REG_DATA_MSB_PH), waits for a short processing time, and reads 4 bytes.
    • Parsing: Decodes the 4-byte response as a Big Endian unsigned integer and divides it by a specific scale factor (e.g., 1000.0) to get the float value.
  • EZO Driver (AtlasScientificEzo):
    • Protocol: Uses a text-based (ASCII) command protocol.
    • Initialization: Minimal or no-op (these devices are typically always ready or auto-on).
    • Read Cycle: Sends the ASCII character 'R', waits for a longer processing time, and reads a fixed 8-byte response.
    • Parsing: Validates "magic" start/end bytes (1 and 0), converts the inner ASCII string (e.g., "23.50") to a float.

Context and factory (AtlasScientific struct)

  • The main AtlasScientific struct acts as the context. It holds a HashMap<AquariumSignal, Box<dyn AtlasScientificDriver>>.
  • Factory Logic: The create_drivers method functions as a factory. It reads the aquarium_control.toml configuration (specifically fields like sensor_type_ph = "oem" or "ezo") and

instantiates the correct driver implementation for each sensor (temperature, pH, conductivity).

  • Execution: The main thread loop calls driver.send_request... and driver.receive_response... on the active driver, unaware of whether it is communicating via binary registers or ASCII commands.

Communication Flow

The architecture decouples protocol logic from bus I/O:

  1. Driver creates an abstraction-specific I2cRequest (containing address, write buffer, read length, delay).
  2. AtlasScientific thread sends this request via a channel to the I2cInterface thread.
  3. I2cInterface thread performs the physical I2C transaction.
  4. AtlasScientific thread receives the raw bytes and passes them back to the driver for parsing.
  5. Additionally, AtlasScientific performs measurement value checks informing deviations to Monitors.

This design enables the system to mix and match sensor types (e.g., an OEM type pH sensor circuit with an EZO type temperature sensor circuit) purely through configuration changes.

Monitors

The Monitors module (src/watchmen/monitors.rs) is a dedicated thread that aggregates and stores historical system state information. It interacts with the following modules:

  • Refill
  • Signal handler

The Monitors thread receives data from the other thread by polling the channels.

The data is transferred in specific structs ("Views"), e.g. RefillView.

Monitors contains a deduplication logic: Data is only stored when it differs from previous sample.

The number of samples is also limited (rolling window), to avoid overflow.

Commissioning

For the commissioning of the aquarium control, there is a separate binary which resides in src/bin/commissioning.rs.