Skip to content

Commit cf856c3

Browse files
committed
Adapting to real 32bit data.
1 parent 0145c0c commit cf856c3

File tree

10 files changed

+157
-40
lines changed

10 files changed

+157
-40
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ else()
7272
lib/StatusRx.cpp
7373
lib/SonarPlayer.cpp
7474
lib/OculusMessageHandler.cpp
75+
lib/SimpleFireMessage.cpp
7576
lib/IoServiceThread.cpp )
7677

7778

include/liboculus/Constants.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ namespace liboculus {
5252

5353
const float AzimuthBeamwidthDeg = 0.6;
5454
const float AzimuthBeamwidthRad = 0.6*M_PI/180.0;
55+
56+
const float MaxRange = 40;
5557
};
5658

5759
namespace Oculus_2100MHz {
@@ -60,5 +62,9 @@ namespace liboculus {
6062

6163
const float AzimuthBeamwidthDeg = 0.4;
6264
const float AzimuthBeamwidthRad = 0.4*M_PI/180.0;
65+
66+
// \todo These shouldn't be fixed, should read from Oculus.h
67+
const float MaxRange = 10; // meters
6368
};
69+
6470
} // namespace liboculus

include/liboculus/ImageData.h

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,38 @@ class ImageData {
8181
return ((uint8_t *)_data)[index];
8282
}
8383

84-
// This function works for either 1- or 2-byte sonar data
85-
// For 1-byte, the 8-bit value is simply cast into the 16-bit return
86-
uint16_t at_uint16(unsigned int beam, unsigned int rangeBin) const {
87-
if ((_data == nullptr) || (beam >= _numBeams) || (rangeBin >= _numRanges)) return 0;
84+
// This function works for either 1- or 2-byte sonar data
85+
// For 1-byte, the 8-bit value is simply cast into the 16-bit return
86+
uint16_t at_uint16(unsigned int beam, unsigned int rangeBin) const {
87+
if ((_data == nullptr) || (beam >= _numBeams) || (rangeBin >= _numRanges)) return 0;
88+
89+
if(_dataSize == 1) {
90+
return at_uint8(beam,rangeBin);
91+
} else if (_dataSize == 2) {
92+
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
93+
CHECK(offset < (_imageSize-1));
94+
return (_data[offset] | _data[offset+1] << 8);
95+
}
96+
97+
return 0;
98+
}
8899

89-
if(_dataSize == 1) {
90-
return at_uint8(beam,rangeBin);
91-
} else if (_dataSize == 2) {
92-
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
93-
CHECK(offset < (_imageSize-1));
94-
return (_data[offset] | _data[offset+1] << 8);
95-
}
100+
uint32_t at_uint32(unsigned int beam, unsigned int rangeBin) const {
101+
if ((_data == nullptr) || (beam >= _numBeams)
102+
|| (rangeBin >= _numRanges)) return 0;
103+
104+
if (_dataSize == 1) {
105+
return at_uint8(beam, rangeBin);
106+
} else if (_dataSize == 2) {
107+
return at_uint16(beam, rangeBin);
108+
} else {
109+
const size_t offset = (rangeBin * _stride) + (beam * _dataSize) + _offset;
110+
CHECK(offset < (_imageSize-1));
111+
return (_data[offset] | _data[offset+1] << 8 | _data[offset+2] << 16 | _data[offset+3] << 24);
112+
}
96113

97-
return 0;
98-
}
114+
return 0;
115+
}
99116

100117
private:
101118
const uint8_t *_data;

include/liboculus/SimpleFireMessage.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class SimpleFireMessage : public MessageHeader {
6767

6868
void dump() const override;
6969

70+
float range() const;
71+
7072
protected:
7173
OculusSimpleFireFlags _flags;
7274
}; // class SimpleFireMessage
@@ -92,7 +94,10 @@ void SimpleFireMessage<FireMsgT>::dump() const {
9294
LOG(DEBUG) << " Ping rate: (unknown) " << static_cast<int>(this->fireMsg()->pingRate);
9395
}
9496

95-
LOG(DEBUG) << " Send gain: " << (this->flags().getSendGain() ? "Yes" : "No");
97+
LOG(DEBUG) << "Flags: Send gain: " << (this->flags().getSendGain() ? "Yes" : "No");
98+
LOG(DEBUG) << "Flags: Range is: " << (this->flags().getRangeAsMeters() ? "Meters" : "Percent");
99+
LOG(DEBUG) << " Range: " << range();
100+
LOG(DEBUG) << "Speed of sound: " << this->fireMsg()->speedOfSound;
96101
}
97102

98103
} // namespace liboculus

include/liboculus/SimplePingResult.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ void SimplePingResult<Ping_t>::dump() const {
186186
LOG(DEBUG) << "Spd of Sound: " << this->ping()->speedOfSoundUsed;
187187
LOG(DEBUG) << " Range res: " << this->ping()->rangeResolution << " m";
188188

189+
if (this->flags().getRangeAsMeters()) {
190+
LOG(DEBUG) << " Calc range: " << this->ping()->rangeResolution*this->ping()->nRanges << " m";
191+
} else {
192+
LOG(DEBUG) << " Pct range: " << this->ping()->rangeResolution*this->ping()->nRanges;
193+
}
194+
189195
LOG(DEBUG) << " Num range: " << this->ping()->nRanges;
190196
LOG(DEBUG) << " Num beams: " << this->ping()->nBeams;
191197

include/liboculus/SonarConfiguration.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class OculusSimpleFireFlags {
7676
};
7777

7878
// Thin OO wrapper around the OculusSimpleFireMessage.
79+
//
80+
// \todo This API is a little messy right now. Probably *shouldn't* keep
81+
// the shadow copy of the SimpleFireMessage{2}, instead
82+
// just make it at time of serialization.
7983
class SonarConfiguration {
8084
public:
8185

@@ -88,7 +92,6 @@ class SonarConfiguration {
8892
SonarConfiguration &setGamma(int input);
8993
SonarConfiguration &setPingRate(PingRateType newRate);
9094
SonarConfiguration &setGainPercent(double input);
91-
SonarConfiguration &setRange(double input);
9295
SonarConfiguration &setFlags(uint8_t flags);
9396
SonarConfiguration &setWaterTemperature(double degC);
9497

@@ -98,13 +101,23 @@ class SonarConfiguration {
98101
} OculusFreqMode;
99102

100103
SonarConfiguration &setFreqMode(OculusFreqMode input);
104+
OculusFreqMode getFreqMode() const {
105+
if (_sfm.masterMode == 1)
106+
return OCULUS_LOW_FREQ;
107+
else if (_sfm.masterMode == 2)
108+
return OCULUS_HIGH_FREQ;
109+
}
101110

102111
SonarConfiguration &setDataSize(DataSizeType sz);
103-
DataSizeType getDataSize() const { return _dataSize; }
112+
DataSizeType getDataSize() const { return _dataSize; }
113+
114+
SonarConfiguration &setRange(double input);
115+
116+
SonarConfiguration &sendRangeAsMeters(bool v);
117+
SonarConfiguration &sendRangeAsMeters() { return sendRangeAsMeters(true); }
118+
SonarConfiguration &sendRangeAsPercent() { return sendRangeAsMeters(false); }
119+
bool getSendRangeAsMeters() const { return _sendRangeAsMeters; }
104120

105-
SonarConfiguration &setRangeAsMeters(bool v);
106-
SonarConfiguration &rangeAsMeters() { return setRangeAsMeters(true); }
107-
SonarConfiguration &rangeAsPercent() { return setRangeAsMeters(false); }
108121

109122
SonarConfiguration &setSendGain(bool v);
110123
SonarConfiguration &sendGain() { return setSendGain(true); }
@@ -122,7 +135,6 @@ class SonarConfiguration {
122135
bool get512Beams() const { return _512beams;}
123136

124137

125-
bool getRangeAsMeters() const { return _rangeAsMeters; }
126138
bool getSendGain() const { return _sendGain; }
127139
//bool getData16Bit() const { return _16bitData; }
128140
bool getSimpleReturn() const { return _simpleReturn; }
@@ -136,7 +148,9 @@ class SonarConfiguration {
136148

137149
mutable OculusSimpleFireMessage2 _sfm;
138150

139-
bool _rangeAsMeters;
151+
bool _sendRangeAsMeters;
152+
float _rangeInMeters;
153+
140154
bool _sendGain;
141155
bool _simpleReturn;
142156
bool _gainAssistance;

lib/DataRx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void DataRx::rxPacket(const boost::system::error_code& ec,
261261
LOG(DEBUG) << "Received " << bytes_transferred << " of LogMessage data";
262262
LOG(INFO) << std::string(_buffer->begin()+sizeof(OculusMessageHeader), _buffer->end());
263263
} else {
264-
LOG(WARNING) << "Ignoring message id " << static_cast<int>(hdr.msgId());
264+
LOG(WARNING) << "Ignoring " << MessageTypeToString(hdr.msgId()) << " message, id " <<static_cast<int>(hdr.msgId());
265265
}
266266

267267
exit:

lib/SimpleFireMessage.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2017-2022 University of Washington
3+
* Author: Aaron Marburg <[email protected]>
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* 3. Neither the name of University of Washington nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
32+
#include <iomanip>
33+
34+
#include "liboculus/SimpleFireMessage.h"
35+
36+
namespace liboculus {
37+
38+
// Specializations for the cases where the behavior actually varies between
39+
// the two SimpleFireMessage types
40+
41+
template<>
42+
float SimpleFireMessage<OculusSimpleFireMessage>::range() const {
43+
return fireMsg()->range;
44+
}
45+
46+
template<>
47+
float SimpleFireMessage<OculusSimpleFireMessage2>::range() const {
48+
return fireMsg()->rangePercent;
49+
}
50+
51+
52+
}

lib/SonarConfiguration.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@
3333

3434
#include "liboculus/SonarConfiguration.h"
3535
#include "liboculus/DataTypes.h"
36+
#include "liboculus/Constants.h"
3637

3738
#include <boost/asio.hpp>
3839
#include <g3log/g3log.hpp>
3940

4041
namespace liboculus {
4142

4243
SonarConfiguration::SonarConfiguration()
43-
: _rangeAsMeters(true),
44+
: _sendRangeAsMeters(true),
45+
_rangeInMeters(5),
4446
_sendGain(true),
4547
_simpleReturn(true),
4648
_gainAssistance(true),
@@ -73,7 +75,7 @@ SonarConfiguration &SonarConfiguration::setRange(double input) {
7375
// 40 meters is the max range for the 1200d model
7476
// may need to use a double instead of uint8_t (depends on flags)
7577
if (input <= 40 && input > 0) {
76-
_sfm.rangePercent = input;
78+
_rangeInMeters = input;
7779
} else {
7880
LOG(WARNING) << "Requested invalid range: " << input;
7981
}
@@ -113,8 +115,8 @@ SonarConfiguration &SonarConfiguration::setDataSize(DataSizeType sz) {
113115
return *this;
114116
}
115117

116-
SonarConfiguration &SonarConfiguration::setRangeAsMeters(bool v) {
117-
_rangeAsMeters = v;
118+
SonarConfiguration &SonarConfiguration::sendRangeAsMeters(bool v) {
119+
_sendRangeAsMeters = v;
118120
return *this;
119121
}
120122

@@ -145,9 +147,22 @@ template <>
145147
std::vector<uint8_t> SonarConfiguration::serialize<OculusSimpleFireMessage2>() const {
146148
updateFlags();
147149

150+
// Corner case. If in 32bit mode, range is always a percentage of max range
151+
// if ((_dataSize == dataSize32Bit) || (!_sendRangeAsMeters)) {
152+
// const float maxRange = ((getFreqMode()==OCULUS_LOW_FREQ) ? Oculus_1200MHz::MaxRange : Oculus_2100MHz::MaxRange);
153+
154+
// _sfm.rangePercent = std::min(_rangeInMeters/maxRange * 100.0,100.0);
155+
156+
157+
// LOG(INFO) << "In 32bit mode, setting range to " << _rangeInMeters << " which is " << _sfm.rangePercent << " percent";
158+
// } else {
159+
_sfm.rangePercent = _rangeInMeters;
160+
//}
161+
148162
std::vector<uint8_t> v;
149163
const auto ptr = reinterpret_cast<const char*>(&_sfm);
150164
v.insert(v.end(), ptr, ptr + sizeof(OculusSimpleFireMessage2));
165+
151166
return v;
152167
}
153168

@@ -179,7 +194,7 @@ void SonarConfiguration::updateFlags() const {
179194
if (_dataSize == dataSize32Bit)
180195
_sfm.extFlags |= 0x00000200;
181196

182-
_sfm.flags = (_rangeAsMeters ? FlagBits::RangeAsMeters : 0 ) |
197+
_sfm.flags = (_rangeInMeters ? FlagBits::RangeAsMeters : 0 ) |
183198
(((_dataSize == dataSize16Bit) || (_dataSize == dataSize32Bit)) ? FlagBits::Data16Bits : 0) |
184199
(_sendGain ? FlagBits::DoSendGain : 0) |
185200
(_simpleReturn ? FlagBits::SimpleReturn : 0) |
@@ -195,15 +210,15 @@ void SonarConfiguration::dump() const {
195210
<< std::hex << std::setw(2) << std::setfill('0')
196211
<< static_cast<unsigned int>(_sfm.flags)
197212
<< std::setw(8)
198-
<< "\n Ext flags 0x"
213+
<< "\n Ext flags 0x"
199214
<< std::setw(8) << static_cast<uint32_t>(_sfm.extFlags)
200215
<< std::dec << std::setw(0)
201-
<< "\n range is meters " << getRangeAsMeters()
202-
<< "\n data size " << DataSizeToString(getDataSize())
203-
<< "\n send gain " << getSendGain()
204-
<< "\n simple return " << getSimpleReturn()
205-
<< "\n gain assistance " << getGainAssistance()
206-
<< "\n use 512 beams " << get512Beams();
216+
<< "\n send range is meters " << getSendRangeAsMeters()
217+
<< "\n data size " << DataSizeToString(getDataSize())
218+
<< "\n send gain " << getSendGain()
219+
<< "\n simple return " << getSimpleReturn()
220+
<< "\n gain assistance " << getGainAssistance()
221+
<< "\n use 512 beams " << get512Beams();
207222
}
208223

209224

tools/oculus_client.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ int main(int argc, char **argv) {
6969
int stopAfter = -1;
7070
app.add_option("-n,--frames", stopAfter, "Stop after (n) frames.");
7171

72+
float range = 4;
73+
app.add_option("-r,--range", range, "Range in meters");
7274

7375
CLI11_PARSE(app, argc, argv);
7476

@@ -109,15 +111,14 @@ int main(int argc, char **argv) {
109111

110112
SonarConfiguration config;
111113
config.setPingRate(pingRateNormal);
112-
if (bitDepth == 8)
114+
config.setRange(range);
115+
if (bitDepth == 8) {
113116
config.setDataSize(dataSize8Bit);
114-
else if (bitDepth == 16)
117+
} else if (bitDepth == 16) {
115118
config.setDataSize(dataSize16Bit);
116-
else if (bitDepth == 32) {
119+
} else if (bitDepth == 32) {
117120
config.dontSendGain()
118121
.noGainAssistance()
119-
.rangeAsPercent()
120-
// .use256Beams()
121122
.setDataSize(dataSize32Bit);
122123
}
123124

@@ -164,7 +165,7 @@ int main(int argc, char **argv) {
164165
}
165166

166167
count++;
167-
if ((stopAfter > 0) && (count >= stopAfter)) _io_thread->stop();
168+
if ((stopAfter > 0) && (count >= stopAfter)) doStop=true;
168169
});
169170

170171
_data_rx.setOnConnectCallback([&]() {

0 commit comments

Comments
 (0)