mag_cal.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Daniel Koch and James Jackson, BYU MAGICC Lab.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
38 #ifndef ROSFLIGHT_SENSORS_CALBRATE_MAG_H
39 #define ROSFLIGHT_SENSORS_CALBRATE_MAG_H
40 
41 #include <ros/ros.h>
43 
44 #include <rosflight_msgs/ParamSet.h>
45 
46 #include <sensor_msgs/MagneticField.h>
47 
48 #include <eigen3/Eigen/Eigen>
49 #include <math.h>
50 #include <random>
51 
52 #include <boost/bind.hpp>
53 
55 #include <vector>
56 
57 namespace rosflight
58 {
59 
64 {
65 public:
66 
67  CalibrateMag();
68 
69  void run();
70 
74  void start_mag_calibration();
75 
76  void do_mag_calibration();
77 
82  bool mag_callback(const sensor_msgs::MagneticField::ConstPtr& mag);
83 
84  void set_reference_magnetic_field_strength(double reference_magnetic_field);
85 
90  bool is_calibrating() { return calibrating_; }
91 
93  const double a11() const { return A_(0, 0); }
94  const double a12() const { return A_(0, 1); }
95  const double a13() const { return A_(0, 2); }
96  const double a21() const { return A_(1, 0); }
97  const double a22() const { return A_(1, 1); }
98  const double a23() const { return A_(1, 2); }
99  const double a31() const { return A_(2, 0); }
100  const double a32() const { return A_(2, 1); }
101  const double a33() const { return A_(2, 2); }
102  const double bx() const { return b_(0, 0); }
103  const double by() const { return b_(1, 0); }
104  const double bz() const { return b_(2, 0); }
105 
106 private:
107  bool set_param(std::string name, double value);
108 
111 
113 
116 
117  Eigen::MatrixXd A_, b_;
118 
120 
122  bool first_time_;
124  double start_time_;
128  double inlier_thresh_;
129  Eigen::Vector3d measurement_prev_;
131 
132  // function to perform RANSAC on ellipsoid data
133  Eigen::MatrixXd ellipsoidRANSAC(EigenSTL::vector_Vector3d meas, int iters, double inlier_thresh);
134 
135  // function to vector from ellipsoid center to surface along input vector
136  Eigen::Vector3d intersect(Eigen::Vector3d r_m, Eigen::Vector3d r_e, Eigen::MatrixXd Q, Eigen::MatrixXd ub, double k);
137 
138  /*
139  sort eigenvalues and eigenvectors output from Eigen library
140  */
141  void eigSort(Eigen::MatrixXd &w, Eigen::MatrixXd &v);
142 
143  /*
144  This function gets ellipsoid parameters via least squares on ellipsoidal data
145  according to the paper: Li, Qingde, and John G. Griffiths. "Least squares ellipsoid
146  specific fitting." Geometric modeling and processing, 2004. proceedings. IEEE, 2004.
147  */
148  Eigen::MatrixXd ellipsoidLS(EigenSTL::vector_Vector3d meas);
149 
150  /*
151  This function compute magnetometer calibration parameters according to Section 5.3 of the
152  paper: Renaudin, Valérie, Muhammad Haris Afzal, and Gérard Lachapelle. "Complete triaxis
153  magnetometer calibration in the magnetic domain." Journal of sensors 2010 (2010).
154  */
155  void magCal(Eigen::MatrixXd u, Eigen::MatrixXd &A, Eigen::MatrixXd &bb);
156 };
157 
158 } // namespace mag_cal
159 
160 #endif // MAVROSFLIGHT_SENSORS_MAG_H
ros::NodeHandle nh_
Definition: mag_cal.h:109
const double a23() const
Definition: mag_cal.h:98
Eigen::Vector3d intersect(Eigen::Vector3d r_m, Eigen::Vector3d r_e, Eigen::MatrixXd Q, Eigen::MatrixXd ub, double k)
Definition: mag_cal.cpp:323
message_filters::Subscriber< sensor_msgs::MagneticField > mag_subscriber_
Definition: mag_cal.h:112
CalibrateMag sensor class.
Definition: mag_cal.h:63
Eigen::MatrixXd b_
Definition: mag_cal.h:117
std::vector< Eigen::Vector3d, Eigen::aligned_allocator< Eigen::Vector3d > > vector_Vector3d
const double a21() const
Definition: mag_cal.h:96
double calibration_time_
seconds to record data for temperature compensation
Definition: mag_cal.h:123
bool calibrating_
whether a temperature calibration is in progress
Definition: mag_cal.h:121
void set_reference_magnetic_field_strength(double reference_magnetic_field)
Definition: mag_cal.cpp:134
ros::NodeHandle nh_private_
Definition: mag_cal.h:110
bool first_time_
waiting for first measurement for calibration
Definition: mag_cal.h:122
const double bz() const
Definition: mag_cal.h:104
int ransac_iters_
number of ransac iterations to fit ellipsoid to mag measurements
Definition: mag_cal.h:125
Eigen::MatrixXd A_
Definition: mag_cal.h:117
const double a31() const
Definition: mag_cal.h:99
ros::ServiceServer mag_cal_srv_
Definition: mag_cal.h:114
Eigen::MatrixXd ellipsoidRANSAC(EigenSTL::vector_Vector3d meas, int iters, double inlier_thresh)
Definition: mag_cal.cpp:203
const double a32() const
Definition: mag_cal.h:100
bool set_param(std::string name, double value)
Definition: mag_cal.cpp:514
const double a13() const
Definition: mag_cal.h:95
const double by() const
Definition: mag_cal.h:103
ros::ServiceClient param_set_client_
Definition: mag_cal.h:115
Eigen::Vector3d measurement_prev_
Definition: mag_cal.h:129
const double a33() const
Definition: mag_cal.h:101
const double a11() const
The const stuff is to make it read-only.
Definition: mag_cal.h:93
double inlier_thresh_
threshold to consider measurement an inlier in ellipsoidRANSAC
Definition: mag_cal.h:128
double start_time_
timestamp of first calibration measurement
Definition: mag_cal.h:124
void magCal(Eigen::MatrixXd u, Eigen::MatrixXd &A, Eigen::MatrixXd &bb)
Definition: mag_cal.cpp:467
Eigen::MatrixXd ellipsoidLS(EigenSTL::vector_Vector3d meas)
Definition: mag_cal.cpp:393
bool is_calibrating()
Check if a calibration is in progress.
Definition: mag_cal.h:90
void start_mag_calibration()
Begin the magnetometer calibration routine.
Definition: mag_cal.cpp:139
bool mag_callback(const sensor_msgs::MagneticField::ConstPtr &mag)
set_refence_magnetic_field_strength
Definition: mag_cal.cpp:161
EigenSTL::vector_Vector3d measurements_
Definition: mag_cal.h:130
const double a12() const
Definition: mag_cal.h:94
double reference_field_strength_
the strength of earth&#39;s magnetic field at your location
Definition: mag_cal.h:119
void eigSort(Eigen::MatrixXd &w, Eigen::MatrixXd &v)
Definition: mag_cal.cpp:342
const double bx() const
Definition: mag_cal.h:102
const double a22() const
Definition: mag_cal.h:97


rosflight
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 20:00:12