-
Notifications
You must be signed in to change notification settings - Fork 1
/
IMU_encoder_positioning.ino
300 lines (247 loc) · 7.72 KB
/
IMU_encoder_positioning.ino
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
#include <elapsedMillis.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
elapsedMillis timeElapsed;
int val;
//Regulates two motors to spin at same speed for encoder of motor one
int encoder0PinA = A1; //J3 motor on board
//int encoder0PinB = 4;
int encoder0Pos = 0; //Motor's angular position read by the encoder
int encoder0PinALast = LOW;
//for encoder for motor two
int encoder1PinA = A2; //J4 motor on board
int encoder1Pos = 0;
int encoder1PinALast = LOW;
//int encoderCountpRev = 360;
//setpoint is turn rate to compare to/reach
int setpoint = 120; //(degrees/sec)
double Integral0 = 0; //accumulated error with motors from desired number of turns
double Integral1 = 0; //accumulated error with motors from desired number of turns
int n = LOW;
int m = LOW;
//for driver for IN1 and IN2 for motor one
int motor0pin1 = 2; // J3 on Board
int motor0pin2 = 3; //pwm (controls voltage signals) pin
int pwm0 = 80; //123
int digital0 = 1; //0?
//for driver for IN1 and IN2 for motor two
int motor1pin1 = 8; // J4 on Board
int motor1pin2 = 5; //pwm pin
int pwm1 = 80; //123
int digital1 = 1; //0?
int encoder0PrevCount = 0;
int lastSpeed0 = 0;
int encoder1PrevCount = 0;
int lastSpeed1 = 0;
double timeSec = 1.0;
double timeMSec = 1000;
double distance = 0;
double linearSpeed0;
double linearSpeed1;
double k = 0.0019; // Constant for converting angular velocity to linear velocity
//PID constants
//P (proportional) is how much to adjust when turn rate is not equal to set rate. Matters most.
double kP = 0.25;//0.20 or .15
//I (integral) is how much to adjust based on accumulated error
double kI = 0.2;//0.01 or .05
//D (derivative) how quickly it deviates from set rate. Adjusts quicker for greater rates
double kD = 0.211;//0.01 or .01
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);
void setup() {
pinMode (encoder0PinA, INPUT);
// pinMode (encoder0PinB, INPUT);
pinMode (motor0pin1, OUTPUT);
pinMode (motor0pin2, OUTPUT);
pinMode (encoder1PinA, INPUT);
//pinMode (encoder1PinB, INPUT);
pinMode (motor1pin1, OUTPUT);
pinMode (motor1pin2, OUTPUT);
Serial.begin (115200);
if (!bno.begin())
{
Serial.print("No BNO055 detected");
while (1);
}
delay(1000);
}
void PID() {
//for motor one, determines if moving forward (equals 1)
//Equals 0 if moving backwards
if (digital0 == 1)
digitalWrite( motor0pin1, HIGH);
else digitalWrite( motor0pin1, LOW);
analogWrite( motor0pin2, pwm0); //Sets analog value of pin from 0 to 255 based on PID algorithm
//for motor two, determines if moving forward (equals 1)
//Equals 0 if moving backwards
if (digital1 == 1)
digitalWrite( motor1pin1, HIGH);
else digitalWrite( motor1pin1, LOW);
analogWrite( motor1pin2, pwm1);
timeElapsed = 0; //variable internal to the board
while ( timeElapsed < 500 ) {
//reading encoder pin data and see whether voltqage data compares to previous read
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
encoder0Pos++;
}
//Set this read as a new encoder reading to compare to
encoder0PinALast = n;
//Same thing but for motor two
m = digitalRead(encoder1PinA);
if ((encoder1PinALast == LOW) && (m == HIGH)) {
encoder1Pos++;
}
encoder1PinALast = m;
}
adjustPWM();
Serial.println(" ");
}
//Set PWM rate for motor based on PID algorithm
void adjustPWM() {
//Current speed calculated
int speedNow0 = calculateSpeed0();
//Deviation from set point
int error0 = setpoint - speedNow0;
//Change in error
double dError0 = ((double)speedNow0 - (double)lastSpeed0) / timeSec;
//Accumulated error
Integral0 += (double) error0;
int speedNow1 = calculateSpeed1();
int error1 = setpoint - speedNow1;
double dError1 = ((double)speedNow1 - (double)lastSpeed1) / timeSec;
Integral1 += (double) error1;
//Cap error of the rate at 255 because it is the highest it can reach
if (Integral0 > 255) Integral0 = 255;
else if (Integral0 < 0) Integral0 = 0;
if (Integral1 > 255) Integral1 = 255;
else if (Integral1 < 0) Integral1 = 0;
//Determine correction value based on PID constants and error
int adjust0 = (kP * (double)error0) + kI * Integral0 + kD * dError0;
int adjust1 = (kP * (double)error1) + kI * Integral1 + kD * dError1;
//Adjusts according to above correction
//Add to pwm if moving backwards and subtract if moving forwards
if (digital0 == 0) pwm0 += adjust0;
else pwm0 -= adjust0;
if (digital1 == 0) pwm1 += adjust1;
else pwm1 -= adjust1;
//Caps rate at max 255 and min 0
if (pwm0 > 255) pwm0 = 255;
else if (pwm0 < 0) pwm0 = 0;
if (pwm1 > 255) pwm1 = 255;
else if (pwm1 < 0) pwm1 = 0;
//Store speeds as global variable for future comparison
lastSpeed0 = speedNow0;
lastSpeed1 = speedNow1;
Serial.print("adjustment0: ");
Serial.println( adjust0);
Serial.print("PWM0: ");
Serial.println( pwm0 );
Serial.print("adjustment1: ");
Serial.println( adjust1);
Serial.print("PWM1: ");
Serial.println( pwm1 );
}
int calculateSpeed0() {
//Calculates difference in encoder positions over time
int speedDetect = (encoder0Pos - encoder0PrevCount) / timeSec;
Serial.print("Encoder0pos: ");
Serial.print( encoder0Pos );
Serial.print(" ");
Serial.println( encoder0PrevCount);
encoder0PrevCount = encoder0Pos;
Serial.print( "Speed0: ");
Serial.println( speedDetect);
return speedDetect;
}
int calculateSpeed1() {
int speedDetect = (encoder1Pos - encoder1PrevCount) / timeSec;
Serial.print("Encoder0pos: ");
Serial.print( encoder1Pos);
Serial.print(" ");
Serial.println( encoder1PrevCount);
encoder1PrevCount = encoder1Pos;
Serial.print( "Speed1: ");
Serial.println( speedDetect);
return speedDetect;
}
void calculateDistance(){
linearSpeed0 = lastSpeed0*k;
linearSpeed1 = lastSpeed1*k;
// distance = average speed of two motors * time / 2
distance += (linearSpeed0+linearSpeed1)*0.5/2;
Serial.print("distance");
Serial.println(distance);
}
void turn(double setAngle){
double currentAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER).x();
while (abs(currentAngle-setAngle) > 10){
currentAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER).x();
Serial.print("currentAnglea: ");
Serial.println(currentAngle);
if (fmod(currentAngle+360-setAngle,360) < 180){
//turn left
digitalWrite(motor0pin2, HIGH);
analogWrite(motor0pin1, 120);
analogWrite(motor1pin2, 120);
digitalWrite(motor1pin1, HIGH);
delay(100);
digitalWrite(motor0pin2, LOW);
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor1pin1, LOW);
delay(200);
} else {
//turn right
analogWrite(motor0pin2, 120);
digitalWrite(motor0pin1, HIGH);
digitalWrite(motor1pin2, HIGH);
analogWrite(motor1pin1, 120);
delay(100);
digitalWrite(motor0pin2, LOW);
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor1pin1, LOW);
delay(200);
}
}
//stop
digitalWrite(motor0pin2, LOW);
digitalWrite(motor0pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor1pin1, LOW);
delay(500);
}
void move(double setDistance, double angle){
turn(angle);
distance = 0;
while (distance < setDistance) {
PID();
calculateDistance();
}
// stop
digitalWrite( motor0pin1, LOW);
digitalWrite( motor0pin2, LOW);
digitalWrite( motor1pin1, LOW);
digitalWrite( motor1pin2, LOW);
delay(500);
}
void loop(){
// draw CUP
// C
move(0.45, 270);
move(0.50, 180);
move(0.45, 90);
move(0.6,28.7);
// U
move(0.50, 180);
move(0.38, 90);
move(0.50, 0);
move(0.6,159.3);
// P
move(0.50, 0);
move(0.38, 90);
move(0.28, 180);
move(0.38, 270);
delay(10000);
}