Goby v2
driver_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 //
23 // Usage (WHOI Micro-Modem): run
24 // > driver_simple /dev/tty_of_modem_A 1
25 //
26 // wait a few seconds
27 //
28 // > driver_simple /dev/tty_of_modem_B 2
29 //
30 // be careful of collisions if you start them at the same time (this is why libamac exists!)
31 
32 // Usage (example ABCModem): run
33 // > driver_simple /dev/tty_of_modem_A 1 ABCDriver
34 // > driver_simple /dev/tty_of_modem_B 2 ABCDriver
35 // Also see abc_modem_simulator.cpp
36 
37 #include "goby/acomms/connect.h"
38 #include "goby/acomms/modem_driver.h"
39 #include "goby/common/logger.h"
40 #include "goby/util/binary.h"
41 
42 #include <iostream>
43 
44 using goby::acomms::operator<<;
45 
46 void handle_data_receive(const goby::acomms::protobuf::ModemTransmission& data_msg);
47 
48 int main(int argc, char* argv[])
49 {
50  if (argc < 3)
51  {
52  std::cout << "usage: driver_simple /dev/tty_of_modem modem_id [type: MMDriver "
53  "(default)|ABCDriver]"
54  << std::endl;
55  return 1;
56  }
57 
58  //
59  // 1. Create and initialize the driver we want
60  //
63 
64  // set the serial port given on the command line
65  cfg.set_serial_port(argv[1]);
66  using google::protobuf::uint32;
67  // set the source id of this modem
68  uint32 our_id = goby::util::as<uint32>(argv[2]);
69  cfg.set_modem_id(our_id);
70 
71  goby::glog.set_name(argv[0]);
72  goby::glog.add_stream(goby::common::logger::DEBUG2, &std::clog);
73 
74  if (argc == 4)
75  {
76  if (boost::iequals(argv[3], "ABCDriver"))
77  {
78  std::cout << "Starting Example driver ABCDriver" << std::endl;
79  driver = new goby::acomms::ABCDriver;
80  }
81  }
82 
83  // default to WHOI MicroModem
84  if (!driver)
85  {
86  std::cout << "Starting WHOI Micro-Modem MMDriver" << std::endl;
87  driver = new goby::acomms::MMDriver;
88  // turn data quality factor message on
89  // (example of setting NVRAM configuration)
90  cfg.AddExtension(micromodem::protobuf::Config::nvram_cfg, "DQF,1");
91  }
92 
93  goby::acomms::connect(&driver->signal_receive, &handle_data_receive);
94 
95  //
96  // 2. Startup the driver
97  //
98  driver->startup(cfg);
99 
100  //
101  // 3. Initiate a transmission cycle with some data
102  //
103 
105  transmit_message.set_type(goby::acomms::protobuf::ModemTransmission::DATA);
106  transmit_message.set_src(goby::util::as<unsigned>(our_id));
107  transmit_message.set_dest(goby::acomms::BROADCAST_ID);
108  transmit_message.set_rate(0);
109 
110  transmit_message.add_frame("Hello, world!");
111  transmit_message.set_ack_requested(false);
112 
113  std::cout << transmit_message << std::endl;
114 
115  driver->handle_initiate_transmission(transmit_message);
116 
117  //
118  // 4. Run the driver
119  //
120 
121  // 10 hz is good
122  int i = 0;
123  while (1)
124  {
125  ++i;
126  driver->do_work();
127 
128  // send another transmission every 60 seconds
129  if (!(i % 600))
130  driver->handle_initiate_transmission(transmit_message);
131 
132  // in here you can initiate more transmissions as you want
133  usleep(100000);
134  }
135 
136  delete driver;
137  return 0;
138 }
139 
140 //
141 // 5. Post the received data
142 //
143 
144 void handle_data_receive(const goby::acomms::protobuf::ModemTransmission& data_msg)
145 {
146  std::cout << "got a message: " << data_msg << std::endl;
147 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
virtual void startup(const protobuf::DriverConfig &cfg)=0
Starts the modem driver. Must be called before poll().
virtual void do_work()=0
Allows the modem driver to do its work.
provides an API to the imaginary ABC modem (as an example how to write drivers)
Definition: abc_driver.h:39
google::protobuf::uint32 uint32
an unsigned 32 bit integer
provides an API to the WHOI Micro-Modem driver
Definition: mm_driver.h:41
boost::signals2::signal< void(const protobuf::ModemTransmission &message)> signal_receive
Called when a binary data transmission is received from the modem.
Definition: driver_base.h:85
const int BROADCAST_ID
special modem id for the broadcast destination - no one is assigned this address. Analogous to 192...
virtual void handle_initiate_transmission(const protobuf::ModemTransmission &m)=0
Virtual initiate_transmission method. Typically connected to MACManager::signal_initiate_transmission...
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.
provides an abstract base class for acoustic modem drivers. This is subclassed by the various drivers...
Definition: driver_base.h:47
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