-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rotatey_Cube.ino
277 lines (225 loc) · 7.74 KB
/
Rotatey_Cube.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
/*
3D Rotatey-Cube is placed under the MIT license
Copyleft (c+) 2016 tobozzo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
This project is heavily inspired from the work of GoblinJuicer https://www.reddit.com/user/GoblinJuicer
who developed and implemented the idea.
Started from this sub https://www.reddit.com/r/arduino/comments/3vmw1k/ive_been_playing_with_a_gyroscope_and_an_lcd/
The code is mainly a remix to make it work with u8glib and mpu6050
Deps:
U8glib library grabbed from: https://github.com/olikraus/u8glib
I2Cdev library grabbed from: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/I2Cdev
MPU650 library grabbed from: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
Pinout OLED and MPU (shared)
VCC => 5V
GND => GND
SCL => A5
SDA => A4
Pinout MPU
AD0 => GND
INT => D2
*/
#include "U8glib.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
#define DEBUG false
// Accel and gyro data
int16_t ax, ay, az, gx, gy, gz;
double MMPI = 1000*M_PI;
long int timeLast = -100, period = 1;
// Overall scale and perspective distance
uint8_t sZ = 4, scale = 16;
// screen center
uint8_t centerX = 64;
uint8_t centerY = 32;
// Initialize cube point arrays
double C1[] = { 1, 1, 1 };
double C2[] = { 1, 1, -1 };
double C3[] = { 1, -1, 1 };
double C4[] = { 1, -1, -1 };
double C5[] = { -1, 1, 1 };
double C6[] = { -1, 1, -1 };
double C7[] = { -1, -1, 1 };
double C8[] = { -1, -1, -1 };
// Initialize cube points coords
uint8_t P1[] = { 0, 0 };
uint8_t P2[] = { 0, 0 };
uint8_t P3[] = { 0, 0 };
uint8_t P4[] = { 0, 0 };
uint8_t P5[] = { 0, 0 };
uint8_t P6[] = { 0, 0 };
uint8_t P7[] = { 0, 0 };
uint8_t P8[] = { 0, 0 };
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send ACK
MPU6050 mpu;
void setup() {
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
// 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
#if DEBUG == true
// initialize serial communication
Serial.begin(115200);
#endif
mpu.initialize();
if (mpu.dmpInitialize() == 0) {
// turn on the DMP, now that it's ready
mpu.setDMPEnabled(true);
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(161);
mpu.setYGyroOffset(48);
mpu.setZGyroOffset(43);
mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
}
}
void loop() {
u8g.firstPage();
do {
cubeloop();
}
while( u8g.nextPage() );
}
void cubeloop() {
period = millis()- timeLast;
timeLast = millis();
// precalc
double MMPI_TIME = MMPI*period;
//Read gyro, apply calibration, ignore small values
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// ignore low values (supply uour own values here, based on Serial console output)
if(abs(gx)<10){
gx = 0;
}
if(abs(gy)<30){
gy = 0;
}
if(abs(gz)<12){
gz = 0;
}
// scale angles down, rotate
vectRotXYZ((double)gy/MMPI_TIME, 1); // X
vectRotXYZ((double)-gx/MMPI_TIME, 2); // Y
vectRotXYZ((double)gz/MMPI_TIME, 3); // Z
#if DEBUG == true
Serial.print(scale);
Serial.print("\t");
Serial.print(ax);
Serial.print("\t");
Serial.print(ay);
Serial.print("\t");
Serial.print(az);
Serial.print("\t");
Serial.print((uint8_t) gx);
Serial.print("\t");
Serial.print((uint8_t) gy);
Serial.print("\t");
Serial.println((uint8_t) gz);
#endif
// calculate each point coords
P1[0] = centerX + scale/(1+C1[2]/sZ)*C1[0]; P1[1] = centerY + scale/(1+C1[2]/sZ)*C1[1];
P2[0] = centerX + scale/(1+C2[2]/sZ)*C2[0]; P2[1] = centerY + scale/(1+C2[2]/sZ)*C2[1];
P3[0] = centerX + scale/(1+C3[2]/sZ)*C3[0]; P3[1] = centerY + scale/(1+C3[2]/sZ)*C3[1];
P4[0] = centerX + scale/(1+C4[2]/sZ)*C4[0]; P4[1] = centerY + scale/(1+C4[2]/sZ)*C4[1];
P5[0] = centerX + scale/(1+C5[2]/sZ)*C5[0]; P5[1] = centerY + scale/(1+C5[2]/sZ)*C5[1];
P6[0] = centerX + scale/(1+C6[2]/sZ)*C6[0]; P6[1] = centerY + scale/(1+C6[2]/sZ)*C6[1];
P7[0] = centerX + scale/(1+C7[2]/sZ)*C7[0]; P7[1] = centerY + scale/(1+C7[2]/sZ)*C7[1];
P8[0] = centerX + scale/(1+C8[2]/sZ)*C8[0]; P8[1] = centerY + scale/(1+C8[2]/sZ)*C8[1];
// draw each cube edge
u8g.drawLine(P1[0], P1[1], P2[0], P2[1]); //1-2
u8g.drawLine(P1[0], P1[1], P3[0], P3[1]); //1-3
u8g.drawLine(P1[0], P1[1], P5[0], P5[1]); //1-5
u8g.drawLine(P2[0], P2[1], P4[0], P4[1]); //2-4
u8g.drawLine(P2[0], P2[1], P6[0], P6[1]); //2-6
u8g.drawLine(P3[0], P3[1], P4[0], P4[1]); //3-4
u8g.drawLine(P3[0], P3[1], P7[0], P7[1]); //3-7
u8g.drawLine(P4[0], P4[1], P8[0], P8[1]); //4-8
u8g.drawLine(P5[0], P5[1], P6[0], P6[1]); //5-6
u8g.drawLine(P5[0], P5[1], P7[0], P7[1]); //5-7
u8g.drawLine(P6[0], P6[1], P8[0], P8[1]); //6-8
u8g.drawLine(P7[0], P7[1], P8[0], P8[1]); //7-8
}
void vectRotXYZ(double angle, int axe) {
int8_t m1; // coords polarity
uint8_t i1, i2; // coords index
switch(axe) {
case 1: // X
i1 = 1; // y
i2 = 2; // z
m1 = -1;
break;
case 2: // Y
i1 = 0; // x
i2 = 2; // z
m1 = 1;
break;
case 3: // Z
i1 = 0; // x
i2 = 1; // y
m1 = 1;
break;
}
double t1 = C1[i1];
double t2 = C1[i2];
C1[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C1[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C2[i1];
t2 = C2[i2];
C2[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C2[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C3[i1];
t2 = C3[i2];
C3[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C3[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C4[i1];
t2 = C4[i2];
C4[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C4[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C5[i1];
t2 = C5[i2];
C5[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C5[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C6[i1];
t2 = C6[i2];
C6[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C6[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C7[i1];
t2 = C7[i2];
C7[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C7[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
t1 = C8[i1];
t2 = C8[i2];
C8[i1] = t1*cos(angle)+(m1*t2)*sin(angle);
C8[i2] = (-m1*t1)*sin(angle)+t2*cos(angle);
}