DESERT 3.5.1
Loading...
Searching...
No Matches
uwsc-mission-coordinator-module.cc
Go to the documentation of this file.
1// Copyright (c) 2024 Regents of the SIGNET lab, University of Padova.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1. Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12// 3. Neither the name of the University of Padova (SIGNET lab) nor the
13// names of its contributors may be used to endorse or promote products
14// derived from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
39#include "uwsc-clmsg.h"
40#include <algorithm>
41#include <iostream>
42#include <uwsmposition.h>
43
47static class UwMissionCoordinatorModuleClass : public TclClass
48{
49public:
54 : TclClass("Plugin/UW/SC/MC")
55 {
56 }
57
62 TclObject *
63 create(int, const char *const *)
64 {
65 return (new UwMissionCoordinatorModule());
66 }
68
70 : PlugIn()
71 , auv_follower()
72{
73}
74
75int
76UwMissionCoordinatorModule::command(int argc, const char *const *argv)
77{
78 Tcl &tcl = Tcl::instance();
79
80 if (argc == 2) {
81 if (strcasecmp(argv[1], "getremovedmines") == 0) {
82 int count = 0;
83
84 for (const auto &auv : auv_follower)
85 count += auv.n_mines;
86
87 tcl.resultf("%d", count);
88
89 return TCL_OK;
90 }
91 } else if (argc == 4) {
92 if (strcasecmp(argv[1], "addAUV") == 0) {
93 auv_follower.emplace_back(std::atoi(argv[2]), std::atoi(argv[3]));
94
95 tcl.resultf("%d", "auv follower added\n");
96 return TCL_OK;
97 }
98 }
99
100 return PlugIn::command(argc, argv);
101}
102
103int
105{
106 if (m->type() == CLMSG_CTR2MC_GETPOS) {
107 int id = ((ClMsgCtr2McPosition *) m)->getSource();
108 Position *p = ((ClMsgCtr2McPosition *) m)->getRovPosition();
109
110 const auto &auv = std::find_if(auv_follower.begin(),
111 auv_follower.end(),
112 [id](const AUV_stats &element) {
113 return element.ctr_id == id;
114 });
115
116 if (auv != auv_follower.end()) {
117 auv->rov_position = p;
118
119 if (auv->rov_status) {
120 auto mine = auv->rov_mine.end() - 1;
121
122 if (mine->state == Mine::MINE_TRACKED &&
123 mine->track_position->getDist(p) == 0)
124 mine->state = Mine::MINE_DETECTED;
125 }
126
127 if (debug_) {
128 std::cout << NOW
129 << " UwMissionCoordinatorModule::recvSyncClMsg()"
130 << " Received ROV (" << auv->ctr_id
131 << ") updated position X: "
132 << auv->rov_position->getX()
133 << " Y: " << auv->rov_position->getY()
134 << " Z: " << auv->rov_position->getZ()
135 << " #mine tracked = " << auv->n_mines
136 << " rov_status = " << auv->rov_status;
137
138 if (auv->rov_status)
139 std::cout << " mine status = "
140 << auv->rov_mine[auv->n_mines - 1].state;
141
142 std::cout << std::endl;
143 }
144
145 return 0;
146 }
147
148 if (debug_)
149 std::cout << NOW << " UwMissionCoordinatorModule::recvSyncClMsg()"
150 << " no auv found with id (" << id << ")" << std::endl;
151
152 } else if (m->type() == CLMSG_TRACK2MC_TRACKPOS) {
153 int id = ((ClMsgTrack2McPosition *) m)->getSource();
154 Position *p = ((ClMsgTrack2McPosition *) m)->getTrackPosition();
155
156 if (isTracked(p))
157 return 0;
158
159 const auto &auv = std::find_if(auv_follower.begin(),
160 auv_follower.end(),
161 [id](const AUV_stats &element) {
162 return element.trk_id == id;
163 });
164
165 if (auv->rov_status) {
166 const auto &mine = auv->rov_mine.end() - 1;
167
168 if (mine->track_position->getDist(p) > 0 &&
169 mine->state == Mine::MINE_DETECTED)
170 removeMine(auv->ctr_id);
171 }
172
173 if (auv != auv_follower.end() && !auv->rov_status) {
174 auv->rov_mine.emplace_back(p, Mine::MINE_TRACKED);
175 auv->n_mines++;
176 auv->rov_status = true;
177
178 ClMsgMc2CtrPosition msg(auv->ctr_id);
179 msg.setRovDestination(p);
180 sendSyncClMsg(&msg);
181
182 if (debug_)
183 std::cout << NOW
184 << " UwMissionCoordinatorModule::recvSyncClMsg()"
185 << " ROV (" << auv->trk_id
186 << ") tracked mine at position X: " << p->getX()
187 << " Y: " << p->getY() << " Z: " << p->getZ()
188 << " #mine tracked = " << auv->n_mines
189 << " rov_status = " << auv->rov_status << std::endl;
190
191 return 0;
192 }
193
194 if (debug_)
195 std::cout << NOW << " UwMissionCoordinatorModule::recvSyncClMsg()"
196 << " can't track mine at position X: " << p->getX()
197 << " Y: " << p->getY() << " Z: " << p->getZ()
198 << std::endl;
199
200 } else if (m->type() == CLMSG_TRACK2MC_GETSTATUS) {
201 int id = ((ClMsgTrack2McStatus *) m)->getSource();
202 bool remove = ((ClMsgTrack2McStatus *) m)->getMineStatus();
203
204 const auto &auv = std::find_if(auv_follower.begin(),
205 auv_follower.end(),
206 [id](const AUV_stats &element) {
207 return element.trk_id == id;
208 });
209
210 if (auv != auv_follower.end() && remove) {
211 removeMine(auv->ctr_id);
212
213 return 0;
214 }
215 }
216
217 return PlugIn::recvSyncClMsg(m);
218}
219
220void
222{
223 const auto &auv = std::find_if(auv_follower.begin(),
224 auv_follower.end(),
225 [id](const AUV_stats &element) { return element.ctr_id == id; });
226
227 if (auv != auv_follower.end() && auv->n_mines > 0) {
228 const auto &mine = auv->rov_mine.end() - 1;
229
230 if (mine->state != Mine::MINE_REMOVED) {
231 mine->state = Mine::MINE_REMOVED;
232 auv->rov_status = false;
233
234 ClMsgMc2CtrStatus msg(auv->ctr_id);
235 msg.setRovStatus(auv->rov_status);
236 sendSyncClMsg(&msg);
237
238 if (debug_)
239 std::cout << NOW << " UwMissionCoordinatorModule::removeMine()"
240 << " Removed mine at position"
241 << " X: " << mine->track_position->getX()
242 << " Y: " << mine->track_position->getY()
243 << " Z: " << mine->track_position->getZ()
244 << std::endl;
245 }
246 } else if (debug_)
247 std::cout << NOW << " UwMissionCoordinatorModule::removeMine()"
248 << " Cannot remove mine detected by ROV (" << id << ")"
249 << std::endl;
250}
251
252bool
254{
255 for (const auto &auv : auv_follower) {
256 for (const auto &mine : auv.rov_mine) {
257 if (mine.track_position->getDist(p) < 1e-3) {
258 if (debug_)
259 std::cout << NOW
260 << " UwMissionCoordinatorModule::isTracked()"
261 << " Mine at position X: " << p->getX()
262 << " Y: " << p->getY() << " Z: " << p->getZ()
263 << " is already tracked by ROV (" << auv.trk_id
264 << ")" << std::endl;
265
266 return true;
267 }
268 }
269 }
270
271 return false;
272}
ClMessage_t CLMSG_TRACK2MC_TRACKPOS
ClMessage_t CLMSG_CTR2MC_GETPOS
ClMessage_t CLMSG_TRACK2MC_GETSTATUS
Class that manages cross layer messages that require the position of the ROV follower.
Definition uwsc-clmsg.h:62
Class that manages cross layer messages that require the new destination of the ROV follower.
Definition uwsc-clmsg.h:103
void setRovDestination(Position *destination)
Sets the ROV follower destination.
Definition uwsc-clmsg.cc:80
Class that manages cross layer messages that require the status of the ROV follower.
Definition uwsc-clmsg.h:145
void setRovStatus(bool detect)
Sets the rov follower status.
Class that manages cross layer messages that require the track position of the ROV follower.
Definition uwsc-clmsg.h:187
Class that manages cross layer messages that require the status of a mine tracked from a rov follower...
Definition uwsc-clmsg.h:228
Class that represents the binding with the tcl configuration script.
UwMissionCoordinatorModuleClass()
Constructor of the class.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwMissionCoordinatorModule class is used to manage AUV followers and to collect statistics about them...
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
virtual int recvSyncClMsg(ClMessage *m) override
Recv syncronous cross layer messages to require an operation from another module.
void removeMine(int id)
Send a signal to the AUV follower to inform it, that the mine it is detecting is removed.
std::vector< AUV_stats > auv_follower
ROV followers info.
UwMissionCoordinatorModule()
Default Constructor of UwMissionCoordinatorModule class.
bool isTracked(Position *p) const
Check if the mine at received position is already tracked.
AUV_stats describes statistics about the AUV follower.
@ MINE_DETECTED
Mine found and started the removing process.
UwMissionCoordinatorModuleClass class_module_uwMC
Provides the definition of the class UWSMPosition.