This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
317 lines (274 loc) · 7.22 KB
/
utils.h
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
#ifndef UTILS_H_
#define UTILS_H_
#include <cmath>
#include <string>
#include <iomanip>
#include <thread>
#if defined SIM || defined REAL_SIM
#include <chrono>
#endif
#include <sys/stat.h>
#include <sys/statvfs.h>
#include "constants.h"
#include "logger/Logger.h"
#include "gps/GPS.h"
namespace os {
enum State {
INITIALIZING,
ACQUIRING_FIX,
FIX_ACQUIRED,
WAITING_LAUNCH,
GOING_UP,
GOING_DOWN,
LANDED,
SHUT_DOWN,
SAFE_MODE,
};
template <typename T>
string to_string_prec(const T value, const int n = 6)
{
ostringstream out;
out << std::setprecision(n) << value;
return out.str();
}
void check_or_create(const string& path, Logger* logger = NULL);
inline bool file_exists(const string& name)
{
struct stat buffer;
return stat(name.c_str(), &buffer) == 0;
}
inline float get_available_disk_space()
{
struct statvfs fs;
statvfs("data", &fs);
return ((float) fs.f_bsize)*fs.f_bavail;
}
State set_state(State new_state);
State get_last_state();
const string state_to_string(State state);
inline State get_real_state()
{
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(1s);
}
double start_alt = GPS::get_instance().get_altitude();
this_thread::sleep_for(5s);
double end_alt = GPS::get_instance().get_altitude();
if (end_alt - start_alt < -10)
{
return set_state(GOING_DOWN);
}
else if (end_alt - start_alt > 5)
{
return set_state(GOING_UP);
}
else if (end_alt > 1500)
{
return set_state(GOING_DOWN);
}
else
{
return set_state(LANDED);
}
}
inline bool has_launched(double launch_altitude)
{
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
if ( ! GPS::get_instance().is_fixed())
{
return false;
}
#if !defined SIM && !defined REAL_SIM
chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
#endif
double first_altitude = GPS::get_instance().get_altitude();
if (first_altitude > launch_altitude + 50*GPS::get_instance().get_VDOP())
{
return true;
}
this_thread::sleep_for(5s);
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
if ( ! GPS::get_instance().is_fixed())
{
return false;
}
#if defined SIM || defined REAL_SIM
return true;
#else
chrono::high_resolution_clock::time_point end = chrono::high_resolution_clock::now();
int sec = chrono::duration_cast<chrono::seconds>(end-start).count();
double second_altitude = GPS::get_instance().get_altitude();
return second_altitude > first_altitude + (10*GPS::get_instance().get_VDOP()*sec)/5;
#endif
}
inline bool has_bursted(double maximum_altitude)
{
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
if ( ! GPS::get_instance().is_fixed())
{
return false;
}
#if !defined SIM && !defined REAL_SIM
chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
#endif
double first_altitude = GPS::get_instance().get_altitude();
if (first_altitude > maximum_altitude ||
(maximum_altitude < 4000 &&
first_altitude > maximum_altitude - 150*GPS::get_instance().get_VDOP()))
{
return false;
}
if (first_altitude < maximum_altitude - 1000)
{
return true;
}
this_thread::sleep_for(5s);
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
if ( ! GPS::get_instance().is_fixed())
{
return false;
}
#if defined SIM || defined REAL_SIM
return true;
#else
chrono::high_resolution_clock::time_point end = chrono::high_resolution_clock::now();
int sec = chrono::duration_cast<chrono::seconds>(end-start).count();
double second_altitude = GPS::get_instance().get_altitude();
return second_altitude < first_altitude - (15*GPS::get_instance().get_VDOP()*sec)/5;
#endif
}
inline bool has_landed()
{
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
if ( ! GPS::get_instance().is_fixed())
{
return false;
}
double first_altitude = GPS::get_instance().get_altitude();
if (first_altitude > 4000)
{
return false;
}
this_thread::sleep_for(5s);
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
if ( ! GPS::get_instance().is_fixed())
{
return false;
}
double second_altitude = GPS::get_instance().get_altitude();
return abs(first_altitude-second_altitude) < 5;
}
inline bool wait_up_for(double altitude, double& maximum_altitude)
{
#if defined SIM && !defined REAL_SIM
this_thread::sleep_for(2min);
if (altitude > FLIGHT_MAX_HEIGHT)
{
maximum_altitude = FLIGHT_MAX_HEIGHT;
return true;
}
else
{
maximum_altitude = altitude;
return false;
}
#elif defined REAL_SIM && !defined SIM
if (altitude > FLIGHT_MAX_HEIGHT)
{
double sleep = (FLIGHT_MAX_HEIGHT - maximum_altitude)/ASCENT_VELOCITY;
this_thread::sleep_for(std::chrono::duration<double>(sleep));
maximum_altitude = FLIGHT_MAX_HEIGHT;
return true;
}
else
{
double sleep = (altitude - maximum_altitude)/ASCENT_VELOCITY;
this_thread::sleep_for(std::chrono::duration<double>(sleep));
maximum_altitude = altitude;
return false;
}
#else
bool bursted;
while ( ! (bursted = has_bursted(maximum_altitude)))
{
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
double current_altitude = GPS::get_instance().get_altitude();
if (current_altitude > maximum_altitude)
{
maximum_altitude = current_altitude;
}
if (current_altitude >= altitude)
{
break;
}
}
return bursted;
#endif
}
inline bool wait_down_for(double altitude) {
#if defined SIM || defined REAL_SIM
if (altitude > FLIGHT_MAX_HEIGHT)
{
return false;
}
#endif
#if defined SIM && !defined REAL_SIM
this_thread::sleep_for(1min);
return false;
#elif defined REAL_SIM && !defined SIM
static double last_altitude;
if (last_altitude > FLIGHT_MAX_HEIGHT) {
double sleep = (FLIGHT_MAX_HEIGHT-altitude)/DESCENT_VELOCITY;
this_thread::sleep_for(std::chrono::duration<double>(sleep));
} else {
double sleep = (last_altitude-altitude)/DESCENT_VELOCITY;
this_thread::sleep_for(std::chrono::duration<double>(sleep));
}
last_altitude = altitude;
return false;
#else
for (int i = 0; i < 10 && ( ! GPS::get_instance().is_fixed() ||
GPS::get_instance().get_VDOP() > MAX_DOP); ++i)
{
this_thread::sleep_for(500ms);
}
while (GPS::get_instance().get_altitude() > altitude &&
! has_landed())
{
this_thread::sleep_for(3s);
}
return has_landed();
#endif
}
}
#endif // UTILS_H_