PID controllers hierarchy revision
This commit is contained in:
46
src/Logic/PIDController.hpp
Normal file
46
src/Logic/PIDController.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// This is a personal academic project. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
|
||||
|
||||
#include "Motor/BrushedMotor.hpp"
|
||||
#include "SavedPidRegulator.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
|
||||
class PIDController : public SavedPidRegulator {
|
||||
public:
|
||||
PIDController(float p, float i, float d, BrushedMotor *rotor,
|
||||
const char *name = "PID", uint16_t dt = 10)
|
||||
: SavedPidRegulator(p, i, d, name, dt) {
|
||||
assert(rotor != nullptr);
|
||||
_rotor = rotor;
|
||||
this->_dt = dt;
|
||||
setLimits(0, _rotor->maxDuty());
|
||||
_pid_timer = millis();
|
||||
ESP_LOGI(_tag, "PID %s initialized with params (%f, %f, %f)", name, Kp, Ki, Kd);
|
||||
}
|
||||
|
||||
void enable() {
|
||||
_rotor->enable();
|
||||
}
|
||||
|
||||
void disable() {
|
||||
_rotor->disable();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
if (millis() - _pid_timer >= _dt) {
|
||||
_rotor->setDuty((uint32_t) getResultTimer());
|
||||
_pid_timer = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void setRotorDuty(uint32_t duty) {
|
||||
/* Used only for test purposes */
|
||||
_rotor->setDuty(duty);
|
||||
}
|
||||
|
||||
private:
|
||||
uint16_t _dt;
|
||||
BrushedMotor *_rotor = nullptr;
|
||||
uint32_t _pid_timer = 0;
|
||||
};
|
||||
Reference in New Issue
Block a user