DESERT 3.6.0
Loading...
Searching...
No Matches
uwrovctr-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
39#include "uwrovctr-module.h"
40#include <iostream>
41#include <sstream>
42
46static class UwROVCtrModuleClass : public TclClass
47{
48public:
53 : TclClass("Module/UW/ROV/CTR")
54 {
55 }
56
61 TclObject *
62 create(int, const char *const *)
63 {
64 return (new UwROVCtrModule());
65 }
67
69 : UwCbrModule()
70 , sn(0)
71 , adaptiveRTO(0)
72 , adaptiveRTO_parameter(0.5)
73 , speed(1)
74 , posit()
75 , pkt()
76{
77 bind("adaptiveRTO_", (int *) &adaptiveRTO);
78 if (adaptiveRTO == 1) {
79 bind("adaptiveRTO_parameter_", (double *) &adaptiveRTO_parameter);
80 }
81
82 if (adaptiveRTO_parameter < 0) {
83 cerr << NOW << "Invalide adaptive RTO parameter < 0, set to 0.5 "
84 << "by default " << std::endl;
86 }
87}
88
89int
90UwROVCtrModule::command(int argc, const char *const *argv)
91{
92 Tcl &tcl = Tcl::instance();
93 if (argc == 2) {
94 if (strcasecmp(argv[1], "getROVMonheadersize") == 0) {
95 tcl.resultf("%d", this->getROVMonHeaderSize());
96
97 return TCL_OK;
98 } else if (strcasecmp(argv[1], "getROVctrheadersize") == 0) {
99 tcl.resultf("%d", this->getROVCTRHeaderSize());
100
101 return TCL_OK;
102 } else if (strcasecmp(argv[1], "getX") == 0) {
103 tcl.resultf("%f", posit->getX());
104
105 return TCL_OK;
106 } else if (strcasecmp(argv[1], "getY") == 0) {
107 tcl.resultf("%f", posit->getY());
108
109 return TCL_OK;
110 } else if (strcasecmp(argv[1], "getZ") == 0) {
111 tcl.resultf("%f", posit->getZ());
112
113 return TCL_OK;
114 }
115 } else if (argc == 3) {
116 if (strcasecmp(argv[1], "setPosition") == 0) {
117 Position *p = dynamic_cast<Position *>(tcl.lookup(argv[2]));
118
119 if(p) {
120 posit = p;
121
122 return TCL_OK;
123 }
124
125 return TCL_ERROR;
126 } else if (strcasecmp(argv[1], "setSpeed") == 0) {
127 std::stringstream speed_ss(argv[2]);
128 if (speed_ss >> speed)
129 return TCL_OK;
130
131 return TCL_ERROR;
132 } else if (strcasecmp(argv[1], "setAdaptiveRTOparameter") == 0) {
133 std::stringstream artop_ss(argv[2]);
134 if (artop_ss >> adaptiveRTO_parameter)
135 return TCL_OK;
136
137 return TCL_ERROR;
138 }
139 } else if (argc == 5) {
140 if (strcasecmp(argv[1], "sendPosition") == 0) {
141 newX = std::atof(argv[2]);
142 newY = std::atof(argv[3]);
143 newZ = std::atof(argv[4]);
144
145 this->reset_retx();
146 this->transmit();
147
148 tcl.resultf("%s %f %f %f", "Position set to ", newX, newY, newZ);
149
150 return TCL_OK;
151 }
152 } else if (argc == 6) {
153 if (strcasecmp(argv[1], "sendPosition") == 0) {
154 newX = std::atof(argv[2]);
155 newY = std::atof(argv[3]);
156 newZ = std::atof(argv[4]);
157 speed = std::atof(argv[5]);
158
159 this->reset_retx();
160 this->transmit();
161
162 tcl.resultf("%s %f %f %f", "Position set to ", newX, newY, newZ);
163 tcl.resultf("%s %f", "Speed set to ", speed);
164
165 return TCL_OK;
166 }
167 }
168
169 return UwCbrModule::command(argc, argv);
170}
171
172void
174{
175 sendPkt();
176
177 if (adaptiveRTO == 1 && rttsamples > 0) {
179
180 if (debug_) {
181 std::cout << NOW << " UwROVCtrModule::RTO set to " << period_
182 << std::endl;
183 }
184 }
185
186 sendTmr_.resched(period_);
187}
188
189void
193
194void
196{
197 if (this->pkt == nullptr) {
198 hdr_uwROV_ctr *uwROVh = HDR_UWROV_CTR(p);
199
200 uwROVh->x() = newX;
201 uwROVh->y() = newY;
202 uwROVh->z() = newZ;
203 uwROVh->speed() = speed;
204 uwROVh->sn() = ++sn;
205
206 this->pkt = p;
207 } else {
208 hdr_uwROV_ctr *uwROVh = HDR_UWROV_CTR(p);
209
210 uwROVh->x() = newX;
211 uwROVh->y() = newY;
212 uwROVh->z() = newZ;
213 uwROVh->speed() = speed;
214 uwROVh->sn() = sn;
215
216 if (debug_)
217 std::cout << NOW << " UwROVCtrModule::initPkt(Packet *p) "
218 << "Retransmitting" << std::endl;
219 }
220
221 if (debug_) {
222 if (rttsamples > 0) {
223 std::cout << NOW << " RTT UwROVCtr: " << sumrtt / rttsamples << " s"
224 << std::endl;
225 }
226 }
227
229
230 if (debug_) {
231 hdr_uwROV_ctr *uwROVh = HDR_UWROV_CTR(p);
232
233 std::cout << NOW << " UwROVCtrModule::initPkt(Packet *p) setting new"
234 << " ROV way-point: X = " << uwROVh->x()
235 << ", Y = " << uwROVh->y() << ", Z = " << uwROVh->z()
236 << std::endl;
237 }
238}
239
240void
241UwROVCtrModule::recv(Packet *p, Handler *h)
242{
243 recv(p);
244}
245
246void
248{
250 x_rov = monitoring->x();
251 y_rov = monitoring->y();
252 z_rov = monitoring->z();
253
254 if (monitoring->ack() == sn + 1) {
255 sendTmr_.force_cancel();
256 this->pkt = nullptr;
257 }
258
259 if (monitoring->ack() > 0 && debug_)
260 std::cout << NOW << " UwROVCtrModule::recv(Packet *p) control ACK "
261 << "received " << monitoring->ack() << std::endl;
262 else if ((monitoring->ack()) < 0 && debug_)
263 std::cout << NOW << " UwROVCtrModule::recv(Packet *p) control error "
264 << "received" << std::endl;
265 if (debug_ > 10)
266 std::cout << NOW << " UwROVCtrModule::recv(Packet *p) ROV monitoring "
267 << "position: X = " << x_rov << ", Y = " << y_rov
268 << ", Z = " << z_rov << std::endl;
269
271}
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.
virtual void recv(Packet *) override
Performs the reception of packets from upper and lower layers.
UwSendTimer sendTmr_
Timer which schedules packet transmissions.
double sumrtt
Sum of RTT samples.
double period_
Period between two consecutive packet transmissions.
virtual void sendPkt()
Allocates, initialize and sends a packet with the default priority flag set from tcl.
int rttsamples
Number of RTT samples.
Class that represents the binding with the tcl configuration script.
UwROVCtrModuleClass()
Constructor of the class.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwROVCtrModule class is used to manage UWROVCtr packets and to collect statistics about them.
float y_rov
Y of the last ROV position monitored.
int adaptiveRTO
Sequence number of the last control packet sent.
float x_rov
X of the last ROV position monitored.
Position * posit
Controller position.
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
float newY
Y of the new position sent to the ROV.
virtual void transmit() override
Creates and transmits a packet.
virtual void initPkt(Packet *p) override
Initializes a control data packet passed as argument with the default values.
double adaptiveRTO_parameter
Parameter for the adaptive RTO.
float newZ
Z of the new position sent to the ROV.
virtual void recv(Packet *) override
Performs the reception of packets from upper and lower layers.
static int getROVCTRHeaderSize()
Returns the size in byte of a hdr_uwROV_ctr packet header.
float z_rov
Z of the last ROV position monitored.
float newX
X of the new position sent to the ROV.
void reset_retx()
Reset retransmissions.
virtual void start() override
Start the controller.
static int getROVMonHeaderSize()
Returns the size in byte of a hdr_uwROV_monitoring packet header.
UwROVCtrModule()
Constructor of UwROVCtrModule class.
float speed
Moving speed sent to the ROV.
hdr_uwROV_ctr describes UWROV_ctr packets for controlling the ROV.
float & z()
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...
UwROVCtrModuleClass class_module_uwROV_ctr
Provides the definition of the class UWROV.
#define HDR_UWROV_MONITORING(p)
#define HDR_UWROV_CTR(p)