STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
actuator_nc.cpp
Go to the documentation of this file.
1
#include <iostream>
2
3
#include "
actuator_nc_config.h
"
4
5
static
bool
serverHeartbeat
=
true
;
6
static
bool
configPacket
=
true
;
7
int
boardID
= 1234;
// Example board ID
8
static
const
std::string
SERVER_ADDRESS
=
"SERVER_ADDRESS"
;
9
static
const
std::string
ACTUATOR_CONTROLLER_ADDRESS
=
10
"ACTUATOR_CONTROLLER_ADDRESS"
;
11
const
int
IPAddress
= 0;
// Example IP address (recieve during packet config)
12
const
bool
necessaryForAbort
=
true
;
// example condition for abort
13
const
std::string
ABORT
=
"ABORT"
;
// example server packet indicating abort
14
enum class
BoardState
{
inactive
,
active
,
disconnected
,
abort
,
abortDone
};
15
BoardState
myBoardState
=
BoardState::inactive
;
16
17
void
sendHeartbeat
(
int
boardID
,
BoardState
boardState
,
18
std::string destination) {
19
(void)
boardID
;
// Mock implementation of sending heartbeat w/ ID
20
}
21
bool
serverHeartbeatReceived
() {
22
return
serverHeartbeat
;
23
}
24
bool
configPacketReceived
() {
25
return
configPacket
;
// Mock implementation
26
}
27
void
storeAddress
() {
28
// stores server address in server heartbeat
29
}
30
void
delay
(
int
ms) {
31
// Mock implementation of delay
32
}
33
bool
heartbeatTimedOut
(
int
currTime,
int
lastHeartbeatTime) {
34
return
(currTime - lastHeartbeatTime) >
HEARTBEAT_TIMEOUT_MS
;
35
}
36
bool
testConnection
() {
37
// mock connection test function
38
return
false
;
// assume connection failed for example
39
}
40
void
streamPackets
() {
41
// mock implementation of streaming packets to server
42
}
43
void
provideAbortData
(
BoardState
boardState
,
int
boardID
,
int
IPAddress
,
44
std::string destination) {
45
// mock implementation of providing abort data to actuator controller
46
}
47
void
abortFinish
(
BoardState
myBoardState
,
int
boardID
) {
48
myBoardState
=
BoardState::abortDone
;
49
sendHeartbeat
(
boardID
,
myBoardState
,
50
SERVER_ADDRESS
);
// send heartbeat to server with abort state
51
mainloop
(
myBoardState
,
boardID
);
// example of looping mainloop after abort
52
}
53
void
broadcastConnectionLoss
() {
54
// mock implementation of broadcasting connection loss to server and
55
// actuator controller
56
}
57
void
ping
() {
58
// mock implementation of pinging server and actuator controller
59
}
60
bool
responseReceived
() {
61
// mock implementation of checking for response from server and actuator
62
// controller
63
return
false
;
// assume no response received for example
64
}
65
bool
abortDonePacketReceived
() {
66
// mock implementation of checking for abort done packet from controller
67
// board
68
69
return
false
;
// assume no abort done packet received for example
70
}
71
void
executeCommands
() {
72
// mock implementation of executing commands from controller board
73
}
74
bool
connectionResume
() {
75
// mock implementation of checking if connection is resumed
76
return
false
;
// assume connection not resumed for example
77
}
78
std::string
serverPacket
() {
79
// mock implementation of checking for server packet
80
return
ABORT
;
// assume ABORT packet received for example
81
}
82
83
int
main
(
int
argc,
char
** argv) {
84
(void)argc;
85
(void)argv;
86
87
setup
();
// setup once
88
89
while
(
true
) {
// loop mainloop until fail
90
mainloop
(
myBoardState
,
boardID
);
91
}
92
93
return
0;
94
}
95
96
void
setup
() {
97
// Power on
98
myBoardState
=
BoardState::inactive
;
99
100
// Waiting for Server
101
// send heartbeats including board ID until it recieves server's heartbeat
102
// response or timeout
103
while
(
serverHeartbeatReceived
() ==
false
) {
104
sendHeartbeat
(
boardID
,
myBoardState
,
SERVER_ADDRESS
);
105
delay
(
HEARTBEAT_SEND_INTERVAL_MS
);
106
}
107
108
storeAddress
();
// store server address in server heartbeat
109
// Waiting for Config
110
while
(
configPacketReceived
() ==
false
) {
111
delay
(
MAINLOOP_POLL_INTERVAL_MS
);
112
}
113
114
// everything is set up, board is active
115
myBoardState
=
BoardState::active
;
116
}
117
118
void
mainloop
(
BoardState
myBoardState
,
int
boardID
) {
// ACTIVE STATE
119
sendHeartbeat
(
boardID
,
myBoardState
,
120
SERVER_ADDRESS
);
// send heartbeat to server
121
storeAddress
();
// store server address in server heartbeat
122
streamPackets
();
// stream packets to server
123
124
if
(
heartbeatTimedOut
(6000, 0)) {
// example time values
125
connectionLoss
(
myBoardState
,
boardID
);
126
}
127
if
(
serverPacket
() ==
ABORT
) {
128
standardAbort
(
myBoardState
,
boardID
);
129
}
130
}
131
132
void
connectionLoss
(
BoardState
myBoardState
,
int
boardID
) {
133
(void)
boardID
;
134
// handle connection loss
135
myBoardState
=
BoardState::disconnected
;
136
int
currentTime = 0;
137
while
(currentTime <
CONNECTION_LOSS_GRACE_MS
) {
138
broadcastConnectionLoss
();
// broadcast connection loss to server and
139
// actuator controller
140
ping
();
141
currentTime +=
MAINLOOP_POLL_INTERVAL_MS
;
142
}
143
if
(
connectionResume
() ==
true
) {
144
mainloop
(
myBoardState
,
145
boardID
);
// resume mainloop if connection is resumed
146
}
147
if
(
responseReceived
() ==
true
) {
148
noConnectionAbort
(
myBoardState
,
149
boardID
);
// abort if connection not resumed
150
}
151
152
else
{
// STANDALONE ABORT
153
myBoardState
=
BoardState::abort
;
// abort if necessary
154
provideAbortData
(
myBoardState
,
boardID
,
IPAddress
,
155
ACTUATOR_CONTROLLER_ADDRESS
);
// provide abort data to
156
// actuator controller
157
abortFinish
(
myBoardState
,
boardID
);
158
}
159
160
// nothing here
161
}
162
163
void
standardAbort
(
BoardState
myBoardState
,
int
boardID
) {
164
while
(
true
) {
165
sendHeartbeat
(
166
boardID
,
BoardState::abort
,
167
SERVER_ADDRESS
);
// send heartbeat to server with abort state
168
delay
(
HEARTBEAT_SEND_INTERVAL_MS
);
169
if
(
abortDonePacketReceived
() ==
true
) {
170
abortFinish
(
myBoardState
,
171
boardID
);
// finish abort if abort done packet received
172
// from controller board
173
break
;
174
}
else
if
(
heartbeatTimedOut
(6000, 0)) {
// example time values
175
connectionLoss
(
myBoardState
,
boardID
);
176
break
;
177
}
178
}
179
}
180
181
void
noConnectionAbort
(
BoardState
myBoardState
,
int
boardID
) {
182
if
(
responseReceived
()) {
// NO CONNECTION ABORT
183
myBoardState
=
BoardState::abort
;
184
185
while
(
abortDonePacketReceived
() ==
false
) {
186
executeCommands
();
// execute commands from controller board as
187
// they come
188
delay
(
HEARTBEAT_SEND_INTERVAL_MS
);
189
}
190
191
abortFinish
(
myBoardState
,
boardID
);
192
}
193
}
CONNECTION_LOSS_GRACE_MS
#define CONNECTION_LOSS_GRACE_MS
Definition
actuator_config.h:15
HEARTBEAT_TIMEOUT_MS
#define HEARTBEAT_TIMEOUT_MS
Definition
actuator_config.h:14
SERVER_ADDRESS
static const std::string SERVER_ADDRESS
Definition
actuator_nc.cpp:8
responseReceived
bool responseReceived()
Definition
actuator_nc.cpp:60
abortFinish
void abortFinish(BoardState myBoardState, int boardID)
Definition
actuator_nc.cpp:47
serverHeartbeatReceived
bool serverHeartbeatReceived()
Definition
actuator_nc.cpp:21
provideAbortData
void provideAbortData(BoardState boardState, int boardID, int IPAddress, std::string destination)
Definition
actuator_nc.cpp:43
ACTUATOR_CONTROLLER_ADDRESS
static const std::string ACTUATOR_CONTROLLER_ADDRESS
Definition
actuator_nc.cpp:9
noConnectionAbort
void noConnectionAbort(BoardState myBoardState, int boardID)
Definition
actuator_nc.cpp:181
broadcastConnectionLoss
void broadcastConnectionLoss()
Definition
actuator_nc.cpp:53
serverHeartbeat
static bool serverHeartbeat
Definition
actuator_nc.cpp:5
configPacket
static bool configPacket
Definition
actuator_nc.cpp:6
setup
void setup()
Definition
actuator_nc.cpp:96
testConnection
bool testConnection()
Definition
actuator_nc.cpp:36
standardAbort
void standardAbort(BoardState myBoardState, int boardID)
Definition
actuator_nc.cpp:163
configPacketReceived
bool configPacketReceived()
Definition
actuator_nc.cpp:24
streamPackets
void streamPackets()
Definition
actuator_nc.cpp:40
connectionResume
bool connectionResume()
Definition
actuator_nc.cpp:74
myBoardState
BoardState myBoardState
Definition
actuator_nc.cpp:15
executeCommands
void executeCommands()
Definition
actuator_nc.cpp:71
abortDonePacketReceived
bool abortDonePacketReceived()
Definition
actuator_nc.cpp:65
mainloop
void mainloop(BoardState myBoardState, int boardID)
Definition
actuator_nc.cpp:118
BoardState
BoardState
Definition
actuator_nc.cpp:14
BoardState::inactive
@ inactive
Definition
actuator_nc.cpp:14
BoardState::disconnected
@ disconnected
Definition
actuator_nc.cpp:14
BoardState::abort
@ abort
Definition
actuator_nc.cpp:14
BoardState::abortDone
@ abortDone
Definition
actuator_nc.cpp:14
BoardState::active
@ active
Definition
actuator_nc.cpp:14
serverPacket
std::string serverPacket()
Definition
actuator_nc.cpp:78
necessaryForAbort
const bool necessaryForAbort
Definition
actuator_nc.cpp:12
storeAddress
void storeAddress()
Definition
actuator_nc.cpp:27
delay
void delay(int ms)
Definition
actuator_nc.cpp:30
boardID
int boardID
Definition
actuator_nc.cpp:7
ABORT
const std::string ABORT
Definition
actuator_nc.cpp:13
connectionLoss
void connectionLoss(BoardState myBoardState, int boardID)
Definition
actuator_nc.cpp:132
ping
void ping()
Definition
actuator_nc.cpp:57
actuator_nc_config.h
HEARTBEAT_SEND_INTERVAL_MS
#define HEARTBEAT_SEND_INTERVAL_MS
Definition
actuator_nc_config.h:9
MAINLOOP_POLL_INTERVAL_MS
#define MAINLOOP_POLL_INTERVAL_MS
Definition
actuator_nc_config.h:11
IPAddress
Definition
Arduino.h:63
heartbeatTimedOut
static bool heartbeatTimedOut()
Definition
main.cpp:366
sendHeartbeat
static void sendHeartbeat()
Definition
main.cpp:241
boardState
static State boardState
Definition
main.cpp:73
main
int main()
Definition
test_abort_ordering.cpp:67
firmware
Avionics_State_Machine_Drafts
actuator_nc.cpp
Generated by
1.15.0