-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllerpid.cpp
135 lines (112 loc) · 3.03 KB
/
controllerpid.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "controllerpid.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
ControllerPID::ControllerPID() : Controller()
{
gainI = 0;
gainD = 0;
samplingFreq = 10;
integral = 0;
derivative = 0;
previousError = 0;
}
ControllerPID::ControllerPID(int outputMin_deg, int outputMax_deg,
double gainP, double gainI, double gainD)
: Controller(outputMin_deg, outputMax_deg, gainP)
{
this->gainI = gainI;
this->gainD = gainD;
samplingFreq = 10;
integral = 0;
derivative = 0;
}
void ControllerPID::computeError()
{
previousError = error;
error = desPos_px - curPos_px;
}
/**
Performs the integral correction for the current position.
@return controller proportional correction as integer.
*/
double ControllerPID::integralCorrection(double delta)
{
integral = integral + gainI*error*delta;
if(integral > UPPERLIMIT/1.5)
integral = UPPERLIMIT/1.5;
else if(integral < LOWERLIMIT/1.5)
integral = LOWERLIMIT/1.5;
return integral;
}
/**
Performs the derivative correction for the current position.
@return controller proportional correction as integer.
*/
double ControllerPID::derivativeCorrection(double delta)
{
double errorDiff = error - previousError;
double absErrorDiff = abs(errorDiff);
if (absErrorDiff > 1)
derivative = gainD * errorDiff / delta;
else
derivative = 0;
return derivative;
}
/**
Performs PID position control, uses sampling frequency for frame delta
calculation
*/
int ControllerPID::positionControl(int desPox_px, int curPos_px)
{
setDesiredPos_px(desPox_px);
double frameDelta = 1 / samplingFreq;
return positionControl(curPos_px, frameDelta);
}
/**
Performs PID position control that sets the desired position.
The output is normalized to work within the application's requirements.
@return normalized control signal.
*/
int ControllerPID::positionControl(int desPos_px, int curPos_px, double frameDelta)
{
setDesiredPos_px(desPos_px);
return positionControl(curPos_px, frameDelta);
}
/**
Performs PID position control.
The output is normalized to work within the application's requirements.
@return normalized control signal.
*/
int ControllerPID::positionControl(int curPos_px, double frameDelta)
{
setCurrentPos_px(curPos_px);
computeError();
double controlSignal = proportionalCorrection() + \
integralCorrection(frameDelta) + derivativeCorrection(frameDelta);
controlSignal = clampSaturation(controlSignal);
//if(abs(error) <= BALL_RAD)
// controlSignal = 0;
//printf("Derivative: %0.2f, Integral: %0.2f, Prop: %0.2f\n",
// derivative, integral, GetGainP()*error);
return normalizeData(controlSignal);
}
void ControllerPID::setGainI(double gainI)
{
this->gainI = gainI;
}
void ControllerPID::setGainD(double gainD)
{
this->gainD = gainD;
}
//Getters
double ControllerPID::getGainI()
{
return gainI;
}
//Getters
double ControllerPID::getGainD()
{
return gainD;
}