Repository navigation
Expand file tree
/
Copy pathHazardSensor_MOOSApp.cpp
More file actions
1767 lines (1461 loc) · 56.1 KB
/
Copy pathHazardSensor_MOOSApp.cpp
File metadata and controls
1767 lines (1461 loc) · 56.1 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************/
/* NAME: Michael Benjamin */
/* ORGN: Dept of Mechanical Eng / CSAIL, MIT Cambridge MA */
/* FILE: HazardSensor_MOOSApp.cpp */
/* DATE: Jan 28th 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 <cmath>
#include "HazardSensor_MOOSApp.h"
#include "XYFormatUtilsHazard.h"
#include "NodeRecordUtils.h"
#include "FileBuffer.h"
#include "AngleUtils.h"
#include "GeomUtils.h"
#include "ColorParse.h"
#include "XYMarker.h"
#include "XYCircle.h"
#include "MBUtils.h"
#include "ACTable.h"
using namespace std;
//---------------------------------------------------------
// Constructor
HazardSensor_MOOSApp::HazardSensor_MOOSApp()
{
// State Variables
// -------------------------------------------------------------
m_last_summary_time = 0; // last time settings options summary posted
// Configuration variables
// -------------------------------------------------------------
m_min_reset_interval = 300; // seconds
m_options_summary_interval = 10; // in timewarped seconds
m_min_queue_msg_interval = 30; // seconds
m_sensor_max_turn_rate = 1.5; // degrees per second
m_ignore_resemblances = false;
m_max_vehicle_speed = 2.05;
m_hazard_file_hazard_cnt = 0;
m_hazard_file_benign_cnt = 0;
m_hazard_file = "None provided.";
// If uniformly random noise used, (m_rn_algorithm = "uniform")
// this variable reflects the range of the uniform variable in
// terms of percentage of the true (pre-noise) range.
m_rn_uniform_pct = 0;
m_seed_random = true;
// Visual preferences
m_circle_duration = -1;
m_show_hazards = true;
m_show_swath = true;
m_show_pd = true;
m_show_pfa = true;
m_color_hazard = "green";
m_color_benign = "light_blue";
m_shape_hazard = "triangle";
m_shape_benign = "triangle";
m_width_hazard = 8;
m_width_benign = 8;
m_swath_len = 5;
m_swath_transparency = 0.2;
m_ac.setMaxEvents(20);
}
//---------------------------------------------------------
// Procedure: OnNewMail
bool HazardSensor_MOOSApp::OnNewMail(MOOSMSG_LIST &NewMail)
{
AppCastingMOOSApp::OnNewMail(NewMail);
MOOSMSG_LIST::iterator p;
for(p=NewMail.begin(); p!=NewMail.end(); p++) {
//p->Trace();
CMOOSMsg msg = *p;
string key = msg.GetKey();
string sval = msg.GetString();
string comm = msg.GetCommunity();
bool handled = false;
if(key == "NODE_REPORT")
handled = handleNodeReport(sval);
else if(key == "UHZ_SENSOR_REQUEST")
handled = handleSensorRequest(sval);
else if(key == "UHZ_SENSOR_CLEAR")
handled = handleSensorClear(sval);
else if(key == "UHZ_CLASSIFY_REQUEST")
handled = handleClassifyRequest(sval);
else if(key == "UHZ_CONFIG_REQUEST")
handled = handleSensorConfig(sval, comm);
else if(key == "PMV_CONNECT") {
postVisuals();
handled = true;
}
else if(key == "SWATH_LENGTH")
{
m_map_swath_length[comm] = atof(sval.c_str());
handled = true;
}
else if(key == "SHOW_SWATH")
{
if (sval == "true")
m_map_swath_show[comm] = true;
else
m_map_swath_show[comm] = false;
handled = true;
}
else if(key == "RemoveHazard") {
// sends a messsage to GUI pMarineViewer
reportEvent("Removed Hazard Label : " +sval);
// removes the hazard from the list
removeHazard(sval);
// makes the mine smaller so it is not visible in GUI pMarineViewer
string report = "x=0,y=0,width=0, label=" + sval;
Notify("VIEW_MARKER",report);
handled = true;
Notify("VIEW_MARKER",report);
}
else if(key == "AddHazard") {
// sends a messsage to GUI pMarineViewer
reportEvent("Added Hazard : " +sval);
// adds the haazard to the list
addHazard(sval);
// posts the mine for the GUI
postVisuals();
handled = true;
postVisuals();
}
if(!handled)
reportRunWarning("Unhandled mail: " + key);
}
return(true);
}
//---------------------------------------------------------
// Procedure: OnConnectToServer
bool HazardSensor_MOOSApp::OnConnectToServer()
{
registerVariables();
return(true);
}
//------------------------------------------------------------
// Procedure: registerVariables
void HazardSensor_MOOSApp::registerVariables()
{
AppCastingMOOSApp::RegisterVariables();
Register("NODE_REPORT", 0);
Register("UHZ_SENSOR_REQUEST", 0);
Register("UHZ_CLASSIFY_REQUEST", 0);
Register("UHZ_CONFIG_REQUEST", 0);
Register("PMV_CONNECT", 0);
Register("RemoveHazard", 0);
Register("AddHazard", 0);
Register("SHOW_SWATH", 0);
Register("SWATH_LENGTH", 0);
}
//---------------------------------------------------------
// Procedure: Iterate()
bool HazardSensor_MOOSApp::Iterate()
{
AppCastingMOOSApp::Iterate();
// Part 1: Consider posting a settings options summary
double elapsed_time = m_curr_time - m_last_summary_time;
if(elapsed_time >= m_options_summary_interval) {
Notify("UHZ_OPTIONS_SUMMARY", m_sensor_prop_summary);
m_last_summary_time = m_curr_time;
}
processSensorRequests();
processClassifyQueue();
AppCastingMOOSApp::PostReport();
return(true);
}
//---------------------------------------------------------
// Procedure: OnStartUp()
bool HazardSensor_MOOSApp::OnStartUp()
{
AppCastingMOOSApp::OnStartUp();
STRING_LIST sParams;
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 = biteStringX(line, '=');
string value = line;
bool handled = false;
if(param == "default_hazard_shape") {
m_shape_hazard = value;
handled = true;
}
else if((param == "default_hazard_color") && isColor(value)) {
m_color_hazard = value;
handled = true;
}
else if((param == "default_hazard_width") && isNumber(value)) {
m_width_hazard = atof(value.c_str());
handled = true;
}
else if((param == "min_reset_interval") && isNumber(value)) {
m_min_reset_interval = atof(value.c_str());
handled = true;
}
else if((param == "max_turn_rate") && isNumber(value)) {
m_sensor_max_turn_rate = atof(value.c_str());
handled = true;
}
else if((param == "max_vehicle_speed") && isNumber(value)) {
m_max_vehicle_speed = atof(value.c_str());
handled = true;
}
else if((param == "classify_period") || (param == "min_classify_interval")) {
if(isNumber(value)) {
m_min_queue_msg_interval = atof(value.c_str());
handled = true;
}
}
else if(param == "default_benign_shape") {
m_shape_benign = value;
handled = true;
}
else if(param == "default_benign_color") {
m_color_benign = value;
handled = true;
}
else if((param == "default_benign_width") && isNumber(value)) {
m_width_benign = atof(value.c_str());
handled = true;
}
else if(param == "show_hazards")
handled = setBooleanOnString(m_show_hazards, value);
else if(param == "ignore_resemblances")
handled = setBooleanOnString(m_ignore_resemblances, value);
else if(param == "show_pd")
handled = setBooleanOnString(m_show_pd, value);
else if(param == "show_pfa")
handled = setBooleanOnString(m_show_pfa, value);
else if(param == "show_swath")
handled = setBooleanOnString(m_show_swath, value);
else if(((param == "show_reports") || (param == "show_detections"))
&& isNumber(value)) {
m_circle_duration = atof(value.c_str());
handled = true;
}
else if((param == "show_reports") || (param == "show_detections")) {
value = tolower(value);
if((value == "unlimited") || (value == "nolimit")) {
m_circle_duration = -1;
handled = true;
}
}
else if(param == "options_summary_interval")
handled = setOptionsSummaryInterval(value);
else if(param == "swath_length")
handled = setSwathLength(value);
else if(param == "show_swath")
handled = setBooleanOnString(m_show_swath, value);
else if(param == "show_hazards")
handled = setBooleanOnString(m_show_hazards, value);
else if(param == "seed_random")
handled = setBooleanOnString(m_seed_random, value);
else if((param == "swath_transparency") && isNumber(value))
handled = setSwathTransparency(value);
else if(param == "rn_algorithm")
handled = setRandomNoiseAlgorithm(value);
else if(param == "sensor_config")
handled = addSensorConfig(value);
else if((param == "rn_uniform_pct") && isNumber(value)) {
m_rn_uniform_pct = vclip(atof(value.c_str()), 0, 1);
handled = true;
}
else if(param == "hazard_file")
{
handled = processHazardFile(value);
}
else if(param == "hazard") // Fix me! Needs to be done on PASS2
handled = addHazard(value);
if(!handled)
reportUnhandledConfigWarning(orig);
}
registerVariables();
postVisuals();
perhapsSeedRandom();
sortSensorProperties();
if(m_map_hazards.size() == 0)
reportConfigWarning("No Hazard Field Laydown Provided.");
if(m_sensor_prop_width.size() == 0)
reportConfigWarning("Sensor was configured with NO sensor_config properties!");
return(true);
}
//------------------------------------------------------------
// Procedure: perhapsSeedRandom
void HazardSensor_MOOSApp::perhapsSeedRandom()
{
if(m_seed_random)
srand(time(NULL));
}
//------------------------------------------------------------
// Procedure: addHazard
bool HazardSensor_MOOSApp::addHazard(string line)
{
XYHazard hazard = string2Hazard(line);
if(!hazard.isSetX() || !hazard.isSetY())
return(false);
if(!hazard.isSetType()) {
reportConfigWarning("Hazard specified with no type: " + line);
return(false);
}
if(hazard.getLabel() == "") {
reportConfigWarning("Hazard specified with no label: " + line);
return(false);
}
string label = hazard.getLabel();
if(label == "") {
label = uintToString(m_map_hazards.size());
hazard.setLabel(label);
}
if(m_map_hazards.count(label) != 0) {
reportConfigWarning("Duplicated hazard label detected:" + label);
return(false);
}
m_map_hazards[label] = hazard;
// Keep a running count of object type for later in appcast report
if(hazard.getType() == "hazard")
m_hazard_file_hazard_cnt++;
else
m_hazard_file_benign_cnt++;
return(true);
}
//------------------------------------------------------------
// Procedure: removeHazzard
bool HazardSensor_MOOSApp::removeHazard(string label)
{
m_map_hazards.erase(label);
// Keep a running count of object type for later in appcast report
m_hazard_file_hazard_cnt--;
return(true);
}
//------------------------------------------------------------
// Procedure: setSwathLength()
bool HazardSensor_MOOSApp::setSwathLength(string line)
{
if(!isNumber(line))
return(false);
m_swath_len = atof(line.c_str());
if(m_swath_len < 1)
m_swath_len = 1;
return(true);
}
//------------------------------------------------------------
// Procedure: addSensorConfig
bool HazardSensor_MOOSApp::addSensorConfig(string line)
{
string str_width, str_exp, str_class, str_max="0";
vector<string> svector = parseString(line, ',');
unsigned int i, vsize = svector.size();
for(i=0; i<vsize; i++) {
string param = tolower(biteStringX(svector[i], '='));
string value = svector[i];
if(param == "width")
str_width = value;
else if(param == "exp")
str_exp = value;
else if((param == "class") || (param == "pclass"))
str_class = value;
else if(param == "max")
str_max = value;
}
if(!isNumber(str_width) || !isNumber(str_exp) ||
!isNumber(str_class) || !isNumber(str_max))
return(false);
double d_width = atof(str_width.c_str());
double d_exp = atof(str_exp.c_str());
double d_class = atof(str_class.c_str());
int i_max = atoi(str_max.c_str());
d_width = vclip_min(d_width, 0);
d_exp = vclip_min(d_exp, 1);
d_class = vclip(d_class, 0, 1);
if(i_max < 0)
i_max = 0;
m_sensor_prop_width.push_back(d_width);
m_sensor_prop_exp.push_back(d_exp);
m_sensor_prop_class.push_back(d_class);
m_sensor_prop_max.push_back((unsigned int)(i_max));
// Build up the sensor property summary, published occassionally
// "width=25,exp=4,class=0.8 : width=10,exp=8,class=0.93"
if(m_sensor_prop_summary != "")
m_sensor_prop_summary += ":";
m_sensor_prop_summary += "width=" + str_width;
m_sensor_prop_summary += ",exp=" + str_exp;
m_sensor_prop_summary += ",class=" + str_class;
if(i_max > 0)
m_sensor_prop_summary += ",max=" + intToString(i_max);
return(true);
}
//---------------------------------------------------------
// Procedure: handleNodeReport
// Example: NAME=alpha,TYPE=KAYAK,UTC_TIME=1267294386.51,
// X=29.66,Y=-23.49,LAT=43.825089, LON=-70.330030,
// SPD=2.00, HDG=119.06,YAW=119.05677,DEPTH=0.00,
// LENGTH=4.0,MODE=ENGAGED
bool HazardSensor_MOOSApp::handleNodeReport(const string& node_report_str)
{
NodeRecord new_node_record = string2NodeRecord(node_report_str);
// In case there is an outstanding RunWarning indicating the lack
// of a node report for a given vehicle, retract it here. This is
// mostly a startup timing issue. Sometimes a sensor request is
// received before a node report. Only a problem if the node report
// never comes. Once we get one, it's no longer a problem.
string vname = new_node_record.getName();
retractRunWarning("No NODE_REPORT received for " + vname);
updateNodeRecords(new_node_record);
return(true);
}
//---------------------------------------------------------
// Procedure: handleSensorRequest
// Example: vname=alpha
bool HazardSensor_MOOSApp::handleSensorRequest(const string& request)
{
string vname;
vector<string> svector = parseString(request, ',');
unsigned int i, vsize = svector.size();
for(i=0; i<vsize; i++) {
string param = tolower(biteStringX(svector[i], '='));
string value = svector[i];
if(param == "vname")
vname = value;
}
if(vname == "") {
reportRunWarning("Sensor detect request with null node name");
return(false);
}
m_map_sensor_reqs[vname]++;
m_map_last_sensor_request[vname] = MOOSTime();
return(true);
}
//---------------------------------------------------------
// Procedure: processSensorRequests()
bool HazardSensor_MOOSApp::processSensorRequests()
{
map<string,double>::iterator p;
for(p=m_map_last_sensor_request.begin(); p!=m_map_last_sensor_request.end(); p++) {
string vname = p->first;
double tstamp = p->second;
double elapsed = MOOSTime() - tstamp;
if(elapsed < 600)
processSensorRequest(vname);
}
return(true);
}
//---------------------------------------------------------
// Procedure: processSensorRequest(vname)
bool HazardSensor_MOOSApp::processSensorRequest(string vname)
{
// Part 2: Determine requesting vehicle's index in the node_record vector
unsigned int j, jsize = m_node_records.size();
unsigned int vix = jsize;
for(j=0; j<jsize; j++) {
if(vname == m_node_records[j].getName())
vix = j;
}
Notify("PSR", vname);
// If nothing is known about this vehicle, we don't know its
// position, so just return false.
if(vix == jsize) {
reportRunWarning("No NODE_REPORT received for " + vname);
return(true); // return true - don't want to report another warning
}
// Part 3: If this vehicle has not initialized the sensor setting,
// then perform a default setting.
if(m_map_swath_width.count(vname) == 0)
setVehicleSensorSetting(vname, 0, 0, true);
// Part 4: Determine the vehicle turn rate and if the sensor is "on"
double turn_rate = m_node_hdg_hist[vix].getTurnRate(2);
bool sensor_on = true;
if(turn_rate > m_sensor_max_turn_rate)
sensor_on = false;
double vehicle_speed = m_node_records[vix].getSpeed();
if(vehicle_speed > m_max_vehicle_speed)
sensor_on = false;
// Part 5: Update the sensor region/polygon based on new position
// and perhaps new sensor setting.
updateNodePolygon(vix, sensor_on);
// Possibly draw the swath
if(m_map_swath_show[vname]) {
if(m_show_pd || m_show_pfa) {
string msg;
string pd_str = doubleToString(m_map_prob_detect[vname],2);
string pfa_str = doubleToString(m_map_prob_false_alarm[vname],2);
if(m_show_pd && m_show_pfa)
msg = pd_str + "/" + pfa_str;
else if(m_show_pd && !m_show_pfa)
msg = pd_str;
else if(!m_show_pd && m_show_pfa)
msg = "pfa=" + pfa_str;
m_node_polygons[vix].set_msg(msg);
}
string poly_spec = m_node_polygons[vix].get_spec();
Notify("VIEW_POLYGON", poly_spec);
}
// If the sensor is effectively off, then we're done
if(!sensor_on)
return(true);
// Part 6: For each hazard, determine if the hazard is newly within
// the sensor swath of the requesting vehicle.
map<string, XYHazard>::iterator p;
for(p=m_map_hazards.begin(); p!=m_map_hazards.end(); p++) {
string hlabel = p->first;
XYHazard hazard = p->second;
bool is_hazard = (hazard.getType() == "hazard");
bool dice_roll_needed = updateVehicleHazardStatus(vix, hlabel);
if(dice_roll_needed) {
if(is_hazard)
m_map_haz_detect_chances[vname]++;
else
m_map_ben_detect_chances[vname]++;
int rand_int = rand() % 10000;
double dice_roll = (double)(rand_int) / 10000;
bool detect_result_normal = rollDetectionDiceNormal(vix, hlabel, dice_roll);
bool detect_result_aspect = rollDetectionDiceAspect(vix, hlabel, dice_roll);
if(detect_result_normal) {
if(is_hazard)
m_map_haz_detect_reports[vname]++;
else
m_map_ben_detect_reports[vname]++;
postHazardDetectionReport(hlabel, vix);
}
else if(detect_result_aspect)
postHazardDetectionAspect(hlabel);
}
}
return(true);
}
//---------------------------------------------------------
// Procedure: handleSensorClear
// Example: vname=alpha
bool HazardSensor_MOOSApp::handleSensorClear(const string& request)
{
string vname;
// Part 1: Parse the string request
vector<string> svector = parseString(request, ',');
unsigned int i, vsize = svector.size();
for(i=0; i<vsize; i++) {
string param = tolower(biteStringX(svector[i], '='));
string value = svector[i];
if(param == "vname")
vname = value;
}
if(vname == "") {
reportRunWarning("Error. Sensor clear request with null node name");
return(false);
}
reportEvent("Sensor clear request for vehicle " + vname);
return(true);
}
//---------------------------------------------------------
// Procedure: handleClassifyRequest
// Example: vname=alpha, label=421
bool HazardSensor_MOOSApp::handleClassifyRequest(const string& request)
{
string vname, haz_label, action, priority = "50";
// Part 1: Parse the string request and announce the request in an event
vector<string> svector = parseString(request, ',');
for(unsigned int i=0; i<svector.size(); i++) {
string param = tolower(biteStringX(svector[i], '='));
string value = svector[i];
if(param == "vname")
vname = value;
else if(param == "label")
haz_label = value;
else if(param == "priority")
priority = value;
else if(param == "action")
action = value;
}
if((action == "clear") && (m_map_classify_queue.count(vname) != 0)) {
m_map_classify_queue[vname].clear();
reportEvent("Classify Queue cleared for " + vname);
return(true);
}
m_map_classify_reqs[vname]++;
// Part 2: Sanity checks
if(vname == "")
return(reportRunWarning("Classify request with null vehicle name"));
if(haz_label == "")
return(reportRunWarning("Classify request with null label"));
if(!isNumber(priority))
return(reportRunWarning("Classify request with bad priority:" + priority));
if(m_map_hazards.count(haz_label) == 0)
return true;
if(m_map_prob_classify.count(vname) == 0)
return(reportRunWarning("Error: No classifier config info for: " + vname));
// Part 3: check if a new classify req may be made. A new class req may be
// accepted each time a pass has been made over the hazard
if((m_map_hazard_passes.count(vname)==0) || (m_map_hazard_passes[vname].size()==0))
return(reportRunWarning("Sensor classify request saturated: " + vname));
double vehicle_heading = m_map_hazard_passes[vname].front();
m_map_hazard_passes[vname].pop_front();
// Part 4: Get hazard, and true type info
XYHazard true_hazard = m_map_hazards[haz_label];
bool is_hazard = (true_hazard.getType() == "hazard");
// Part 5: Perhaps modify the PC based on resemblance factor
double prob_classify = m_map_prob_classify[vname];
double pc_prime = prob_classify;
if((!is_hazard) && true_hazard.hasResemblance() && !m_ignore_resemblances) {
double resemblance = true_hazard.getResemblance();
pc_prime += (1-prob_classify) * (1-resemblance);
}
// Part 6: Perhaps modify the PC based on aspect angle
bool use_aspect_info = true_hazard.hasAspect();
if(!true_hazard.hasAspectRangeMin() || !true_hazard.hasAspectRangeMax())
use_aspect_info = false;
double pc_dbl_prime = pc_prime;
if(use_aspect_info) {
double hazard_aspect_opt = true_hazard.getAspect();
double aspect_diff = aspectDiff(vehicle_heading, hazard_aspect_opt);
double range_min = true_hazard.getAspectRangeMin();
double range_max = true_hazard.getAspectRangeMax();
double degrade_pct = 0;
if(aspect_diff <= range_min)
degrade_pct = 0;
else if(aspect_diff >= range_max)
degrade_pct = 1;
else
degrade_pct = (aspect_diff - range_min) / (range_max - range_min);
degrade_pct = vclip(degrade_pct, 0, 1);
pc_dbl_prime = 0.5 + ((1-degrade_pct) * (pc_prime-0.5));
}
// Part 7: Roll the classification dice.
double dice_roll = randomDouble(0, 1);
// Example: | dice_roll
// pc = 0.9 | 0.50 --> proper classifcation
// pc_prime = 0.94 | 0.98 --> incorrect classification
// pc_dbl_prime = 0.75 | 0.85 --> incorrect classification, aspect_affected
bool aspect_affected = false;
if(dice_roll > pc_dbl_prime) {
is_hazard = !is_hazard; // apply incorrect classification
if(dice_roll <= pc_prime)
aspect_affected = true; // Declare aspect_affected
}
string msg = "pc=" + doubleToString(prob_classify);
msg += ", pcp=" + doubleToString(pc_prime);
msg += ", pcdb=" + doubleToString(pc_dbl_prime);
msg += ", dice=" + doubleToString(dice_roll);
msg += ", aff=" + boolToString(aspect_affected);
reportEvent(msg);
// Part 8: Build the report to be added to the classify queue
XYHazard post_hazard = true_hazard;
if(is_hazard)
post_hazard.setType("hazard");
else
post_hazard.setType("benign");
string hazard_spec = post_hazard.getSpec("hr,color,shape,width,xpos,ypos");
// Part 9: Create an entry for the classify queue and push it on...
ClassifyEntry entry;
entry.setHazard(post_hazard);
entry.setAspectAffected(aspect_affected);
entry.setPriority(atof(priority.c_str()));
bool clock_reset = false;
if(m_map_classify_queue[vname].size() == 0)
clock_reset = true;
if(action == "top") {
m_map_classify_queue[vname].pushTop(entry);
clock_reset = true;
}
else
m_map_classify_queue[vname].push(entry);
if(clock_reset)
m_map_classify_last_time[vname] = m_curr_time;
return(true);
}
//---------------------------------------------------------
// Procedure: handleSensorConfig
// Example: vname=alpha,width=50,pd=0.9
bool HazardSensor_MOOSApp::handleSensorConfig(const string& config,
const string& msg_src)
{
// Part 1: Parse the incoming configuration request
string vname, str_width, str_pd;
vector<string> svector = parseString(config, ',');
unsigned int i, vsize = svector.size();
for(i=0; i<vsize; i++) {
string param = tolower(biteStringX(svector[i], '='));
string value = svector[i];
if(param == "vname")
vname = value;
else if(param == "width")
str_width = value;
else if(param == "pd")
str_pd = value;
}
// Part 2: Check for correctness of incoming request
if((vname == "") || (vname != msg_src)) {
reportConfigWarning("Bad sensor config request from: "+vname + " src: "+msg_src);
return(false);
}
if(((str_width != "") && !isNumber(str_width)) || !isNumber(str_pd)) {
reportConfigWarning("Bad sensor config request from: " + vname);
return(false);
}
// Part 3: Clip the numerical values if necessary
double d_width = atof(str_width.c_str());
double d_pd = atof(str_pd.c_str());
d_width = vclip_min(d_width, 0);
d_pd = vclip(d_pd, 0, 1);
// Part 4: Process the sensor setting request
if(str_width == "")
return(setVehicleSensorSettingPD(vname, d_pd));
else
return(setVehicleSensorSetting(vname, d_width, d_pd));
}
//------------------------------------------------------------
// Procedure: processClassifyQueue
void HazardSensor_MOOSApp::processClassifyQueue()
{
map<string, ClassifyQueue>::iterator p;
for(p=m_map_classify_queue.begin(); p!=m_map_classify_queue.end(); p++) {
string vname = p->first;
// Part 1: Determine if an entry is to be popped for this vehicle.
if(m_map_classify_last_time[vname] == 0)
m_map_classify_last_time[vname] = m_curr_time;
double elapsed = m_curr_time - m_map_classify_last_time[vname];
if(elapsed < m_min_queue_msg_interval)
continue;
if(m_map_classify_queue[vname].size() == 0)
continue;
m_map_classify_last_time[vname] = m_curr_time;
// Part 2: Get the entry
ClassifyEntry entry = m_map_classify_queue[vname].pop();
bool aspect_affected = entry.getAspectAffected();
// Part 3: Build and post the vehicle specific hazard report
string spec = entry.getHazard().getSpec("hr");
Notify("UHZ_HAZARD_REPORT_"+toupper(vname), spec);
m_map_classify_answ[vname]++;
// Part 4: Build and post the general hazard report
string full_spec = "vname=" + vname + "," + spec;
Notify("UHZ_HAZARD_REPORT", full_spec);
// Part 5: Build/Post visual artifacts using VIEW_CIRCLE
if((m_circle_duration == -1) || (m_circle_duration > 0)) {
XYHazard post_hazard = entry.getHazard();
bool is_hazard = (tolower(post_hazard.getType()) == "hazard");
XYCircle circ(post_hazard.getX(), post_hazard.getY(), 10);
if(is_hazard) {
circ.set_color("edge", "yellow");
circ.set_color("fill", "yellow");
}
else {
circ.set_color("edge", "green");
circ.set_color("fill", "green");
}
//circ.set_label(label);
circ.setDrawVertices(5);
circ.set_vertex_size(0);
circ.set_edge_size(1);
circ.set_transparency(0.3);
circ.set_time(m_curr_time);
// circ.setDuration(m_circle_duration);
if(aspect_affected) {
circ.set_color("fill", "pink");
circ.set_edge_size(2);
circ.set_transparency(0.5);
}
Notify("VIEW_CIRCLE", circ.get_spec());
}
}
}
//---------------------------------------------------------
// Procedure: postVisuals()
void HazardSensor_MOOSApp::postVisuals()
{
if(m_show_hazards == false)
return;
map<string, XYHazard>::iterator p;
for(p=m_map_hazards.begin(); p!=m_map_hazards.end(); p++) {
XYHazard hazard = p->second;
string color = m_color_hazard;
string shape = m_shape_hazard;
double width = m_width_hazard;
double hr = 0.5;
double hx = hazard.getX();
double hy = hazard.getY();
if(hazard.hasResemblance())
hr = hazard.getResemblance();
double aspect = -1;
if(hazard.hasAspect())
aspect = hazard.getAspect();
double aspect_line_len = 15;
if(hazard.hasAspectRangeMin() && hazard.hasAspectRangeMax()) {
double range_min = hazard.getAspectRangeMin();
double range_max = hazard.getAspectRangeMax();
double range_avg = (range_min + range_max) / 2.0;
double range_pct = range_avg / 90;
aspect_line_len = 5 + (range_pct * 25);
}
if(hazard.getType() != "hazard") {
color = m_color_benign;
shape = m_shape_benign;
width = m_width_benign;
}
if(hazard.getColor() != "")
color = hazard.getColor();
if(hazard.getShape() != "")
shape = hazard.getShape();
if(hazard.getWidth() >= 0)
width = hazard.getWidth();
XYMarker marker;
marker.set_vx(hx);
marker.set_vy(hy);
marker.set_label(hazard.getLabel());
marker.set_width(width);
marker.set_type(shape);
if(hazard.getType() != "hazard")
marker.set_transparency(1-hr);
marker.set_active(true);
marker.set_color("primary_color", color);
string spec = marker.get_spec();
Notify("VIEW_MARKER", spec);
if(aspect > 0) {
double x1,y1,x2,y2;
projectPoint(aspect, aspect_line_len, hx, hy, x1, y1);
projectPoint(aspect+180, aspect_line_len, hx, hy, x2, y2);
XYSegList segl;
segl.add_vertex(x1, y1);
segl.add_vertex(x2, y2);
segl.set_vertex_size(0);
segl.set_edge_size(1);
segl.set_color("edge", "gray70");
Notify("VIEW_SEGLIST", segl.get_spec());
}
}
}