Goby v2
whoi_ranging.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 // > whoi_ranging /dev/tty_of_modem
25 
26 #include "goby/acomms/connect.h"
27 #include "goby/acomms/modem_driver.h"
28 #include "goby/common/logger.h"
29 
30 #include <iostream>
31 
32 using goby::acomms::operator<<;
33 
34 int usage()
35 {
36  std::cout << "usage: whoi_ranging /dev/tty_of_modem narrowband|remus max_range_meters"
37  << std::endl;
38  return 1;
39 }
40 
41 void handle_receive(const goby::acomms::protobuf::ModemTransmission& reply_msg);
42 
43 int main(int argc, char* argv[])
44 {
45  if (argc < 4)
46  return usage();
47 
48  std::string type = argv[2];
49  if (type != "narrowband" && type != "remus")
50  return usage();
51 
52  goby::glog.set_name(argv[0]);
53  goby::glog.add_stream(goby::common::logger::DEBUG2, &std::clog);
54 
55  //
56  // 1. Create and initialize the Micro-Modem driver
57  //
60 
61  // set the serial port given on the command line
62  cfg.set_serial_port(argv[1]);
63  using google::protobuf::uint32;
64  // set the source id of this modem
65  uint32 our_id = 1;
66  cfg.set_modem_id(our_id);
67 
68  // these parameters could be set on a ping by ping basis as well, we'll set them all here in configuration
69  if (type == "remus")
70  {
72  cfg.MutableExtension(micromodem::protobuf::Config::remus_lbl);
73 
74  remus_params->set_turnaround_ms(50);
75  // enable all four beacons
76  remus_params->set_enable_beacons(0x0F);
77  remus_params->set_lbl_max_range(goby::util::as<unsigned>(argv[3]));
78  }
79  else if (type == "narrowband")
80  {
81  // Benthos UAT-376 example from Micro-Modem Software Interface Guide
83  cfg.MutableExtension(micromodem::protobuf::Config::narrowband_lbl);
84 
85  narrowband_params->set_turnaround_ms(20);
86  narrowband_params->set_transmit_freq(26000);
87  narrowband_params->set_transmit_ping_ms(5);
88  narrowband_params->set_receive_ping_ms(5);
89  narrowband_params->add_receive_freq(25000);
90  narrowband_params->set_transmit_flag(true);
91  narrowband_params->set_lbl_max_range(goby::util::as<unsigned>(argv[3]));
92  }
93 
94  goby::acomms::connect(&driver.signal_receive, &handle_receive);
95 
96  //
97  // 2. Startup the driver
98  //
99  driver.startup(cfg);
100 
101  //
102  // 3. Initiate an LBL ping
103  //
104 
106  request_msg.set_src(our_id);
107  request_msg.set_type(goby::acomms::protobuf::ModemTransmission::DRIVER_SPECIFIC);
108  request_msg.SetExtension(micromodem::protobuf::type,
109  (type == "remus")
110  ? micromodem::protobuf::MICROMODEM_REMUS_LBL_RANGING
111  : micromodem::protobuf::MICROMODEM_NARROWBAND_LBL_RANGING);
112 
113  std::cout << "Sending " << type << " LBL ranging request:\n" << request_msg << std::endl;
114 
115  driver.handle_initiate_transmission(request_msg);
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  // this is when any signals will be emitted and the modem serial comms are performed
127  driver.do_work();
128 
129  // send another transmission every 60 seconds
130  if (!(i % 600))
131  {
132  driver.handle_initiate_transmission(request_msg);
133  std::cout << "Sending " << type << " LBL ranging request:\n"
134  << request_msg << std::endl;
135  }
136 
137  // 0.1 sec
138  usleep(100000);
139  }
140  return 0;
141 }
142 
143 void handle_receive(const goby::acomms::protobuf::ModemTransmission& reply_msg)
144 {
145  std::cout << "Received LBL ranging reply:\n" << reply_msg << std::endl;
146 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
google::protobuf::uint32 uint32
an unsigned 32 bit integer
void handle_initiate_transmission(const protobuf::ModemTransmission &m)
See ModemDriverBase::handle_initiate_transmission()
Definition: mm_driver.cpp:521
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
void do_work()
See ModemDriverBase::do_work()
Definition: mm_driver.cpp:463
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 startup(const protobuf::DriverConfig &cfg)
Starts the driver.
Definition: mm_driver.cpp:76
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