MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/sensing/ins_complementary.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file ins_complementary.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Julien Lecoeur
00037  *
00038  * \brief This file performs the 3D position estimation, either by direct
00039  * integration or with correction with the GPS and pos_est->barometer
00040  *
00041  ******************************************************************************/
00042 
00043 
00044 #ifndef INS_COMPLEMENTARY_HPP__
00045 #define INS_COMPLEMENTARY_HPP__
00046 
00047 #include <cstdbool>
00048 
00049 #include "status/state.hpp"
00050 #include "drivers/gps.hpp"
00051 #include "drivers/barometer.hpp"
00052 #include "drivers/sonar.hpp"
00053 #include "drivers/px4flow.hpp"
00054 
00055 #include "sensing/ahrs.hpp"
00056 #include "sensing/ins.hpp"
00057 #include "util/coord_conventions.hpp"
00058 #include "util/constants.hpp"
00059 
00060 class INS_complementary: public INS
00061 {
00062 public:
00063 
00064     friend class Mavlink_waypoint_handler;
00065 
00069     struct conf_t
00070     {
00071         global_position_t origin;   
00072 
00073         float kp_gps_XY_pos;                    
00074         float kp_gps_Z_pos;                     
00075         float kp_gps_XY_vel;                    
00076         float kp_gps_Z_vel;                     
00077         float kp_gps_XY_pos_dgps;               
00078         float kp_gps_Z_pos_dgps;                
00079         float kp_gps_XY_vel_dgps;               
00080         float kp_gps_Z_vel_dgps;                
00081         float kp_gps_XY_pos_rtk;                
00082         float kp_gps_Z_pos_rtk;                 
00083         float kp_gps_XY_vel_rtk;                
00084         float kp_gps_Z_vel_rtk;                 
00085         float timeout_gps_us;                   
00086         uint32_t use_gps;                       
00087 
00088         float kp_sonar_alt;                     
00089         float kp_sonar_vel;                     
00090         float timeout_sonar_us;                 
00091         uint32_t use_sonar;                     
00092 
00093         float kp_baro_alt;                      
00094         float kp_baro_vel;                      
00095         float timeout_baro_us;                  
00096         uint32_t use_baro;                      
00097 
00098         float kp_flow_vel;                      
00099         float flow_gyro_comp_threshold;         
00100         float timeout_flow_us;                  
00101         uint32_t use_flow;                      
00102 
00103         float kp_acc_bias;                      
00104         uint32_t use_acc_bias;                  
00105     };
00106 
00107 
00108     enum fence_violation_state_t
00109     {
00110         IN_FENCE = 0,
00111         OUTSIDE_FENCE1,
00112         OUTSIDE_FENCE2
00113     };
00114 
00115 
00129     INS_complementary(State& state, const Barometer& barometer, const Sonar& sonar, const Gps& gps, const PX4Flow& flow, const AHRS& ahrs, const conf_t config = default_config());
00130 
00131 
00137     bool update(void);
00138 
00139 
00140     static inline conf_t default_config();
00141 
00147     float last_update_s(void) const;
00148 
00149 
00155     std::array<float,3> position_lf(void) const;
00156 
00157 
00163     std::array<float,3> velocity_lf(void) const;
00164 
00165 
00171     float absolute_altitude(void) const;
00172 
00173 
00181     bool is_healthy(INS::healthy_t type) const;
00182 
00183 
00184     local_position_t local_position_;        
00185     std::array<float,3> vel_;                
00186     std::array<float,3> acc_bias_;           
00187     conf_t config_;                          
00188 
00189 private:
00190     const AHRS& ahrs_;                       
00191     State& state_;                           
00192     const Gps& gps_;                         
00193     const Barometer& barometer_;             
00194     const Sonar& sonar_;                     
00195     const PX4Flow& flow_;                    
00196 
00197     float dt_s_;                            
00198     float last_update_s_;                   
00199     uint64_t last_gps_pos_update_us_;       
00200     uint64_t last_gps_vel_update_us_;       
00201     uint64_t last_barometer_update_us_;     
00202     uint64_t last_sonar_update_us_;         
00203     uint64_t last_flow_update_us_;          
00204 
00205     bool is_gps_pos_initialized_;           
00206     local_position_t fence_position_;        
00207 
00208     float barometer_bias_;                   
00209     bool is_barometer_calibrated_;           
00210 
00215     void reset_velocity_altitude(void);
00216 
00217 
00222     void integration(void);
00223 
00227     void correction_from_3d_pos(std::array<float,3> error, std::array<float,3> gain);
00228     void correction_from_3d_vel(std::array<float,3> error, std::array<float,3> gain);
00229     void correction_from_z_pos(float error, float gain);
00230     void correction_from_z_vel(float error, float gain);
00231 
00232 
00236     void correction_from_gps_pos(void);
00237 
00238 
00242     void correction_from_gps_vel(void);
00243 
00244 
00248     void correction_from_gps(void);
00249 
00250 
00254     void correction_from_barometer(void);
00255 
00256 
00260     void correction_from_sonar(void);
00261 
00262 
00266     void correction_from_flow(void);
00267 
00268 
00276     void check_first_gps_fix(void);
00277 
00278 
00284     void calibrate_barometer(void);
00285 
00286 };
00287 
00288 INS_complementary::conf_t INS_complementary::default_config()
00289 {
00290     conf_t conf = {};
00291 
00292     // default origin location (EFPL Esplanade)
00293     conf.origin        = ORIGIN_EPFL;
00294 
00295     // GPS with 3d fix
00296     conf.kp_gps_XY_pos  = 2.0f;
00297     conf.kp_gps_Z_pos   = 1.0f;
00298     conf.kp_gps_XY_vel  = 2.0f;
00299     conf.kp_gps_Z_vel   = 1.0f;
00300     conf.timeout_gps_us = 1e6f;
00301     conf.use_gps        = 1;
00302 
00303     // GPS with DGPS fix
00304     conf.kp_gps_XY_pos_dgps  = 20.0f;
00305     conf.kp_gps_Z_pos_dgps   = 10.0f;
00306     conf.kp_gps_XY_vel_dgps  = 20.0f;
00307     conf.kp_gps_Z_vel_dgps   = 10.0f;
00308 
00309     // GPS with RTK fix
00310     conf.kp_gps_XY_pos_rtk  = 200.0f;
00311     conf.kp_gps_Z_pos_rtk   = 100.0f;
00312     conf.kp_gps_XY_vel_rtk  = 200.0f;
00313     conf.kp_gps_Z_vel_rtk   = 100.0f;
00314 
00315 
00316     // Barometer
00317     conf.kp_baro_alt     = 2.0f;
00318     conf.kp_baro_vel     = 0.5f;
00319     conf.timeout_baro_us = 1e6f;
00320     conf.use_baro        = 1;
00321 
00322     // Sonar
00323     conf.kp_sonar_alt       = 5.0f;
00324     conf.kp_sonar_vel       = 3.0f;
00325     conf.timeout_sonar_us   = 1e6f;
00326     conf.use_sonar          = 1;
00327 
00328     // Optic flow
00329     conf.kp_flow_vel                = 4.0f;
00330     conf.flow_gyro_comp_threshold   = 0.5f;
00331     conf.timeout_flow_us            = 1e6f;
00332     conf.use_flow                   = 1;
00333 
00334     // Accelerometer bias
00335     conf.kp_acc_bias     = 0.001f;
00336     conf.use_acc_bias    = 0;
00337 
00338     return conf;
00339 };
00340 
00341 #endif // INS_COMPLEMENTARY_HPP__
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines