-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathother_pid_algorithm.cpp
More file actions
412 lines (352 loc) · 12.4 KB
/
other_pid_algorithm.cpp
File metadata and controls
412 lines (352 loc) · 12.4 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <Arduino.h>
#include <Wire.h>
#include <Servo.h>
#include <Adafruit_SSD1306.h>
#define PWMfreq 2000
#define LEFT_F_MOTOR PB_9
#define LEFT_B_MOTOR PB_8
#define RIGHT_F_MOTOR PB_6
#define RIGHT_B_MOTOR PB_7
#define TAPE_SENSOR_R PA3
#define TAPE_SENSOR_L PA4
#define p_pot PA1
#define d_pot PA2
#define max_input 1023
#define midpoint 512
#define ON_OFF_THRESHOLD 200 //can investigate this number more... above this number is when it is on tape
#define BAR_THRESHOLD 800
#define STARTING_DUMMY_VALUE 10000
int previous_error = STARTING_DUMMY_VALUE; //dummy starting value
int dist_between_sensors = 8; //may have to do more precise measurements on this...
//need to have it be far enough away for this error to have meaning but also for there to be big enough range in reflectance values...
double gain_scaling_factor = 1.0; //scales down the gain input from the potentiometers.
int current_state_count = 1; //beginning of counting
int previous_state_count = STARTING_DUMMY_VALUE; //dummy starting value
int previous_state = STARTING_DUMMY_VALUE; //dummy starting value;
double correction_scaling_factor = 5; //play around with this value to get g value to fit within duty range.
int max_motor_duty = 65535; //max number the duty can be in motor format
int min_motor_duty = 1; //min number used for linearization?
int nominal_motor_L_duty = 34500;
int nominal_motor_R_duty = 34500;
int num_loops = 0;
int max_states_before_clear = 5;
double slope_scaling_factor = 100.0;
#define ELEVATOR_SERVO PB0
#define WHACKER_SERVO PB1
#define BACK_SERVO PA7
#define HATCH_DOWN 180
#define HATCH_UP 0
#define ELEVATOR_DOWN 0
#define ELEVATOR_UP 150
#define WHACKER_OPEN 0
#define WHACKER_CLOSE 180
#define COLLISION_SENSOR PA6
#define ECHO PA11
#define TRIG PA12
#define SONAR_DETECTION_DISTANCE -100
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // This display does not have a reset pin accessible
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo whacker_servo;
Servo elevator_servo;
Servo back_servo;
// TwoWire Wire(SCL, SDA, SOFT_STANDARD);
// interrupt
void activate_back_trigger();
volatile int collisions = 0;
void run_motor(int duty, PinName motorPin_F, PinName motorPin_B) {
//duty: if > 0, turn motor forward as described above
// if < 0, turn motor backward as described above
//if the duty scaling is negative then run the motor backwards, if positive then run motor forwards
if (duty > 0) {
pwm_start(motorPin_B,PWMfreq,1,TICK_COMPARE_FORMAT);
pwm_start(motorPin_F,PWMfreq,duty,TICK_COMPARE_FORMAT);
} else {
duty = duty*(-1);
pwm_start(motorPin_F,PWMfreq,1,TICK_COMPARE_FORMAT);
if (duty == 0) {
duty = 1;
}
pwm_start(motorPin_B,PWMfreq,duty,TICK_COMPARE_FORMAT);
}
};
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
//DC Motors
pinMode(LEFT_F_MOTOR, OUTPUT);
pinMode(LEFT_B_MOTOR, OUTPUT);
pinMode(RIGHT_F_MOTOR, OUTPUT);
pinMode(RIGHT_B_MOTOR, OUTPUT);
//servos
whacker_servo.attach(WHACKER_SERVO);
elevator_servo.attach(ELEVATOR_SERVO);
back_servo.attach(BACK_SERVO);
//LM311
pinMode(COLLISION_SENSOR, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(COLLISION_SENSOR), activate_back_trigger, RISING);
//Sonar
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
//tape sensor pid
pinMode(TAPE_SENSOR_L, INPUT);
pinMode(TAPE_SENSOR_R, INPUT);
pinMode(p_pot,INPUT);
pinMode(d_pot,INPUT);
run_motor(nominal_motor_L_duty, LEFT_F_MOTOR, LEFT_B_MOTOR);
run_motor(nominal_motor_R_duty, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
}
// runs a certain servo from angle start to angle end, increment angle by int speed each loop
void run_servo(Servo this_servo, int start, int end, int speed){
if (start < end){
for (int angle = start; angle <= end; angle+= speed){
this_servo.write(angle);
delay(100);
}
}
else{
for (int angle = start; angle >= end; angle-= speed){
this_servo.write(angle);
delay(100);
}
}
}
void activate_back_trigger(){
collisions ++;
}
int prev_collisions = 0;
double prev_distance = 100.0;
double distance = 100;
double prev_d = 0;
int prev_position_L = 0, prev_position_R=0;
void loop() {
// set all servos at initial positions
whacker_servo.write(WHACKER_OPEN);
elevator_servo.write(ELEVATOR_DOWN+20);
back_servo.write(HATCH_UP);
if (num_loops % 10 == 0){
// read distance with sonar
digitalWrite(TRIG, LOW);
delay(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(TRIG, HIGH);
delay(10);
digitalWrite(TRIG, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
double duration = pulseIn(ECHO, HIGH);
// Calculating the distance
distance= duration*0.034/2;
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("distance: ");
display.println(distance);
display.display();
if (distance < 30){
run_motor(1, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
run_motor(1, LEFT_F_MOTOR, LEFT_B_MOTOR);
}
else {
// // take the average of the previous sonar reading and current sonar reading to prevent noise errors
// if ((distance + prev_distance)/2 < SONAR_DETECTION_DISTANCE){
// //if distance detected by sonar is less than SONAR_DETECTION_DISTANCE, close whacker and raise elevator
// run_servo(whacker_servo, WHACKER_OPEN, WHACKER_CLOSE, 15);
// run_servo(whacker_servo, WHACKER_CLOSE, WHACKER_OPEN, 15);
// delay(100);
// run_servo(elevator_servo, ELEVATOR_DOWN, ELEVATOR_UP, 15);
// delay(1000);
// run_servo(elevator_servo, ELEVATOR_UP, ELEVATOR_DOWN, 15);
// delay(100);
// }
// prev_distance = distance;
// // number of times the back hatch has collided
// display.print("Collisions: ");
// display.println(collisions);
// display.display();
// // if contact switch activated, lower the back hatch
// if (collisions > prev_collisions){
// delay(2000);
// back_servo.write(HATCH_DOWN);
// delay(3000);
// prev_collisions = collisions;
// }
int position_L_analog = analogRead(TAPE_SENSOR_L);
int position_R_analog = analogRead(TAPE_SENSOR_R);
int position_L;
int position_R;
int error;
//convert analog tape sensor reading to digital position
if (position_L_analog > ON_OFF_THRESHOLD && position_L_analog < BAR_THRESHOLD) {
position_L = 1; //on tape
}
else if (position_L_analog > BAR_THRESHOLD){
position_L = 5;
}
else {
position_L = 0; //off tape
}
if (position_R_analog > ON_OFF_THRESHOLD && position_R_analog < BAR_THRESHOLD) {
position_R = 1; //on tape
}
else if (position_R_analog > BAR_THRESHOLD){
position_R = 5;
}
else {
position_R = 0; //off tape
}
display.print("left sensor: ");
display.print(position_L_analog);
display.print(" ");
display.println(position_L);
display.print("right sensor: ");
display.print(position_R_analog);
display.print(" ");
display.println(position_R);
display.display();
if (position_L == 1 and position_R == 1) {
if (prev_position_L == 1 && prev_position_R == 1){
error = 0;
}
else if (prev_position_L == 0 && prev_position_R == 1){
error = -1;
}
else if (prev_position_L == 1 && prev_position_R == 0){
error = 1;
}
}
else if (position_L == 0 and position_R == 1) {
if (prev_position_L == 1 && prev_position_R == 1){
error = -1;
}
else if (prev_position_L == 0 && prev_position_R == 1){
error = -2;
}
else if (prev_position_L == 0 && prev_position_R == 0){
error = -3;
}
}
else if (position_L == 1 and position_R == 0) {
if (prev_position_L == 1 && prev_position_R == 1){
error = 1;
}
else if (prev_position_L == 1 && prev_position_R == 0){
error = 2;
}
else if (prev_position_L == 0 && prev_position_R == 0){
error = 3;
}
}
else if (position_L == 0 && position_R == 0){
if (prev_position_L == 0 && prev_position_R == 1){
error = -5;
}
else if (prev_position_L == 1 && prev_position_R == 0){
error = 5;
}
else if (prev_position_L == 0 && prev_position_R == 0){
if (previous_error < 0){
error = -dist_between_sensors;
}
else {
error = dist_between_sensors;
}
}
}
else if (position_R == 5 && position_L == 5){
run_motor(-nominal_motor_R_duty, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
run_motor(-nominal_motor_L_duty, LEFT_F_MOTOR, LEFT_B_MOTOR);
error = 42;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("at the bar ");
display.display();
delay(2000);
}
int p_gain = analogRead(p_pot); //purely for testing - once we have the final values we will remove the potentiometers
int d_gain = analogRead(d_pot);
// //int i_gain - probably shouldnt impement this...
p_gain = p_gain * gain_scaling_factor;
d_gain = d_gain * gain_scaling_factor;
display.print("p gain: ");
display.println(p_gain);
display.print("d gain: ");
display.println(d_gain);
display.display();
//proportional control
double p = error*p_gain;
// //derivative control - wrote when i was very tired so I may have made mistakes
// double slope = 0;
// double d = 0; //for very first state the derivative control should be 0
// //should this go before or after the d calculation block? if before, then am taking the difference in time including the
// if (previous_error == error) {
// current_state_count++;
// }
// else if (previous_error != STARTING_DUMMY_VALUE) {
// previous_state_count = current_state_count; // if a change in state has occurred, the current number of instances now becomes the number of instances in the previous state.
// previous_state = previous_error; //store the previous state when change occurs
// current_state_count = 1; //start at 1 because 1 instance has occurred.
// }
// if (previous_state_count != STARTING_DUMMY_VALUE) {
// slope = (double)(error - previous_state) / (previous_state_count + current_state_count - 1) * slope_scaling_factor; //-1 because i want it to be the true difference in time (time final - time initial), not with an extra increment (look at the graph at the end of lecture 5 to understand what I mean)
// d = slope*d_gain;
// }
// if (current_state_count > max_states_before_clear && error == 0){
// d = 0;
// prev_d = 0;
// }
double d = (error - previous_error) * d_gain;
previous_error = error;
//potential bug: derivative does not go to 0 as soon as tape sensors are aligned on tape. Have to test and see if this is an issue or not.
int g = (p+d) * correction_scaling_factor;
display.print("g ");
display.println((int)g);
display.print("error ");
display.println(error);
display.display();
int motor_duty;
if (error > 0) {
motor_duty = nominal_motor_R_duty + g;
if (motor_duty > max_motor_duty) {
motor_duty = max_motor_duty;
}
if (motor_duty < 1){
motor_duty = 1;
}
if (error == dist_between_sensors){
run_motor(nominal_motor_R_duty, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
run_motor(1, LEFT_F_MOTOR, LEFT_B_MOTOR);
}
else{
run_motor(motor_duty, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
}
}
else if (error < 0) {
motor_duty = nominal_motor_L_duty + g*(-1);
if (motor_duty > max_motor_duty) {
motor_duty = max_motor_duty;
}
if (motor_duty < 1){
motor_duty = 1;
}
if (error == -dist_between_sensors){
run_motor(nominal_motor_L_duty ,LEFT_F_MOTOR, LEFT_B_MOTOR);
run_motor(1, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
}
else{
run_motor(nominal_motor_L_duty + g*(-1),LEFT_F_MOTOR, LEFT_B_MOTOR);
}
}
else {
run_motor(nominal_motor_R_duty, RIGHT_F_MOTOR, RIGHT_B_MOTOR);
run_motor(nominal_motor_L_duty, LEFT_F_MOTOR, LEFT_B_MOTOR);
}
}
//have to incorporate linearization through experimentation
num_loops++;
};