/
LiftDrag.cc
530 lines (447 loc) · 17.3 KB
/
LiftDrag.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
* 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 "LiftDrag.hh"
#include <algorithm>
#include <string>
#include <vector>
#include <ignition/common/Profiler.hh>
#include <ignition/plugin/Register.hh>
#include <ignition/transport/Node.hh>
#include <sdf/Element.hh>
#include "ignition/gazebo/Link.hh"
#include "ignition/gazebo/Model.hh"
#include "ignition/gazebo/Util.hh"
#include "ignition/gazebo/components/AngularVelocity.hh"
#include "ignition/gazebo/components/Inertial.hh"
#include "ignition/gazebo/components/JointPosition.hh"
#include "ignition/gazebo/components/LinearVelocity.hh"
#include "ignition/gazebo/components/Link.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/ExternalWorldWrenchCmd.hh"
#include "ignition/gazebo/components/Pose.hh"
using namespace ignition;
using namespace gazebo;
using namespace systems;
class ignition::gazebo::systems::LiftDragPrivate
{
// Initialize the system
public: void Load(const EntityComponentManager &_ecm,
const sdf::ElementPtr &_sdf);
/// \brief Compute lift and drag forces and update the corresponding
/// components
/// \param[in] _ecm Immutable reference to the EntityComponentManager
public: void Update(EntityComponentManager &_ecm);
/// \brief Model interface
public: Model model{kNullEntity};
/// \brief Coefficient of Lift / alpha slope.
/// Lift = C_L * q * S
/// where q (dynamic pressure) = 0.5 * rho * v^2
public: double cla = 1.0;
/// \brief Coefficient of Drag / alpha slope.
/// Drag = C_D * q * S
/// where q (dynamic pressure) = 0.5 * rho * v^2
public: double cda = 0.01;
/// \brief Coefficient of Moment / alpha slope.
/// Moment = C_M * q * S
/// where q (dynamic pressure) = 0.5 * rho * v^2
public: double cma = 0.01;
/// \brief angle of attach when airfoil stalls
public: double alphaStall = IGN_PI_2;
/// \brief Cl-alpha rate after stall
public: double claStall = 0.0;
/// \brief Cd-alpha rate after stall
/// \todo(anyone): what's flat plate drag?
public: double cdaStall = 1.0;
/// \brief Cm-alpha rate after stall
public: double cmaStall = 0.0;
/// \brief air density
/// at 25 deg C it's about 1.1839 kg/m^3
/// At 20 °C and 101.325 kPa, dry air has a density of 1.2041 kg/m3.
public: double rho = 1.2041;
/// \brief if the shape is aerodynamically radially symmetric about
/// the forward direction. Defaults to false for wing shapes.
/// If set to true, the upward direction is determined by the
/// angle of attack.
public: bool radialSymmetry = false;
/// \brief effective planeform surface area
public: double area = 1.0;
/// \brief initial angle of attack
public: double alpha0 = 0.0;
/// \brief center of pressure in link local coordinates with respect to the
/// link's center of mass
public: ignition::math::Vector3d cp = math::Vector3d::Zero;
/// \brief Normally, this is taken as a direction parallel to the chord
/// of the airfoil in zero angle of attack forward flight.
public: ignition::math::Vector3d forward = math::Vector3d::UnitX;
/// \brief A vector in the lift/drag plane, perpendicular to the forward
/// vector. Inflow velocity orthogonal to forward and upward vectors
/// is considered flow in the wing sweep direction.
public: ignition::math::Vector3d upward = math::Vector3d::UnitZ;
/// \brief how much to change CL per radian of control surface joint
/// value.
public: double controlJointRadToCL = 4.0;
/// \brief Link entity targeted this plugin.
public: Entity linkEntity;
/// \brief Joint entity that actuates a control surface for this lifting body
public: Entity controlJointEntity;
/// \brief Set during Load to true if the configuration for the system is
/// valid and the post-update can run
public: bool validConfig{false};
/// \brief Copy of the sdf configuration used for this plugin
public: sdf::ElementPtr sdfConfig;
/// \brief Initialization flag
public: bool initialized{false};
};
//////////////////////////////////////////////////
void LiftDragPrivate::Load(const EntityComponentManager &_ecm,
const sdf::ElementPtr &_sdf)
{
this->cla = _sdf->Get<double>("cla", this->cla).first;
this->cda = _sdf->Get<double>("cda", this->cda).first;
this->cma = _sdf->Get<double>("cma", this->cma).first;
this->alphaStall = _sdf->Get<double>("alpha_stall", this->alphaStall).first;
this->claStall = _sdf->Get<double>("cla_stall", this->claStall).first;
this->cdaStall = _sdf->Get<double>("cda_stall", this->cdaStall).first;
this->cmaStall = _sdf->Get<double>("cma_stall", this->cmaStall).first;
this->rho = _sdf->Get<double>("air_density", this->rho).first;
this->radialSymmetry = _sdf->Get<bool>("radial_symmetry",
this->radialSymmetry).first;
this->area = _sdf->Get<double>("area", this->area).first;
this->alpha0 = _sdf->Get<double>("a0", this->alpha0).first;
this->cp = _sdf->Get<ignition::math::Vector3d>("cp", this->cp).first;
// blade forward (-drag) direction in link frame
this->forward =
_sdf->Get<ignition::math::Vector3d>("forward", this->forward).first;
this->forward.Normalize();
// blade upward (+lift) direction in link frame
this->upward = _sdf->Get<ignition::math::Vector3d>(
"upward", this->upward) .first;
this->upward.Normalize();
this->controlJointRadToCL = _sdf->Get<double>(
"control_joint_rad_to_cl", this->controlJointRadToCL).first;
if (_sdf->HasElement("link_name"))
{
sdf::ElementPtr elem = _sdf->GetElement("link_name");
auto linkName = elem->Get<std::string>();
this->linkEntity = this->model.LinkByName(_ecm, linkName);
if (this->linkEntity == kNullEntity)
{
ignerr << "Link with name[" << linkName << "] not found. "
<< "The LiftDrag will not generate forces\n";
this->validConfig = false;
return;
}
}
else
{
this->validConfig = false;
}
if (_sdf->HasElement("control_joint_name"))
{
auto controlJointName = _sdf->Get<std::string>("control_joint_name");
this->controlJointEntity = this->model.JointByName(_ecm, controlJointName);
if (this->controlJointEntity == kNullEntity)
{
ignerr << "Joint with name[" << controlJointName << "] does not exist.\n";
}
else
{
this->validConfig = false;
}
}
// If we reached here, we have a valid configuration
this->validConfig = true;
}
//////////////////////////////////////////////////
LiftDrag::LiftDrag()
: System(), dataPtr(std::make_unique<LiftDragPrivate>())
{
}
//////////////////////////////////////////////////
void LiftDragPrivate::Update(EntityComponentManager &_ecm)
{
IGN_PROFILE("LiftDragPrivate::Update");
// get linear velocity at cp in world frame
const auto worldLinVel =
_ecm.Component<components::WorldLinearVelocity>(this->linkEntity);
const auto worldAngVel =
_ecm.Component<components::WorldAngularVelocity>(this->linkEntity);
const auto worldPose =
_ecm.Component<components::WorldPose>(this->linkEntity);
components::JointPosition *controlJointPosition = nullptr;
if (this->controlJointEntity != kNullEntity)
{
controlJointPosition =
_ecm.Component<components::JointPosition>(this->controlJointEntity);
}
if (!worldLinVel || !worldAngVel || !worldPose)
return;
const auto &pose = worldPose->Data();
const auto cpWorld = pose.Rot().RotateVector(this->cp);
const auto vel = worldLinVel->Data() + worldAngVel->Data().Cross(cpWorld);
if (vel.Length() <= 0.01)
return;
const auto velI = vel.Normalized();
// rotate forward and upward vectors into world frame
const auto forwardI = pose.Rot().RotateVector(this->forward);
ignition::math::Vector3d upwardI;
if (this->radialSymmetry)
{
// use inflow velocity to determine upward direction
// which is the component of inflow perpendicular to forward direction.
ignition::math::Vector3d tmp = forwardI.Cross(velI);
upwardI = forwardI.Cross(tmp).Normalize();
}
else
{
upwardI = pose.Rot().RotateVector(this->upward);
}
// spanwiseI: a vector normal to lift-drag-plane described in world frame
const auto spanwiseI = forwardI.Cross(upwardI).Normalize();
const double minRatio = -1.0;
const double maxRatio = 1.0;
// check sweep (angle between velI and lift-drag-plane)
double sinSweepAngle = ignition::math::clamp(
spanwiseI.Dot(velI), minRatio, maxRatio);
// get cos from trig identity
const double cosSweepAngle = 1.0 - sinSweepAngle * sinSweepAngle;
double sweep = std::asin(sinSweepAngle);
// truncate sweep to within +/-90 deg
while (std::fabs(sweep) > 0.5 * IGN_PI)
{
sweep = sweep > 0 ? sweep - IGN_PI : sweep + IGN_PI;
}
// angle of attack is the angle between
// velI projected into lift-drag plane
// and
// forward vector
//
// projected = spanwiseI Xcross ( vector Xcross spanwiseI)
//
// so,
// removing spanwise velocity from vel
// Note: Original code had:
// const auto velInLDPlane = vel - vel.Dot(spanwiseI)*velI;
// I believe the projection should be onto spanwiseI which then gets removed
// from vel
const auto velInLDPlane = vel - vel.Dot(spanwiseI)*spanwiseI;
// get direction of drag
const auto dragDirection = -velInLDPlane.Normalized();
// get direction of lift
const auto liftI = spanwiseI.Cross(velInLDPlane).Normalized();
// compute angle between upwardI and liftI
// in general, given vectors a and b:
// cos(theta) = a.Dot(b)/(a.Length()*b.Lenghth())
// given upwardI and liftI are both unit vectors, we can drop the denominator
// cos(theta) = a.Dot(b)
const double cosAlpha =
ignition::math::clamp(liftI.Dot(upwardI), minRatio, maxRatio);
// Is alpha positive or negative? Test:
// forwardI points toward zero alpha
// if forwardI is in the same direction as lift, alpha is positive.
// liftI is in the same direction as forwardI?
double alpha = this->alpha0 - std::acos(cosAlpha);
if (liftI.Dot(forwardI) >= 0.0)
alpha = this->alpha0 + std::acos(cosAlpha);
// normalize to within +/-90 deg
while (fabs(alpha) > 0.5 * IGN_PI)
{
alpha = alpha > 0 ? alpha - IGN_PI : alpha + IGN_PI;
}
// compute dynamic pressure
const double speedInLDPlane = velInLDPlane.Length();
const double q = 0.5 * this->rho * speedInLDPlane * speedInLDPlane;
// compute cl at cp, check for stall, correct for sweep
double cl;
if (alpha > this->alphaStall)
{
cl = (this->cla * this->alphaStall +
this->claStall * (alpha - this->alphaStall)) *
cosSweepAngle;
// make sure cl is still great than 0
cl = std::max(0.0, cl);
}
else if (alpha < -this->alphaStall)
{
cl = (-this->cla * this->alphaStall +
this->claStall * (alpha + this->alphaStall))
* cosSweepAngle;
// make sure cl is still less than 0
cl = std::min(0.0, cl);
}
else
cl = this->cla * alpha * cosSweepAngle;
// modify cl per control joint value
if (controlJointPosition)
{
cl = cl + this->controlJointRadToCL * controlJointPosition->Data()[0];
/// \todo(anyone): also change cm and cd
}
// compute lift force at cp
ignition::math::Vector3d lift = cl * q * this->area * liftI;
// compute cd at cp, check for stall, correct for sweep
double cd;
if (alpha > this->alphaStall)
{
cd = (this->cda * this->alphaStall +
this->cdaStall * (alpha - this->alphaStall))
* cosSweepAngle;
}
else if (alpha < -this->alphaStall)
{
cd = (-this->cda * this->alphaStall +
this->cdaStall * (alpha + this->alphaStall))
* cosSweepAngle;
}
else
cd = (this->cda * alpha) * cosSweepAngle;
// make sure drag is positive
cd = std::fabs(cd);
// drag at cp
ignition::math::Vector3d drag = cd * q * this->area * dragDirection;
// compute cm at cp, check for stall, correct for sweep
double cm;
if (alpha > this->alphaStall)
{
cm = (this->cma * this->alphaStall +
this->cmaStall * (alpha - this->alphaStall))
* cosSweepAngle;
// make sure cm is still great than 0
cm = std::max(0.0, cm);
}
else if (alpha < -this->alphaStall)
{
cm = (-this->cma * this->alphaStall +
this->cmaStall * (alpha + this->alphaStall))
* cosSweepAngle;
// make sure cm is still less than 0
cm = std::min(0.0, cm);
}
else
cm = this->cma * alpha * cosSweepAngle;
/// \todo(anyone): implement cm
/// for now, reset cm to zero, as cm needs testing
cm = 0.0;
// compute moment (torque) at cp
// spanwiseI used to be momentDirection
ignition::math::Vector3d moment = cm * q * this->area * spanwiseI;
// force and torque about cg in world frame
ignition::math::Vector3d force = lift + drag;
ignition::math::Vector3d torque = moment;
// Correct for nan or inf
force.Correct();
this->cp.Correct();
torque.Correct();
// We want to apply the force at cp. The old LiftDrag plugin did the
// following:
// this->link->AddForceAtRelativePosition(force, this->cp);
// The documentation of AddForceAtRelativePosition says:
//> Add a force (in world frame coordinates) to the body at a
//> position relative to the center of mass which is expressed in the
//> link's own frame of reference.
// But it appears that 'cp' is specified in the link frame so it probably
// should have been
// this->link->AddForceAtRelativePosition(
// force, this->cp - this->link->GetInertial()->CoG());
//
// \todo(addisu) Create a convenient API for applying forces at offset
// positions
const auto totalTorque = torque + cpWorld.Cross(force);
Link link(this->linkEntity);
link.AddWorldWrench(_ecm, force, totalTorque);
// Debug
// auto linkName = _ecm.Component<components::Name>(this->linkEntity)->Data();
// igndbg << "=============================\n";
// igndbg << "Link: [" << linkName << "] pose: [" << pose
// << "] dynamic pressure: [" << q << "]\n";
// igndbg << "spd: [" << vel.Length() << "] vel: [" << vel << "]\n";
// igndbg << "LD plane spd: [" << velInLDPlane.Length() << "] vel : ["
// << velInLDPlane << "]\n";
// igndbg << "forward (inertial): " << forwardI << "\n";
// igndbg << "upward (inertial): " << upwardI << "\n";
// igndbg << "q: " << q << "\n";
// igndbg << "cl: " << cl << "\n";
// igndbg << "lift dir (inertial): " << liftI << "\n";
// igndbg << "Span direction (normal to LD plane): " << spanwiseI << "\n";
// igndbg << "sweep: " << sweep << "\n";
// igndbg << "alpha: " << alpha << "\n";
// igndbg << "lift: " << lift << "\n";
// igndbg << "drag: " << drag << " cd: " << cd << " cda: "
// << this->cda << "\n";
// igndbg << "moment: " << moment << "\n";
// igndbg << "force: " << force << "\n";
// igndbg << "torque: " << torque << "\n";
// igndbg << "totalTorque: " << totalTorque << "\n";
}
//////////////////////////////////////////////////
void LiftDrag::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm, EventManager &)
{
this->dataPtr->model = Model(_entity);
if (!this->dataPtr->model.Valid(_ecm))
{
ignerr << "The LiftDrag system should be attached to a model entity. "
<< "Failed to initialize." << std::endl;
return;
}
this->dataPtr->sdfConfig = _sdf->Clone();
}
//////////////////////////////////////////////////
void LiftDrag::PreUpdate(const UpdateInfo &_info, EntityComponentManager &_ecm)
{
IGN_PROFILE("LiftDrag::PreUpdate");
// \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;
}
if (!this->dataPtr->initialized)
{
// We call Load here instead of Configure because we can't be guaranteed
// that all entities have been created when Configure is called
this->dataPtr->Load(_ecm, this->dataPtr->sdfConfig);
this->dataPtr->initialized = true;
if (this->dataPtr->validConfig)
{
Link link(this->dataPtr->linkEntity);
link.EnableVelocityChecks(_ecm, true);
if ((this->dataPtr->controlJointEntity != kNullEntity) &&
!_ecm.Component<components::JointPosition>(
this->dataPtr->controlJointEntity))
{
_ecm.CreateComponent(this->dataPtr->controlJointEntity,
components::JointPosition());
}
}
}
if (_info.paused)
return;
// This is not an "else" because "initialized" can be set in the if block
// above
if (this->dataPtr->initialized && this->dataPtr->validConfig)
{
this->dataPtr->Update(_ecm);
}
}
IGNITION_ADD_PLUGIN(LiftDrag,
ignition::gazebo::System,
LiftDrag::ISystemConfigure,
LiftDrag::ISystemPreUpdate)
IGNITION_ADD_PLUGIN_ALIAS(LiftDrag, "ignition::gazebo::systems::LiftDrag")