Goby v2
test.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 // tests fixed TDMA
23 
24 #include "goby/acomms/amac.h"
25 #include "goby/acomms/connect.h"
26 #include "goby/common/logger.h"
27 #include "goby/util/sci.h"
28 
30 const int num_cycles_check = 3;
31 int first_cycle = -1;
32 int current_cycle = -1;
33 int me = 1;
34 
35 using goby::acomms::operator<<;
36 
37 void initiate_transmission(const goby::acomms::protobuf::ModemTransmission& msg)
38 {
39  std::cout << "We were told to start transmission of " << msg << std::endl;
40  assert(msg.src() == me);
41  double cycles_since_day =
42  (goby::common::goby_time().time_of_day().total_milliseconds() / 1000.0) /
43  mac.cycle_duration();
44 
45  std::cout << std::setprecision(15) << cycles_since_day << std::endl;
46  std::cout << std::setprecision(15) << goby::util::unbiased_round(cycles_since_day, 0)
47  << std::endl;
48 
49  current_cycle = cycles_since_day;
50  if (first_cycle == -1)
51  first_cycle = current_cycle;
52 
53  assert(mac.cycle_count() == 3);
54 
55  assert(goby::util::unbiased_round(
56  cycles_since_day - goby::util::unbiased_round(cycles_since_day, 0), 1) == 0);
57 }
58 
59 int main(int argc, char* argv[])
60 {
61  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
62  goby::glog.set_name(argv[0]);
63 
64  // add slots as part of cfg
66  cfg.set_modem_id(me);
67  cfg.set_type(goby::acomms::protobuf::MAC_FIXED_DECENTRALIZED);
68 
70  downlink_slot.set_src(1);
71  downlink_slot.set_rate(0);
72  downlink_slot.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
73  downlink_slot.set_slot_seconds(0.1);
74  cfg.add_slot()->CopyFrom(downlink_slot);
75 
77  uplink3_slot.set_src(3);
78  uplink3_slot.set_rate(0);
79  uplink3_slot.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
80  uplink3_slot.set_slot_seconds(0.1);
81  cfg.add_slot()->CopyFrom(uplink3_slot);
82 
84  uplink4_slot.set_src(4);
85  uplink4_slot.set_rate(0);
86  uplink4_slot.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
87  uplink4_slot.set_slot_seconds(0.1);
88  cfg.add_slot()->CopyFrom(uplink4_slot);
89 
90  goby::acomms::connect(&mac.signal_initiate_transmission, &initiate_transmission);
91 
92  mac.startup(cfg);
93 
94  while (first_cycle == -1 || (current_cycle < first_cycle + num_cycles_check))
95  {
96  mac.do_work();
97  usleep(1e2);
98  }
99 
100  first_cycle = -1;
101  current_cycle = -1;
102 
103  mac.shutdown();
104 
105  cfg.Clear();
106  me = 3;
107 
108  cfg.set_modem_id(me);
109  cfg.set_type(goby::acomms::protobuf::MAC_FIXED_DECENTRALIZED);
110  mac.startup(cfg);
111 
112  // add slots not through cfg
113  mac.clear();
114  mac.push_back(downlink_slot);
115  mac.push_back(uplink3_slot);
116  mac.update();
117 
118  mac.push_back(uplink4_slot);
119  mac.remove(downlink_slot);
120  mac.push_back(downlink_slot);
121  mac.update();
122 
123  while (first_cycle == -1 || (current_cycle < first_cycle + num_cycles_check))
124  mac.get_io_service().run_one();
125 
126  std::cout << "all tests passed" << std::endl;
127 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
void startup(const protobuf::MACConfig &cfg)
Starts the MAC with given configuration.
Definition: mac_manager.cpp:65
void do_work()
Allows the MAC timer to do its work. Does not block. If you prefer more control you can directly cont...
Definition: mac_manager.h:76
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
Definition: time.h:104
provides an API to the goby-acomms MAC library. MACManager is essentially a std::list<protobuf::Modem...
Definition: mac_manager.h:51
void shutdown()
Shutdown the MAC.
void connect(Signal *signal, Slot slot)
connect a signal to a slot (e.g. function pointer)
Definition: connect.h:36
common::FlexOstream glog
Access the Goby logger through this object.
void update()
You must call this after any change to the underlying list that would invalidate iterators or change ...
boost::signals2::signal< void(const protobuf::ModemTransmission &m)> signal_initiate_transmission
Signals when it is time for this platform to begin transmission of an acoustic message at the start o...
Definition: mac_manager.h:91
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...
Definition: flex_ostream.h:96