-
Notifications
You must be signed in to change notification settings - Fork 48
/
IMUDriver.cpp
301 lines (235 loc) · 8.8 KB
/
IMUDriver.cpp
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
/*
* Copyright (C) 2013-2015 Fondazione Istituto Italiano di Tecnologia RBCS & iCub Facility & ADVR
* Authors: see AUTHORS file.
* CopyPolicy: Released under the terms of the LGPLv2.1 or any later version, see LGPL.TXT or LGPL3.TXT
*/
#include "IMUDriver.h"
#include <GazeboYarpPlugins/Handler.hh>
#include <GazeboYarpPlugins/common.h>
#include <boost/bind/bind.hpp>
#include <gazebo/sensors/ImuSensor.hh>
#include <yarp/os/Log.h>
#include <yarp/os/LogStream.h>
#include <ignition/math/Quaternion.hh>
using namespace boost::placeholders;
using namespace yarp::dev;
const unsigned YarpIMUChannelsNumber = 12; //The IMU has 12 fixed channels
constexpr size_t rpyStartIdx = 0;
constexpr size_t accelStartIdx = 3;
constexpr size_t gyroStartIdx = 6;
constexpr size_t magnStartIdx = 9;
const std::string YarpIMUScopedName {"sensorScopedName"};
GazeboYarpIMUDriver::GazeboYarpIMUDriver() = default;
GazeboYarpIMUDriver::~GazeboYarpIMUDriver() = default;
/**
*
* Export an inertial sensor.
* The network interface is a single Port.
* We will stream bottles with 12 floats:
* 0 1 2 = Euler orientation data (Kalman filter processed)
* 3 4 5 = Calibrated 3-axis (X, Y, Z) acceleration data
* 6 7 8 = Calibrated 3-axis (X, Y, Z) gyroscope data
* 9 10 11 = Calibrated 3-axis (X, Y, Z) magnetometer data
*
* \todo TODO check orientation data
*/
void GazeboYarpIMUDriver::onUpdate(const gazebo::common::UpdateInfo &/*_info*/)
{
ignition::math::Vector3d euler_orientation = this->m_parentSensor->Orientation().Euler();
ignition::math::Vector3d linear_acceleration = this->m_parentSensor->LinearAcceleration();
ignition::math::Vector3d angular_velocity = this->m_parentSensor->AngularVelocity();
/** \todo TODO ensure that the timestamp is the right one */
m_lastTimestamp.update(this->m_parentSensor->LastUpdateTime().Double());
std::lock_guard<std::mutex> lock(m_dataMutex);
for (unsigned i = 0; i < 3; i++) {
m_imuData[0 + i] = GazeboYarpPlugins::convertRadiansToDegrees(euler_orientation[i]);
}
for (unsigned i = 0; i < 3; i++) {
m_imuData[3 + i] = linear_acceleration[i];
}
for (unsigned i = 0; i < 3; i++) {
m_imuData[6 + i] = GazeboYarpPlugins::convertRadiansToDegrees(angular_velocity[i]);
}
}
//DEVICE DRIVER
bool GazeboYarpIMUDriver::open(yarp::os::Searchable& config)
{
{
std::lock_guard<std::mutex> lock(m_dataMutex);
m_imuData.resize(YarpIMUChannelsNumber, 0.0);
}
//Get gazebo pointers
std::string sensorScopedName(config.find(YarpIMUScopedName).asString());
std::string sensorName(config.find("sensor_name").asString());
m_sensorName = GazeboYarpPlugins::lastPartOfStringAfterSeparator(sensorName, "::");
m_frameName = GazeboYarpPlugins::lastPartOfStringAfterSeparator(sensorName, "::");
m_parentSensor = dynamic_cast<gazebo::sensors::ImuSensor*>(GazeboYarpPlugins::Handler::getHandler()->getSensor(sensorScopedName));
if (!m_parentSensor) {
yError() << "GazeboYarpIMUDriver Error: IMU sensor was not found";
return false;
}
if (!config.check("useInitialSensorOrientationAsReference")) {
m_parentSensor->SetWorldToReferenceOrientation(ignition::math::Quaterniond::Identity);
}
//Connect the driver to the gazebo simulation
using namespace boost::placeholders;
this->m_updateConnection = gazebo::event::Events::ConnectWorldUpdateBegin(boost::bind(&GazeboYarpIMUDriver::onUpdate, this, _1));
return true;
}
bool GazeboYarpIMUDriver::close()
{
this->m_updateConnection.reset();
return true;
}
//GENERIC SENSOR
bool GazeboYarpIMUDriver::read(yarp::sig::Vector &out)
{
if (m_imuData.size() != YarpIMUChannelsNumber ) {
return false;
}
///< \todo TODO this should be avoided by properly modifyng the wrapper
if (out.size() != m_imuData.size()) {
out.resize(m_imuData.size());
}
std::lock_guard<std::mutex> lock(m_dataMutex);
out = m_imuData;
return true;
}
bool GazeboYarpIMUDriver::getChannels(int *nc)
{
if (!nc) return false;
*nc = YarpIMUChannelsNumber;
return true;
}
bool GazeboYarpIMUDriver::calibrate(int /*ch*/, double /*v*/)
{
return true; //Calibration is not needed in simulation
}
//PRECISELY TIMED
yarp::os::Stamp GazeboYarpIMUDriver::getLastInputStamp()
{
return m_lastTimestamp;
}
size_t GazeboYarpIMUDriver::getNrOfThreeAxisGyroscopes() const
{
return 1;
}
yarp::dev::MAS_status GazeboYarpIMUDriver::getThreeAxisGyroscopeStatus(size_t sens_index) const
{
return genericGetStatus(sens_index);
}
bool GazeboYarpIMUDriver::getThreeAxisGyroscopeName(size_t sens_index, std::string& name) const
{
return genericGetSensorName(sens_index, name);
}
bool GazeboYarpIMUDriver::getThreeAxisGyroscopeFrameName(size_t sens_index, std::string& frameName) const
{
return genericGetFrameName(sens_index, frameName);
}
bool GazeboYarpIMUDriver::getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
{
return genericGetMeasure(sens_index, out, timestamp, gyroStartIdx);
}
size_t GazeboYarpIMUDriver::getNrOfThreeAxisLinearAccelerometers() const
{
return 1;
}
yarp::dev::MAS_status GazeboYarpIMUDriver::getThreeAxisLinearAccelerometerStatus(size_t sens_index) const
{
return genericGetStatus(sens_index);
}
bool GazeboYarpIMUDriver::getThreeAxisLinearAccelerometerName(size_t sens_index, std::string& name) const
{
return genericGetSensorName(sens_index, name);
}
bool GazeboYarpIMUDriver::getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string& frameName) const
{
return genericGetFrameName(sens_index, frameName);
}
bool GazeboYarpIMUDriver::getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
{
return genericGetMeasure(sens_index, out, timestamp, accelStartIdx);
}
size_t GazeboYarpIMUDriver::getNrOfOrientationSensors() const
{
return 1;
}
yarp::dev::MAS_status GazeboYarpIMUDriver::getOrientationSensorStatus(size_t sens_index) const
{
return genericGetStatus(sens_index);
}
bool GazeboYarpIMUDriver::getOrientationSensorName(size_t sens_index, std::string& name) const
{
return genericGetSensorName(sens_index, name);
}
bool GazeboYarpIMUDriver::getOrientationSensorFrameName(size_t sens_index, std::string& frameName) const
{
return genericGetFrameName(sens_index, frameName);
}
bool GazeboYarpIMUDriver::getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector& rpy, double& timestamp) const
{
return genericGetMeasure(sens_index, rpy, timestamp, rpyStartIdx);
}
size_t GazeboYarpIMUDriver::getNrOfThreeAxisMagnetometers() const
{
return 1;
}
yarp::dev::MAS_status GazeboYarpIMUDriver::getThreeAxisMagnetometerStatus(size_t sens_index) const
{
return genericGetStatus(sens_index);
}
bool GazeboYarpIMUDriver::getThreeAxisMagnetometerName(size_t sens_index, std::string& name) const
{
return genericGetSensorName(sens_index, name);
}
bool GazeboYarpIMUDriver::getThreeAxisMagnetometerFrameName(size_t sens_index, std::string& frameName) const
{
return genericGetFrameName(sens_index, frameName);
}
bool GazeboYarpIMUDriver::getThreeAxisMagnetometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
{
return genericGetMeasure(sens_index, out, timestamp, magnStartIdx);
}
yarp::dev::MAS_status GazeboYarpIMUDriver::genericGetStatus(size_t sens_index) const
{
if (sens_index != 0)
{
yError() << "GazeboYarpIMUDriver: sens_index must be equal to 0, since there is only one sensor in consideration";
return yarp::dev::MAS_status::MAS_ERROR;
}
return yarp::dev::MAS_status::MAS_OK;
}
bool GazeboYarpIMUDriver::genericGetSensorName(size_t sens_index, std::string& name) const
{
if (sens_index != 0)
{
yError() << "GazeboYarpIMUDriver: sens_index must be equal to 0, since there is only one sensor in consideration";
return false;
}
name = m_sensorName;
return true;
}
bool GazeboYarpIMUDriver::genericGetFrameName(size_t sens_index, std::string& frameName) const
{
if (sens_index != 0)
{
yError() << "GazeboYarpIMUDriver: sens_index must be equal to 0, since there is only one sensor in consideration";
return false;
}
frameName = m_frameName;
return true;
}
bool GazeboYarpIMUDriver::genericGetMeasure(size_t sens_index, yarp::sig::Vector &out, double ×tamp, size_t startIdx) const {
if (sens_index != 0)
{
yError() << "GazeboYarpIMUDriver: sens_index must be equal to 0, since there is only one sensor in consideration";
return false;
}
out.resize(3);
std::lock_guard<std::mutex> lock(m_dataMutex);
out[0] = m_imuData[startIdx];
out[1] = m_imuData[startIdx + 1];
out[2] = m_imuData[startIdx + 2];
timestamp = m_lastTimestamp.getTime();
return true;
}