-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.c
344 lines (275 loc) · 8 KB
/
cube.c
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
#include "GL/freeglut.h"
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "I2CWrapper.h"
#include "mpu6050.h"
#include <math.h>
float X_AXIS = 0.0, Y_AXIS = 0.0, Z_AXIS = 0.0;
float DIRECTION = 1;
GForceStruct Data;
const unsigned long long nano = 1000000000;
GLfloat vertices[][3] = {
{ -0.3, 0.3, -0.3 },
{ -0.3, 0.3, 0.3 },
{ 0.3, 0.3, 0.3 },
{ 0.3, 0.3, -0.3 },
{ -0.3, -0.3, -0.3 },
{ -0.3, -0.3, 0.3 },
{ 0.3, -0.3, 0.3 },
{ 0.3, -0.3, -0.3 }
};
GLfloat colors[][3] = {
{ 0.0, 0.0, 1.0 }, // blue
{ 1.0, 1.0, 0.0 }, // yellow
{ 0.0, 1.0, 0.0 }, // green
{ 1.0, 0.0, 0.0 }, // red
{ 0.5, 0.5, 0.0 }, // gold
{ 0.0, 1.0, 1.0 } // magenta
};
//unsigned long t1 = 0, t2 = 0, delay = 0;
float dt = 0;
int i2c_handle;
const BUS = 1;
int I2C_Current_Slave_Adress=0x68;
float accel_dynamic_range = 2.0;
float gyro_dynamic_range = 250.0;
float SIGNIFICANCE_FACTOR = 0.95;
float PI = 3.141592;
float curr_gyrox, curr_gyroy, curr_gyroz;
float gyrox_off, gyroy_off, gyroz_off;
float anglex, angley, anglez;
double start, end;
float before_anglex, before_angley, before_anglez;
float rot_x, rot_y, rot_z, gy_anglex, gy_angley, gy_anglez;
float velox=0.0, veloy=0.0, veloz=0.0;
float dis_x, dis_y, dis_z;
float dis_accelx, dis_accely, dis_accelz;
float tran_x, tran_y, tran_z;
float before_dis_x, before_dis_y, before_dis_z;
const float AccFactor=16.0 /32768.0;
const float GyroFactor=500.0 / 32768.0;
int ExitFlag=0;
float* ziro()
{
static float arr[3];
i2c_handle = I2CWrapperOpen(BUS,I2C_Current_Slave_Adress);
Setup_MPU6050(i2c_handle);
Get_Accel_Values(i2c_handle,&Data);
start = (double)clock() / CLOCKS_PER_SEC;
int16_t accelx_raw = Data.Gx;
int16_t accely_raw = Data.Gy;
int16_t accelz_raw = Data.Gz;
int16_t temp_raw = Data.Temperature;
int16_t gyrox_raw = Data.Gyrox;
int16_t gyroy_raw = Data.Gyroy;
int16_t gyroz_raw = Data.Gyroz;
anglex = before_anglex;
angley = before_angley;
anglez = before_anglez;
float accelx = (((float) ((float) accelx_raw)/((float) 16384.0)));
float accely = (((float) ((float) accely_raw)/((float) 16384.0)));
float accelz = (((float) ((float) accelz_raw)/((float) 16384.0)));
float temp = ((float) temp_raw)/340.0 + 36.53;
float gyrox = (((float) ((float) gyrox_raw)/((float) 131.0)));
float gyroy = (((float) ((float) gyroy_raw)/((float) 131.0)));
float gyroz = (((float) ((float) gyroz_raw)/((float) 131.0)));
gy_anglex += (gyrox)*((float) dt/1000000.0);
gy_angley += (gyroy)*((float) dt/1000000.0);
gy_anglez += (gyroz)*((float) dt/1000000.0);
float xAcc_ang = atan2(accelx, accelz)*180/PI;
float yAcc_ang = atan2(accely, accelz)*180/PI;
float zAcc_ang = atan2(accelx, accelz)*180/PI;
rot_x = atanf(accely / sqrt(accelx*accelx + accelz*accelz))*180/PI;
rot_y = -atanf(accelx/ sqrt(accely*accely + accelz*accelz))*180/PI;
anglex = (anglex*SIGNIFICANCE_FACTOR) + (1-SIGNIFICANCE_FACTOR)*rot_x;
angley = (angley*SIGNIFICANCE_FACTOR) + (1-SIGNIFICANCE_FACTOR)*rot_y;
anglez = (anglez+gy_anglez)*SIGNIFICANCE_FACTOR + (1-SIGNIFICANCE_FACTOR)*rot_z;
before_anglex = anglex;
before_angley = angley;
before_anglez = anglez;
float angles360x = anglex;
float angles360y = angley;
float angles360z = anglez;
while (angles360x > 360.0) {
angles360x -= 360.0;
}
while (angles360x < 0) {
angles360x += 360.0;
}
while (angles360y > 360.0) {
angles360y -= 360.0;
}
while (angles360y < 0) {
angles360y += 360.0;
}
while (angles360z > 360.0) {
angles360z -= 360.0;
}
while (angles360z < 0) {
angles360z += 360.0;
}
arr[0] = angles360x;
arr[1] = angles360y;
arr[2] = angles360z;
end = (((double)clock()) / CLOCKS_PER_SEC);
dt = (end - start);
return arr;
}
void drawBitmapText(char *str, float x, float y, float z)
{
glRasterPos3f(x, y, z);
while(*str)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *str);
str++;
}
}
void draw_line()
{
float legth = 0.5;
glPushMatrix(); //X축 붉은색
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(legth, 0.0, 0.0);
glVertex3f(-legth, 0.0, 0.0);
glEnd();
drawBitmapText("-Z", legth-0.05, 0.0, 0.0);
drawBitmapText("+Z", -legth, 0.0, 0.0);
glPopMatrix();
glPushMatrix(); //+X축 원불
glColor3f(1.0, 0.0, 0.0);
glTranslatef(legth, 0.0, 0.0);
glRotatef(90,0.0, legth, 0.0);
glutSolidCone(0.01,0.03,100,100);
glEnd();
glPopMatrix();
glPushMatrix(); //+X축 원불
glColor3f(1.0, 0.0, 0.0);
glTranslatef(-legth, 0.0, 0.0);
glRotatef(-90,0.0, legth, 0.0);
glutSolidCone(0.01,0.03,100,100);
glEnd();
glPopMatrix();
glPushMatrix(); //Y축 녹색
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, legth, 0.0);
glVertex3f(0.0, -legth, 0.0);
glEnd();
drawBitmapText("-X", 0.0, legth-0.05, 0.0);
drawBitmapText("+X", 0.0, -legth, 0.0);
glPopMatrix();
glPushMatrix(); //+Y축 원불
glColor3f(0.0, 1.0, 0.0);
glTranslatef(0.0, legth, 0.0);
glRotatef(-90,legth, 0.0, 0.0);
glutSolidCone(0.01,0.03,100,100);
glEnd();
glPopMatrix();
glPushMatrix(); //-Y축 원불
glColor3f(0.0, 1.0, 0.0);
glTranslatef(0.0, -legth, 0.0);
glRotatef(270,-legth, 0.0, 0.0);
glutSolidCone(0.01,0.03,100,100);
glEnd();
glPopMatrix();
glPushMatrix(); //Z축 파란색
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, legth);
glVertex3f(0.0, 0.0, -legth);
glEnd();
drawBitmapText("-Y", 0.0, 0.0, legth-0.05);
drawBitmapText("+Y", 0.0, 0.0, -legth);
glPopMatrix();
glPushMatrix(); //+Z축 원불
glColor3f(0.0, 0.0, 1.0);
glTranslatef(0.0, 0.0, legth);
glRotatef(90, 0.0, 0.0, legth);
glutSolidCone(0.01,0.03,100,100);
glEnd();
glPopMatrix();
glPushMatrix(); //-Z축 원불
glColor3f(0.0, 0.0, 1.0);
glTranslatef(0.0, 0.0, -legth);
glRotatef(180, legth, 0.0, 0.0);
glutSolidCone(0.01,0.03,100,100);
glEnd();
glPopMatrix();
glFlush();
}
void display(){
float* ptr;
ptr = ziro();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,0);
glRotatef(X_AXIS, 1.0, 0.0, 0.0);
glRotatef(Y_AXIS, 0.0, 1.0, 0.0);
glRotatef(Z_AXIS, 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[1]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[6]);
glVertex3fv(vertices[5]);
glColor3fv(colors[2]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[7]);
glVertex3fv(vertices[6]);
glVertex3fv(vertices[2]);
glColor3fv(colors[3]);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[7]);
glVertex3fv(vertices[4]);
glColor3fv(colors[4]);
glVertex3fv(vertices[7]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[5]);
glVertex3fv(vertices[6]);
glColor3fv(colors[5]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[5]);
glEnd();
X_AXIS = ptr[0];
Y_AXIS = ptr[1];
//Z_AXIS = ptr[2];
draw_line();
glutSwapBuffers();
}
void Init(){
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Cube");
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0) ;
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv){
glutInit(&argc, argv);
Init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}