-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandlejoust.ino
289 lines (239 loc) · 9.06 KB
/
candlejoust.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
// https://github.com/ElectronicCats/mpu6050
#include "MPU6050_6Axis_MotionApps20.h"
#include "I2Cdev.h"
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
const int ACCEL_ADDR = 0x3B;
const int POWER_ADDR = 0x6B; // PWR_MGMT_1 register
const int LED_DATA_PIN = 16; // Pin to communicate with LED; old values of 16, 5
#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards
const int BUTTON_PIN = 6; // button pin used to restart game
const double WARN_ACCEL = 2800;
const double MAX_ACCEL = 3500;
const int16_t BRIGHTNESS = 200;
bool running;
Adafruit_NeoPixel strip(1, LED_DATA_PIN, NEO_GRB + NEO_KHZ800);
// I'm colorblind lol. fetched from here: https://rgbcolorcode.com/color/flame
const uint32_t FLAME = strip.gamma32(strip.Color(88, 226, 34));
const uint32_t BLUE = strip.Color(0, 0, 255);
const uint32_t BLANK = strip.Color(0, 0, 0);
// These chunks were copied from examples in https://github.com/ElectronicCats/mpu6050
MPU6050 accelerometer;
// 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
// A class to represent the acceleration vectors and interact with the accelerometer. This covers way too many concerns - imo accelerometer logic shouldn't be baked into here - but hey, this entire project is a hack and it's small & functional ;)
class xyz {
public:
int16_t X, Y, Z;
double totalAccel;
xyz(void) {
X = Y = Z = 0;
}
xyz get_values() {
// display initial world-frame acceleration, adjusted to remove gravity
// and rotated based on known orientation from quaternion
accelerometer.dmpGetQuaternion(&q, fifoBuffer);
accelerometer.dmpGetAccel(&aa, fifoBuffer);
accelerometer.dmpGetGravity(&gravity, &q);
accelerometer.dmpGetLinearAccel(&aaReal, &aa, &gravity);
accelerometer.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
Serial.print("aaWorld\t");
Serial.print(aaWorld.x);
Serial.print("\t");
Serial.print(aaWorld.y);
Serial.print("\t");
Serial.println(aaWorld.z);
X = aaWorld.x;
Y = aaWorld.y;
Z = aaWorld.z;
// display real acceleration, adjusted to remove gravity
/*accelerometer.dmpGetQuaternion(&q, fifoBuffer);
accelerometer.dmpGetAccel(&aa, fifoBuffer);
accelerometer.dmpGetGravity(&gravity, &q);
accelerometer.dmpGetLinearAccel(&aaReal, &aa, &gravity);
Serial.print("areal\t");
Serial.print(aaReal.x);
Serial.print("\t");
Serial.print(aaReal.y);
Serial.print("\t");
Serial.println(aaReal.z);
X = aaReal.x;
Y = aaReal.y;
Z = aaReal.z;
*/
totalAccel = total_accel();
return *this;
}
xyz& operator=(xyz volatile& rhs) {
X = rhs.X;
Y = rhs.Y;
Z = rhs.Z;
return *this;
}
// de gua's theorem, applied to acceleration vectors
double total_accel() {
double t;
t = sqrt(pow(X, 2) + pow(Y, 2) + pow(Z, 2));
//Serial.print("total_accel(): ");
//Serial.println(t);
return t;
}
bool warn() {
if(totalAccel > WARN_ACCEL) {
Serial.print("totalAccel: ");
Serial.println(totalAccel);
return true;
}
else
return false;
}
bool fail() {
if(totalAccel > MAX_ACCEL) {
Serial.print("totalAccel: ");
Serial.println(totalAccel);
return true;
}
else
return false;
}
};
xyz accel;
// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}
// a great deal of this code was copied & pasted from the MPU-6050 examples in the ElectronicCats library: https://github.com/ElectronicCats/mpu6050
void init_gyro(){
Serial.print("init_gyro() called\n");
Wire.begin();
accelerometer.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelerometer.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// load and configure the DMP
Serial.println(F("Initializing DMP..."));
devStatus = accelerometer.dmpInitialize();
// supply your own gyro offsets here, scaled for min sensitivity
accelerometer.setXGyroOffset(220);
accelerometer.setYGyroOffset(76);
accelerometer.setZGyroOffset(-85);
accelerometer.setZAccelOffset(1688); // 1688 factory default for my test chip
// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// Calibration Time: generate offsets and calibrate our MPU6050
accelerometer.CalibrateAccel(6);
accelerometer.CalibrateGyro(6);
accelerometer.PrintActiveOffsets();
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
accelerometer.setDMPEnabled(true);
// enable Arduino interrupt detection
Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
Serial.print(digitalPinToInterrupt(INTERRUPT_PIN));
Serial.println(F(")..."));
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
mpuIntStatus = accelerometer.getIntStatus();
// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = accelerometer.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}
Serial.print("MPU-6050 initialized\n");
}
// stop the game.
// TODO: set gyro/accel to low power mode, provided
// we don't have to re-calibrate
void stop(){
Serial.println("stop() called\n");
running = false;
strip.show();
strip.setPixelColor(0, BLUE);
strip.show();
Serial.print("Ending. Press the reset button to continue\n");
}
// use the LED to warn the player that you're close
// to being too fast. there are some timing oddities -
// the game is effectively paused when this is happening.
void warning(){
Serial.println("warning() called");
strip.show();
for (int i=0; i < 4; i++) {
strip.setPixelColor(0, BLANK);
strip.show();
delay(50);
strip.setPixelColor(0, FLAME);
strip.show();
delay(50);
}
}
void start() {
Serial.println("start() called");
running = true;
strip.setPixelColor(0, FLAME);
strip.show();
}
void setup(){
Serial.begin(9600); // this is for the board output for testing
Serial.print("started serial port in setup();\n");
strip.begin();
strip.setBrightness(BRIGHTNESS);
strip.setPixelColor(0, BLANK);
strip.show();
init_gyro();
pinMode(BUTTON_PIN, INPUT_PULLUP);
start();
delay(150); // initial readings for the Z axis take some time to normalize
Serial.print("setup() exiting\n");
}
void loop(){
if (!dmpReady) return;
// if the button has been pressed, restart the game (without all the pesky
// hardware calibration)
if (!running && digitalRead(BUTTON_PIN) == LOW) {
start();
}
// the timing here is still a bit wacky (there are edge cases) but should be good enough
if(running && accelerometer.dmpGetCurrentFIFOPacket(fifoBuffer)) { // gets current packet from DMP
accel.get_values();
if(accel.fail()){
stop();
return;
}
if(accel.warn()){
warning();
// discard latest data, as it seems cumulative over the course of the warning sequence
accelerometer.dmpGetCurrentFIFOPacket(fifoBuffer);
return;
}
// scale brightness down, like a real candle
strip.setPixelColor(0, FLAME); // https://github.com/adafruit/Adafruit_NeoPixel/blob/7f3ebe002a270ebf5298200c411085bba6ad131d/Adafruit_NeoPixel.cpp#L3359
strip.setBrightness(BRIGHTNESS - ( BRIGHTNESS * (accel.total_accel() / MAX_ACCEL )));
strip.show();
}
}