Goby v2
amac_simple.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 #include "goby/acomms/amac.h"
23 #include "goby/acomms/connect.h"
24 #include "goby/common/logger.h"
25 
26 #include <iostream>
27 
28 using goby::acomms::operator<<;
29 
30 void init_transmission(const goby::acomms::protobuf::ModemTransmission& msg);
31 
32 int main(int argc, char* argv[])
33 {
34  goby::glog.set_name(argv[0]);
35  goby::glog.add_stream(goby::common::logger::DEBUG1, &std::clog);
36 
37  //
38  // 1. Create a MACManager
39  //
41 
42  //
43  // 2. Configure with a few slots
44  //
46  cfg.set_modem_id(1);
47  cfg.set_type(goby::acomms::protobuf::MAC_FIXED_DECENTRALIZED);
48  goby::acomms::protobuf::ModemTransmission* slot = cfg.add_slot();
49  slot->set_src(1);
50  slot->set_rate(0);
51  slot->set_type(goby::acomms::protobuf::ModemTransmission::DATA);
52  slot->set_slot_seconds(5);
53 
54  //
55  // 3. Set up the callback
56  //
57 
58  // give a callback to use for actually initiating the transmission. this would be bound to goby::acomms::ModemDriverBase::initiate_transmission if using libmodemdriver.
59  goby::acomms::connect(&mac.signal_initiate_transmission, &init_transmission);
60 
61  //
62  // 4. Let it run for a bit alone in the world
63  //
64  mac.startup(cfg);
65  for (unsigned i = 1; i < 150; ++i)
66  {
67  mac.do_work();
68  usleep(100000);
69  }
70 
71  //
72  // 5. Add some slots (modem ids 2 & 3, and LBL ping from 1): remember MACManager is a std::list<goby::acomms::protobuf::ModemTransmission> at heart
73  //
74 
75  // one way we can do this
77  new_slot.set_src(2);
78  new_slot.set_rate(0);
79  new_slot.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
80  new_slot.set_slot_seconds(5);
81 
82  mac.push_back(new_slot);
83 
84  // another way
85  mac.resize(mac.size() + 1);
86  mac.back().CopyFrom(mac.front());
87  mac.back().set_src(3);
88 
89  mac.resize(mac.size() + 1);
90  mac.back().CopyFrom(mac.front());
91  mac.back().set_type(goby::acomms::protobuf::ModemTransmission::DRIVER_SPECIFIC);
92  mac.back().SetExtension(micromodem::protobuf::type,
93  micromodem::protobuf::MICROMODEM_REMUS_LBL_RANGING);
94 
95  // must call update after manipulating MACManager before calling do_work again.
96  mac.update();
97 
98  //
99  // 6. Run it
100  //
101 
102  for (;;)
103  {
104  mac.do_work();
105  usleep(100000);
106  }
107 
108  return 0;
109 }
110 
111 void init_transmission(const goby::acomms::protobuf::ModemTransmission& msg)
112 {
113  std::cout << "starting transmission with these values: " << msg << std::endl;
114 }
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
provides an API to the goby-acomms MAC library. MACManager is essentially a std::list<protobuf::Modem...
Definition: mac_manager.h:51
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