-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCutdown_Commission.cpp
312 lines (243 loc) · 11.3 KB
/
Cutdown_Commission.cpp
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
/* Author: Alex St. Clair
* Filename: Cutdown_Commission.cpp
* Created: 1-9-19
*
* Used to initially flash the Flash-Emulated EEPROM
* and test hardware when a board is commissioned.
*/
#include "Cutdown_Commission.h"
#include <Cutdown.h>
#include <Cutdown_Configure.h>
#define SPSC_LATITUDE 40.011
#define SPSC_LONGITUDE -105.246
#define LARAMIE_LATITUDE 41.312907
#define LARAMIE_LONGITUDE -105.660194
#define MCMURDO_LATITUDE -77.848943
#define MCMURDO_LONGITUDE 166.661243
#define PAWNEE_LATITUDE 40.768439
#define PAWNEE_LONGITUDE -104.638230
// -------- CHANGE CONFIGS HERE --------
#define SERIAL_NUMBER 200
#define SYSTEM_MODE MODE_CUTDOWN
#define SQUIB_MODE ONE_SQUIB
#define CUTAWAY_TIMER 18000 // s (5 hours)
#define CUTAWAY_BACKUP_TIMER 18300 // s (5 hours 5 minutes)
#define CUTDOWN_TIMER 7200 // s (>18 hours)
#define CUTDOWN_BACKUP_TIMER 7500 // s (>18 hours)
#define LOW_ALT_TIMER 120 // s
#define CUTDOWN_HEIGHT 30.0 // km
#define CUTDOWN_DISTANCE 100.0 // km
#define CUTAWAY_CEILING 700 // hPA ~= 10k ft ~= 3 km
#define FLY_VOLTAGE_SETPOINT 11.8 // V
#define LOW_VOLTAGE_SETPOINT 11.1 // V
#define CRIT_VOLTAGE_SETPOINT 10.6 // V
#define DEFAULT_LATITUDE MCMURDO_LATITUDE
#define DEFAULT_LONGITUDE MCMURDO_LONGITUDE
#define BURST_FALL_RATE 10.0 // m/s
#define SINK_NUM_SAMPLES 10 // 10 samples at 0.5 Hz
#define DEFAULT_TEMP_SETPOINT 5.0 // C
// -------------------------------------
Cutdown commission_cutdown;
Cutdown_Configuration_t config_compare = {0};
bool write_config(void)
{
bool success = true;
cutdown_config.config_version = CURRENT_CONFIG_VERSION;
cutdown_config.serial_number = SERIAL_NUMBER;
if (SYSTEM_MODE == MODE_CUTAWAY) {
cutdown_config.system_mode = MODE_CUTAWAY;
cutdown_config.primary_timer = CUTAWAY_TIMER;
cutdown_config.primary_timer_remaining = CUTAWAY_TIMER;
cutdown_config.backup_timer = CUTAWAY_BACKUP_TIMER;
} else if (SYSTEM_MODE == MODE_CUTDOWN) {
cutdown_config.system_mode = MODE_CUTDOWN;
cutdown_config.primary_timer = CUTDOWN_TIMER;
cutdown_config.primary_timer_remaining = CUTDOWN_TIMER;
cutdown_config.backup_timer = CUTDOWN_BACKUP_TIMER;
} else {
Serial.println("Invalid system mode for config!");
return false;
}
cutdown_config.low_alt_timer = LOW_ALT_TIMER;
cutdown_config.trigger_height = CUTDOWN_HEIGHT;
cutdown_config.trigger_distance = CUTDOWN_DISTANCE;
cutdown_config.cutaway_ceiling = CUTAWAY_CEILING;
cutdown_config.min_fly_voltage = FLY_VOLTAGE_SETPOINT;
cutdown_config.low_batt_voltage = LOW_VOLTAGE_SETPOINT;
cutdown_config.critical_batt_voltage = CRIT_VOLTAGE_SETPOINT;
cutdown_config.origin_lat = DEFAULT_LATITUDE;
cutdown_config.origin_long = DEFAULT_LONGITUDE;
cutdown_config.temp_set_point = DEFAULT_TEMP_SETPOINT;
cutdown_config.burst_fall_rate = BURST_FALL_RATE;
cutdown_config.sink_num_samples = SINK_NUM_SAMPLES;
cutdown_config.squib_mode = SQUIB_MODE;
cutdown_config.trigger_type = TRIG_NONE;
cutdown_config.ceiling_reached = false;
Serial.println("Writing config to FEE");
write_config_to_fee();
config_compare = cutdown_config;
Serial.println("Reading back config from FEE");
load_config_from_fee();
Serial.println("Verifying config read from FEE");
success &= (config_compare.config_version == cutdown_config.config_version);
Serial.print("Version: 0x"); Serial.println(cutdown_config.config_version, HEX);
success &= (config_compare.serial_number == cutdown_config.serial_number);
Serial.print("Serial #: "); Serial.println(cutdown_config.serial_number);
success &= (config_compare.primary_timer == cutdown_config.primary_timer);
Serial.print("Primary timer: "); Serial.println(cutdown_config.primary_timer);
success &= (config_compare.primary_timer_remaining == cutdown_config.primary_timer_remaining);
Serial.print("Remaining primary timer: "); Serial.println(cutdown_config.primary_timer_remaining);
success &= (config_compare.backup_timer == cutdown_config.backup_timer);
Serial.print("Backup timer: "); Serial.println(cutdown_config.backup_timer);
success &= (config_compare.low_alt_timer == cutdown_config.low_alt_timer);
Serial.print("Low alt timer: "); Serial.println(cutdown_config.low_alt_timer);
success &= (config_compare.trigger_height == cutdown_config.trigger_height);
Serial.print("Trigger height: "); Serial.println(cutdown_config.trigger_height);
success &= (config_compare.trigger_distance == cutdown_config.trigger_distance);
Serial.print("Trigger distance: "); Serial.println(cutdown_config.trigger_distance);
success &= (config_compare.cutaway_ceiling == cutdown_config.cutaway_ceiling);
Serial.print("Cutaway ceiling: "); Serial.println(cutdown_config.cutaway_ceiling);
success &= (config_compare.min_fly_voltage == cutdown_config.min_fly_voltage);
Serial.print("Min fly voltage: "); Serial.println(cutdown_config.min_fly_voltage);
success &= (config_compare.low_batt_voltage == cutdown_config.low_batt_voltage);
Serial.print("Low voltage: "); Serial.println(cutdown_config.low_batt_voltage);
success &= (config_compare.critical_batt_voltage == cutdown_config.critical_batt_voltage);
Serial.print("Crit voltage: "); Serial.println(cutdown_config.critical_batt_voltage);
success &= (config_compare.origin_lat == cutdown_config.origin_lat);
Serial.print("Latitude: "); Serial.println(cutdown_config.origin_lat);
success &= (config_compare.origin_long == cutdown_config.origin_long);
Serial.print("Longitude: "); Serial.println(cutdown_config.origin_long);
success &= (config_compare.temp_set_point == cutdown_config.temp_set_point);
Serial.print("Temp set point: "); Serial.println(cutdown_config.temp_set_point);
success &= (config_compare.burst_fall_rate == cutdown_config.burst_fall_rate);
Serial.print("Burst fall rate: "); Serial.println(cutdown_config.burst_fall_rate);
success &= (config_compare.sink_num_samples == cutdown_config.sink_num_samples);
Serial.print("Sink num samples: "); Serial.println(cutdown_config.sink_num_samples);
success &= (config_compare.ceiling_reached == cutdown_config.ceiling_reached);
Serial.print("Ceiling reached: "); Serial.println(cutdown_config.ceiling_reached);
success &= (config_compare.squib_mode == cutdown_config.squib_mode);
Serial.print("Squibs: "); Serial.println(cutdown_config.squib_mode);
success &= (config_compare.trigger_type == cutdown_config.trigger_type);
Serial.print("Trigger result: "); Serial.println(cutdown_config.trigger_type);
success &= (config_compare.system_mode == cutdown_config.system_mode);
Serial.print("Mode: ");
if (cutdown_config.system_mode == MODE_CUTDOWN) {
Serial.println("CUTDOWN");
} else if (cutdown_config.system_mode == MODE_CUTAWAY) {
Serial.println("CUTAWAY");
} else {
Serial.println("ERROR!");
}
return success;
}
void commission_setup(void)
{
delay(5000);
if (load_config_from_fee()) {
Serial.println("Config already in place, overwriting");
}
// write initial configurations to Flash-Emulated EEPROM
if (write_config()) {
Serial.println("\nConfig write successful\n");
} else {
Serial.println("\nERROR: invalid config\n");
while(1);
}
delay(1000);
Serial.println("Initializing drivers");
cutdown_pinmux();
commission_cutdown.oled.init();
commission_cutdown.attiny.init();
commission_cutdown.adc.init();
commission_cutdown.gps.init();
Wire.begin();
commission_cutdown.baro.begin();
Serial.println("Initialized drivers\n");
delay(1000);
Serial.println("Writing OLED test lines");
commission_cutdown.oled.write_line("abcdefghijklmnop",LINE1);
commission_cutdown.oled.write_line("qrstuvwxyz123456",LINE2);
Serial.println("Completed OLED lines\n");
delay(1000);
Serial.println("Writing ATtiny timer");
if (commission_cutdown.attiny.write_timer(cutdown_config.backup_timer)) {
Serial.println("Successfully wrote ATtiny timer\n");
} else {
Serial.println("ERROR: unable to write ATtiny timer\n");
}
delay(1000);
Serial.println("Checking ADC values");
Serial.print("Primary squib: ");
Serial.print(commission_cutdown.adc.squib_pri.read());
if (check_squib(commission_cutdown.adc.squib_pri.check())) {
Serial.println(" (appears in place)");
} else {
Serial.println(" (appears missing)");
}
Serial.print("Backup squib: ");
Serial.print(commission_cutdown.adc.squib_bck.read());
if (check_squib(commission_cutdown.adc.squib_bck.check())) {
Serial.println(" (appears in place)");
} else {
Serial.println(" (appears missing)");
}
Serial.print("Batt pri: ");
Serial.println(commission_cutdown.adc.v_batt_pri.read());
Serial.print("Batt sec: ");
Serial.println(commission_cutdown.adc.v_batt_bck.read());
Serial.print("Thermistor: ");
Serial.print(commission_cutdown.adc.thermistor.read());
Serial.print(" (");
Serial.print(calculate_temperature(commission_cutdown.adc.thermistor.check()));
Serial.println(" C)");
Serial.println("Checked all ADC values\n");
delay(1000);
Serial.println("Checking GPS");
if (commission_cutdown.gps.stop_nmea()) {
Serial.println("Muted NMEA strings");
} else {
Serial.println("ERROR: unable to mute NMEA strings");
}
if (commission_cutdown.gps.set_airborne()) {
Serial.println("Set airborne");
} else {
Serial.println("ERROR: unable to set airborne");
}
Serial.println("Finished checking GPS\n");
delay(1000);
Serial.println("Checking pressure sensor");
Serial.print("Pressure: ");
Serial.print(commission_cutdown.baro.getPressure() / 100.0f);
Serial.println(" hPa");
Serial.print("Temperature: ");
Serial.print(commission_cutdown.baro.getTemperature());
Serial.println(" C");
Serial.println("Finished checking pressure sensor\n");
if (commission_cutdown.arm_signal()) {
Serial.println("System reads armed");
} else {
Serial.println("System reads unarmed");
}
}
void commission_loop(void)
{
if (commission_cutdown.arm_signal()) {
Serial.println("System switched to armed");
delay(10000);
Serial.println("Firing pri squib in 1 s");
delay(1000);
digitalWrite(SQUIB_PRI_GATE, HIGH);
delay(1000);
digitalWrite(SQUIB_PRI_GATE, LOW);
Serial.println("Firing sec squib in 1 s");
delay(1000);
digitalWrite(SQUIB_BCK_GATE, HIGH);
delay(1000);
digitalWrite(SQUIB_BCK_GATE, LOW);
Serial.println("Powering off in 10 seconds unless disarmed");
delay(10000);
if (commission_cutdown.arm_signal()) {
cutdown_poweroff();
}
}
}