-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
1,626 additions
and
841 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "pi_controller.h" | ||
|
||
namespace dentra { | ||
namespace tion { | ||
namespace auto_co2 { | ||
|
||
void PIController::reset(float kp, float ti, int db) { | ||
this->kp_ = kp; | ||
this->ti_ = ti; | ||
this->db_ = db; | ||
this->reset(); | ||
} | ||
|
||
void PIController::reset(float kp, float ti, int db, float min, float max) { | ||
this->kp_ = kp; | ||
this->ti_ = ti; | ||
this->db_ = db; | ||
this->v_oa_min_ = min; | ||
this->v_oa_max_ = max; | ||
this->reset(); | ||
} | ||
|
||
float PIController::update(int setpoint, int current) { | ||
// 7: error from setpoint [ppm] | ||
const auto e = setpoint - current; | ||
|
||
// 8: error from setpoint, including dead band [ppm] | ||
const auto e_db = // | ||
/**/ current < setpoint - this->db_ ? e - this->db_ : | ||
/**/ current > setpoint + this->db_ ? e + this->db_ | ||
: 0; | ||
|
||
// 9: integral error [min.ppm-CO2] | ||
const float i = this->ib_ + (this->dt_() / 60.0f) * e_db; | ||
|
||
// 10: candidate outdoor airflow rate [L/s] | ||
const float v_oa_c = -this->kp_ * (e_db + (i / this->ti_)); | ||
|
||
// 11: integral error with anti-integral windup [min.ppm-CO2] | ||
if (v_oa_c < this->v_oa_min_) { | ||
this->ib_ = -this->ti_ * (e_db + (this->v_oa_min_ / this->kp_)); | ||
} else if (v_oa_c > this->v_oa_max_) { | ||
this->ib_ = -this->ti_ * (e_db + (this->v_oa_max_ / this->kp_)); | ||
} else { | ||
this->ib_ = i; | ||
} | ||
|
||
// 12: outdoor airflow rate [L/s] | ||
const float v_oa = -this->kp_ * (e_db + (this->ib_ / this->ti_)); | ||
|
||
return v_oa; | ||
} | ||
|
||
} // namespace auto_co2 | ||
} // namespace tion | ||
} // namespace dentra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cmath> | ||
|
||
#include "utils.h" | ||
|
||
namespace dentra { | ||
namespace tion { | ||
namespace auto_co2 { | ||
|
||
/// @brief PI Controller. | ||
/// @link https://www.sciencedirect.com/science/article/pii/S0378778823009477 | ||
class PIController { | ||
public: | ||
/// @param kp proportional gain [L/s.ppm-CO2] | ||
/// @param ti integral gain [min] | ||
/// @param db dead band [ppm] | ||
/// @param v_oa_min minimum outdoor airflow rate [L/s] | ||
/// @param v_oa_max maximum outdoor airflow rate [L/s] | ||
PIController(float kp, float ti, int db = 0, float min = NAN, float max = NAN) | ||
: kp_(kp), ti_(ti), db_(db), v_oa_min_(min), v_oa_max_(max) {} | ||
|
||
/// @param setpoint CO2 setpoint [ppm] | ||
/// @param current CO2 concentration [ppm] | ||
/// @return outdoor airflow rate [L/s] | ||
float update(int setpoint, int current); | ||
|
||
void set_min(float min) { this->v_oa_min_ = min; } | ||
void set_max(float max) { this->v_oa_min_ = max; } | ||
|
||
/// @brief Resets integral error. | ||
void reset() { this->ib_ = 0; } | ||
/// @brief Resets Kp, Ti, db without touching min and max | ||
void reset(float kp, float ti, int db); | ||
void reset(float kp, float ti, int db, float min, float max); | ||
|
||
protected: | ||
/// proportional gain [L/s.ppm-CO2] | ||
float kp_; | ||
/// integral gain [min] | ||
float ti_; | ||
/// dead band [ppm] | ||
int db_; | ||
/// minimum outdoor airflow rate [L/s] | ||
float v_oa_min_; | ||
/// maximum outdoor airflow rate [L/s] | ||
float v_oa_max_; | ||
/// integral error with anti-integral windup [min.ppm-CO2]. | ||
float ib_{}; | ||
/// last time used in dt_() calculation. | ||
uint32_t last_time_{}; | ||
|
||
/// time step [s]. | ||
float dt_() { | ||
const auto now = tion::millis(); | ||
const float dt = this->last_time_ == 0 ? 0.0f : (now - this->last_time_) * 0.001f; | ||
this->last_time_ = now; | ||
return dt; | ||
} | ||
}; | ||
|
||
} // namespace auto_co2 | ||
} // namespace tion | ||
} // namespace dentra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.