gazebo_wind_plugin.cpp
7.62 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
/*
* Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
* Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
* Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
* Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
* Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
* Copyright 2016 Anton Matosov
*
* 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 "gazebo_wind_plugin.h"
#include "common.h"
namespace gazebo {
GazeboWindPlugin::~GazeboWindPlugin() {
update_connection_->~Connection();
}
void GazeboWindPlugin::Load(physics::WorldPtr world, sdf::ElementPtr sdf) {
world_ = world;
double wind_gust_start = kDefaultWindGustStart;
double wind_gust_duration = kDefaultWindGustDuration;
if (sdf->HasElement("robotNamespace")) {
namespace_ = sdf->GetElement("robotNamespace")->Get<std::string>();
} else {
gzerr << "[gazebo_wind_plugin] Please specify a robotNamespace.\n";
}
node_handle_ = transport::NodePtr(new transport::Node());
node_handle_->Init(namespace_);
if (sdf->HasElement("xyzOffset"))
xyz_offset_ = sdf->GetElement("xyzOffset")->Get<ignition::math::Vector3d>();
else
gzerr << "[gazebo_wind_plugin] Please specify a xyzOffset.\n";
getSdfParam<std::string>(sdf, "windPubTopic", wind_pub_topic_, wind_pub_topic_);
double pub_rate = 2.0;
getSdfParam<double>(sdf, "publishRate", pub_rate, pub_rate); //Wind topic publishing rates
pub_interval_ = (pub_rate > 0.0) ? 1/pub_rate : 0.0;
getSdfParam<std::string>(sdf, "frameId", frame_id_, frame_id_);
// Get the wind params from SDF.
getSdfParam<double>(sdf, "windVelocityMean", wind_velocity_mean_, wind_velocity_mean_);
getSdfParam<double>(sdf, "windVelocityMax", wind_velocity_max_, wind_velocity_max_);
getSdfParam<double>(sdf, "windVelocityVariance", wind_velocity_variance_, wind_velocity_variance_);
getSdfParam<ignition::math::Vector3d>(sdf, "windDirectionMean", wind_direction_mean_, wind_direction_mean_);
getSdfParam<double>(sdf, "windDirectionVariance", wind_direction_variance_, wind_direction_variance_);
// Get the wind gust params from SDF.
getSdfParam<double>(sdf, "windGustStart", wind_gust_start, wind_gust_start);
getSdfParam<double>(sdf, "windGustDuration", wind_gust_duration, wind_gust_duration);
getSdfParam<double>(sdf, "windGustVeloctiyMean", wind_gust_velocity_mean_, wind_gust_velocity_mean_);
getSdfParam<double>(sdf, "windGustVelocityMax", wind_gust_velocity_max_, wind_gust_velocity_max_);
getSdfParam<double>(sdf, "windGustVelocityVariance", wind_gust_velocity_variance_, wind_gust_velocity_variance_);
getSdfParam<ignition::math::Vector3d>(sdf, "windGustDirectionMean", wind_gust_direction_mean_, wind_gust_direction_mean_);
getSdfParam<double>(sdf, "windGustDirectionVariance", wind_gust_direction_variance_, wind_gust_direction_variance_);
wind_direction_mean_.Normalize();
wind_gust_direction_mean_.Normalize();
wind_gust_start_ = common::Time(wind_gust_start);
wind_gust_end_ = common::Time(wind_gust_start + wind_gust_duration);
// Set random wind velocity mean and standard deviation
wind_velocity_distribution_.param(std::normal_distribution<double>::param_type(wind_velocity_mean_, sqrt(wind_velocity_variance_)));
// Set random wind direction mean and standard deviation
wind_direction_distribution_X_.param(std::normal_distribution<double>::param_type(wind_direction_mean_.X(), sqrt(wind_direction_variance_)));
wind_direction_distribution_Y_.param(std::normal_distribution<double>::param_type(wind_direction_mean_.Y(), sqrt(wind_direction_variance_)));
wind_direction_distribution_Z_.param(std::normal_distribution<double>::param_type(wind_direction_mean_.Z(), sqrt(wind_direction_variance_)));
// Set random wind gust velocity mean and standard deviation
wind_gust_velocity_distribution_.param(std::normal_distribution<double>::param_type(wind_gust_velocity_mean_, sqrt(wind_gust_velocity_variance_)));
// Set random wind gust direction mean and standard deviation
wind_gust_direction_distribution_X_.param(std::normal_distribution<double>::param_type(wind_gust_direction_mean_.X(), sqrt(wind_gust_direction_variance_)));
wind_gust_direction_distribution_Y_.param(std::normal_distribution<double>::param_type(wind_gust_direction_mean_.Y(), sqrt(wind_gust_direction_variance_)));
wind_gust_direction_distribution_Z_.param(std::normal_distribution<double>::param_type(wind_gust_direction_mean_.Z(), sqrt(wind_gust_direction_variance_)));
// Listen to the update event. This event is broadcast every
// simulation iteration.
update_connection_ = event::Events::ConnectWorldUpdateBegin(boost::bind(&GazeboWindPlugin::OnUpdate, this, _1));
wind_pub_ = node_handle_->Advertise<physics_msgs::msgs::Wind>("~/" + wind_pub_topic_, 10);
#if GAZEBO_MAJOR_VERSION >= 9
last_time_ = world_->SimTime();
#else
last_time_ = world_->GetSimTime();
#endif
}
// This gets called by the world update start event.
void GazeboWindPlugin::OnUpdate(const common::UpdateInfo& _info) {
// Get the current simulation time.
#if GAZEBO_MAJOR_VERSION >= 9
common::Time now = world_->SimTime();
#else
common::Time now = world_->GetSimTime();
#endif
if ((now - last_time_).Double() < pub_interval_ || pub_interval_ == 0.0) {
return;
}
last_time_ = now;
// Calculate the wind force.
// Get normal distribution wind strength
double wind_strength = std::abs(wind_velocity_distribution_(wind_velocity_generator_));
wind_strength = (wind_strength > wind_velocity_max_) ? wind_velocity_max_ : wind_strength;
// Get normal distribution wind direction
ignition::math::Vector3d wind_direction;
wind_direction.X() = wind_direction_distribution_X_(wind_direction_generator_);
wind_direction.Y() = wind_direction_distribution_Y_(wind_direction_generator_);
wind_direction.Z() = wind_direction_distribution_Z_(wind_direction_generator_);
// Calculate total wind velocity
ignition::math::Vector3d wind = wind_strength * wind_direction;
ignition::math::Vector3d wind_gust(0, 0, 0);
// Calculate the wind gust velocity.
if (now >= wind_gust_start_ && now < wind_gust_end_) {
// Get normal distribution wind gust strength
double wind_gust_strength = std::abs(wind_gust_velocity_distribution_(wind_gust_velocity_generator_));
wind_gust_strength = (wind_gust_strength > wind_gust_velocity_max_) ? wind_gust_velocity_max_ : wind_gust_strength;
// Get normal distribution wind gust direction
ignition::math::Vector3d wind_gust_direction;
wind_gust_direction.X() = wind_gust_direction_distribution_X_(wind_gust_direction_generator_);
wind_gust_direction.Y() = wind_gust_direction_distribution_Y_(wind_gust_direction_generator_);
wind_gust_direction.Z() = wind_gust_direction_distribution_Z_(wind_gust_direction_generator_);
wind_gust = wind_gust_strength * wind_gust_direction;
}
gazebo::msgs::Vector3d* wind_v = new gazebo::msgs::Vector3d();
wind_v->set_x(wind.X() + wind_gust.X());
wind_v->set_y(wind.Y() + wind_gust.Y());
wind_v->set_z(wind.Z() + wind_gust.Z());
wind_msg.set_frame_id(frame_id_);
wind_msg.set_time_usec(now.Double() * 1e6);
wind_msg.set_allocated_velocity(wind_v);
wind_pub_->Publish(wind_msg);
}
GZ_REGISTER_WORLD_PLUGIN(GazeboWindPlugin);
}