-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubicCTL.ino
423 lines (328 loc) · 12.8 KB
/
CubicCTL.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
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
412
413
414
415
416
417
418
419
420
421
422
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include <avr/wdt.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,4); // RX, TX
#include <EEPROM.h> // needed to save calibration routine
#include "writeAnything.h"
int eepromAdr = 1;
int calib = 0;
//battery monitor settings
int batMonPin = A7; // input pin for the voltage divider
float pinVoltage = 0; // variable to hold the calculated voltage
float batteryVoltage = 0;
// timer function
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 10000; // interval at which to blink (milliseconds)
struct
{
int XGyroOff = 0;
int YGyroOff = 0;
int ZGyroOff = 0;
int XAccelOff = 0;
int YAccelOff = 0;
int ZAccelOff = 0;
} calibrate;
const int analogPinX = A0; // the number of the analog pin
const int analogPinY = A1; // the number of the analog pin
const int button1 = 10; // the number of the pushbutton pin
const int button2 = 7; // the number of the pushbutton pin
const int button3 = 8; // the number of the pushbutton pin
const int button4 = 9; // the number of the pushbutton pin
const int button5 = 5; // the number of the pushbutton pin
int analogX = 0;
int analogY = 0;
int analogXMapped = 0;
int analogYMapped = 0;
float degree = 0;
int Ba = 1;
int Bb = 1;
int Bc = 1;
int Bd = 1;
int Be = 1;
int Calibrating = 0;
// calibration defines
int buffersize=800; //Amount of readings used to average, make it higher to get more precision but sketch will be slower (default:1000)
int acel_deadzone=8; //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge (default:8)
int giro_deadzone=1; //Giro error allowed, make it lower to get more precision, but sketch may not converge (default:1)
int16_t ax, ay, az,gx, gy, gz;
int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;
// class default I2C address is 0x68
MPU6050 mpu;
// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}
// ================================================================
// === INITIAL SETUP ===
// ================================================================
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(button5, INPUT_PULLUP);
wdt_enable(WDTO_500MS);
delay(15);
// start bluetooth serial
mySerial.begin(115200);
mySerial.print("Starting...");
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
devStatus = mpu.dmpInitialize();
// read offsets from eeprom
EEPROM_readAnything(0,calibrate);
mpu.setXGyroOffset(calibrate.XGyroOff);
mpu.setYGyroOffset(calibrate.YGyroOff);
mpu.setZGyroOffset(calibrate.ZGyroOff);
mpu.setXAccelOffset(calibrate.XAccelOff);
mpu.setYAccelOffset(calibrate.YAccelOff);
mpu.setZAccelOffset(calibrate.ZAccelOff);
mpu.setDMPEnabled(true);
// enable Arduino interrupt detection
attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
// set our DMP Ready flag so the main loop() function knows it's okay to use it
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
}
void loop() {
wdt_reset();
//check and run calibration
if (calib == 1) {
wdt_disable() //disable watchdog whilst calibrating
EEPROM_readAnything(0,calibrate);
state = 0;
mySerial.print("<");
mySerial.print(",");
mySerial.print("000");
mySerial.print(",");
mySerial.print("000");
mySerial.print(",");
mySerial.print("000");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
mySerial.print("0");
mySerial.print(",");
//mySerial.print("0");
//mySerial.print(",");
mySerial.print(calib);
mySerial.print(",");
mySerial.println(">");
if (state==0){
meansensors();
state++;
}
if (state==1) {
calibration();
state++;
}
if (state==2) {
meansensors();
calibrate.XGyroOff = gx_offset;
calibrate.YGyroOff = gy_offset;
calibrate.ZGyroOff = gz_offset;
calibrate.XAccelOff = ax_offset;
calibrate.YAccelOff = ay_offset;
calibrate.ZAccelOff = az_offset;
// save calibration struct to eeprom
EEPROM_writeAnything(0,calibrate);
mpu.setXGyroOffset(calibrate.XGyroOff);
mpu.setYGyroOffset(calibrate.YGyroOff);
mpu.setZGyroOffset(calibrate.ZGyroOff);
mpu.setXAccelOffset(calibrate.XAccelOff);
mpu.setYAccelOffset(calibrate.YAccelOff);
mpu.setZAccelOffset(calibrate.ZAccelOff);
//Now calibrated so move straight onto normal operation
calib = 0;
}
wdt_enable() // re-enable watchdog
}
//Normal Operation
else{
// if programming failed, don't try to do anything
if (!dmpReady) return;
// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {
// other program behavior stuff here
//read serial port for calibration char.
while (mySerial.available() > 0) {
calib = mySerial.parseInt();
};
// read battery voltage
pinVoltage = (analogRead(batMonPin)) * 0.00488; // Calculate the voltage on the A/D pin
// A reading of 1 for the A/D = 0.0048mV
// if we multiply the A/D reading by 0.00488 then
// we get the voltage on the pin.
batteryVoltage = pinVoltage * 1.86;; // Use the ratio calculated for the voltage divider
analogXMapped = map ((analogRead(analogPinX)),0,1024,0,200);
analogYMapped = map ((analogRead(analogPinY)),0,1024,0,200);
analogXMapped = constrain(analogXMapped, 0, 200);
analogYMapped = constrain(analogYMapped, 0, 200);
//check buttons
Ba = ButtonConvert(button1);
Bb = ButtonConvert(button2);
Bc = ButtonConvert(button3);
Bd = ButtonConvert(button4);
Be = ButtonConvert(button5);
}
// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
// get current FIFO count
fifoCount = mpu.getFIFOCount();
// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
// otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);
// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
mySerial.print("<");
mySerial.print(",");
degree = map_double((ypr[0] * 180/M_PI), -180, 180, 360, 0);
mySerial.print(degree);
mySerial.print(",");
mySerial.print(ypr[1] * 180/M_PI);
mySerial.print(",");
mySerial.print(ypr[2] * 180/M_PI);
mySerial.print(",");
mySerial.print(analogXMapped);
mySerial.print(",");
mySerial.print(analogYMapped);
mySerial.print(",");
mySerial.print(Ba);
mySerial.print(",");
mySerial.print(Bb);
mySerial.print(",");
mySerial.print(Bc);
mySerial.print(",");
mySerial.print(Bd);
mySerial.print(",");
mySerial.print(Be);
mySerial.print(",");
//mySerial.print(batteryVoltage);
//mySerial.print(",");
mySerial.print(Calibrating);
mySerial.print(",");
mySerial.println(">");
}
}
}
/////////////////////////////////// FUNCTIONS FOR OPERATION ////////////////////////////////////
//Buttons are using pullups, so read 1 when low and 0 when high. this function
//flips the 0 and 1 so 1 is high
int ButtonConvert (int button){
int result = digitalRead(button);
//flip 0 and 1
int temp = !result;
return temp;
};
float map_double(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/////////////////////////////////// FUNCTIONS FOR CALIBRATION ////////////////////////////////////
void meansensors(){
long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;
while (i<(buffersize+101)){
// read raw accel/gyro measurements from device
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
buff_ax=buff_ax+ax;
buff_ay=buff_ay+ay;
buff_az=buff_az+az;
buff_gx=buff_gx+gx;
buff_gy=buff_gy+gy;
buff_gz=buff_gz+gz;
}
if (i==(buffersize+100)){
mean_ax=buff_ax/buffersize;
mean_ay=buff_ay/buffersize;
mean_az=buff_az/buffersize;
mean_gx=buff_gx/buffersize;
mean_gy=buff_gy/buffersize;
mean_gz=buff_gz/buffersize;
}
i++;
delay(2); //Needed so we don't get repeated measures
}
}
void calibration(){
ax_offset=-mean_ax/8;
ay_offset=-mean_ay/8;
az_offset=(16384-mean_az)/8;
gx_offset=-mean_gx/4;
gy_offset=-mean_gy/4;
gz_offset=-mean_gz/4;
while (1){
int ready=0;
mpu.setXAccelOffset(ax_offset);
mpu.setYAccelOffset(ay_offset);
mpu.setZAccelOffset(az_offset);
mpu.setXGyroOffset(gx_offset);
mpu.setYGyroOffset(gy_offset);
mpu.setZGyroOffset(gz_offset);
meansensors();
Serial.println("Calibrating...");
if (abs(mean_ax)<=acel_deadzone) ready++;
else ax_offset=ax_offset-mean_ax/acel_deadzone;
if (abs(mean_ay)<=acel_deadzone) ready++;
else ay_offset=ay_offset-mean_ay/acel_deadzone;
if (abs(16384-mean_az)<=acel_deadzone) ready++;
else az_offset=az_offset+(16384-mean_az)/acel_deadzone;
if (abs(mean_gx)<=giro_deadzone) ready++;
else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);
if (abs(mean_gy)<=giro_deadzone) ready++;
else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);
if (abs(mean_gz)<=giro_deadzone) ready++;
else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);
if (ready==6) break;
}
}