-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathaltimeter.cc
More file actions
302 lines (259 loc) · 10.3 KB
/
altimeter.cc
File metadata and controls
302 lines (259 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <sdf/sdf.hh>
#include <gz/math/Helpers.hh>
#include <gz/msgs/altimeter.pb.h>
#include <gz/sensors/AltimeterSensor.hh>
#include <gz/sensors/SensorFactory.hh>
#include "test_config.hh" // NOLINT(build/include)
#include "TransportTestTools.hh"
/// \brief Helper function to create an altimeter sdf element
sdf::ElementPtr AltimeterToSdf(const std::string &_name,
const gz::math::Pose3d &_pose, const double _updateRate,
const std::string &_topic, const bool _alwaysOn,
const bool _visualize)
{
std::ostringstream stream;
stream
<< "<?xml version='1.0'?>"
<< "<sdf version='1.6'>"
<< " <model name='m1'>"
<< " <link name='link1'>"
<< " <sensor name='" << _name << "' type='altimeter'>"
<< " <pose>" << _pose << "</pose>"
<< " <topic>" << _topic << "</topic>"
<< " <update_rate>"<< _updateRate <<"</update_rate>"
<< " <alwaysOn>" << _alwaysOn <<"</alwaysOn>"
<< " <visualize>" << _visualize << "</visualize>"
<< " </sensor>"
<< " </link>"
<< " </model>"
<< "</sdf>";
sdf::SDFPtr sdfParsed(new sdf::SDF());
sdf::init(sdfParsed);
if (!sdf::readString(stream.str(), sdfParsed))
return sdf::ElementPtr();
return sdfParsed->Root()->GetElement("model")->GetElement("link")
->GetElement("sensor");
}
/// \brief Helper function to create an altimeter sdf element with noise
sdf::ElementPtr AltimeterToSdfWithNoise(const std::string &_name,
const gz::math::Pose3d &_pose, const double _updateRate,
const std::string &_topic, const bool _alwaysOn,
const bool _visualize, double _mean, double _stddev, double _bias)
{
std::ostringstream stream;
stream
<< "<?xml version='1.0'?>"
<< "<sdf version='1.6'>"
<< " <model name='m1'>"
<< " <link name='link1'>"
<< " <sensor name='" << _name << "' type='altimeter'>"
<< " <pose>" << _pose << "</pose>"
<< " <topic>" << _topic << "</topic>"
<< " <update_rate>"<< _updateRate <<"</update_rate>"
<< " <alwaysOn>" << _alwaysOn <<"</alwaysOn>"
<< " <visualize>" << _visualize << "</visualize>"
<< " <altimeter>"
<< " <vertical_position>"
<< " <noise type='gaussian'>"
<< " <mean>" << _mean << "</mean>"
<< " <stddev>" << _stddev << "</stddev>"
<< " <bias_mean>" << _bias << "</bias_mean>"
<< " </noise>"
<< " </vertical_position>"
<< " <vertical_velocity>"
<< " <noise type='gaussian'>"
<< " <mean>" << _mean << "</mean>"
<< " <stddev>" << _stddev << "</stddev>"
<< " <bias_mean>" << _bias << "</bias_mean>"
<< " </noise>"
<< " </vertical_velocity>"
<< " </altimeter>"
<< " </sensor>"
<< " </link>"
<< " </model>"
<< "</sdf>";
sdf::SDFPtr sdfParsed(new sdf::SDF());
sdf::init(sdfParsed);
if (!sdf::readString(stream.str(), sdfParsed))
return sdf::ElementPtr();
return sdfParsed->Root()->GetElement("model")->GetElement("link")
->GetElement("sensor");
}
/// \brief Test altimeter sensor
class AltimeterSensorTest: public testing::Test
{
// Documentation inherited
protected: void SetUp() override
{
gz::common::Console::SetVerbosity(4);
}
};
/////////////////////////////////////////////////
TEST_F(AltimeterSensorTest, CreateAltimeter)
{
// Create SDF describing an altimeter sensor
const std::string name = "TestAltimeter";
const std::string topic = "/gz/sensors/test/altimeter";
const std::string topicNoise = "/gz/sensors/test/altimeter_noise";
const double updateRate = 30;
const bool alwaysOn = 1;
const bool visualize = 1;
// Create sensor SDF
gz::math::Pose3d sensorPose(gz::math::Vector3d(0.25, 0.0, 0.5),
gz::math::Quaterniond::Identity);
sdf::ElementPtr altimeterSdf = AltimeterToSdf(name, sensorPose,
updateRate, topic, alwaysOn, visualize);
sdf::ElementPtr altimeterSdfNoise = AltimeterToSdfWithNoise(name, sensorPose,
updateRate, topicNoise, alwaysOn, visualize, 1.0, 0.2, 10.0);
// create the sensor using sensor factory
gz::sensors::SensorFactory sf;
std::unique_ptr<gz::sensors::AltimeterSensor> sensor =
sf.CreateSensor<gz::sensors::AltimeterSensor>(altimeterSdf);
ASSERT_NE(nullptr, sensor);
EXPECT_EQ(name, sensor->Name());
EXPECT_EQ(topic, sensor->Topic());
EXPECT_DOUBLE_EQ(updateRate, sensor->UpdateRate());
std::unique_ptr<gz::sensors::AltimeterSensor> sensorNoise =
sf.CreateSensor<gz::sensors::AltimeterSensor>(altimeterSdfNoise);
ASSERT_NE(nullptr, sensorNoise);
EXPECT_EQ(name, sensorNoise->Name());
EXPECT_EQ(topicNoise, sensorNoise->Topic());
EXPECT_DOUBLE_EQ(updateRate, sensorNoise->UpdateRate());
}
/////////////////////////////////////////////////
TEST_F(AltimeterSensorTest, SensorReadings)
{
// Create SDF describing an altimeter sensor
const std::string name = "TestAltimeter";
const std::string topic = "/gz/sensors/test/altimeter";
const std::string topicNoise = "/gz/sensors/test/altimeter_noise";
const double updateRate = 30;
const bool alwaysOn = 1;
const bool visualize = 1;
// Create sensor SDF
gz::math::Pose3d sensorPose(gz::math::Vector3d(0.25, 0.0, 0.5),
gz::math::Quaterniond::Identity);
sdf::ElementPtr altimeterSdf = AltimeterToSdf(name, sensorPose,
updateRate, topic, alwaysOn, visualize);
sdf::ElementPtr altimeterSdfNoise = AltimeterToSdfWithNoise(name, sensorPose,
updateRate, topicNoise, alwaysOn, visualize, 1.0, 0.2, 10.0);
// create the sensor using sensor factory
gz::sensors::SensorFactory sf;
auto sensor =
sf.CreateSensor<gz::sensors::AltimeterSensor>(altimeterSdf);
ASSERT_NE(nullptr, sensor);
EXPECT_FALSE(sensor->HasConnections());
auto sensorNoise =
sf.CreateSensor<gz::sensors::AltimeterSensor>(altimeterSdfNoise);
ASSERT_NE(nullptr, sensorNoise);
// verify initial readings
EXPECT_DOUBLE_EQ(0.0, sensor->VerticalReference());
EXPECT_DOUBLE_EQ(0.0, sensor->VerticalVelocity());
EXPECT_DOUBLE_EQ(0.0, sensor->VerticalPosition());
// verify initial readings
EXPECT_DOUBLE_EQ(0.0, sensorNoise->VerticalReference());
EXPECT_DOUBLE_EQ(0.0, sensorNoise->VerticalVelocity());
EXPECT_DOUBLE_EQ(0.0, sensorNoise->VerticalPosition());
// set state and verify readings
double vertRef = 1.0;
sensor->SetVerticalReference(vertRef);
sensorNoise->SetVerticalReference(vertRef);
EXPECT_DOUBLE_EQ(vertRef, sensor->VerticalReference());
EXPECT_DOUBLE_EQ(vertRef, sensorNoise->VerticalReference());
double pos = 2.0;
sensor->SetPosition(pos);
sensorNoise->SetPosition(pos);
EXPECT_DOUBLE_EQ(pos - vertRef, sensor->VerticalPosition());
EXPECT_DOUBLE_EQ(pos - vertRef, sensorNoise->VerticalPosition());
double vertVel = 3.0;
sensor->SetVerticalVelocity(vertVel);
sensorNoise->SetVerticalVelocity(vertVel);
EXPECT_DOUBLE_EQ(vertVel, sensor->VerticalVelocity());
EXPECT_DOUBLE_EQ(vertVel, sensorNoise->VerticalVelocity());
pos = -6.0;
sensor->SetPosition(pos);
sensorNoise->SetPosition(pos);
EXPECT_DOUBLE_EQ(pos - vertRef, sensor->VerticalPosition());
EXPECT_DOUBLE_EQ(pos - vertRef, sensorNoise->VerticalPosition());
// verify msg received on the topic
WaitForMessageTestHelper<gz::msgs::Altimeter> msgHelper(topic);
EXPECT_TRUE(sensor->HasConnections());
sensor->Update(std::chrono::steady_clock::duration(std::chrono::seconds(1)));
EXPECT_TRUE(msgHelper.WaitForMessage()) << msgHelper;
auto msg = msgHelper.Message();
EXPECT_EQ(1, msg.header().stamp().sec());
EXPECT_EQ(0, msg.header().stamp().nsec());
EXPECT_DOUBLE_EQ(vertRef, msg.vertical_reference());
EXPECT_DOUBLE_EQ(pos - vertRef, msg.vertical_position());
EXPECT_DOUBLE_EQ(vertVel, msg.vertical_velocity());
// verify msg with noise received on the topic
WaitForMessageTestHelper<gz::msgs::Altimeter>
msgHelperNoise(topicNoise);
sensorNoise->Update(std::chrono::steady_clock::duration(
std::chrono::seconds(1)), false);
EXPECT_TRUE(msgHelperNoise.WaitForMessage()) << msgHelperNoise;
auto msgNoise = msgHelperNoise.Message();
EXPECT_EQ(1, msg.header().stamp().sec());
EXPECT_EQ(0, msg.header().stamp().nsec());
EXPECT_DOUBLE_EQ(vertRef, msgNoise.vertical_reference());
EXPECT_FALSE(gz::math::equal(pos - vertRef,
msgNoise.vertical_position()));
EXPECT_FALSE(gz::math::equal(vertVel, msgNoise.vertical_velocity()));
}
/////////////////////////////////////////////////
TEST_F(AltimeterSensorTest, Topic)
{
const std::string name = "TestAltimeter";
const double updateRate = 30;
const bool alwaysOn = 1;
const bool visualize = 1;
auto sensorPose = gz::math::Pose3d();
// Factory
gz::sensors::SensorFactory factory;
// Default topic
{
const std::string topic;
auto altimeterSdf = AltimeterToSdf(name, sensorPose,
updateRate, topic, alwaysOn, visualize);
auto altimeter = factory.CreateSensor<
gz::sensors::AltimeterSensor>(altimeterSdf);
ASSERT_NE(nullptr, altimeter);
EXPECT_EQ("/altimeter", altimeter->Topic());
}
// Convert to valid topic
{
const std::string topic = "/topic with spaces/@~characters//";
auto altimeterSdf = AltimeterToSdf(name, sensorPose,
updateRate, topic, alwaysOn, visualize);
auto altimeter = factory.CreateSensor<
gz::sensors::AltimeterSensor>(altimeterSdf);
ASSERT_NE(nullptr, altimeter);
EXPECT_EQ("/topic_with_spaces/characters", altimeter->Topic());
}
// Invalid topic
{
const std::string topic = "@@@";
auto altimeterSdf = AltimeterToSdf(name, sensorPose,
updateRate, topic, alwaysOn, visualize);
auto sensor = factory.CreateSensor<
gz::sensors::AltimeterSensor>(altimeterSdf);
ASSERT_EQ(nullptr, sensor);
}
}