-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPIDLoop.cpp
More file actions
280 lines (221 loc) · 8.09 KB
/
PIDLoop.cpp
File metadata and controls
280 lines (221 loc) · 8.09 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "PIDLoop.h"
PIDLoop::PIDLoop() //:
//filter()
{
k_p_Angle = .01;
k_i_Angle = .001;
k_d_Angle = .001;
p_Angle = 0;
i_Angle = 0;
d_Angle = 0;
angle_error = 0;
angleOutput = 0;
last_angle_error = 0;
angleMaxError = 2;
iteration_time = .005;
k_p_Y = .025;
k_i_Y = .001;
k_d_Y = .001;
p_Y = 0;
i_Y = 0;
d_Y = 0;
y_error = 0;
last_y_error = 0;
yOutput = 0;
yMaxError = 6;
k_p_X = .05;
k_i_X = .05;
k_d_X = .05;
p_X = 0;
i_X = 0;
d_X = 0;
x_error = 0;
last_x_error = 0;
xOutput = 0;
xMaxError = 3;
k_p_YEnc = .05;
k_i_YEnc = .05;
k_d_YEnc = .05;
p_YEnc = 0.0;
i_YEnc = 0.0;
d_YEnc = 0.0;
yEnc_error = 0.0;
last_yEnc_error = 0.0;
yEncOutput = 0.0;
yEncMaxError = 2;
}
void PIDLoop::resetPIDAngle() { //reset angle pid values
p_Angle = 0;
i_Angle = 0;
d_Angle = 0;
}
void PIDLoop::resetPIDX() { //reset x pid values
p_X = 0;
i_X = 0;
d_X = 0;
}
void PIDLoop::resetPIDY() { //reset y pid values
p_Y = 0;
i_Y = 0;
d_Y = 0;
}
void PIDLoop::setAngle(float pAngleInput, float iAngleInput, float dAngleInput) { //set angle PID constants
k_p_Angle = pAngleInput;
k_i_Angle = iAngleInput;
k_d_Angle = dAngleInput;
}
void PIDLoop::setX(float pXInput, float iXInput, float dXInput) { //set x PID constants
k_p_X = pXInput;
k_i_X = iXInput;
k_d_X = dXInput;
}
void PIDLoop::setY(float pYInput, float iYInput, float dYInput) { //set y PID constants
k_p_Y = pYInput;
k_i_Y = iYInput;
k_d_Y = dYInput;
}
float PIDLoop::PIDAngle(float angleOffset, float desiredAngle) {
//put in separate loop - not a while loop - keep checking and updating every runthrough of the normal loop - boolean for if this is running to stop you from manually moving the robot while the loop is running
std::ofstream logger; logger.open("/var/loggerFile.txt", std::ofstream::out); //start logger
logger << "Loop entered\n";
angle_error = angleOffset - desiredAngle; //calculate error
angle_error = fabs(angle_error) > 180 ? 180 - angle_error : angle_error; //scale error to take shortest path
if (desiredAngle == 0 && angleOffset > 180) {
angle_error = angleOffset - 360;
}
p_Angle = k_p_Angle * angle_error; //calculate p
i_Angle += k_i_Angle * (angle_error * iteration_time); //calculate i
d_Angle = k_d_Angle * ((angle_error - last_angle_error) / iteration_time); //calculate d
angleOutput = p_Angle + i_Angle + d_Angle; //calculate output
last_angle_error = angle_error; //set last angle error for d value
angleOutput = fabs(angleOutput) < .14 ? std::copysign(.14, angleOutput) : angleOutput; //if angleOutput is below min, set to min
angleOutput = fabs(angleOutput) > .9 ? std::copysign(.9, angleOutput) : angleOutput; //if angleOutput is above max, set to max
//angleOutput = angle_error < 0 ? angleOutput : -angleOutput;
if (fabs(angle_error) < Constants::angleErrorLimit) { //if done moving
i_Angle = 0;
angleOutput = 0;
}
angleOutput = -angleOutput;
logger << p_Angle << " " << angle_error << " " << angleOutput << "\n"; //output to log file
//frc::Wait(iteration_time);
logger.close();
/*SmartDashboard::PutNumber("Accumulated i", i_Angle);
SmartDashboard::PutNumber("Desired Angle", desiredAngle);
SmartDashboard::PutNumber("angleOffset", angleOffset);
SmartDashboard::PutNumber("angle_error", angle_error);*/
return angleOutput;
}
/*float PIDLoop::PIDX(float distance, float angleOffset, float cameraOffset) {
float k_p_X = .05;
float k_i_X = .05;
float k_d_X = .05;
float p_X;
float i_X = 0;
float d_X;
float x_error;
float last_x_error = 0;
float xOffset;
float xOutput;
float xMaxError = 3;
std::ofstream logger; logger.open("/var/loggerFile.txt", std::ofstream::out);
logger << "Loop entered\n";
xOffset = distance * (sin(angleOffset) - (cameraOffset / 2)); //math is currently on my phone but will be put on google drive and in notebook
x_error = xOffset;
p_X = k_p_X * x_error;
i_X += k_i_X * (x_error * iteration_time);
d_X = k_d_X * ((x_error - last_x_error) / iteration_time);
xOutput = p_X + i_X + d_X;
last_x_error = x_error;
xOffset = distance * (sin(angleOffset) - (cameraOffset / 2)); //math is currently on my phone but will be put on google drive and in notebook
x_error = xOffset;
xOutput = 0;
frc::Wait(iteration_time);
logger.close();
return 0;
}*/
float PIDLoop::PIDX(float angleToGear) {
std::ofstream logger; logger.open("/var/loggerFile.txt", std::ofstream::out); //open logger
logger << "Loop entered\n";
x_error = angleToGear; //error value
p_X = k_p_X * x_error; //calculate p
i_X += k_i_X * (x_error * iteration_time); //calculate i
d_X = k_d_X * ((x_error - last_x_error) / iteration_time); //calculate d
xOutput = p_X + i_X + d_X; //calculate output
last_x_error = x_error; //set last x error for d value
xOutput = fabs(xOutput) > .7 ? std::copysign(.7, xOutput) : xOutput; //if xOutput is above max, set to max
xOutput = fabs(xOutput) < .2 ? std::copysign(.2, xOutput) : xOutput; //if xOutput is below min, set to min
if (fabs(x_error) < xMaxError) { //if done
xOutput = 0;
i_X = 0;
}
//frc::Wait(iteration_time);
//frc::Wait(.005);
logger.close(); //close logger
//SmartDashboard::PutNumber("x_error", x_error);
return xOutput;
}
float PIDLoop::PIDY(float lDistance, float rDistance) {
float averageDistance;
std::ofstream logger; logger.open("/var/loggerFile.txt", std::ofstream::out); //start logger
logger << "Loop entered\n";
//averageDistance = filter.ultrasonicFilter(lDistance, rDistance);
averageDistance = ultrasonicFilter(lDistance, rDistance);
if (averageDistance == -1) {
return -1;
}
y_error = averageDistance - 12; //error - stop about a foot away
p_Y = k_p_Y * y_error; //calculate p
i_Y += k_i_Y * (y_error * iteration_time); //calculate i
d_Y = k_d_Y * ((y_error - last_y_error) / iteration_time); //calculate d
yOutput = p_Y + i_Y + d_Y; //calculate yOutput
last_y_error = y_error; //set last y error for d value
yOutput = fabs(yOutput) > .7 ? std::copysign(.7, yOutput) : yOutput; //if above max set to max
yOutput = fabs(yOutput) < .2 ? std::copysign(.2, yOutput) : yOutput; //if below min set to min
if (y_error < yMaxError) { //if able to stop
yOutput = 0;
i_Y = 0;
}
//frc::Wait(iteration_time);
logger.close(); //close logger
/*SmartDashboard::PutNumber("y_error", y_error);
SmartDashboard::PutNumber("avgDist", averageDistance);
SmartDashboard::PutNumber("i_Y", i_Y);*/
return -yOutput;
}
float PIDLoop::PIDYEncoder(float desiredDistance, float encoderDistance) {
yEnc_error = desiredDistance - encoderDistance; //desired distance = encoder distance at t = 0 + however far the ultrasonics say we need to move at t = 0
p_YEnc = k_p_YEnc * yEnc_error; //calculate p
i_YEnc += k_i_YEnc * (yEnc_error * iteration_time); //calculate i
d_YEnc = k_d_YEnc * ((yEnc_error - last_yEnc_error) / iteration_time); //calculate d
yEncOutput = p_YEnc + i_YEnc + d_YEnc; //calculate yOutput
last_yEnc_error = yEnc_error; //set last y error for d value
yEncOutput = fabs(yEncOutput) > .7 ? std::copysign(.7, yEncOutput) : yEncOutput; //if above max set to max
yEncOutput = fabs(yEncOutput) < Constants::minForwardPower ? std::copysign(Constants::minForwardPower, yEncOutput) : yEncOutput; //if below min set to min
if (yEnc_error < yEncMaxError) { //if able to stop
yEncOutput = 0;
i_YEnc = 0;
}
SmartDashboard::PutNumber("yEnc_error", yEnc_error);
SmartDashboard::PutNumber("i_YEnc", i_YEnc);
return -yOutput;
}
void PIDLoop::resetPIDYEnc() {
p_YEnc = 0;
i_YEnc = 0;
d_YEnc = 0;
}
void PIDLoop::setYEnc(float pYEncInput, float iYEncInput, float dYEncInput) {
k_p_YEnc = pYEncInput;
k_i_YEnc = iYEncInput;
k_d_YEnc = dYEncInput;
}
float PIDLoop::ultrasonicFilter(float left, float right) { //to smooth out the ultrasonic values
float leftOutput = 0;
float rightOutput = 0;
float gain = .2;
leftOutput = gain * left + (1 - gain) * lastLeftUltrasonic; //larry math
rightOutput = gain * right + (1 - gain) * lastRightUltrasonic;
lastLeftUltrasonic = left;
lastRightUltrasonic = right;
return (leftOutput + rightOutput) / 2; //average
}