-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsac2020_lib.h
More file actions
267 lines (226 loc) · 7.45 KB
/
sac2020_lib.h
File metadata and controls
267 lines (226 loc) · 7.45 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
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
/**
* [ANOTHER FINE PRODUCT FROM THE NONSENSE FACTORY]
*
* Flight software for the Longhorn Rocketry Association's Spaceport America
* Cup 2020 rocket. Built for the LRA Generation 2 flight computer, which also
* flies in LRA's NASA Student Launch 2020 rocket. The configurability of the
* software--combined with modularity and all-in-one-package style of the
* Generation 2 flight computer--means the software can be easily adapted to run
* in any high-power rocket.
*
* @file sac2020_lib.h
* @purpose Supporting code that is common to both main and auxiliary flight
* computer nodes.
* @author Stefan deBruyn
* @updated 2/22/2020
*/
#ifndef SAC2020_LIB_H
#define SAC2020_LIB_H
#include <vector>
#include "sac2020_state.h"
// Magnotomitor
// min: { -3347, -5192, -4355} max: { +3467, +2301, +3515}
#define MAG0MAX 3467
#define MAG0MIN -3347
#define MAG1MAX 2301
#define MAG1MIN -5192
#define MAG2MAX 3515
#define MAG2MIN -4355
//float magOffset[3] = { (MAG0MAX + MAG0MIN) / 2, (MAG1MAX + MAG1MIN) / 2, (MAG2MAX + MAG2MIN) / 2 };
//double magGain[3];
#ifndef FF
/**
* Whether or not computer is being tested on the ground, inert, outside
* of a rocket. THIS SHOULD BE COMMENTED OUT ON LAUNCH DAY.
*/
// #define GROUND_TEST
/**
* Toggles debug prints to Serial.
*/
#define DEBUG_SERIAL Serial
/**
* Flight network communication line (serial line between main and aux FCs).
*/
#define FNW_SERIAL Serial1
/**
* Serial line used to communicate with Bluetooth module.
*/
#define BLE_SERIAL Serial3
/**
* Baud rate for FNW_SERIAL.
*/
#define FNW_BAUD 115200
/**
* Whether or not we are using the flight computer network. If defined, the
* flight computers will attempt to handshake on setup() and block until
* consensus is established.
*/
#define USING_FNW
/**
* Size of the telemetry packets sent from main to aux. The size of the
* Arduino serial rx/tx buffers must be >= this number. The buffer size is
* 64 by default and may need to be modified in the Serial source code.
*/
#define FNW_PACKET_SIZE 127
/**
* Maximum time to wait for a handshake when establishing FNW connection.
*/
#define FNW_CONN_TIMEOUT 5
/**
* Whether or not a packet was received over FNW.
*/
#define FNW_PACKET_AVAILABLE (FNW_SERIAL.available() == FNW_PACKET_SIZE)
#endif
/**
* Cause of a state machine transition, used in debugging.
*/
typedef enum Reason : uint8_t
{
CONDITION,
TIMEOUT
} Reason_t;
/**
* Determines where time stands relative to an event window. Should be followed
* by an EVENT_WINDOW_EVAL().
*
* @param k_t Current time.
* @param k_t_low Lower event window bound. <= 0 indicates no bound.
* @param k_t_high Upper event window bound. <= 0 indicates no bound.
*/
#define EVENT_WINDOW_INIT(k_t, k_t_low, k_t_high) \
float _t_since_liftoff = k_t; \
bool _grace_period_over = _t_since_liftoff >= k_t_low; \
bool _timed_out = k_t_high > 0 && _t_since_liftoff >= k_t_high; \
Reason_t _reason;
/**
* Evaluates whether or not an event should trigger. Should be preceeded by an
* EVENT_WINDOW_INIT().
*
* @param k_conds_ok If time-invariant trigger conditions are met, i.e. those
* conditions not related to the detection window.
*
* @ret If the event should trigger.
*/
#define EVENT_WINDOW_EVAL(k_conds_ok) \
((_reason = (_timed_out ? Reason_t::TIMEOUT : Reason_t::CONDITION)) || \
(_grace_period_over && (_timed_out || k_conds_ok)))
/**
* Gets a string representation of an event detection reason. Should be
* preceeded by an EVENT_WINDOW_EVAL() that evaluated to true.
*/
#define EVENT_WINDOW_REASON \
(_reason == Reason_t::CONDITION ? "CONDITION" : "TIMEOUT")
/********************************* TYPEDEFS ***********************************/
/**
* Denotes the status of a particular system component.
*/
typedef enum Status : uint8_t
{
OFFLINE, // Uninitialized.
ONLINE, // Initialized and running.
FAULT // Encountered an issue.
} Status_t;
/**
* Tokens are 1-byte pieces of metadata sent between flight computers.
*/
typedef uint8_t token_t;
/********************************* GLOBALS ************************************/
/**
* Tokens sent between flight computers.
*/
const token_t FNW_TOKEN_NIL = 0x00; // Null token.
const token_t FNW_TOKEN_AOK = 0xAA; // Something went OK.
const token_t FNW_TOKEN_ERR = 0xBB; // Something went wrong.
const token_t FNW_TOKEN_HSH = 0xCC; // Handshake payload follows.
const token_t FNW_TOKEN_VEC = 0xDD; // State vector payload follows.
/******************************** PROTOTYPES **********************************/
/**
* Controls the status LEDs on the flight computer board.
*/
class LEDController final
{
public:
/**
* Configures a new LED controller.
*
* @param k_pins List of LED pin numbers. These will be set to OUTPUT
* mode, and stay solid by default.
*/
LEDController(std::vector<uint8_t> k_pins);
/**
* Makes a pin flash while the controller is run.
*
* @param k_pin Pin number.
*/
void flash(uint8_t k_pin);
/**
* Makes a pin solid while the controller is run.
*
* @param k_pin Pin number.
*/
void solid(uint8_t k_pin);
/**
* Runs the controller by blinking LEDs as appropriate. Should be called
* at a relatively high frequency.
*
* @param k_t Current time in seconds.
* @param k_pin Pin to run. If -1, all pins run.
*/
void run(float k_t, int32_t k_pin=-1);
/**
* Lowers all LEDs.
*/
void lower_all();
/**
* Raises all LEDs.
*/
void raise_all();
private:
/**
* Number of state changes (either HIGH -> LOW or LOW -> HIGH) to make on
* a flashing LED per second.
*/
static constexpr uint8_t PULSES = 3;
/**
* State of a single pin.
*/
typedef struct Pin
{
uint8_t num; // Pin number.
bool high; // Whether or not LED is on.
bool flash; // True if flashing, false if solid.
float t_flash_last; // Time of last flash. -1 if none.
} Pin_t;
/**
* Pin states, updated as the controller is run.
*/
std::vector<Pin_t> m_pins;
};
/**
* Raises a flight computer fault for a particular component.
*
* @param k_pin Fault indicator LED pin.
* @param k_msg Fault message, printed to DEBUG_SERIAL if defined.
* @param k_stat Global status for component to be set to Status_t::FAULT.
* @param k_ledc LED controller.
*/
void fault(uint8_t k_pin, const char* k_msg, Status_t& k_stat,
LEDController* k_ledc);
/**
* Gets the time according to Arduino's millis(), reinterpreted as seconds.
*
* @ret Current system time in seconds.
*/
double time_s();
/**
* Sanitizes a string literal with FF format codes in it. Operates on the same
* pointer and so destroys the results of previous calls. Supports a maximum
* string size of 128 characters (including null terminator).
*
* @param k_str String to sanitize.
* @param k_len String length.
*
* @ret Sanitized string.
*/
char* sanitize_ff_fmt(const char k_str[], uint32_t k_len);
#endif