-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlight_ManualMotion.cpp
More file actions
131 lines (115 loc) · 4.02 KB
/
Flight_ManualMotion.cpp
File metadata and controls
131 lines (115 loc) · 4.02 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
/*
* ManualMotion.cpp
* Author: Alex St. Clair
* Created: October 2019
*/
#include "StratoPIB.h"
enum ManualMotionStates_t {
ST_ENTRY,
ST_SEND_RA,
ST_WAIT_RAACK,
ST_START_MOTION,
ST_VERIFY_MOTION,
ST_MONITOR_MOTION,
ST_TM_ACK,
};
static ManualMotionStates_t manualmotion_state = ST_ENTRY;
static bool resend_attempted = false;
bool StratoPIB::Flight_ManualMotion(bool restart_state)
{
if (restart_state) manualmotion_state = ST_ENTRY;
switch (manualmotion_state) {
case ST_ENTRY:
case ST_SEND_RA:
RA_ack_flag = NO_ACK;
zephyrTX.RA();
manualmotion_state = ST_WAIT_RAACK;
scheduler.AddAction(RESEND_RA, ZEPHYR_RESEND_TIMEOUT);
log_nominal("Sending RA");
break;
case ST_WAIT_RAACK:
if (ACK == RA_ack_flag) {
manualmotion_state = ST_START_MOTION;
resend_attempted = false;
log_nominal("RA ACK");
} else if (NAK == RA_ack_flag) {
resend_attempted = false;
ZephyrLogWarn("Cannot perform motion, RA NAK");
return true;
} else if (CheckAction(RESEND_RA)) {
if (!resend_attempted) {
resend_attempted = true;
manualmotion_state = ST_SEND_RA;
} else {
ZephyrLogWarn("Never received RAAck");
resend_attempted = false;
return true;
}
}
break;
case ST_START_MOTION:
if (mcb_motion_ongoing) {
ZephyrLogWarn("Motion commanded while motion ongoing");
inst_substate = MODE_ERROR; // will force exit of Flight_Profile
}
if (StartMCBMotion()) {
manualmotion_state = ST_VERIFY_MOTION;
scheduler.AddAction(RESEND_MOTION_COMMAND, MCB_RESEND_TIMEOUT);
} else {
ZephyrLogWarn("Motion start error");
inst_substate = MODE_ERROR; // will force exit of Flight_Profile
}
break;
case ST_VERIFY_MOTION:
if (mcb_motion_ongoing) { // set in the Ack handler
log_nominal("MCB commanded motion");
scheduler.AddAction(ACTION_MOTION_TIMEOUT, max_profile_seconds);
manualmotion_state = ST_MONITOR_MOTION;
}
if (CheckAction(RESEND_MOTION_COMMAND)) {
if (!resend_attempted) {
resend_attempted = true;
manualmotion_state = ST_START_MOTION;
} else {
resend_attempted = false;
ZephyrLogWarn("MCB never confirmed motion");
inst_substate = MODE_ERROR; // will force exit of Flight_Profile
}
}
break;
case ST_MONITOR_MOTION:
if (CheckAction(ACTION_MOTION_STOP)) {
// todo: verification of motion stop
ZephyrLogFine("Commanded motion stop");
return true;
break;
}
if (CheckAction(ACTION_MOTION_TIMEOUT)) {
SendMCBTM(CRIT, "MCB Motion took longer than expected");
mcbComm.TX_ASCII(MCB_CANCEL_MOTION);
inst_substate = MODE_ERROR; // will force exit of Flight_Profile
break;
}
if (!mcb_motion_ongoing) {
SendMCBTM(FINE, "Finished commanded manual motion");
manualmotion_state = ST_TM_ACK;
scheduler.AddAction(RESEND_TM, ZEPHYR_RESEND_TIMEOUT);
}
break;
case ST_TM_ACK:
if (ACK == TM_ack_flag) {
log_nominal("Zephyr ACKed motion TM");
return true;
} else if (NAK == TM_ack_flag || CheckAction(RESEND_TM)) {
// attempt one resend
log_error("Needed to resend TM");
zephyrTX.TM(); // message is still saved in XMLWriter, no need to reconstruct
return true;
}
break;
default:
// unknown state, exit
return true;
}
return false; // assume incomplete
}