-
Notifications
You must be signed in to change notification settings - Fork 3
/
pid.h
69 lines (61 loc) · 1.25 KB
/
pid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include "Arduino.h"
#include "config.h"
#include "reference.h"
#include "stepper.h"
#include "pressure.h"
#include <PID_v1.h>
double setpoint=0, input=0, output=0;
double kp=K_P,ki=K_I,kd=K_D;
PID pid(&input, &output, &setpoint, K_P, K_I, K_D, P_ON_M, DIRECT);
double reference = 0;
void pidSetup(){
output = 0;
pid.SetMode(AUTOMATIC);
pid.SetSampleTime(SAMPLE_TIME);
pid.SetOutputLimits(-1000,1000);
stepperUpdateSpeed(0);
}
void pidReset(){
pid.SetMode(MANUAL);
stepperGoTo(0,MIN_FREQ);
stepperDisable();
pid.SetMode(AUTOMATIC);
}
void pidRun(){
reference = getReference();
float pressure = getPressure();
input = pressure - reference;
if(pressure>MAX_PRESSURE){
pidReset();
}
if(reference == PEEP){
pid.SetMode(MANUAL);
output = 0;
stepperGoTo(0,HOMING_FREQ);
stepperDisable();
pid.SetMode(AUTOMATIC);
}else{
stepperEnable();
if(pid.Compute())
stepperUpdateSpeed(output);
}
}
void pidSetP(double v){
kp = v;
pid.SetTunings(kp,ki,kd);
}
void pidSetI(double v){
ki = v;
pid.SetTunings(kp,ki,kd);
}
void pidSetD(double v){
kd = v;
pid.SetTunings(kp,ki,kd);
}
void pidSetPID(double _p,double _i,double _d){
kp = _p;
ki = _i;
kd = _d;
pid.SetTunings(kp,ki,kd);
}