-
Notifications
You must be signed in to change notification settings - Fork 799
/
liftdrag_plugin.cpp
470 lines (395 loc) · 15.2 KB
/
liftdrag_plugin.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
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
/*
* Copyright (C) 2014-2016 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 <algorithm>
#include <string>
#include "common.h"
#include "gazebo/common/Assert.hh"
#include "gazebo/physics/physics.hh"
#include "gazebo/sensors/SensorManager.hh"
#include "gazebo/transport/transport.hh"
#include "gazebo/msgs/msgs.hh"
#include "liftdrag_plugin/liftdrag_plugin.h"
#include "Force.pb.h"
using namespace gazebo;
GZ_REGISTER_MODEL_PLUGIN(LiftDragPlugin)
/////////////////////////////////////////////////
LiftDragPlugin::LiftDragPlugin() : cla(1.0), cda(0.01), cma(0.0), rho(1.2041)
{
this->cp = ignition::math::Vector3d(0, 0, 0);
this->forward = ignition::math::Vector3d(1, 0, 0);
this->upward = ignition::math::Vector3d(0, 0, 1);
this->wind_vel_ = ignition::math::Vector3d(0.0, 0.0, 0.0);
this->area = 1.0;
this->alpha0 = 0.0;
this->alpha = 0.0;
this->sweep = 0.0;
this->velocityStall = 0.0;
// 90 deg stall
this->alphaStall = 0.5*M_PI;
this->claStall = 0.0;
this->radialSymmetry = false;
/// \TODO: what's flat plate drag?
this->cdaStall = 1.0;
this->cmaStall = 0.0;
/// how much to change CL per every radian of the control joint value
this->controlJointRadToCL = 4.0;
// How much Cm changes with a change in control surface deflection angle
this->cm_delta = 0.0;
}
/////////////////////////////////////////////////
LiftDragPlugin::~LiftDragPlugin()
{
}
/////////////////////////////////////////////////
void LiftDragPlugin::Load(physics::ModelPtr _model,
sdf::ElementPtr _sdf)
{
GZ_ASSERT(_model, "LiftDragPlugin _model pointer is NULL");
GZ_ASSERT(_sdf, "LiftDragPlugin _sdf pointer is NULL");
this->model = _model;
this->sdf = _sdf;
this->world = this->model->GetWorld();
GZ_ASSERT(this->world, "LiftDragPlugin world pointer is NULL");
#if GAZEBO_MAJOR_VERSION >= 9
this->physics = this->world->Physics();
this->last_pub_time = this->world->SimTime();
#else
this->physics = this->world->GetPhysicsEngine();
this->last_pub_time = this->world->GetSimTime();
#endif
GZ_ASSERT(this->physics, "LiftDragPlugin physics pointer is NULL");
GZ_ASSERT(_sdf, "LiftDragPlugin _sdf pointer is NULL");
if (_sdf->HasElement("radial_symmetry"))
this->radialSymmetry = _sdf->Get<bool>("radial_symmetry");
if (_sdf->HasElement("a0"))
this->alpha0 = _sdf->Get<double>("a0");
if (_sdf->HasElement("cla"))
this->cla = _sdf->Get<double>("cla");
if (_sdf->HasElement("cda"))
this->cda = _sdf->Get<double>("cda");
if (_sdf->HasElement("cma"))
this->cma = _sdf->Get<double>("cma");
if (_sdf->HasElement("alpha_stall"))
this->alphaStall = _sdf->Get<double>("alpha_stall");
if (_sdf->HasElement("cla_stall"))
this->claStall = _sdf->Get<double>("cla_stall");
if (_sdf->HasElement("cda_stall"))
this->cdaStall = _sdf->Get<double>("cda_stall");
if (_sdf->HasElement("cma_stall"))
this->cmaStall = _sdf->Get<double>("cma_stall");
if (_sdf->HasElement("cm_delta"))
this->cm_delta = _sdf->Get<double>("cm_delta");
if (_sdf->HasElement("cp"))
this->cp = _sdf->Get<ignition::math::Vector3d>("cp");
// blade forward (-drag) direction in link frame
if (_sdf->HasElement("forward"))
this->forward = _sdf->Get<ignition::math::Vector3d>("forward");
this->forward.Normalize();
// blade upward (+lift) direction in link frame
if (_sdf->HasElement("upward"))
this->upward = _sdf->Get<ignition::math::Vector3d>("upward");
this->upward.Normalize();
if (_sdf->HasElement("area"))
this->area = _sdf->Get<double>("area");
if (_sdf->HasElement("air_density"))
this->rho = _sdf->Get<double>("air_density");
if (_sdf->HasElement("link_name"))
{
sdf::ElementPtr elem = _sdf->GetElement("link_name");
// GZ_ASSERT(elem, "Element link_name doesn't exist!");
std::string linkName = elem->Get<std::string>();
this->link = this->model->GetLink(linkName);
// GZ_ASSERT(this->link, "Link was NULL");
if (!this->link)
{
gzerr << "Link with name[" << linkName << "] not found. "
<< "The LiftDragPlugin will not generate forces\n";
}
else
{
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&LiftDragPlugin::OnUpdate, this));
}
}
if (_sdf->HasElement("robotNamespace"))
{
namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
} else {
gzerr << "[gazebo_liftdrag_plugin] Please specify a robotNamespace.\n";
}
node_handle_ = transport::NodePtr(new transport::Node());
node_handle_->Init(namespace_);
if (_sdf->HasElement("topic_name")) {
const auto lift_force_topic = this->sdf->Get<std::string>("topic_name");
lift_force_pub_ = node_handle_->Advertise<physics_msgs::msgs::Force>("~/" + lift_force_topic);
gzdbg << "Publishing to ~/" << lift_force_topic << std::endl;
}
if (_sdf->HasElement("windSubTopic")){
this->wind_sub_topic_ = _sdf->Get<std::string>("windSubTopic");
wind_sub_ = node_handle_->Subscribe("~/" + wind_sub_topic_, &LiftDragPlugin::WindVelocityCallback, this);
}
if (_sdf->HasElement("control_joint_name"))
{
std::string controlJointName = _sdf->Get<std::string>("control_joint_name");
this->controlJoint = this->model->GetJoint(controlJointName);
if (!this->controlJoint)
{
gzerr << "Joint with name[" << controlJointName << "] does not exist.\n";
}
}
if (_sdf->HasElement("control_joint_rad_to_cl"))
this->controlJointRadToCL = _sdf->Get<double>("control_joint_rad_to_cl");
}
/////////////////////////////////////////////////
void LiftDragPlugin::OnUpdate()
{
GZ_ASSERT(this->link, "Link was NULL");
// get linear velocity at cp in inertial frame
#if GAZEBO_MAJOR_VERSION >= 9
ignition::math::Vector3d vel = this->link->WorldLinearVel(this->cp) - wind_vel_;
const common::Time current_time = this->world->SimTime();
#else
ignition::math::Vector3d vel = ignitionFromGazeboMath(this->link->GetWorldLinearVel(this->cp)) - wind_vel_;
const common::Time current_time = this->world->GetSimTime();
#endif
ignition::math::Vector3d velI = vel;
velI.Normalize();
const double dt = (current_time - this->last_pub_time).Double();
if (vel.Length() <= 0.01)
return;
// pose of body
#if GAZEBO_MAJOR_VERSION >= 9
ignition::math::Pose3d pose = this->link->WorldPose();
#else
ignition::math::Pose3d pose = ignitionFromGazeboMath(this->link->GetWorldPose());
#endif
// rotate forward and upward vectors into inertial frame
ignition::math::Vector3d forwardI = pose.Rot().RotateVector(this->forward);
if (forwardI.Dot(vel) <= 0.0){
// Only calculate lift or drag if the wind relative velocity is in the same direction
return;
}
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 inertial frame
ignition::math::Vector3d 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);
this->sweep = asin(sinSweepAngle);
// truncate sweep to within +/-90 deg
while (fabs(this->sweep) > 0.5 * M_PI)
this->sweep = this->sweep > 0 ? this->sweep - M_PI
: this->sweep + M_PI;
// get cos from trig identity
double cosSweepAngle = sqrt(1.0 - sin(this->sweep) * sin(this->sweep));
// 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
ignition::math::Vector3d velInLDPlane = vel - vel.Dot(spanwiseI)*spanwiseI;
// get direction of drag
ignition::math::Vector3d dragDirection = -velInLDPlane;
dragDirection.Normalize();
// get direction of lift
ignition::math::Vector3d liftI = spanwiseI.Cross(velInLDPlane);
liftI.Normalize();
// get direction of moment
ignition::math::Vector3d momentDirection = spanwiseI;
// 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)
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?
if (liftI.Dot(forwardI) >= 0.0)
this->alpha = this->alpha0 + acos(cosAlpha);
else
this->alpha = this->alpha0 - acos(cosAlpha);
// normalize to within +/-90 deg
while (fabs(this->alpha) > 0.5 * M_PI)
this->alpha = this->alpha > 0 ? this->alpha - M_PI
: this->alpha + M_PI;
// compute dynamic pressure
double speedInLDPlane = velInLDPlane.Length();
double q = 0.5 * this->rho * speedInLDPlane * speedInLDPlane;
// compute cl at cp, check for stall, correct for sweep
double cl;
if (this->alpha > this->alphaStall)
{
cl = (this->cla * this->alphaStall +
this->claStall * (this->alpha - this->alphaStall))
* cosSweepAngle;
// make sure cl is still great than 0
cl = std::max(0.0, cl);
}
else if (this->alpha < -this->alphaStall)
{
cl = (-this->cla * this->alphaStall +
this->claStall * (this->alpha + this->alphaStall))
* cosSweepAngle;
// make sure cl is still less than 0
cl = std::min(0.0, cl);
}
else
cl = this->cla * this->alpha * cosSweepAngle;
// modify cl per control joint value
double controlAngle;
if (this->controlJoint)
{
#if GAZEBO_MAJOR_VERSION >= 9
controlAngle = this->controlJoint->Position(0);
#else
controlAngle = this->controlJoint->GetAngle(0).Radian();
#endif
cl = cl + this->controlJointRadToCL * controlAngle;
/// \TODO: also change 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 (this->alpha > this->alphaStall)
{
cd = (this->cda * this->alphaStall +
this->cdaStall * (this->alpha - this->alphaStall))
* cosSweepAngle;
}
else if (this->alpha < -this->alphaStall)
{
cd = (-this->cda * this->alphaStall +
this->cdaStall * (this->alpha + this->alphaStall))
* cosSweepAngle;
}
else
cd = (this->cda * this->alpha) * cosSweepAngle;
// make sure drag is positive
cd = 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 (this->alpha > this->alphaStall)
{
cm = (this->cma * this->alphaStall +
this->cmaStall * (this->alpha - this->alphaStall))
* cosSweepAngle;
// make sure cm is still great than 0
cm = std::max(0.0, cm);
}
else if (this->alpha < -this->alphaStall)
{
cm = (-this->cma * this->alphaStall +
this->cmaStall * (this->alpha + this->alphaStall))
* cosSweepAngle;
// make sure cm is still less than 0
cm = std::min(0.0, cm);
}
else
cm = this->cma * this->alpha * cosSweepAngle;
// Take into account the effect of control surface deflection angle to Cm
cm += this->cm_delta * controlAngle;
// compute moment (torque) at cp
ignition::math::Vector3d moment = cm * q * this->area * momentDirection;
// force about cg in inertial frame
ignition::math::Vector3d force = lift + drag;
// debug
//
// if ((this->link->GetName() == "wing_1" ||
// this->link->GetName() == "wing_2") &&
// (vel.Length() > 50.0 &&
// vel.Length() < 50.0))
if (0)
{
gzdbg << "=============================\n";
gzdbg << "sensor: [" << this->GetHandle() << "]\n";
gzdbg << "Link: [" << this->link->GetName()
<< "] pose: [" << pose
<< "] dynamic pressure: [" << q << "]\n";
gzdbg << "spd: [" << vel.Length()
<< "] vel: [" << vel << "]\n";
gzdbg << "LD plane spd: [" << velInLDPlane.Length()
<< "] vel : [" << velInLDPlane << "]\n";
gzdbg << "forward (inertial): " << forwardI << "\n";
gzdbg << "upward (inertial): " << upwardI << "\n";
gzdbg << "lift dir (inertial): " << liftI << "\n";
gzdbg << "Span direction (normal to LD plane): " << spanwiseI << "\n";
gzdbg << "sweep: " << this->sweep << "\n";
gzdbg << "alpha: " << this->alpha << "\n";
gzdbg << "lift: " << lift << "\n";
gzdbg << "drag: " << drag << " cd: "
<< cd << " cda: " << this->cda << "\n";
gzdbg << "moment: " << moment << "\n";
gzdbg << "force: " << force << "\n";
gzdbg << "moment: " << moment << "\n";
}
// Correct for nan or inf
force.Correct();
this->cp.Correct();
moment.Correct();
// apply forces at cg (with torques for position shift)
this->link->AddForceAtRelativePosition(force, this->cp);
this->link->AddTorque(moment);
auto relative_center = this->link->RelativePose().Pos() + this->cp;
// Publish force and center of pressure for potential visual plugin.
// - dt is used to control the rate at which the force is published
// - it only gets published if 'topic_name' is defined in the sdf
if (dt > 1.0 / 10 && this->sdf->HasElement("topic_name")) {
msgs::Vector3d* force_center_msg = new msgs::Vector3d;
force_center_msg->set_x(relative_center.X());
force_center_msg->set_y(relative_center.Y());
force_center_msg->set_z(relative_center.Z());
msgs::Vector3d* force_vector_msg = new msgs::Vector3d;
ignition::math::Quaterniond veh_q_world_to_body = pose.Rot();
auto force_in_body = veh_q_world_to_body.RotateVectorReverse(force);
// Transform force to link coordinates
force_vector_msg->set_x(force_in_body.X());
force_vector_msg->set_y(force_in_body.Y());
force_vector_msg->set_z(force_in_body.Z());
physics_msgs::msgs::Force force_msg;
force_msg.set_allocated_center(force_center_msg);
force_msg.set_allocated_force(force_vector_msg);
lift_force_pub_->Publish(force_msg);
this->last_pub_time = current_time;
}
}
void LiftDragPlugin::WindVelocityCallback(const boost::shared_ptr<const physics_msgs::msgs::Wind> &msg) {
wind_vel_ = ignition::math::Vector3d(msg->velocity().x(),
msg->velocity().y(),
msg->velocity().z());
}