gazebo_magnetometer_plugin.h
4.01 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
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @brief Magnetometer Plugin
*
* This plugin simulates magnetometer data
*
* @author Elia Tarasov <elias.tarasov@gmail.com>
*/
#ifndef _GAZEBO_MAGNETOMETER_PLUGIN_HH_
#define _GAZEBO_MAGNETOMETER_PLUGIN_HH_
#include <random>
#include <string>
#include <Eigen/Core>
#include <MagneticField.pb.h>
#include <Groundtruth.pb.h>
#include <gazebo/common/common.hh>
#include <gazebo/common/Plugin.hh>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/transport/transport.hh>
#include <gazebo/msgs/msgs.hh>
#include <ignition/math.hh>
#include <common.h>
#include <geo_mag_declination.h>
namespace gazebo {
static constexpr auto kDefaultMagnetometerTopic = "mag";
static constexpr auto kDefaultPubRate = 100.0; // [Hz]. Note: corresponds to most of the mag devices supported in PX4
// Default values for use with ADIS16448 IMU
static constexpr auto kDefaultNoiseDensity = 0.4*1e-3; // [gauss / sqrt(hz)]
static constexpr auto kDefaultRandomWalk = 6.4*1e-6; // [gauss * sqrt(hz)]
static constexpr auto kDefaultBiasCorrelationTime = 6.0e+2; // [s]
typedef const boost::shared_ptr<const sensor_msgs::msgs::Groundtruth> GtPtr;
class MagnetometerPlugin : public ModelPlugin {
public:
MagnetometerPlugin();
virtual ~MagnetometerPlugin();
protected:
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf);
virtual void OnUpdate(const common::UpdateInfo&);
void addNoise(Eigen::Vector3d* magnetic_field, const double dt);
void GroundtruthCallback(GtPtr&);
void getSdfParams(sdf::ElementPtr sdf);
private:
std::string namespace_;
physics::ModelPtr model_;
physics::WorldPtr world_;
std::string mag_topic_;
transport::NodePtr node_handle_;
transport::PublisherPtr pub_mag_;
transport::SubscriberPtr gt_sub_;
std::string gt_sub_topic_;
double groundtruth_lat_rad_;
double groundtruth_lon_rad_;
event::ConnectionPtr update_connection_;
sensor_msgs::msgs::MagneticField mag_message_;
common::Time last_time_;
common::Time last_pub_time_;
unsigned int pub_rate_;
double noise_density_;
double random_walk_;
double bias_correlation_time_;
Eigen::Vector3d bias_;
std::default_random_engine random_generator_;
std::normal_distribution<double> standard_normal_distribution_;
}; // class MagnetometerPlugin
} // namespace gazebo
#endif // _GAZEBO_MAGNETOMETER_PLUGIN_HH_