-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.pde
138 lines (123 loc) · 3.33 KB
/
state.pde
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
/* state.pde
keep track of current state variables
like orientation, position, battery life, motor speed, etc.
*/
/* description of flight modes!
SAFEMODE - stabilized, slowly de-throttle (for possible in-flight failures)
LANDED - zero throttle, determined by proximity to land
STABILIZE - orientation stabilization, no position constraints
ALT_HOLD - adjust throttle to maintain constant altitude
POS_HOLD - constant altitude and GPS position (for photography?)
*/
void changeFlightmode(uint8_t newmode)
{
switch (newmode)
{
case SAFEMODE:
yaw_hold = yaw;
safemodeLift = targetLift;
break;
case LANDED:
break;
case STABILIZE:
yaw_hold = yaw;
break;
case ALT_HOLD:
// store current alt
yaw_hold = yaw;
zpos_hold = altitude;
break;
case POS_HOLD:
// store current 3d position
yaw_hold = yaw;
xpos_hold = gps_xpos;
ypos_hold = gps_ypos;
zpos_hold = altitude;
break;
default: // somethings wrong
changeFlightmode(SAFEMODE);
return;
}
flightMode = newmode;
}
// loop through each batter and get a reading
// raw data needs to be converted to a voltage
// then maybe converted to a capacity estimate
void checkBattery(int index)
{
// volts = read * 5 * 2.424 / 1024
//batterylevel[index] = 0.118*analogRead(BATT_PIN[index]);
}
// check serial port for imu data
void checkIMU()
{
char buffer[4];
float temp1, temp2, temp3, temp4;
uint8_t count = 0;
// allow for multiple things, but dont get stuck here
while (SERIAL_IMU.available() && count<24)
{
// check for start of wireless message
switch (SERIAL_IMU.read())
{
count++;
case 'I': // orientation
SERIAL_IMU.readBytes(buffer,4);
memcpy(&temp1,buffer,4);
SERIAL_IMU.readBytes(buffer,4);
memcpy(&temp2,buffer,4);
SERIAL_IMU.readBytes(buffer,4);
memcpy(&temp3,buffer,4);
SERIAL_IMU.readBytes(buffer,4);
memcpy(&temp4,buffer,4);
if (temp1 + temp2 + temp3 == temp4)
{
pitch = temp1;
roll = temp2;
yaw = temp3;
newimu = 1;
//SERIAL_DEBUG.println(pitch);
//} else {
// SERIAL_DEBUG.print(temp4);
// SERIAL_DEBUG.print("\t");
// SERIAL_DEBUG.println(temp1+temp2+temp3);
}
count += 16;
#ifdef DEBUG
/*
SERIAL_DEBUG.print(pitch);
SERIAL_DEBUG.print("\t");
SERIAL_DEBUG.print(roll);
SERIAL_DEBUG.print("\t");
SERIAL_DEBUG.println(yaw);
*/
#endif
break;
case 'S': // gps status
SERIAL_IMU.readBytes((char*)&gps_quality,1);
count += 1;
break;
case 'P': // gps position
SERIAL_IMU.readBytes(buffer,4);
memcpy(&gps_xpos,buffer,4);
SERIAL_IMU.readBytes(buffer,4);
memcpy(&gps_ypos,buffer,4);
SERIAL_IMU.readBytes(buffer,4);
memcpy(&gps_zpos,buffer,4);
count += 12;
break;
case 'V': // gps velocity
SERIAL_IMU.readBytes(buffer,4);
memcpy(&gps_xvel,buffer,4);
SERIAL_IMU.readBytes(buffer,4);
memcpy(&gps_yvel,buffer,4);
count += 8;
break;
}
}
}
// poll for imu data
void pollIMU()
{
SERIAL_IMU.write(0x01);
}