Skip to content

Commit 3e3784e

Browse files
committed
Minor improvement to handling of gain data from sonar.
1 parent bbdc91b commit 3e3784e

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

include/liboculus/GainData.h

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
#pragma once
3232

33+
#include <netinet/in.h>
34+
#include <vector>
35+
#include <iostream>
36+
3337
#include <g3log/g3log.hpp> // needed for CHECK macro
3438

3539
#include "Oculus/Oculus.h"
@@ -45,37 +49,39 @@ class GainData {
4549
typedef T DataType;
4650

4751
GainData()
48-
: _data(nullptr),
49-
_stride(0),
50-
_numRanges(0),
51-
_imageSize(0)
52+
: _data()
5253
{;}
5354

5455
GainData(const GainData &other) = default;
5556

56-
GainData(const T *data, uint32_t imageSz, size_t strideBytes, size_t nRanges)
57-
: _data(data),
58-
_stride(strideBytes/sizeof(T)),
59-
_numRanges(nRanges),
60-
_imageSize(imageSz) {}
57+
GainData(const T *data, uint32_t imageSz,
58+
size_t strideBytes, size_t nRanges)
59+
: _data() {
60+
// Only works for four-byte types
61+
assert(sizeof(T) == 4);
62+
for (size_t i = 0; i < nRanges; i++) {
63+
const size_t index = (i*strideBytes)/sizeof(T);
6164

62-
int size() const { return _numRanges; }
65+
const uint32_t *d = reinterpret_cast<const uint32_t *>(&data[index]);
6366

64-
T at(unsigned int i) const {
65-
CHECK(i < _numRanges) << "Requested gain " << i << " out of range";
67+
// uint32_t h = ntohl(*d);
68+
uint32_t h = *d;
69+
T *f = reinterpret_cast<T *>(&h);
6670

67-
const size_t index = i*_stride;
68-
CHECK(index*sizeof(T) < _imageSize);
71+
_data.push_back(*f);
72+
}
73+
}
6974

70-
return _data[index];
75+
int size() const { return _data.size(); }
76+
77+
T at(unsigned int i) const {
78+
return _data.at(i);
7179
}
7280

7381
T operator[](unsigned int i) const { return at(i); }
7482

7583
private:
76-
const T *_data;
77-
size_t _stride, _numRanges;
78-
uint32_t _imageSize;
84+
std::vector<T> _data;
7985
};
8086

8187
} // namespace liboculus

tools/oculus_client.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool doStop = false;
4040

4141
// Catch signals
4242
void signalHandler(int signo) {
43-
if(_io_thread) _io_thread->stop();
43+
if (_io_thread) _io_thread->stop();
4444
doStop = true;
4545
}
4646

@@ -112,12 +112,13 @@ int main(int argc, char **argv) {
112112
SonarConfiguration config;
113113
config.setPingRate(pingRateNormal);
114114
config.setRange(range);
115+
115116
if (bitDepth == 8) {
116117
config.setDataSize(dataSize8Bit);
117118
} else if (bitDepth == 16) {
118119
config.setDataSize(dataSize16Bit);
119120
} else if (bitDepth == 32) {
120-
config.dontSendGain()
121+
config.sendGain()
121122
.noGainAssistance()
122123
.setDataSize(dataSize32Bit);
123124
}
@@ -126,6 +127,7 @@ int main(int argc, char **argv) {
126127
DataRx _data_rx(_io_thread->context());
127128
StatusRx _status_rx(_io_thread->context());
128129

130+
// Callback for a SimplePingResultV1
129131
_data_rx.setCallback<liboculus::SimplePingResultV1>([&](const liboculus::SimplePingResultV1 &ping) {
130132
// Pings send to the callback are always valid
131133

@@ -147,6 +149,7 @@ int main(int argc, char **argv) {
147149
if ((stopAfter > 0) && (count >= stopAfter)) _io_thread->stop();
148150
});
149151

152+
// Callback for a SimplePingResultV2
150153
_data_rx.setCallback<liboculus::SimplePingResultV2>([&](const liboculus::SimplePingResultV2 &ping) {
151154
// Pings send to the callback are always valid
152155

@@ -159,6 +162,15 @@ int main(int argc, char **argv) {
159162

160163
ping.dump();
161164

165+
const auto bearings = ping.bearings();
166+
LOG(INFO) << "Azimuth range = " << bearings.front() << " - " << bearings.back();
167+
168+
const auto gains = ping.gains();
169+
if (gains.size() > 0) {
170+
LOG(INFO) << "First five gains " << gains[10] << ", " << gains[11] << ", "
171+
<< gains[12] << ", " << gains[13] << ", " << gains[14];
172+
}
173+
162174
if (output.is_open()) {
163175
const char *cdata = reinterpret_cast<const char *>(ping.buffer()->data());
164176
output.write(cdata, ping.buffer()->size());
@@ -168,13 +180,16 @@ int main(int argc, char **argv) {
168180
if ((stopAfter > 0) && (count >= stopAfter)) doStop=true;
169181
});
170182

183+
// Callback when connection to a sonar
171184
_data_rx.setOnConnectCallback([&]() {
172185
config.dump();
173186
_data_rx.sendSimpleFireMessage(config);
174187
});
175188

176-
// Connect client
189+
// Connect the client
177190
if (ipAddr == "auto") {
191+
// To auto-detect, when the StatusRx connects,
192+
// configure the DataRx
178193
_status_rx.setCallback([&](const SonarStatus &status, bool is_valid){
179194
if (!is_valid || _data_rx.isConnected()) return;
180195
_data_rx.connect(status.ipAddr());
@@ -184,11 +199,11 @@ int main(int argc, char **argv) {
184199
}
185200
_io_thread->start();
186201

187-
// Imprecise statistic for now...
188202
int lastCount = 0;
189203
while (!doStop) {
190-
auto c = count;
191204

205+
// Very rough Hz calculation right now
206+
const auto c = count;
192207
LOG(INFO) << "Received pings at " << c-lastCount << " Hz";
193208

194209
lastCount = c;

0 commit comments

Comments
 (0)