1
1
2
+ #include < fstream>
2
3
#include < memory>
3
- #include < thread>
4
4
#include < string>
5
- #include < fstream >
5
+ #include < thread >
6
6
7
7
using std::string;
8
8
9
9
#include < boost/asio.hpp>
10
10
#include < boost/bind.hpp>
11
11
12
- #include < libg3logger/g3logger.h>
13
12
#include < CLI/CLI.hpp>
13
+ #include < libg3logger/g3logger.h>
14
14
15
15
#include " liboculus/DataRx.h"
16
- #include " liboculus/StatusRx.h"
17
16
#include " liboculus/IoServiceThread.h"
18
- #include " liboculus/SonarPlayer.h"
19
17
#include " liboculus/PingAgreesWithConfig.h"
18
+ #include " liboculus/SonarPlayer.h"
19
+ #include " liboculus/StatusRx.h"
20
20
21
- using std::ofstream;
22
21
using std::ios_base;
22
+ using std::ofstream;
23
23
using std::shared_ptr;
24
24
25
- using liboculus::SonarConfiguration;
26
- using liboculus::IoServiceThread;
27
25
using liboculus::DataRx;
28
- using liboculus::StatusRx ;
26
+ using liboculus::IoServiceThread ;
29
27
using liboculus::SimplePingResult;
30
- using liboculus::SonarStatus ;
28
+ using liboculus::SonarConfiguration ;
31
29
using liboculus::SonarPlayerBase;
32
- // using liboculus::SonarPlayer;
30
+ using liboculus::SonarStatus;
31
+ using liboculus::StatusRx;
32
+ // using liboculus::SonarPlayer;
33
33
34
34
int playbackSonarFile (const std::string &filename, ofstream &output,
35
35
int stopAfter = -1 );
@@ -40,7 +40,8 @@ bool doStop = false;
40
40
41
41
// Catch signals
42
42
void signalHandler (int signo) {
43
- if (_io_thread) _io_thread->stop ();
43
+ if (_io_thread)
44
+ _io_thread->stop ();
44
45
doStop = true ;
45
46
}
46
47
@@ -50,21 +51,26 @@ int main(int argc, char **argv) {
50
51
CLI::App app{" Simple Oculus Sonar app" };
51
52
52
53
int verbosity = 0 ;
53
- app.add_flag (" -v,--verbose" , verbosity, " Additional output (use -vv for even more!)" );
54
+ app.add_flag (" -v,--verbose" , verbosity,
55
+ " Additional output (use -vv for even more!)" );
54
56
55
57
string ipAddr (" auto" );
56
- app.add_option (" ip" , ipAddr, " IP address of sonar or \" auto\" to automatically detect." );
58
+ app.add_option (" ip" , ipAddr,
59
+ " IP address of sonar or \" auto\" to automatically detect." );
57
60
58
61
string outputFilename (" " );
59
- app.add_option (" -o,--output" , outputFilename, " Saves raw sonar data to specified file." );
62
+ app.add_option (" -o,--output" , outputFilename,
63
+ " Saves raw sonar data to specified file." );
60
64
61
- string inputFilename (" " );
62
- app.add_option (" -i,--input" , inputFilename,
63
- " Reads raw sonar data from specified file. Plays file contents rather than contacting \" real\" sonar on network." );
65
+ // Playback currently not working
66
+ // string inputFilename("");
67
+ // app.add_option("-i,--input", inputFilename,
68
+ // "Reads raw sonar data from specified file. Plays file "
69
+ // "contents rather than contacting \"real\" sonar on
70
+ // network.");
64
71
65
72
int bitDepth (8 );
66
- app.add_option (" -b,--bits" , bitDepth,
67
- " Bit depth oof data (8,16,32)" );
73
+ app.add_option (" -b,--bits" , bitDepth, " Bit depth oof data (8,16,32)" );
68
74
69
75
int stopAfter = -1 ;
70
76
app.add_option (" -n,--frames" , stopAfter, " Stop after (n) frames." );
@@ -98,10 +104,10 @@ int main(int argc, char **argv) {
98
104
}
99
105
100
106
// If playing back an input file, run a different main loop ...
101
- if (!inputFilename.empty ()) {
102
- playbackSonarFile (inputFilename, output, stopAfter);
103
- return 0 ;
104
- }
107
+ // if (!inputFilename.empty()) {
108
+ // playbackSonarFile(inputFilename, output, stopAfter);
109
+ // return 0;
110
+ // }
105
111
106
112
int count = 0 ;
107
113
@@ -118,64 +124,64 @@ int main(int argc, char **argv) {
118
124
} else if (bitDepth == 16 ) {
119
125
config.setDataSize (dataSize16Bit);
120
126
} else if (bitDepth == 32 ) {
121
- config.sendGain ()
122
- .noGainAssistance ()
123
- .setDataSize (dataSize32Bit);
127
+ config.sendGain ().noGainAssistance ().setDataSize (dataSize32Bit);
124
128
}
125
129
126
130
_io_thread.reset (new IoServiceThread);
127
131
DataRx _data_rx (_io_thread->context ());
128
132
StatusRx _status_rx (_io_thread->context ());
129
133
130
134
// Callback for a SimplePingResultV1
131
- _data_rx.setCallback <liboculus::SimplePingResultV1>([&](const liboculus::SimplePingResultV1 &ping) {
132
- // Pings send to the callback are always valid
133
-
134
- {
135
- const auto valid = checkPingAgreesWithConfig (ping, config);
136
- if (!valid) {
137
- LOG (WARNING) << " Mismatch between requested config and ping" ;
138
- }
139
- }
140
-
141
- ping.dump ();
142
-
143
- if (output.is_open ()) {
144
- const char *cdata = reinterpret_cast <const char *>(ping.buffer ()->data ());
145
- output.write (cdata, ping.buffer ()->size ());
146
- }
147
-
148
- count++;
149
- if ((stopAfter > 0 ) && (count >= stopAfter)) _io_thread->stop ();
150
- });
135
+ _data_rx.setCallback <liboculus::SimplePingResultV1>(
136
+ [&](const liboculus::SimplePingResultV1 &ping) {
137
+ // Pings send to the callback are always valid, don't need to check
138
+ // again
139
+
140
+ {
141
+ const auto valid = checkPingAgreesWithConfig (ping, config);
142
+ if (!valid) {
143
+ LOG (WARNING) << " Mismatch between requested config and ping" ;
144
+ }
145
+ }
146
+
147
+ ping.dump ();
148
+
149
+ if (output.is_open ()) {
150
+ const char *cdata =
151
+ reinterpret_cast <const char *>(ping.buffer ()->data ());
152
+ output.write (cdata, ping.buffer ()->size ());
153
+ }
154
+
155
+ count++;
156
+ if ((stopAfter > 0 ) && (count >= stopAfter))
157
+ _io_thread->stop ();
158
+ });
151
159
152
160
// Callback for a SimplePingResultV2
153
- _data_rx.setCallback <liboculus::SimplePingResultV2>([&](const liboculus::SimplePingResultV2 &ping) {
154
- // Pings send to the callback are always valid
155
-
156
- // {
157
- // const auto valid = checkPingAgreesWithConfig(ping, config);
158
- // if (!valid) {
159
- // LOG(WARNING) << "Mismatch between requested config and ping";
160
- // }
161
- // }
162
-
163
- ping.dump ();
164
-
165
- // const auto gains = ping.gains();
166
- // if (gains.size() > 0) {
167
- // LOG(INFO) << "First five gains " << gains[10] << ", " << gains[11] << ", "
168
- // << gains[12] << ", " << gains[13] << ", " << gains[14];
169
- // }
170
-
171
- if (output.is_open ()) {
172
- const char *cdata = reinterpret_cast <const char *>(ping.buffer ()->data ());
173
- output.write (cdata, ping.buffer ()->size ());
174
- }
175
-
176
- count++;
177
- if ((stopAfter > 0 ) && (count >= stopAfter)) doStop=true ;
178
- });
161
+ _data_rx.setCallback <liboculus::SimplePingResultV2>(
162
+ [&](const liboculus::SimplePingResultV2 &ping) {
163
+ // Pings send to the callback are always valid, don't need to check
164
+ // again
165
+
166
+ {
167
+ const auto valid = checkPingAgreesWithConfig (ping, config);
168
+ if (!valid) {
169
+ LOG (WARNING) << " Mismatch between requested config and ping" ;
170
+ }
171
+ }
172
+
173
+ ping.dump ();
174
+
175
+ if (output.is_open ()) {
176
+ const char *cdata =
177
+ reinterpret_cast <const char *>(ping.buffer ()->data ());
178
+ output.write (cdata, ping.buffer ()->size ());
179
+ }
180
+
181
+ count++;
182
+ if ((stopAfter > 0 ) && (count >= stopAfter))
183
+ doStop = true ;
184
+ });
179
185
180
186
// Callback when connection to a sonar
181
187
_data_rx.setOnConnectCallback ([&]() {
@@ -185,13 +191,15 @@ int main(int argc, char **argv) {
185
191
186
192
// Connect the client
187
193
if (ipAddr == " auto" ) {
188
- // To auto-detect , when the StatusRx connects,
189
- // configure the DataRx
190
- _status_rx. setCallback ([&]( const SonarStatus &status, bool is_valid){
191
- if (!is_valid || _data_rx. isConnected ()) return ;
194
+ // For auto-deteciont , when the StatusRx connects configure the DataRx
195
+ _status_rx. setCallback ([&]( const SonarStatus &status, bool is_valid) {
196
+ if (! is_valid || _data_rx. isConnected ())
197
+ return ;
192
198
_data_rx.connect (status.ipAddr ());
193
199
});
194
200
} else {
201
+ // Otherwise, just (attempt to) connect the DataRx to the specified IP
202
+ // address
195
203
_data_rx.connect (ipAddr);
196
204
}
197
205
_io_thread->start ();
@@ -201,7 +209,7 @@ int main(int argc, char **argv) {
201
209
202
210
// Very rough Hz calculation right now
203
211
const auto c = count;
204
- LOG (INFO) << " Received pings at " << c- lastCount << " Hz" ;
212
+ LOG (INFO) << " Received pings at " << c - lastCount << " Hz" ;
205
213
206
214
lastCount = c;
207
215
sleep (1 );
@@ -210,47 +218,51 @@ int main(int argc, char **argv) {
210
218
_io_thread->stop ();
211
219
_io_thread->join ();
212
220
213
- if (output.is_open ()) output.close ();
221
+ if (output.is_open ())
222
+ output.close ();
214
223
215
224
LOG (INFO) << " At exit" ;
216
225
217
226
return 0 ;
218
227
}
219
228
220
-
221
- int playbackSonarFile (const std::string &filename, ofstream &output, int stopAfter) {
222
- shared_ptr<SonarPlayerBase> player (SonarPlayerBase::OpenFile (filename));
223
-
224
- if ( !player ) {
225
- LOG (WARNING) << " Unable to open sonar file" ;
226
- return -1 ;
227
- }
228
-
229
- if ( !player->open (filename) ) {
230
- LOG (INFO) << " Failed to open " << filename;
231
- return -1 ;
232
- }
233
-
234
- int count = 0 ;
235
- // SimplePingResult ping;
236
- // while( player->nextPing(ping) && !player->eof() ) {
237
- // if (!ping.valid()) {
238
- // LOG(WARNING) << "Invalid ping";
239
- // continue;
240
- // }
241
-
242
- // ping.dump();
243
-
244
- // if (output.is_open()) {
245
- // const char *cdata = reinterpret_cast<const char *>(ping.buffer().data());
246
- // output.write(cdata, ping.buffer().size());
247
- // }
248
-
249
- // count++;
250
- // if( (stopAfter > 0) && (count >= stopAfter) ) break;
251
- // }
252
-
253
- LOG (INFO) << count << " sonar packets decoded" ;
254
-
255
- return 0 ;
256
- }
229
+ // Playback not currently working
230
+ //
231
+ // int playbackSonarFile(const std::string &filename, ofstream &output,
232
+ // int stopAfter) {
233
+ // shared_ptr<SonarPlayerBase> player(SonarPlayerBase::OpenFile(filename));
234
+
235
+ // if (!player) {
236
+ // LOG(WARNING) << "Unable to open sonar file";
237
+ // return -1;
238
+ // }
239
+
240
+ // if (!player->open(filename)) {
241
+ // LOG(INFO) << "Failed to open " << filename;
242
+ // return -1;
243
+ // }
244
+
245
+ // int count = 0;
246
+ // // SimplePingResult ping;
247
+ // // while( player->nextPing(ping) && !player->eof() ) {
248
+ // // if (!ping.valid()) {
249
+ // // LOG(WARNING) << "Invalid ping";
250
+ // // continue;
251
+ // // }
252
+
253
+ // // ping.dump();
254
+
255
+ // // if (output.is_open()) {
256
+ // // const char *cdata = reinterpret_cast<const char
257
+ // *>(ping.buffer().data());
258
+ // // output.write(cdata, ping.buffer().size());
259
+ // // }
260
+
261
+ // // count++;
262
+ // // if( (stopAfter > 0) && (count >= stopAfter) ) break;
263
+ // // }
264
+
265
+ // LOG(INFO) << count << " sonar packets decoded";
266
+
267
+ // return 0;
268
+ // }
0 commit comments