You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.1 KiB
53 lines
1.1 KiB
2 years ago
|
/**
|
||
|
* adapted from the Arduino PID library
|
||
|
*
|
||
|
* Created on 2020/01/08.
|
||
|
*/
|
||
|
|
||
|
#ifndef ARDUINOPID_H
|
||
|
#define ARDUINOPID_H
|
||
|
|
||
|
#include <stdint.h>
|
||
|
|
||
|
enum PIDCtlMode {
|
||
|
PID_MANUAL = 0,
|
||
|
PID_AUTOMATIC = 1,
|
||
|
};
|
||
|
|
||
|
enum PIDDirection {
|
||
|
PID_DIRECT = 0,
|
||
|
PID_REVERSE = 1,
|
||
|
};
|
||
|
|
||
|
struct PID {
|
||
|
/*working variables*/
|
||
|
uint32_t lastTime;
|
||
|
float Input, Output, Setpoint;
|
||
|
float ITerm, lastInput;
|
||
|
float kp, ki, kd;
|
||
|
uint32_t SampleTime; // millis
|
||
|
float outMin, outMax;
|
||
|
enum PIDCtlMode ctlMode; // false
|
||
|
enum PIDDirection controllerDirection;
|
||
|
};
|
||
|
|
||
|
#define PID_DEFAULT() { .SampleTime = 1000, .ctlMode=PID_MANUAL, .controllerDirection=PID_DIRECT }
|
||
|
|
||
|
void PID_Compute(struct PID *self, float Input);
|
||
|
|
||
|
void PID_SetTunings(struct PID *self, float Kp, float Ki, float Kd);
|
||
|
|
||
|
void PID_SetSampleTime(struct PID *self, uint32_t NewSampleTime);
|
||
|
|
||
|
void PID_SetOutputLimits(struct PID *self, float Min, float Max);
|
||
|
|
||
|
void PID_SetCtlMode(struct PID *self, enum PIDCtlMode Mode);
|
||
|
|
||
|
void PID_Initialize(struct PID *self);
|
||
|
|
||
|
void PID_SetSetpoint(struct PID *self, float Setpoint);
|
||
|
|
||
|
void PID_SetControllerDirection(struct PID *self, enum PIDDirection Direction);
|
||
|
|
||
|
#endif //ARDUINOPID_H
|