DESERT 3.6.0
Loading...
Searching...
No Matches
uwrov-module.cc
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 "uwrov-module.h"
39#include <iostream>
40
47static class UwROVModuleClass : public TclClass
48{
49public:
54 : TclClass("Module/UW/ROV")
55 {
56 }
57
62 TclObject *
63 create(int, const char *const *)
64 {
65 return (new UwROVModule());
66 }
68
69void
71{
72 module->sendAck();
73}
74
76 : UwCbrModule()
77 , last_sn_confirmed(0)
78 , ack(0)
79 , ackPriority(0)
80 , ackNotPgbk(0)
81 , drop_old_waypoints(0)
82 , ackTimeout(10)
83 , posit()
84 , ackTimer_(this)
85 , ackPolicy(ACK_PIGGYBACK)
86 , log_flag(0)
87 , out_file_stats(0)
88{
89 bind("ackTimeout_", (int *) &ackTimeout);
90 bind("ackPriority_", (int *) &ackPriority);
91 bind("drop_old_waypoints_", (int *) &drop_old_waypoints);
92 bind("log_flag_", (int *) &log_flag);
93
94 if (ackTimeout < 0) {
95 cerr << NOW << " Invalide ACK timout < 0, timeout set to 10 by defaults"
96 << std::endl;
97 ackTimeout = 10;
98 }
99}
100
101int
102UwROVModule::command(int argc, const char *const *argv)
103{
104 Tcl &tcl = Tcl::instance();
105
106 if (argc == 2) {
107 if (strcasecmp(argv[1], "getROVMonheadersize") == 0) {
108 tcl.resultf("%d", getROVMonHeaderSize());
109
110 return TCL_OK;
111 } else if (strcasecmp(argv[1], "getROVctrheadersize") == 0) {
112 tcl.resultf("%d", getROVCTRHeaderSize());
113
114 return TCL_OK;
115 } else if (strcasecmp(argv[1], "getX") == 0) {
116 tcl.resultf("%f", posit->getX());
117
118 return TCL_OK;
119 } else if (strcasecmp(argv[1], "getY") == 0) {
120 tcl.resultf("%f", posit->getY());
121
122 return TCL_OK;
123 } else if (strcasecmp(argv[1], "getZ") == 0) {
124 tcl.resultf("%f", posit->getZ());
125
126 return TCL_OK;
127 } else if (strcasecmp(argv[1], "getAckNotPgbk") == 0) {
128 tcl.resultf("%d", ackNotPgbk);
129
130 return TCL_OK;
131 }
132 } else if (argc == 3) {
133 if (strcasecmp(argv[1], "setPosition") == 0) {
134 UWSMPosition *p = dynamic_cast<UWSMPosition *>(tcl.lookup(argv[2]));
135 if (p) {
136 posit = p;
137
138 tcl.resultf("%s", "Position set\n");
139
140 return TCL_OK;
141 }
142
143 tcl.result("Position error\n");
144
145 return TCL_ERROR;
146 }
147 if (strcasecmp(argv[1], "setAckPolicy") == 0) {
148 if (atof(argv[2]) == 1) {
150 return TCL_OK;
151 }
152 if (atof(argv[2]) == 2) {
154 return TCL_OK;
155 }
156 if (atof(argv[2]) == 3) {
158 return TCL_OK;
159 }
160 }
161 if (strcasecmp(argv[1], "setAckTimeout") == 0) {
162 ackTimeout = atof(argv[2]);
163
164 return TCL_OK;
165 }
166 if (strcasecmp(argv[1], "setAckPriority") == 0) {
167 ackPriority = atof(argv[2]);
168
169 return TCL_OK;
170 }
171 } else if (argc == 5) {
172 if (strcasecmp(argv[1], "setdest") == 0) {
173 posit->setdest(atof(argv[2]), atof(argv[3]), atof(argv[4]));
174
175 return TCL_OK;
176 }
177 } else if (argc == 6) {
178 if (strcasecmp(argv[1], "setdest") == 0) {
179 posit->setdest(atof(argv[2]), atof(argv[3]), atof(argv[4]), atof(argv[5]));
180
181 return TCL_OK;
182 }
183 }
184
185 return UwCbrModule::command(argc, argv);
186}
187
188void
190{
192 hdr_uwcbr *uwcbrh = HDR_UWCBR(p);
193
194 uwROVh->x() = posit->getX();
195 uwROVh->y() = posit->getY();
196 uwROVh->z() = posit->getZ();
197 uwROVh->ack() = ack;
198
199 ack = 0;
200
201 if (debug_)
202 std::cout << NOW << " UwROVModule::initPkt(Packet *p) ROV current "
203 << "position: X = " << uwROVh->x() << ", Y = " << uwROVh->y()
204 << ", Z = " << uwROVh->z() << std::endl;
205
206 ackTimer_.force_cancel();
207
209
210 if (debug_) {
211 std::cout << NOW << " UwROVModule::sending packet with priority "
212 << (int) uwcbrh->priority() << std::endl;
213 }
214
215 priority_ = 0;
216}
217
218void
219UwROVModule::recv(Packet *p, Handler *h)
220{
221 recv(p);
222}
223
224void
226{
227 hdr_uwROV_ctr *uwROVh = HDR_UWROV_CTR(p);
228
229 if (drop_old_waypoints == 1 && uwROVh->sn() <= last_sn_confirmed) {
230 // obsolete packets
231 if (debug_)
232 std::cout << NOW << " UwROVModule::old waypoint with sn "
233 << uwROVh->sn() << " dropped " << std::endl;
234
235 } else {
236 // packet in order
237 posit->setdest(uwROVh->x(), uwROVh->y(), uwROVh->z(), uwROVh->speed());
238 last_sn_confirmed = uwROVh->sn();
239 }
240
242 priority_ = (char) ackPriority;
243
244 if (log_flag == 1) {
245 out_file_stats.open("my_log_file.csv", std::ios_base::app);
246 out_file_stats << left << "time: " << NOW
247 << ", positions ROV: x = " << posit->getX()
248 << ", y = " << posit->getY() << ", z = " << posit->getZ()
249 << std::endl;
250
251 out_file_stats.close();
252 }
253
254 if (debug_) {
255 std::cout << NOW
256 << " UwROVModule::recv(Packet *p) ROV received new "
257 "way point: X = "
258 << uwROVh->x() << ", Y = " << uwROVh->y()
259 << ", Z = " << uwROVh->z() << std::endl;
260 }
261
263
264 if (ackPolicy == ACK_IMMEDIATELY) {
265
266 if (ackPriority == 0) {
268
269 if (debug_)
270 cout << NOW << " ACK sent immediately with standard priority "
271 << std::endl;
272 } else {
274
275 if (debug_)
276 cout << NOW << " ACK sent immediately with high priority "
277 << std::endl;
278 }
279 }
280
281 if (ackPolicy == ACK_PGBK_OR_TO) {
282 ackTimer_.resched(ackTimeout);
283 }
284}
285
286void
288{
289 ackNotPgbk++;
290
291 if (ackPriority == 0) {
293
294 if (debug_)
295 cout << NOW << " ACK timeout expired, ACK sent with standard "
296 << "priority " << std::endl;
297 } else {
299
300 if (debug_)
301 cout << NOW << " ACK timeout expired, ACK sent with high priority "
302 << std::endl;
303 }
304}
virtual double getY() override
Get the current projection on y-axis of the node postion If necessary (updating time expired),...
virtual double getX() override
Get the current projection on x-axis of the node postion If necessary (updating time expired),...
virtual void setdest(double x_dest, double y_dest, double z_dest, double speed)
virtual double getZ() override
Get the current projection on z-axis of the node postion If necessary (updating time expired),...
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 int command(int argc, const char *const *argv) override
TCL command interpreter.
int debug_
Flag to enable several levels of debug.
char priority_
Priority of the data packets.
virtual void recv(Packet *) override
Performs the reception of packets from upper and lower layers.
virtual void sendPktHighPriority()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
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.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwROVModuleClass()
Constructor of the class.
UwROVModule class is used to manage UWROV packets and to collect statistics about them.
int drop_old_waypoints
< Number of ACK not sent in piggyback when ackPolicy = 2.
int ack
If not zero, contains the ACK to the last command packet received.
virtual void sendAck()
Sends ACK if ackTimeout expire;.
UwROVModule()
Default Constructor of UwROVModule class.
static int getROVMonHeaderSize()
Returns the size in byte of a hdr_uwROV_monitoring packet header.
int ackTimeout
< Flag set to 1 to drop waypoints with sequence number lower or equal than last_sn_confirmed.
UWSMPosition * posit
ROV position.
UWROV_ACK_POLICY ackPolicy
Flag to set the policy for ACK transimission, ACK_PIGGYBACK: ACK is always sent in piggyback,...
std::ofstream out_file_stats
Output stream for the textual file of debug.
virtual void recv(Packet *) override
Performs the reception of packets from upper and lower layers.
UwROVSendAckTimer ackTimer_
Timer to schedule ACK transmission.
int ackPriority
Flag to give higher priority to ACK or not.
virtual void initPkt(Packet *p) override
Initializes a monitoring data packet passed as argument with the default values.
int last_sn_confirmed
Sequence number of the last command packet received.
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
int log_flag
Flag to enable log file writing.
static int getROVCTRHeaderSize()
Returns the size in byte of a hdr_uwROV_ctr packet header.
virtual void expire(Event *e)
hdr_uwROV_ctr describes UWROV_ctr packets for controlling the ROV.
float & z()
static int offset_
Required by the PacketHeaderManager.
double & sn()
float & speed()
float & x()
float & y()
hdr_uwROV_monitoring describes UWROV_monitoring packets sent by the ROV to the base station for monit...
static int offset_
Required by the PacketHeaderManager.
hdr_uwcbr describes UWCBR packets.
char & priority()
Reference to the priority_ variable.
#define HDR_UWCBR(p)
UwROVModuleClass class_module_uwROV
Provides the definition of the class UWROV.
#define HDR_UWROV_MONITORING(p)
#define HDR_UWROV_CTR(p)