-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.cpp
215 lines (188 loc) · 5.73 KB
/
pid.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
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
#include "pid.h"
int32_t Bias_L,Bias_R;
int32_t encoderL, encoderR;
int32_t distance, angle;
int32_t pwm_L1_bias,pwm_L0_bias,pwm_R1_bias,pwm_R0_bias;
int8_t pwmLe, pwmRi;
// PID settings and gains
double KP_DISTANCE = 0.011;
double KI_DISTANCE = 0.0;
double KD_DISTANCE = 0.008;
double KP_ANGLE = 0.0065;
double KI_ANGLE = 0.0;
double KD_ANGLE = 0.0065;
double AnglRANGE = 30.0;
double DistRANGE = 70.0;
// Setpoints
double setPointDistance = 0;
double setPointAngle = 0;
double aCorrection = 0;
double dCorrection = 0;
double distance_double = static_cast<double>(distance);
double angle_double = static_cast<double>(angle);
AutoPID pidDistance(&distance_double, &setPointDistance, &dCorrection, -DistRANGE, DistRANGE, KP_DISTANCE, KI_DISTANCE, KD_DISTANCE);
AutoPID pidAngle(&angle_double, &setPointAngle, &aCorrection, -AnglRANGE, AnglRANGE, KP_ANGLE, KI_ANGLE, KD_ANGLE);
bool pid_done= false;
void setMotorPWM(int8_t pwmL, int8_t pwmR){
int8_t PWM[2] = {pwmL,pwmR};
// Serial.print(PWM[0]);
// Serial.print(',');
// Serial.print(PWM[1]);
// Serial.print(',');
WireWriteDataArray(31,PWM, 2);
}
void PIDwork(){
int8_t rot_L , rot_R;
rot_L = rot_R =0;
resetEncoders();
pid_done= false;
while (!pid_done){
// Read encoder values
encoderL = getLeftEncoderCounts();
encoderR = getRightEncoderCounts();
// Calculate distance and angle
distance = encoderL + (encoderR-encoderL)/2;
angle = encoderR - encoderL;
if (RTooClose()&&rot_L ==0)
{
setMotorPWM(32,-32);
delay(175);
setMotorPWM(0,0);
delay(2000);
rot_L++;
initializeEncoders();
};
if (LTooClose()&&rot_R==0
){
setMotorPWM(-32,32);
delay(1075);
setMotorPWM(0,0);
delay(2000);
rot_R++;
initializeEncoders();
};
distance_double = static_cast<double>(distance);
angle_double = static_cast<double>(angle);
if(abs(angle_double) >50000){resetEncoders();}
Serial.print(encoderL);
Serial.print(',');
Serial.print(encoderR);
Serial.print(',');
Serial.print(distance_double);
Serial.print(',');
Serial.print(angle_double);
// Run PID controllers
pidDistance.run();
pidAngle.run();
pid_done = (abs(dCorrection)<0.8)&&(abs(aCorrection)<0.5);
Serial.print(',');
Serial.print(dCorrection);
Serial.print(',');
Serial.println(aCorrection);
pwmLe = (dCorrection - aCorrection);
//pwmLe = pwmLe>=0?((pwmLe+pwm_L1_bias)*100/(100+pwm_L1_bias)):((pwmLe+pwm_L0_bias)*100/(100-pwm_L0_bias));
pwmLe = pwmLe>=0?(pwmLe*(100-pwm_L1_bias)/100+pwm_L1_bias):((pwmLe)*(100+pwm_L0_bias)/100+pwm_L0_bias);
pwmRi = (dCorrection + aCorrection);
//pwmRi = pwmRi>=0?((pwmRi+pwm_R1_bias)*100/(100+pwm_R1_bias)):((pwmRi+pwm_R0_bias)*100/(100-pwm_R0_bias));
pwmRi = pwmRi>=0?(pwmRi*(100-pwm_R1_bias)/100+pwm_R1_bias):((pwmRi)*(100+pwm_R0_bias)/100+pwm_R0_bias);
// Control the motors
setMotorPWM(pwmLe, pwmRi);
}
setMotorPWM(0, 0);
resetEncoders();
Serial.println("resetPID");
}
void PIDsetup(){
// Initialize encoder values
encoderL = 0;
encoderR = 0;
// Initialize distance and angle variables
distance = 0;
angle = 0;
// Set initial setpoints
setPointDistance = 0;
setPointAngle = 0;
pidDistance.setTimeStep(25);
pidAngle.setTimeStep(25);
int32_t encoder_start_temp = getLeftEncoderCounts();
for(pwm_L1_bias =40;pwm_L1_bias<100 && abs(encoder_start_temp-getLeftEncoderCounts())<5;pwm_L1_bias++){
setMotorPWM(pwm_L1_bias, 0);
delay(100);
}
pwm_L1_bias-=3;
setMotorPWM(0, 0);
delay(400);
encoder_start_temp = getLeftEncoderCounts();
for(pwm_L0_bias = -40;pwm_L0_bias>-100 && abs(encoder_start_temp-getLeftEncoderCounts())<5;pwm_L0_bias--){
setMotorPWM(pwm_L0_bias, 0);
delay(100);
}
pwm_L0_bias+=3;
setMotorPWM(0, 0);
delay(400);
encoder_start_temp = getRightEncoderCounts();
for(pwm_R1_bias = 40;pwm_R1_bias<100 && abs(encoder_start_temp-getRightEncoderCounts())<5;pwm_R1_bias++){
setMotorPWM(0, pwm_R1_bias);
delay(100);
}
pwm_R1_bias-=3;
setMotorPWM(0, 0);
delay(400);
encoder_start_temp = getRightEncoderCounts();
for(pwm_R0_bias = -40;pwm_R0_bias>-100 && abs(encoder_start_temp-getRightEncoderCounts())<5;pwm_R0_bias--){
setMotorPWM(0, pwm_R0_bias);
delay(100);
}
pwm_R0_bias+=3;
setMotorPWM(0, 0);
resetEncoders();
}
void Move(int n){
AnglRANGE = 20.0;
DistRANGE = 80.0;
setPointAngle = 0;
setPointDistance= -1350 * n ;
pidAngle.setOutputRange(-AnglRANGE,AnglRANGE);
pidDistance.setOutputRange(-DistRANGE,DistRANGE);
PIDwork();
setMotorPWM(0, 0);
}
void Turn(int n){
switch(n){
case 0:
break;
case 1:
setMotorPWM(-42,42);
delay(475);
setMotorPWM(0,0);
delay(1000);
initializeEncoders();
break;
case 2:
setMotorPWM(-42,42);
delay(475);
setMotorPWM(-42,42);
delay(475);
setMotorPWM(0,0);
initializeEncoders();
delay(1000);
break;
case -1:
setMotorPWM(42,-42);
delay(485);
setMotorPWM(0,0);
delay(1000);
initializeEncoders();
break;
}
setMotorPWM(0, 0);
// AnglRANGE = 90.0;
// DistRANGE = 10.0;
// setPointAngle = 700*n;
// setPointDistance= 0;
// pidAngle.setOutputRange(-AnglRANGE,AnglRANGE);
// pidDistance.setOutputRange(-DistRANGE,DistRANGE);
// PIDwork();
// setMotorPWM(0, 0);
// initializeEncoders();
}