-
Notifications
You must be signed in to change notification settings - Fork 0
/
orientation.c
53 lines (44 loc) · 1.32 KB
/
orientation.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
#include "orientation.h"
orientation_t orient;
void OrientSys_Init()
{
int s, i;
do {
s = MPU_Init(MPU6050_RANGE_2G, MPU6050_SCALE_2000DPS);
WHITE_LED = ~WHITE_LED;
for (i = 0; i < 20; i++)
__delay_ms(10);
} while (!s);
WHITE_LED = 0;
//Reset Rotation
orient.rot.x = 0;
orient.rot.y = 0;
orient.rot.z = 0;
orient.flags = 0;
}
void OrientSys_Update()
{
vec3f gyro, accel;
MPU_GetGyro(&gyro);
MPU_GetAccel(&accel);
orient.rot.x = atan(accel.x / sqrt(pow(accel.y, 2) + pow(accel.z, 2))) * 57.2958f;
orient.rot.y = atan(accel.y / sqrt(pow(accel.x, 2) + pow(accel.z, 2))) * 57.2958f;
orient.rot.z = atan(accel.z / sqrt(pow(accel.y, 2) + pow(accel.x, 2))) * 57.2958f;
OrientSys_SetFlag(UP, orient.rot.y < -40);
OrientSys_SetFlag(DOWN, orient.rot.y > 40);
OrientSys_SetFlag(LEFT, orient.rot.x < -40);
OrientSys_SetFlag(RIGHT, orient.rot.x > 40);
OrientSys_SetFlag(STABLE, !OrientSys_Is(UP) && !OrientSys_Is(DOWN) && !OrientSys_Is(LEFT) && !OrientSys_Is(RIGHT));
}
byte OrientSys_Is(orientation_states_t state)
{
return (orient.flags & state) != 0;
}
void OrientSys_SetFlag(orientation_states_t state, byte value)
{
if (value) {
orient.flags |= state;
} else {
orient.flags &= ~(state);
}
}