DESERT 3.5.1
Loading...
Searching...
No Matches
uwsc-tracker-follower-module.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2024 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
40#include <iostream>
41
45static class UwSCFTrackerModuleClass : public TclClass {
46public:
47
51 UwSCFTrackerModuleClass() : TclClass("Module/UW/SC/TRACKERF") {
52 }
53
58 TclObject* create(int, const char*const*) {
59 return (new UwSCFTrackerModule());
60 }
62
65 , mine_positions()
66 , auv_position()
67 , auv_state()
68 , demine_period(0)
69 , mine_measure{0}
70 , mine_timer(this)
71{
72 bind("demine_period_", (double*) &demine_period);
73}
74
75
76int UwSCFTrackerModule::command(int argc, const char*const* argv) {
77 Tcl& tcl = Tcl::instance();
78
79 if(argc == 3){
80 if (strcasecmp(argv[1], "setTrack") == 0) {
81 UWSMPosition* p = dynamic_cast<UWSMPosition*> (tcl.lookup(argv[2]));
82
83 if(p)
84 {
85 mine_positions.emplace_back(p);
87 tcl.resultf("%s", "position Setted\n");
88 return TCL_OK;
89 }
90
91 tcl.resultf("%s", "position error\n");
92 return TCL_ERROR;
93 }
94 }
95
96 return UwTrackerModule::command(argc,argv);
97}
98
99void
101{
103
105 {
106 Position temp_position;
107 temp_position.setX(track_measure.x());
108 temp_position.setY(track_measure.y());
109 temp_position.setZ(track_measure.z());
110
111 for (auto it = mine_positions.begin(); it != mine_positions.end();++it)
112 {
113 if((*it)->getDist(&temp_position) < 1e-3)
114 {
115 if (it == mine_positions.end()-1)
116 {
117 track_position = *(--it);
118 mine_positions.erase(++it);
119 }
120 else
121 {
122 track_position = *(++it);
123 mine_positions.erase(--it);
124 }
125 break;
126 }
127 }
128
129 mine_measure.mine_remove() = false;
130
132 }
133
134 if (auv_state == FollowerState::DETECT && mine_positions.size() <= 1)
135 {
136 mine_measure.mine_remove() = false;
138 }
139
141}
142
143void
145{
149
150 hdr_uwSCFTracker* uwscf_track_h = HDR_UWSCFTRACK(p);
151 *uwscf_track_h = mine_measure;
152
154}
155
156void
158{
160 auv_position.getX() == track_measure.x() &&
161 auv_position.getY() == track_measure.y() &&
162 auv_position.getZ() == track_measure.z())
163 {
164 mine_measure.mine_remove() = true;
165
167
168 mine_timer.resched(demine_period);
169 sendTmr_.resched(demine_period);
171
172 if (debug_)
173 std::cout << NOW << "UwSCFTrackerModule::updateMineRemove()"
174 << " Mine at position: X = " << track_measure.x()
175 << ", Y = " << track_measure.y()
176 << ", Z = " << track_measure.z()
177 << " is detected" << std::endl;
178 }
179
180 auv_position.setX(getPosition()->getX());
181 auv_position.setY(getPosition()->getY());
182 auv_position.setZ(getPosition()->getZ());
183
186
189 {
192 }
193}
194
197{
198 UWSMPosition* new_track_position (track_position);
199 double min_distance = new_track_position->getDist(&auv_position);
200
201 mine_measure.timestamp() = NOW;
202
203 for (const auto& pos : mine_positions)
204 {
205 double distance = pos->getDist(&auv_position);
206 if(distance < min_distance)
207 {
208 min_distance = distance;
209 new_track_position = pos;
210 }
211 }
212
213 if (debug_)
214 std::cout << NOW << " UwSCFTrackerModule::updateTrackPosition()"
215 << "New track position: X = " << new_track_position->getX()
216 << " Y = " << new_track_position->getY()
217 << " Z = " << new_track_position->getZ()
218 << std::endl;
219
220 return new_track_position;
221}
222
223void
225{
226 auv_position.setX(getPosition()->getX());
227 auv_position.setY(getPosition()->getY());
228 auv_position.setZ(getPosition()->getZ());
229
231
233}
234
235void
237{
238 mine_timer.force_cancel();
240}
241
242void
244{
245 module->updateMineRemove();
246}
virtual double getY()
Method that return the current projection of the node on the y-axis.
virtual double getX()
Method that return the current projection of the node on the x-axis.
virtual double getZ()
Method that return the current projection of the node on the z-axis.
virtual void setX(double x)
int debug_
Flag to enable several levels of debug.
UwSendTimer sendTmr_
Timer which schedules packet transmissions.
Class that represents the binding with the tcl configuration script.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwSCFTrackerModuleClass()
Constructor of the class.
UwSCFTrackerModule class is used to track mines via sonar and share tracking information via packets.
void updateMineRemove()
Update the mine measure.
virtual void stop() override
Stop to send packets.
FollowerState auv_state
Current state of the follower.
hdr_uwSCFTracker mine_measure
Detected mine packets.
void sendPkt()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
UWSMPosition * updateTrackPosition()
Update the current track position with the nearest mine position.
double demine_period
Timer to schedule packets transmission.
virtual void start() override
Start to send packets.
virtual void initPkt(Packet *p) override
Initializes a monitoring data packet passed as argument with the default values.
std::vector< UWSMPosition * > mine_positions
Positions of the mines in the area.
UwSCFTrackerModule()
Default Constructor of UwSCFTrackerModule class.
Position auv_position
Current position of the follower.
UwUpdateMineStatus mine_timer
Timer to schedule detecting measurements.
UwTrackerModule class is used to track mobile nodes via sonar and share tracking information via pack...
virtual void stop()
Stop to send packets.
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
UwUpdateTrackMeasure measure_timer
timer to schedule tracking measurements
double tracking_period
period between tracking measurements
void sendPkt()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
hdr_uwTracker track_measure
Track position.
virtual void start()
Start to send packets.
virtual void initPkt(Packet *p)
Initializes a monitoring data packet passed as argument with the default values.
UWSMPosition * track_position
Track position.
hdr_uwSCFTracker describes the position and status of the current detected mine.
float & timestamp()
Reference to the timestamp variable.
float & y()
Reference to the y variable.
float & x()
Reference to the x variable.
bool & mine_remove()
Reference to the speed variable.
float & z()
Reference to the z variable.
float & x()
Reference to the x variable.
float & z()
Reference to the z variable.
float & y()
Reference to the y variable.
UwSCFTrackerModuleClass class_module_uwSCFTracker
Provides the definition of the class UWSCFTRACKER.
#define HDR_UWSCFTRACK(p)