Humidistat
Arduino firmware for a humidistat (humidity controller)
Loading...
Searching...
No Matches
FlowController.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include "FlowController.h"
3#include "Controller.h"
4
5FlowController::FlowController(const FlowSensor *fs, const ConfigStore *cs, uint8_t solenoidPin, uint8_t pwmRes)
6 : Controller(cs, cs->FC_Kp, cs->FC_Ki, cs->FC_Kd, cs->FC_Kf, cs->FC_dt, cs->S_lowValue, 1, 0, cs->S_lowValue),
7 fs(*fs), solenoidPin(solenoidPin), pwmRes(pwmRes) {}
8
10 if (millis() - sensorLastRead < cs.FC_dt)
11 return;
12 sensorLastRead = millis();
13
15
16 // Read flowrate (and update PV if not NaN)
17 double flowrate = fs.readFlowrate();
18 if (!isnan(flowrate))
19 pv = flowrate;
20
21 // Run PID cycle if active
22 pid.compute();
23
24 // Actuate solenoid (convert normalised double CV to integer PWM value)
25 analogWrite(solenoidPin, static_cast<int>(cv * ipow(2, pwmRes)));
26}
27
Base class for a controller. Owns a PID instance, and holds a reference to a ConfigStore instance.
Definition Controller.h:11
bool active
Definition Controller.h:19
double pv
Process variable.
Definition Controller.h:21
const ConfigStore & cs
Definition Controller.h:14
double cv
Control variable.
Definition Controller.h:23
unsigned long sensorLastRead
Definition Controller.h:16
const uint8_t pwmRes
const FlowSensor & fs
FlowController(const FlowSensor *fs, const ConfigStore *cs, uint8_t solenoidPin, uint8_t pwmRes)
Constructor.
const uint8_t solenoidPin
Read flow rate using a Omron D6F-P0010 MEMS flow sensor.
Definition FlowSensor.h:9
double readFlowrate() const
Read the flow rate.
Definition FlowSensor.cpp:8
void setAuto(bool inAuto)
Set the mode of the controller.
Definition PID.cpp:52
bool compute()
Run a cycle of the PID loop.
Definition PID.cpp:17
double cvMin
Definition PID.h:34
void setGains(double Kp, double Ki, double Kd, double Kf, uint16_t dt)
Set the gains and timestep.
Definition PID.cpp:59
constexpr T ipow(T base, unsigned int pow)
Constexpr function for computing integer power.
Definition imath.h:10
const uint8_t pwmRes
Definition main.cpp:44
Config store containing variables, which can be stored in EEPROM.
Definition EEPROMConfig.h:7
uint16_t FC_dt
double FC_Kp
Flow controller PID parameters.
double S_lowValue
Minimum solenoid duty cycle (deadband)
uint16_t dt
Global interval for PID/logger (based on polling rate of sensor, in millis)