DESERT 3.5.1
Loading...
Searching...
No Matches
uwranging_tdma.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//
29
39#include <mmac.h>
40#include <mac.h>
41#include "uwranging_tdma.h"
42#include "uwranging_tdma_hdr.h"
43#include <iostream>
44#include <tclcl.h>
45#include <cmath>
46#include <iomanip>
47
48//define a macro to print debug messages
49#define DEBUG(level,text) {if (1) {std::cout << NOW << " UwRangingTDMA(" << node_id << "): " << text << std::endl;}}
53static class UwRangingTDMAModuleClass : public TclClass
54{
55
56public:
61 : TclClass("Module/UW/RANGING_TDMA")
62 {
63 }
68 TclObject *
69 create(int, const char *const *)
70 {
71 return (new UwRangingTDMA());
72 }
73
75
76//initializing static variables
78
80 : UwTDMA()
81 ,node_id(count_nodes++)
82 ,slot_id(node_id)
83 ,n_nodes(tot_slots)
84 ,slotidmax(SLOTIDMAX_HDR - std::fmod(SLOTIDMAX_HDR,n_nodes) -1)
85 ,owtt_vec()
86 ,owtt_map()
87{
89 owtt_vec.resize(((n_nodes * (n_nodes - 1)) / 2) + 1, -1.0);
90 owtt_vec[0] = 0.; //so that owtt_vec[owtt_map[a][a]] will return 0
91
92 if (count_nodes > n_nodes) {
93 std::cerr << NOW << " UwRangingTDMA() instances are " << count_nodes
94 << " but parameter tot_slots is set to " << tot_slots << std::endl;
95 }
96
97 // initialize the owtt_map
98 owtt_map.resize(n_nodes, std::vector<int>(n_nodes, 0));
99 {
100 int temp = 1;
101 for (int ni = 0; ni < n_nodes; ni++)
102 {
103 for (int nj = ni + 1; nj < n_nodes; nj++)
104 {
105 owtt_map[ni][nj] = temp;
106 owtt_map[nj][ni] = temp++;
107 }
108 }
109 }
110}
111
115
116void
118{
119 // build the ranging packet
120 Packet *p = Packet::alloc();
121 hdr_cmn *ch = HDR_CMN(p);
122 hdr_mac *mach = HDR_MAC(p);
124 ch->ptype() = PT_UWRANGING_TDMA;
125 mach->set(MF_CONTROL, addr, BCAST_ADDR);
126 mach->macDA() = BCAST_ADDR; // hdr_mac->set() doesn't set correctly broadcast address as -1
127 rangh->slotId() = slot_id;
128 //DEBUG(5,"sending ping with slot_id: " << slot_id)
129 slot_id += n_nodes;
130 if(slot_id > slotidmax) {
132 }
133 for(int i= 0; i< n_nodes;i++){
134 if ( i != node_id) {
135 //rangh->times().push_back(half_float::half_cast<half_float::half>(owtt_vec[owtt_map[node_id][i]]));
136 rangh->times().push_back(owtt_vec[owtt_map[node_id][i]]);
137 }
138 }
139 // DEBUG(5,"sending ping with values:")
140 // for (auto &&i : rangh->times())
141 // {
142 // std::cout << i*1500.0 << " ";
143 // }
144 // std::cout << std::endl;
145
146 (ch->size()) += rangh->getSize();
147 incrCtrlPktsTx();
149}
150
151void
153{
154 sendRange();
155 //txData() should be called by UwTDMA::Phy2MacEndTx()
156}
157
158void
164
165void
167{
168 hdr_cmn *ch = HDR_CMN(p);
169 incrCtrlPktsRx(); //use to count all control packets (good+errored ones)
170 if(ch->ptype() == PT_UWRANGING_TDMA) {
171
174 if (!(ch->error())) {
176 int origin_node = ((rangh->slotId()) % n_nodes);
177 double range_rx_time = std::fmod(NOW-Mac2PhyTxDuration(p),slot_duration*(slotidmax+1));
178 double tt = range_rx_time - ((rangh->slotId())*slot_duration);
179 //DEBUG(0,"reveiced ping from "<<origin_node << " with values")
180 {
181 int i=0;
182 for (auto el : rangh->times())
183 {
184 if (i == origin_node) {++i;}
185 if (el >= 0.) {
186 owtt_vec[owtt_map[origin_node][i]] = el; //copy owttimes data from packet
187 }
188 ++i;
189 }
190 }
191 owtt_vec[owtt_map[node_id][origin_node]] = tt; //save measured owtt
192 //DEBUG(0,"update [" << node_id << "][" << origin_node << "] : " << tt*1500.0)
193 } else {incrXCtrlPktsRx();} //use incrXCtrlPktsRx() to count control packets with errors
194 } else {incrXCtrlPktsRx();}
195 Packet::free(p);
196 }
197 else {UwTDMA::Phy2MacEndRx(p);}
198}
199
200void
202{
204 if(sea_trial_) {
205 sendDown(p, 0.01);
206 } else {
207 MMac::Mac2PhyStartTx(p);
208 }
209}
210
211int
212UwRangingTDMA::command(int argc, const char *const *argv)
213{
214 Tcl &tcl = Tcl::instance();
215 if (argc == 4)
216 {
217 if (strcasecmp(argv[1], "get_distance") == 0)
218 {
219 int n1 = atoi(argv[2]);
220 int n2 = atoi(argv[3]);
221 if (n1 >= 0 && n1 < n_nodes && n2 >= 0 && n2 < n_nodes)
222 {
223 tcl.resultf("%.17f", owtt_vec[owtt_map[n1][n2]]);
224 return TCL_OK;
225 }
226 return TCL_ERROR;
227 }
228 }
229 return UwTDMA::command(argc, argv);
230}
Class that represent the binding of the protocol with tcl.
UwRangingTDMAModuleClass()
Constructor of the TDMAGenericModule class.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
Class that represents a UwRangingTDMA Node.
int node_id
id of the node (0 to n_nodes-1)
int n_nodes
number of nodes
virtual void Mac2PhyStartTx(Packet *p)
Method called when the Mac Layer start to transmit a Packet.
virtual void sendRange()
sends the ranging packet at the beginning of my slot
virtual void Phy2MacStartRx(const Packet *p)
Method called when the Phy Layer start to receive a Packet.
std::vector< std::vector< int > > owtt_map
of size [n_nodes][n_nodes] maps(nodeX,nodeY) -> owtt_vec index
static int count_nodes
counts the instantiated nodes, used for assigning node ids in default contructor
int slotidmax
maximum slot_id allowable in packet header
UwRangingTDMA()
Constructor of the UwRangingTDMA class.
virtual void Phy2MacEndRx(Packet *p)
Method called when the Phy Layer finish to receive a Packet.
virtual ~UwRangingTDMA()
Destructor of the UwRangingTDMA class.
std::vector< double > owtt_vec
vector of lenght D = n_nodes*(n_nodes-1)/2 + 1 where the one way travel times are stored
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
int slot_id
= node_id + k*n_nodes; slot_id value is written in the outgoing ranging packet then k is incremented
virtual void stateTxData()
Change transceiver status and and start to transmit if in my slot Used when there's spare time,...
Class that represents a TDMA Node.
Definition uwtdma.h:92
UWTDMA_STATUS transceiver_status
Variable holding the status enum type.
Definition uwtdma.h:209
int tot_slots
Number of slots in the frame (fair_mode)
Definition uwtdma.h:217
int sea_trial_
Written log variable.
Definition uwtdma.h:213
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
Definition uwtdma.cpp:423
virtual void Phy2MacEndRx(Packet *p)
Method called when the Phy Layer finish to receive a Packet.
Definition uwtdma.cpp:281
@ RECEIVING
Definition uwtdma.h:206
@ TRANSMITTING
Definition uwtdma.h:206
@ IDLE
Definition uwtdma.h:206
double slot_duration
Slot duration.
Definition uwtdma.h:224
int slot_number
set the position of the node in the frame (fair_mode) (starting from 0 to tot_slots-1)
Definition uwtdma.h:218
Header of the token bus protocol.
slotid_t & slotId()
Returns a reference to the nodeid_ variable.
size_t getSize() const
Returns the size of this header.
std::vector< uwrange_time_t > & times()
Returns a reference to the travel times array.
UwRangingTDMAModuleClass class_module_uwrangingtdma
Provides the definition of the class UwRangingTDMA.
Common structures and variables in the protocol.
constexpr size_t SLOTIDMAX_HDR
#define HDR_RANGING_TDMA(p)
alias defined to access the hdr_ranging_tdma HEADER