-
Notifications
You must be signed in to change notification settings - Fork 0
/
HazardMgr.cpp
389 lines (309 loc) · 11.1 KB
/
HazardMgr.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*****************************************************************/
/* NAME: Michael Benjamin */
/* ORGN: Dept of Mechanical Eng / CSAIL, MIT Cambridge MA */
/* FILE: HazardMgr.cpp */
/* DATE: Oct 26th 2012 */
/* */
/* This file is part of MOOS-IvP */
/* */
/* MOOS-IvP is free software: you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation, either version */
/* 3 of the License, or (at your option) any later version. */
/* */
/* MOOS-IvP is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty */
/* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
/* the GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public */
/* License along with MOOS-IvP. If not, see */
/* <http://www.gnu.org/licenses/>. */
/*****************************************************************/
#include <iterator>
#include "MBUtils.h"
#include "HazardMgr.h"
#include "XYFormatUtilsHazard.h"
#include "XYFormatUtilsPoly.h"
#include "ACTable.h"
using namespace std;
//---------------------------------------------------------
// Constructor
HazardMgr::HazardMgr()
{
// Config variables
m_swath_width_desired = 25;
m_pd_desired = 0.9;
// swath
show_swath = "true";
swath_length = "5";
// State Variables
m_sensor_config_requested = false;
m_sensor_config_set = false;
m_swath_width_granted = 0;
m_pd_granted = 0;
m_sensor_config_reqs = 0;
m_sensor_config_acks = 0;
m_sensor_report_reqs = 0;
m_detection_reports = 0;
m_summary_reports = 0;
}
//---------------------------------------------------------
// Procedure: OnNewMail
bool HazardMgr::OnNewMail(MOOSMSG_LIST &NewMail)
{
AppCastingMOOSApp::OnNewMail(NewMail);
MOOSMSG_LIST::iterator p;
for(p=NewMail.begin(); p!=NewMail.end(); p++) {
CMOOSMsg &msg = *p;
string key = msg.GetKey();
string sval = msg.GetString();
#if 0 // Keep these around just for template
string comm = msg.GetCommunity();
double dval = msg.GetDouble();
string msrc = msg.GetSource();
double mtime = msg.GetTime();
bool mdbl = msg.IsDouble();
bool mstr = msg.IsString();
#endif
if(key == "UHZ_CONFIG_ACK")
handleMailSensorConfigAck(sval);
else if(key == "UHZ_OPTIONS_SUMMARY")
handleMailSensorOptionsSummary(sval);
else if(key == "UHZ_DETECTION_REPORT")
handleMailDetectionReport(sval);
else if(key == "HAZARDSET_REQUEST")
handleMailReportRequest();
else if(key == "UHZ_MISSION_PARAMS")
handleMailMissionParams(sval);
else
reportRunWarning("Unhandled Mail: " + key);
}
return(true);
}
//---------------------------------------------------------
// Procedure: OnConnectToServer
bool HazardMgr::OnConnectToServer()
{
registerVariables();
return(true);
}
//---------------------------------------------------------
// Procedure: Iterate()
// happens AppTick times per second
bool HazardMgr::Iterate()
{
AppCastingMOOSApp::Iterate();
if(!m_sensor_config_requested)
{
postSensorConfigRequest();
Notify("SHOW_SWATH", show_swath);
Notify("SWATH_LENGTH", swath_length);
}
if(m_sensor_config_set)
postSensorInfoRequest();
AppCastingMOOSApp::PostReport();
return(true);
}
//---------------------------------------------------------
// Procedure: OnStartUp()
// happens before connection is open
bool HazardMgr::OnStartUp()
{
AppCastingMOOSApp::OnStartUp();
STRING_LIST sParams;
m_MissionReader.EnableVerbatimQuoting(true);
if(!m_MissionReader.GetConfiguration(GetAppName(), sParams))
reportConfigWarning("No config block found for " + GetAppName());
STRING_LIST::iterator p;
for(p=sParams.begin(); p!=sParams.end(); p++) {
string orig = *p;
string line = *p;
string param = tolower(biteStringX(line, '='));
string value = line;
bool handled = false;
if((param == "swath_width") && isNumber(value)) {
m_swath_width_desired = atof(value.c_str());
handled = true;
}
else if(((param == "sensor_pd") || (param == "pd")) && isNumber(value)) {
m_pd_desired = atof(value.c_str());
handled = true;
}
else if(param == "report_name") {
value = stripQuotes(value);
m_report_name = value;
handled = true;
}
else if(param == "region") {
XYPolygon poly = string2Poly(value);
if(poly.is_convex())
m_search_region = poly;
handled = true;
}
else if(param == "swath_length")
{
swath_length = value;
handled = true;
}
else if(param == "show_swath")
{
show_swath = value;
handled = true;
}
if(!handled)
reportUnhandledConfigWarning(orig);
}
m_hazard_set.setSource(m_host_community);
m_hazard_set.setName(m_report_name);
m_hazard_set.setRegion(m_search_region);
registerVariables();
return(true);
}
//---------------------------------------------------------
// Procedure: registerVariables
void HazardMgr::registerVariables()
{
AppCastingMOOSApp::RegisterVariables();
Register("UHZ_DETECTION_REPORT", 0);
Register("UHZ_CONFIG_ACK", 0);
Register("UHZ_OPTIONS_SUMMARY", 0);
Register("UHZ_MISSION_PARAMS", 0);
Register("HAZARDSET_REQUEST", 0);
}
//---------------------------------------------------------
// Procedure: postSensorConfigRequest
void HazardMgr::postSensorConfigRequest()
{
string request = "vname=" + m_host_community;
request += ",width=" + doubleToStringX(m_swath_width_desired,2);
request += ",pd=" + doubleToStringX(m_pd_desired,2);
m_sensor_config_requested = true;
m_sensor_config_reqs++;
Notify("UHZ_CONFIG_REQUEST", request);
}
//---------------------------------------------------------
// Procedure: postSensorInfoRequest
void HazardMgr::postSensorInfoRequest()
{
string request = "vname=" + m_host_community;
m_sensor_report_reqs++;
Notify("UHZ_SENSOR_REQUEST", request);
}
//---------------------------------------------------------
// Procedure: handleMailSensorConfigAck
bool HazardMgr::handleMailSensorConfigAck(string str)
{
// Expected ack parameters:
string vname, width, pd, pfa, pclass;
// Parse and handle ack message components
bool valid_msg = true;
string original_msg = str;
vector<string> svector = parseString(str, ',');
unsigned int i, vsize = svector.size();
for(i=0; i<vsize; i++) {
string param = biteStringX(svector[i], '=');
string value = svector[i];
if(param == "vname")
vname = value;
else if(param == "pd")
pd = value;
else if(param == "width")
width = value;
else if(param == "pfa")
pfa = value;
else if(param == "pclass")
pclass = value;
else
valid_msg = false;
}
if((vname=="")||(width=="")||(pd=="")||(pfa=="")||(pclass==""))
valid_msg = false;
if(!valid_msg)
reportRunWarning("Unhandled Sensor Config Ack:" + original_msg);
if(valid_msg) {
m_sensor_config_set = true;
m_sensor_config_acks++;
m_swath_width_granted = atof(width.c_str());
m_pd_granted = atof(pd.c_str());
}
return(valid_msg);
}
//---------------------------------------------------------
// Procedure: handleMailDetectionReport
// Note: The detection report should look something like:
// UHZ_DETECTION_REPORT = vname=betty,x=51,y=11.3,label=12
bool HazardMgr::handleMailDetectionReport(string str)
{
m_detection_reports++;
XYHazard new_hazard = string2Hazard(str);
new_hazard.setType("hazard");
string hazlabel = new_hazard.getLabel();
if(hazlabel == "") {
reportRunWarning("Detection report received for hazard w/out label");
return(false);
}
int ix = m_hazard_set.findHazard(hazlabel);
if(ix == -1)
m_hazard_set.addHazard(new_hazard);
else
m_hazard_set.setHazard(ix, new_hazard);
string event = "New Detection, label=" + new_hazard.getLabel();
event += ", x=" + doubleToString(new_hazard.getX(),1);
event += ", y=" + doubleToString(new_hazard.getY(),1);
reportEvent(event);
string req = "vname=" + m_host_community + ",label=" + hazlabel;
Notify("UHZ_CLASSIFY_REQUEST", req);
return(true);
}
//---------------------------------------------------------
// Procedure: handleMailReportRequest
void HazardMgr::handleMailReportRequest()
{
m_summary_reports++;
m_hazard_set.findMinXPath(20);
//unsigned int count = m_hazard_set.findMinXPath(20);
string summary_report = m_hazard_set.getSpec("final_report");
Notify("HAZARDSET_REPORT", summary_report);
}
//---------------------------------------------------------
// Procedure: handleMailMissionParams
// Example: UHZ_MISSION_PARAMS = penalty_missed_hazard=100,
// penalty_nonopt_hazard=55,
// penalty_false_alarm=35,
// penalty_max_time_over=200,
// penalty_max_time_rate=0.45,
// transit_path_width=25,
// search_region = pts={-150,-75:-150,-50:40,-50:40,-75}
void HazardMgr::handleMailMissionParams(string str)
{
vector<string> svector = parseStringZ(str, ',', "{");
unsigned int i, vsize = svector.size();
for(i=0; i<vsize; i++) {
string param = biteStringX(svector[i], '=');
string value = svector[i];
// This needs to be handled by the developer. Just a placeholder.
}
}
//------------------------------------------------------------
// Procedure: buildReport()
bool HazardMgr::buildReport()
{
m_msgs << "Config Requested:" << endl;
m_msgs << " swath_width_desired: " << m_swath_width_desired << endl;
m_msgs << " pd_desired: " << m_pd_desired << endl;
m_msgs << " config requests sent: " << m_sensor_config_reqs << endl;
m_msgs << " acked: " << m_sensor_config_acks << endl;
m_msgs << "------------------------ " << endl;
m_msgs << "Config Result:" << endl;
m_msgs << " config confirmed: " << boolToString(m_sensor_config_set) << endl;
m_msgs << " swath_width_granted: " << m_swath_width_granted << endl;
m_msgs << " pd_granted: " << m_pd_granted << endl << endl;
m_msgs << "--------------------------------------------" << endl << endl;
m_msgs << " sensor requests: " << m_sensor_report_reqs << endl;
m_msgs << " detection reports: " << m_detection_reports << endl << endl;
m_msgs << " Hazardset Reports Requested: " << m_summary_reports << endl;
m_msgs << " Hazardset Reports Posted: " << m_summary_reports << endl;
m_msgs << " Report Name: " << m_report_name << endl;
return(true);
}