-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoDataSender.ino
More file actions
290 lines (239 loc) · 7.75 KB
/
ArduinoDataSender.ino
File metadata and controls
290 lines (239 loc) · 7.75 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
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Dns.h>
#include <CurieIMU.h>
#include <MadgwickAHRS.h>
#define PIN 4
int val = 0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress local(10, 7, 51, 123);
IPAddress ip(10, 7, 51, 109);
unsigned int localPort = 8888; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
DNSClient dnsclient;
// chris's declarations
int last = 0;
int count = 0;
int lastMod = 0;
int sum = 0;
int numMagnet = 6;
float wheelDiameter = 6.25;
//8.7 on the 2016 robot
unsigned long distanceTraveled0 = 0;
unsigned long distanceTraveled1 = 0;
#define INTERVAL 8
const int windowTime = 1000; //milliseconds
const int intervalTime = windowTime / INTERVAL; //milliseconds
int pulses[INTERVAL];
float velocity0 = 0; //inches per second
float velocity1 = 0; //inches per second
// chris's decl
Madgwick filter;
float microsPerReading, microsPrevious, lastMicros; float accelScale, gyroScale;
int aix, aiy, aiz;
int gix, giy, giz;
float ax, ay, az;
float gx, gy, gz;
float roll, pitch, heading;
float microsNow;
float originalHeading;
void setup() {
// randomSeed(analogRead(0)); // can't be connected
int [] pinNums = {4, 8}
pinMode(pinNums[0], INPUT_PULLUP);
pinMode(pinNums[1], INPUT_PULLUP);
Serial.begin(9600);
velocity = 0;
distanceTraveled = 0;
setupIMU();
}
void loop() {
// chris's stuff
// put your main code here, to run repeatedly:
int x = 1 - digitalRead(4);
unsigned long timeX = millis() / intervalTime;
int mod = timeX % INTERVAL;
sendSensorData(pinNums[0]);
sendSensorData(pinNums[1]);
}
void loopIMU() {
microsNow = micros();
//Heading
if (microsNow - microsPrevious >= microsPerReading) {
// read raw data from CurieIMU
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
// convert from raw data to gravity and degrees/second units
ax = convertRawAcceleration(aix);
ay = convertRawAcceleration(aiy);
az = convertRawAcceleration(aiz);
gx = convertRawGyro(gix);
gy = convertRawGyro(giy);
gz = convertRawGyro(giz);
// update the filter, which computes orientation
filter.updateIMU(gx, gy, gz, ax, ay, az);
// roll = filter.geftRoll();
// pitch = filter.getPitch();
heading = filter.getYaw();
// increment previous time, so we keep proper pace
microsPrevious = microsPrevious + microsPerReading;
lastMicros = microsNow;
}
}
string sendSensorData(int pinNum) {
int x = 1 - digitalRead(pinNum);
unsigned long timeX = millis() / intervalTime;
int mod = timeX % INTERVAL;
if (lastMod != mod) {
sum = 0;
if (timeX >= INTERVAL) {
for (int i = 0; i < INTERVAL; i++) {
sum += pulses[i];
}
if (pinNum == pinNums[0])
{ velocity0 = sum * wheelDiameter * PI / numMagnet / (windowTime / 1000.0);
} else {
velocity1 = sum * wheelDiameter * PI / numMagnet / (windowTime / 1000.0);
} else {
//pulses array has not been completely filled in yet
for (int i = 0; i < mod; i++) {
sum += pulses[i];
}
if (pinNum == pinNums[0])
{ velocity0 = sum * wheelDiameter * PI * INTERVAL / mod / numMagnet / (windowTime / 1000.0);
} else {
velocity1 = sum * wheelDiameter * PI * INTERVAL / mod / numMagnet / (windowTime / 1000.0);
}
}
// Serial.print("Velocity = ");
// Serial.println(velocity);
if(pinNum == pinNums[0]){
distanceTraveled0 += (velocity0 * intervalTime) / 1000.0;
}else{
distanceTraveled1 += (velocity1 * intervalTime) / 1000.0;
}
// Serial.print("distanceTraveled: ");
// Serial.println(distanceTraveled);
pulses[mod] = 0;
}
if (last == 0 && x == 1) pulses[mod]++;
// for (int i = 0; i < INTERVAL; i++) {
// Serial.print(pulses[i]);
// Serial.print(" ");
// }
// Serial.println();
lastMod = mod;
last = x;
loopIMU();
if(pinNum == pinNums[0]){
String sendString = "[" + "left" + "," + String(heading) + "," + String(velocity) + "," + String(distanceTraveled) + "]";
}else{
String sendString = "[" + "right" + "," + String(heading) + "," + String(velocity1) + "," + String(distanceTraveled1) + "]";
}
Serial.println(sendString);
}
}
float convertRawAcceleration(int aRaw) {
// since we are using 2G range
// -2g maps to a raw value of -32768
// +2g maps to a raw value of 32767
float a = (aRaw * 2.0) / 32768.0;
return a;
}
float convertRawGyro(int gRaw) {
// since we are using 250 degrees/intervalTime range
// -250 maps to a raw value of -32768
// +250 maps to a raw value of 32767
float g = (gRaw * 250.0) / 32768.0;
return g;
}
void setupIMU() {
// start the IMU and filter
CurieIMU.begin();
CurieIMU.setGyroRate(25);
CurieIMU.setAccelerometerRate(25);
filter.begin(25);
//calibration
CurieIMU.initialize();
//Serial.print("Starting Gyroscope calibration...");
CurieIMU.autoCalibrateGyroOffset();
//Serial.println(" Done");
//Serial.print("Starting Acceleration calibration...");
CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
//Serial.println(" Done");
//Serial.println("Enabling Gyroscope/Acceleration offset compensation");
CurieIMU.setGyroOffsetEnabled(true);
CurieIMU.setAccelOffsetEnabled(true);
// Set the accelerometer range to 2G
CurieIMU.setAccelerometerRange(2);
// Set the gyroscope range to 250 degrees/second
CurieIMU.setGyroRange(250);
// verify connection
//Serial.println("Testing device connections...");
if (CurieIMU.testConnection()) {
//Serial.println("CurieIMU connection successful");
} else {
//Serial.println("CurieIMU connection failed");
}
// initialize variables to pace updates to correct rate
microsPerReading = 1000000 / 25;
microsPrevious = micros();
// check if it's time to read data and update the filter
microsNow = micros();
// read raw data from CurieIMU
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
// convert from raw data to gravity and degrees/second units
ax = convertRawAcceleration(aix);
ay = convertRawAcceleration(aiy);
az = convertRawAcceleration(aiz);
gx = convertRawGyro(gix);
gy = convertRawGyro(giy);
gz = convertRawGyro(giz);
// update the filter, which computes orientation
filter.updateIMU(gx, gy, gz, ax, ay, az);
// print the heading, pitch and roll
//roll = filter.getRoll();
//pitch = filter.getPitch();
heading = filter.getYaw();
originalHeading = heading;
}
/*
* if (lastMod != mod) {
sum = 0;
if (timeX >= INTERVAL) {
for (int i = 0; i < INTERVAL; i++) {
sum += pulses[i];
}
velocity = sum * wheelDiameter * PI / numMagnet / (windowTime / 1000.0);
} else {
//pulses array has not been completely filled in yet
for (int i = 0; i < mod; i++) {
sum += pulses[i];
}
velocity = sum * wheelDiameter * PI * INTERVAL / mod / numMagnet / (windowTime / 1000.0);
}
// Serial.print("Velocity = ");
// Serial.println(velocity);
distanceTraveled += (velocity * intervalTime) / 1000.0;
// Serial.print("distanceTraveled: ");
// Serial.println(distanceTraveled);
pulses[mod] = 0;
}
if (last == 0 && x == 1) pulses[mod]++;
// for (int i = 0; i < INTERVAL; i++) {
// Serial.print(pulses[i]);
// Serial.print(" ");
// }
// Serial.println();
lastMod = mod;
last = x;
loopIMU();
String sendString = "[" + String(heading) + "," + String(velocity) + "," + String(distanceTraveled) + "]";
Serial.println(sendString);
* /
*/