-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensors.cpp
More file actions
336 lines (308 loc) · 7.05 KB
/
Copy pathsensors.cpp
File metadata and controls
336 lines (308 loc) · 7.05 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
#include "sensors.h"
#include "config.h"
#include <math.h>
#include <SharpIR.h>
#include <Wire.h>
SharpIR psd_sensor(PSD_PIN, PSD_model);
int leftLastStateA = LOW;
int leftLastStateB = LOW;
int rightLastStateA = LOW;
int rightLastStateB = LOW;
volatile int leftPulseCount = 0;
unsigned long leftLastTime = 0;
volatile int rightPulseCount = 0;
unsigned long rightLastTime = 0;
void Sensors::updateHealth()
{
unsigned long currentTime = millis();
if (currentTime - lastHealthUpdate > 5 * 100 || true)
{
#ifdef DEBUG
Serial.print("Used Wifi: ");
Serial.println(usedWifi);
#endif
if (usedWifi > 0) {
#ifdef DEBUG
Serial.print("Sending health deduct to: ");
Serial.println(TOPHAT_I2C_ADDR);
#endif
// usedWifi /= 2;
I2Ctophat.beginTransmission(TOPHAT_I2C_ADDR);
I2Ctophat.write(usedWifi);
I2Ctophat.endTransmission();
usedWifi = 0;
}
I2Ctophat.requestFrom(TOPHAT_I2C_ADDR, 1);
if (I2Ctophat.available())
{
uint8_t byte1 = I2Ctophat.read();
Serial.printf("Recieved data: %d\n", byte1);
health = byte1;
}
lastHealthUpdate = currentTime;
}
}
uint32_t med3filt(uint32_t a, uint32_t b, uint32_t c)
{
uint32_t middle;
if ((a <= b) && (a <= c))
middle = (b <= c) ? b : c;
else if ((b <= a) && (b <= c))
middle = (a <= c) ? a : c;
else
middle = (a <= b) ? a : b;
return middle;
}
void Sensors::leftUpdateEncoderA()
{
int stateA = digitalRead(leftEncodePinA);
int stateB = digitalRead(leftEncodePinB);
if (stateA != leftLastStateA)
{
if (stateA == stateB)
{
leftPulseCount++;
}
else
{
leftPulseCount--;
}
}
leftLastStateA = stateA;
}
void Sensors::leftUpdateEncoderB()
{
int stateA = digitalRead(leftEncodePinA);
int stateB = digitalRead(leftEncodePinB);
if (stateB != leftLastStateB)
{
if (stateA == stateB)
{
leftPulseCount--;
}
else
{
leftPulseCount++;
}
}
leftLastStateB = stateB;
}
void Sensors::rightUpdateEncoderA()
{
int stateA = digitalRead(rightEncodePinA);
int stateB = digitalRead(rightEncodePinB);
if (stateA != rightLastStateA)
{
if (stateA == stateB)
{
rightPulseCount++;
}
else
{
rightPulseCount--;
}
// rightPulseCount += 2;
}
rightLastStateA = stateA;
}
void Sensors::rightUpdateEncoderB()
{
int stateA = digitalRead(rightEncodePinA);
int stateB = digitalRead(rightEncodePinB);
if (stateB != rightLastStateB)
{
if (stateA == stateB)
{
rightPulseCount--;
}
else
{
rightPulseCount++;
}
}
rightLastStateB = stateB;
}
void Sensors::updateLeftSpeed()
{
unsigned long currentTime = millis();
if (currentTime - leftLastTime >= 10)
{
leftRPM = (leftPulseCount / (float)(pulsesPerRevolution * 4)) * (60000.0 / (currentTime - leftLastTime)); // RPM
leftPulseCount = 0;
leftLastTime = currentTime;
}
}
void Sensors::updateRightSpeed()
{
unsigned long currentTime = millis();
if (currentTime - rightLastTime >= 10)
{
rightRPM = (rightPulseCount / (float)(pulsesPerRevolution * 4)) * (60000.0 / (currentTime - rightLastTime)); // RPM
rightPulseCount = 0;
rightLastTime = currentTime;
}
}
void Sensors::updateLocalization()
{
static uint16_t l_x, l_y;
if (vive1->status() == VIVE_RECEIVING)
{
static uint16_t x0, y0, oldx1, oldx2, oldy1, oldy2;
oldx2 = oldx1;
oldy2 = oldy1;
oldx1 = x0;
oldy1 = y0;
x0 = vive1->xCoord();
y0 = vive1->yCoord();
l_x = med3filt(x0, oldx1, oldx2);
l_y = med3filt(y0, oldy1, oldy2);
if (l_x > 8000 || l_y > 8000 || l_x < 1000 || l_y < 1000)
{
l_x = 0;
l_y = 0;
}
}
else
{
l_x = 0;
l_y = 0;
vive1->sync(5);
}
if (l_x != 0 && l_y != 0) {
loc1.setPoint(l_x, l_y);
}
if (vive2->status() == VIVE_RECEIVING)
{
static uint16_t x0, y0, oldx1, oldx2, oldy1, oldy2;
oldx2 = oldx1;
oldy2 = oldy1;
oldx1 = x0;
oldy1 = y0;
x0 = vive2->xCoord();
y0 = vive2->yCoord();
l_x = med3filt(x0, oldx1, oldx2);
l_y = med3filt(y0, oldy1, oldy2);
if (l_x > 8000 || l_y > 8000 || l_x < 1000 || l_y < 1000)
{
l_x = 0;
l_y = 0;
}
}
else
{
l_x = 0;
l_y = 0;
vive2->sync(5);
}
if (l_x != 0 && l_y != 0) {
loc2.setPoint(l_x, l_y);
}
#ifdef DEBUG
loc1.print();
loc2.print();
#endif
float dx = loc2.x - loc1.x;
float dy = loc2.y - loc1.y;
bearing = atan2(dy, dx);
location.setPoint((loc1.x + loc2.x) / 2.0, (loc1.y + loc2.y) / 2.0);
}
void Sensors::updateRightwardDistance()
{
rightwardDistance = psd_sensor.distance();
}
void Sensors::updateForwardDistance()
{
if (tofSensor.dataReady())
{
int newDist = tofSensor.distance();
if (newDist == -1)
{
return;
}
forwardDistance = newDist;
tofSensor.clearInterrupt();
}
}
void Sensors::updateState()
{
#ifdef TOPHAT
updateHealth();
#endif
#ifdef MOTORS
updateLeftSpeed();
updateRightSpeed();
#endif
#ifdef VIVE
updateLocalization();
#endif
#ifdef TOF
updateForwardDistance();
#endif
#ifdef PSD
updateRightwardDistance();
#endif
}
void Sensors::startup()
{
#ifdef TOPHAT
pinMode(TOPHAT_XSHUT_PIN, OUTPUT);
digitalWrite(TOPHAT_XSHUT_PIN, LOW);
#endif
#ifdef VIVE
vive1 = new Vive510(VIVE1_PIN);
vive2 = new Vive510(VIVE2_PIN);
vive1->begin();
vive2->begin();
#endif
#ifdef MOTORS
pinMode(leftEncodePinA, INPUT_PULLUP);
pinMode(leftEncodePinB, INPUT_PULLUP);
pinMode(rightEncodePinA, INPUT_PULLUP);
pinMode(rightEncodePinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(leftEncodePinA), leftUpdateEncoderA, CHANGE);
attachInterrupt(digitalPinToInterrupt(leftEncodePinB), leftUpdateEncoderB, CHANGE);
attachInterrupt(digitalPinToInterrupt(rightEncodePinA), rightUpdateEncoderA, CHANGE);
attachInterrupt(digitalPinToInterrupt(rightEncodePinB), rightUpdateEncoderB, CHANGE);
#endif
#ifdef TOF
if (!tofSensor.begin(TOF_I2C_ADDR, &I2Ctof))
{
Serial.print(F("Error on init of VL sensor: "));
Serial.println(tofSensor.vl_status);
while (1)
delay(10);
}
Serial.println(F("VL53L1X sensor OK!"));
Serial.print(F("Sensor ID: 0x"));
Serial.println(tofSensor.sensorID(), HEX);
if (!tofSensor.startRanging())
{
Serial.print(F("Couldn't start ranging: "));
Serial.println(tofSensor.vl_status);
while (1)
delay(10);
}
Serial.println(F("Ranging started"));
tofSensor.setTimingBudget(15);
Serial.print(F("Timing budget (ms): "));
Serial.println(tofSensor.getTimingBudget());
#endif
}
void plotData(int currentSpeed, int desiredSpeed, float error, float proportional, float integralTerm, float derivativeTerm, int pidOutput)
{
Serial.print(millis());
Serial.print(" ");
Serial.print(currentSpeed);
Serial.print(" ");
Serial.print(desiredSpeed);
Serial.print(" ");
Serial.print(error);
Serial.print(" ");
Serial.print(proportional);
Serial.print(" ");
Serial.print(integralTerm);
Serial.print(" ");
Serial.print(derivativeTerm);
Serial.print(" ");
Serial.println(pidOutput);
}