-
Notifications
You must be signed in to change notification settings - Fork 270
/
Imu.cc
260 lines (219 loc) · 8.01 KB
/
Imu.cc
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
/*
* 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 "Imu.hh"
#include <unordered_map>
#include <utility>
#include <string>
#include <ignition/plugin/Register.hh>
#include <sdf/Element.hh>
#include <ignition/common/Profiler.hh>
#include <ignition/transport/Node.hh>
#include <ignition/sensors/SensorFactory.hh>
#include <ignition/sensors/ImuSensor.hh>
#include "ignition/gazebo/components/AngularVelocity.hh"
#include "ignition/gazebo/components/Imu.hh"
#include "ignition/gazebo/components/Gravity.hh"
#include "ignition/gazebo/components/LinearAcceleration.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/Pose.hh"
#include "ignition/gazebo/components/ParentEntity.hh"
#include "ignition/gazebo/components/Sensor.hh"
#include "ignition/gazebo/components/World.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
#include "ignition/gazebo/Util.hh"
using namespace ignition;
using namespace gazebo;
using namespace systems;
/// \brief Private Imu data class.
class ignition::gazebo::systems::ImuPrivate
{
/// \brief A map of IMU entity to its IMU sensor.
public: std::unordered_map<Entity,
std::unique_ptr<ignition::sensors::ImuSensor>> entitySensorMap;
/// \brief Ign-sensors sensor factory for creating sensors
public: sensors::SensorFactory sensorFactory;
public: Entity worldEntity = kNullEntity;
/// \brief Create IMU sensor
/// \param[in] _ecm Mutable reference to ECM.
public: void CreateImuEntities(EntityComponentManager &_ecm);
/// \brief Update IMU sensor data based on physics data
/// \param[in] _ecm Immutable reference to ECM.
public: void Update(const EntityComponentManager &_ecm);
/// \brief Remove IMU sensors if their entities have been removed from
/// simulation.
/// \param[in] _ecm Immutable reference to ECM.
public: void RemoveImuEntities(const EntityComponentManager &_ecm);
};
//////////////////////////////////////////////////
Imu::Imu() : System(), dataPtr(std::make_unique<ImuPrivate>())
{
}
//////////////////////////////////////////////////
Imu::~Imu() = default;
//////////////////////////////////////////////////
void Imu::PreUpdate(const UpdateInfo &/*_info*/,
EntityComponentManager &_ecm)
{
IGN_PROFILE("Imu::PreUpdate");
this->dataPtr->CreateImuEntities(_ecm);
}
//////////////////////////////////////////////////
void Imu::PostUpdate(const UpdateInfo &_info,
const EntityComponentManager &_ecm)
{
IGN_PROFILE("Imu::PostUpdate");
// \TODO(anyone) Support rewind
if (_info.dt < std::chrono::steady_clock::duration::zero())
{
ignwarn << "Detected jump back in time ["
<< std::chrono::duration_cast<std::chrono::seconds>(_info.dt).count()
<< "s]. System may not work properly." << std::endl;
}
// Only update and publish if not paused.
if (!_info.paused)
{
this->dataPtr->Update(_ecm);
for (auto &it : this->dataPtr->entitySensorMap)
{
// Update measurement time
auto time = math::durationToSecNsec(_info.simTime);
dynamic_cast<sensors::Sensor *>(it.second.get())->Update(
math::secNsecToDuration(time.first, time.second), false);
}
}
this->dataPtr->RemoveImuEntities(_ecm);
}
//////////////////////////////////////////////////
void ImuPrivate::CreateImuEntities(EntityComponentManager &_ecm)
{
IGN_PROFILE("ImuPrivate::CreateImuEntities");
// Get World Entity
if (kNullEntity == this->worldEntity)
this->worldEntity = _ecm.EntityByComponents(components::World());
if (kNullEntity == this->worldEntity)
{
ignerr << "Missing world entity." << std::endl;
return;
}
// Get the world acceleration (defined in world frame)
auto gravity = _ecm.Component<components::Gravity>(worldEntity);
if (nullptr == gravity)
{
ignerr << "World missing gravity." << std::endl;
return;
}
// Create IMUs
_ecm.EachNew<components::Imu, components::ParentEntity>(
[&](const Entity &_entity,
const components::Imu *_imu,
const components::ParentEntity *_parent)->bool
{
// create sensor
std::string sensorScopedName =
removeParentScope(scopedName(_entity, _ecm, "::", false), "::");
sdf::Sensor data = _imu->Data();
data.SetName(sensorScopedName);
// check topic
if (data.Topic().empty())
{
std::string topic = scopedName(_entity, _ecm) + "/imu";
data.SetTopic(topic);
}
std::unique_ptr<sensors::ImuSensor> sensor =
this->sensorFactory.CreateSensor<
sensors::ImuSensor>(data);
if (nullptr == sensor)
{
ignerr << "Failed to create sensor [" << sensorScopedName << "]"
<< std::endl;
return true;
}
// set sensor parent
std::string parentName = _ecm.Component<components::Name>(
_parent->Data())->Data();
sensor->SetParent(parentName);
// set gravity - assume it remains fixed
sensor->SetGravity(gravity->Data());
// Get initial pose of sensor and set the reference z pos
// The WorldPose component was just created and so it's empty
// We'll compute the world pose manually here
math::Pose3d p = worldPose(_entity, _ecm);
sensor->SetOrientationReference(p.Rot());
// Set topic
_ecm.CreateComponent(_entity, components::SensorTopic(sensor->Topic()));
this->entitySensorMap.insert(
std::make_pair(_entity, std::move(sensor)));
return true;
});
}
//////////////////////////////////////////////////
void ImuPrivate::Update(const EntityComponentManager &_ecm)
{
IGN_PROFILE("ImuPrivate::Update");
_ecm.Each<components::Imu,
components::WorldPose,
components::AngularVelocity,
components::LinearAcceleration>(
[&](const Entity &_entity,
const components::Imu * /*_imu*/,
const components::WorldPose *_worldPose,
const components::AngularVelocity *_angularVel,
const components::LinearAcceleration *_linearAccel)->bool
{
auto it = this->entitySensorMap.find(_entity);
if (it != this->entitySensorMap.end())
{
const auto &imuWorldPose = _worldPose->Data();
it->second->SetWorldPose(imuWorldPose);
// Set the IMU angular velocity (defined in imu's local frame)
it->second->SetAngularVelocity(_angularVel->Data());
// Set the IMU linear acceleration in the imu local frame
it->second->SetLinearAcceleration(_linearAccel->Data());
}
else
{
ignerr << "Failed to update IMU: " << _entity << ". "
<< "Entity not found." << std::endl;
}
return true;
});
}
//////////////////////////////////////////////////
void ImuPrivate::RemoveImuEntities(
const EntityComponentManager &_ecm)
{
IGN_PROFILE("ImuPrivate::RemoveImuEntities");
_ecm.EachRemoved<components::Imu>(
[&](const Entity &_entity,
const components::Imu *)->bool
{
auto sensorId = this->entitySensorMap.find(_entity);
if (sensorId == this->entitySensorMap.end())
{
ignerr << "Internal error, missing IMU sensor for entity ["
<< _entity << "]" << std::endl;
return true;
}
this->entitySensorMap.erase(sensorId);
return true;
});
}
IGNITION_ADD_PLUGIN(Imu, System,
Imu::ISystemPreUpdate,
Imu::ISystemPostUpdate
)
IGNITION_ADD_PLUGIN_ALIAS(Imu, "ignition::gazebo::systems::Imu")