DESERT 3.5.1
Loading...
Searching...
No Matches
uwtracker-module.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 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
38#include "uwtracker-module.h"
39#include "mphy_pktheader.h"
40#include <iostream>
41#include <limits>
42#include <math.h>
43#include <uwsmposition.h>
44#define HDR_UWTRACK(p) (hdr_uwTracker::access(p))
45extern packet_t PT_UWTRACK;
52static class UwTrackerModuleClass : public TclClass {
53public:
54
58 UwTrackerModuleClass() : TclClass("Module/UW/TRACKER") {
59 }
60
65 TclObject* create(int, const char*const*) {
66 return (new UwTrackerModule());
67 }
69
71 : UwCbrModule()
72 , track_position(nullptr)
73 , track_measure{0}
74 , max_tracking_distance(std::numeric_limits<int>::max())
75 , send_only_active_trace(0)
76 , tracking_period(0)
77 , measure_timer(this)
78{
79 bind("max_tracking_distance_", (double*) &max_tracking_distance);
80 bind("send_only_active_trace_", (int*) &send_only_active_trace);
81 bind("tracking_period_", (double*) &tracking_period);
82}
83
85 : UwCbrModule()
86 , track_position(p)
87 , max_tracking_distance(std::numeric_limits<int>::max())
88 , send_only_active_trace(0)
89 , track_my_position(0)
90 , tracking_period(0)
91 , measure_timer(this)
92{
93 bind("max_tracking_distance_", (double*) &max_tracking_distance);
94 bind("send_only_active_trace_", (int*) &send_only_active_trace);
95 bind("track_my_position_", (int*) &track_my_position);
96 bind("tracking_period_", (double*) &tracking_period);
97}
98
100
101int UwTrackerModule::command(int argc, const char*const* argv) {
102 Tcl& tcl = Tcl::instance();
103 if(argc == 3){
104 if (strcasecmp(argv[1], "setTrack") == 0) {
105 UWSMPosition* p = dynamic_cast<UWSMPosition*> (tcl.lookup(argv[2]));
107 tcl.resultf("%s", "position Setted\n");
108 return TCL_OK;
109 }
110 if (strcasecmp(argv[1], "setMaxTrackDistance") == 0) {
111 max_tracking_distance = atof(argv[2]);
112 tcl.resultf("%s", "max_tracking_distance Setted\n");
113 return TCL_OK;
114 }
115 if (strcasecmp(argv[1], "setTrackMyPosition") == 0) {
116 track_my_position = atoi(argv[2]);
117 tcl.resultf("%s", "max_tracking_distance Setted\n");
118 return TCL_OK;
119 }
120 }
121 return UwCbrModule::command(argc,argv);
122}
123
124void
126{
127 Position track_tmp_pos;
128 track_tmp_pos.setX(track_measure.x());
129 track_tmp_pos.setY(track_measure.y());
130 track_tmp_pos.setZ(track_measure.z());
132 track_tmp_pos.getDist(getPosition()) < max_tracking_distance) {
133 return UwCbrModule::sendPkt();
134 }
135
136}
137
139 hdr_uwTracker* uw_track_h = HDR_UWTRACK(p);
140 *uw_track_h = track_measure;
141 if (debug_)
142 std::cout << NOW << " UwTrackerModule::initPkt(Packet *p) Track current "
143 << "position: X = " << uw_track_h->x() <<
144 ", Y = " << uw_track_h->y()
145 << ", Z = " << uw_track_h->z()
146 << ", speed = " << uw_track_h->speed() << std::endl;
149 }
151}
152
153void
155{
156 hdr_MPhy *ph = HDR_MPHY(p);
157 hdr_uwTracker* uw_track_h = HDR_UWTRACK(p);
158 hdr_uwcbr *uwcbrh = HDR_UWCBR(p);
159 hdr_cmn *ch = hdr_cmn::access(p);
160 hdr_uwip *uwiph = hdr_uwip::access(p);
161 if (tracefile_enabler_) {
162 tracefile << NOW << " " << ch->timestamp() << " " << uwcbrh->sn()
163 << " " << (int) uwiph->saddr() << " " << (int) uwiph->daddr()
164 << " " << ch->size();
165 Position track_tmp_pos;
166 track_tmp_pos.setX(uw_track_h->x());
167 track_tmp_pos.setY(uw_track_h->y());
168 track_tmp_pos.setZ(uw_track_h->z());
169 if(track_my_position || track_tmp_pos.getDist(ph->srcPosition) < max_tracking_distance) {
170 tracefile << " " << uw_track_h->timestamp()
171 << " " << uw_track_h->x() << " "
172 << uw_track_h->y() << " " << uw_track_h->z() << " "
173 << uw_track_h->speed();
174 } else {
175 tracefile << " NO TRACKS IN RANGE, distance = "
176 << track_tmp_pos.getDist(ph->srcPosition) << " max distance = "
178 }
179 tracefile <<"\n";
180 tracefile.flush();
181 }
182}
183
185{
186 track_measure.timestamp() = NOW;
187 if(track_position) {
192 } else {
193 track_measure.x() = 0;
194 track_measure.y() = 0;
195 track_measure.z() = 0;
196 track_measure.speed() = 0;
197 }
198 measure_timer.resched(tracking_period); // schedule next measure
199}
200
206
208{
209 measure_timer.force_cancel();
211}
212
214{
215 module->updateTrackMeasure();
216}
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.
double getSpeed() const
Method that return the actual speed.
virtual double getZ()
Method that return the current projection of the node on the z-axis.
UwCbrModule class is used to manage UWCBR packets and to collect statistics about them.
virtual void initPkt(Packet *p)
Initializes a data packet passed as argument with the default values.
virtual void stop()
Stop to send packets.
int debug_
Flag to enable several levels of debug.
int tracefile_enabler_
True if enable tracefile of received packets, default disabled.
virtual void start()
Start to send packets.
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
std::ofstream tracefile
virtual void sendPkt()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
Class that represents the binding with the tcl configuration script.
UwTrackerModuleClass()
Constructor of the class.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
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.
virtual ~UwTrackerModule()
Destructor of UwTrackerModule class.
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.
int send_only_active_trace
send only active trace
int track_my_position
track also my position
virtual void initPkt(Packet *p)
Initializes a monitoring data packet passed as argument with the default values.
UwTrackerModule()
Default Constructor of UwTrackerModule class.
UWSMPosition * track_position
Track position.
double max_tracking_distance
Maximum tracking distance, in [m].
void updateTrackMeasure()
Update the track measure.
virtual void printReceivedPacket(Packet *p)
Print to tracefile details about a received packet.
virtual void expire(Event *e)
hdr_uwROV_ctr describes UWROV_ctr packets for controlling the ROV.
float & timestamp()
Reference to the timestamp variable.
float & speed()
Reference to the speed variable.
float & x()
Reference to the x variable.
float & z()
Reference to the z variable.
float & y()
Reference to the y variable.
static int offset_
Required by the PacketHeaderManager.
hdr_uwcbr describes UWCBR packets.
uint16_t & sn()
Reference to the sn_ variable.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:95
static hdr_uwip * access(const Packet *p)
Definition uwip-module.h:86
#define HDR_UWCBR(p)
#define HDR_UWTRACK(p)
Provides the definition of the class UWSMPosition.
UwTrackerModuleClass class_module_uwTrack
packet_t PT_UWTRACK
Provides the definition of the class UWROV.