forked from OpenStratos/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
110 lines (86 loc) · 2.56 KB
/
utils.h
File metadata and controls
110 lines (86 loc) · 2.56 KB
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
#ifndef UTILS_H_
#define UTILS_H_
#include <cmath>
#include <string>
#include <thread>
#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,
};
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()
{
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 > 8000) return set_state(GOING_DOWN);
else return set_state(LANDED);
}
inline bool has_launched(double launch_altitude)
{
for (int i = 0; ! GPS::get_instance().is_fixed() && i < 10; ++i);
if ( ! GPS::get_instance().is_fixed()) return false;
double first_altitude = GPS::get_instance().get_altitude();
if (first_altitude > launch_altitude + 100) return true;
this_thread::sleep_for(5s);
double second_altitude = GPS::get_instance().get_altitude();
#if defined SIM || defined REAL_SIM
return true;
#else
return second_altitude > first_altitude + 10;
#endif
}
inline bool has_bursted(double maximum_altitude)
{
for (int i = 0; ! GPS::get_instance().is_fixed() && i < 10; ++i);
if ( ! GPS::get_instance().is_fixed()) return false;
double first_altitude = GPS::get_instance().get_altitude();
if (first_altitude < maximum_altitude - 1000) return true;
this_thread::sleep_for(6s);
double second_altitude = GPS::get_instance().get_altitude();
#if defined SIM || defined REAL_SIM
return true;
#else
return second_altitude < first_altitude - 10;
#endif
}
inline bool has_landed()
{
for (int i = 0; ! GPS::get_instance().is_fixed() && i < 10; ++i);
if ( ! GPS::get_instance().is_fixed()) return false;
double first_altitude = GPS::get_instance().get_altitude();
this_thread::sleep_for(5s);
double second_altitude = GPS::get_instance().get_altitude();
return abs(first_altitude-second_altitude) < 5;
}
}
#endif // UTILS_H_