MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/drones/mav.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 mav.hpp
00034  *
00035  * \author MAV'RIC Team
00036  *
00037  * \brief MAV class
00038  *
00039  ******************************************************************************/
00040 
00041 
00042 #ifndef MAV_HPP_
00043 #define MAV_HPP_
00044 
00045 #include <cstdbool>
00046 #include <cstdint>
00047 
00048 
00049 #include "communication/data_logging.hpp"
00050 #include "communication/hud_telemetry.hpp"
00051 #include "communication/mavlink_communication.hpp"
00052 #include "communication/mavlink_stream.hpp"
00053 #include "communication/mavlink_waypoint_handler.hpp"
00054 #include "communication/onboard_parameters.hpp"
00055 
00056 #include "flight_controller/flight_controller_copter.hpp"
00057 #include "control/position_controller.hpp"
00058 #include "control/velocity_controller_copter.hpp"
00059 #include "control/attitude_controller.hpp"
00060 #include "control/rate_controller.hpp"
00061 #include "control/servos_mix_matrix.hpp"
00062 
00063 #include "drivers/battery.hpp"
00064 #include "drivers/gps.hpp"
00065 #include "drivers/gps_mocap.hpp"
00066 #include "drivers/gps_hub.hpp"
00067 #include "drivers/sonar.hpp"
00068 #include "drivers/servos_telemetry.hpp"
00069 #include "drivers/state_display.hpp"
00070 #include "drivers/px4flow.hpp"
00071 
00072 #include "hal/common/file.hpp"
00073 #include "hal/common/led.hpp"
00074 
00075 #include "manual_control/manual_control.hpp"
00076 #include "manual_control/remote_default_config.hpp"
00077 
00078 #include "mission/mission_planner.hpp"
00079 #include "mission/mission_handler_critical_landing.hpp"
00080 #include "mission/mission_handler_critical_navigating.hpp"
00081 #include "mission/mission_handler_hold_position.hpp"
00082 #include "mission/mission_handler_landing.hpp"
00083 #include "mission/mission_handler_manual.hpp"
00084 #include "mission/mission_handler_navigating.hpp"
00085 #include "mission/mission_handler_on_ground.hpp"
00086 #include "mission/mission_handler_takeoff.hpp"
00087 
00088 #include "navigation/navigation_directto.hpp"
00089 #include "navigation/vector_field_waypoint.hpp"
00090 
00091 #include "sensing/ahrs.hpp"
00092 #include "sensing/ahrs_ekf.hpp"
00093 #include "sensing/ahrs_qfilter.hpp"
00094 #include "sensing/altitude_estimation.hpp"
00095 #include "sensing/imu.hpp"
00096 #include "sensing/ins_complementary.hpp"
00097 #include "sensing/ins_kf.hpp"
00098 #include "sensing/ahrs_ekf_mocap.hpp"
00099 
00100 #include "simulation/simulation.hpp"
00101 
00102 #include "status/geofence.hpp"
00103 #include "status/geofence_cylinder.hpp"
00104 #include "status/state.hpp"
00105 #include "status/state_machine.hpp"
00106 
00107 #include "util/coord_conventions.hpp"
00108 #include "util/print_util.hpp"
00109 
00110 extern "C"
00111 {
00112 #include "sensing/altitude.h"
00113 }
00114 
00115 
00119 class MAV
00120 {
00121 public:
00122     static const uint32_t N_TELEM  = 30;
00123     static const uint32_t N_MSG_CB = 20;
00124     static const uint32_t N_CMD_CB = 20;
00125     static const uint32_t N_PARAM  = 120;
00126     typedef Mavlink_communication_T<N_TELEM, N_MSG_CB, N_CMD_CB, N_PARAM> Mavlink_communication;
00127 
00128 
00132      struct conf_t
00133     {
00134         State::conf_t state_config;
00135         Data_logging::conf_t data_logging_continuous_config;
00136         Data_logging::conf_t data_logging_stat_config;
00137         Scheduler::conf_t scheduler_config;
00138         Mavlink_communication::conf_t mavlink_communication_config;
00139         Mavlink_waypoint_handler::conf_t waypoint_handler_config;
00140         Mission_planner::conf_t mission_planner_config;
00141         Mission_handler_landing::conf_t mission_handler_landing_config;
00142         AHRS_qfilter::conf_t qfilter_config;
00143         AHRS_ekf::conf_t ahrs_ekf_config;
00144         INS_complementary::conf_t ins_complementary_config;
00145         Manual_control::conf_t manual_control_config;
00146         remote_conf_t remote_config;
00147         Geofence_cylinder::conf_t safety_geofence_config;
00148         Geofence_cylinder::conf_t emergency_geofence_config;
00149     };
00150 
00158     static inline conf_t default_config(uint8_t sysid = 1);
00159 
00160 
00168     static inline conf_t dronedome_config(uint8_t sysid = 1);
00169 
00170 
00174     MAV(    Imu& imu,
00175             Barometer& barometer,
00176             Gps& gps,
00177             Sonar& sonar,
00178             PX4Flow& flow,
00179             Serial& serial_mavlink,
00180             Satellite& satellite,
00181             State_display& state_display,
00182             File& file_flash,
00183             Battery& battery,
00184             File& file1,
00185             File& file2,
00186             Flight_controller& flight_controller,
00187             const conf_t& config = default_config());
00188 
00189     /*
00190      * \brief   Initializes MAV
00191      * \details  Calls all init functions (init_*());
00192      *
00193      * \return  success
00194      */
00195     virtual bool init(void);
00196 
00201     void loop(void);
00202 
00203 
00210     inline Mavlink_communication& get_communication(){return communication;};
00211 
00212 
00219     inline Scheduler& get_scheduler(){return scheduler;};
00220 
00221 
00222 protected:
00223 
00224     virtual bool init_main_task(void);
00225     virtual bool init_state(void);
00226     virtual bool init_communication(void);
00227     virtual bool init_data_logging(void);
00228     virtual bool init_gps(void);
00229     virtual bool init_imu(void);
00230     virtual bool init_barometer(void);
00231     virtual bool init_sonar(void);
00232     virtual bool init_ahrs(void);
00233     virtual bool init_ins(void);
00234     virtual bool init_mocap(void);
00235     virtual bool init_flow(void);
00236     virtual bool init_controller(void) = 0;
00237     virtual bool init_mission_planning(void);
00238     virtual bool init_hud(void);
00239     // virtual bool init_servos(void);
00240     virtual bool init_ground_control(void);
00241 
00242     virtual bool main_task(void);
00243     static inline bool main_task_func(MAV* mav)
00244     {
00245         return mav->main_task();
00246     };
00247 
00248     Imu&            imu;                
00249     Barometer&      barometer;          
00250     Gps&            gps;                
00251     Sonar&          sonar;              
00252     PX4Flow&        flow;               
00253     Serial&         serial_mavlink;     
00254     Satellite&      satellite;          
00255     State_display&  state_display_;     
00256     File&           file_flash;         
00257     Battery&        battery;            
00258 
00259     // Motion capture, position
00260     Gps_mocap       gps_mocap;          
00261     Gps_hub<2>      gps_hub;            
00262     Ahrs_ekf_mocap  ahrs_ekf_mocap;     
00263 
00264     Manual_control manual_control;                              
00265 
00266     State state;                                                
00267 
00268     Scheduler_T<20>       scheduler;
00269     Mavlink_communication   communication;
00270 
00271     AHRS&           ahrs_;              
00272     AHRS_ekf        ahrs_ekf;
00273     AHRS_qfilter    ahrs_qfilter;
00274 
00275     INS&                ins_;                                   
00276     INS_complementary   ins_complementary;                      
00277     INS_kf              ins_kf;                                 
00278 
00279     Flight_controller&    flight_controller_;
00280 
00281     Mavlink_waypoint_handler waypoint_handler;                  
00282 
00283     Mission_handler_registry mission_handler_registry;          
00284 
00285     Mission_handler_hold_position       hold_position_handler;
00286     Mission_handler_landing             landing_handler;
00287     Mission_handler_navigating          navigating_handler;
00288     Mission_handler_on_ground           on_ground_handler;
00289     Mission_handler_manual              manual_ctrl_handler;
00290     Mission_handler_takeoff             takeoff_handler;
00291     Mission_handler_critical_landing    critical_landing_handler;
00292     Mission_handler_critical_navigating critical_navigating_handler;
00293 
00294     Mission_planner mission_planner_;                            
00295 
00296     Geofence_cylinder safety_geofence_;                         
00297     Geofence_cylinder emergency_geofence_;                      
00298 
00299     State_machine state_machine;                                
00300 
00301     hud_telemetry_t hud;                                        
00302     servos_telemetry_t servos_telemetry;
00303 
00304     Data_logging_T<10>    data_logging_continuous;
00305     Data_logging_T<10>    data_logging_stat;
00306 
00307     uint8_t sysid_;    
00308     conf_t config_;    
00309 };
00310 
00311 
00312 MAV::conf_t MAV::default_config(uint8_t sysid)
00313 {
00314     conf_t conf                                                = {};
00315 
00316     conf.state_config = State::default_config();
00317 
00318     conf.data_logging_continuous_config                  = Data_logging::default_config();
00319     conf.data_logging_continuous_config.continuous_write = true;
00320     conf.data_logging_continuous_config.log_data         = 0;
00321 
00322     conf.data_logging_stat_config                  = Data_logging::default_config();
00323     conf.data_logging_stat_config.continuous_write = false;
00324     conf.data_logging_stat_config.log_data         = 0;
00325 
00326     conf.scheduler_config = Scheduler::default_config();
00327 
00328     conf.waypoint_handler_config = Mavlink_waypoint_handler::default_config();
00329 
00330     conf.mission_planner_config = Mission_planner::default_config();
00331     conf.mission_handler_landing_config = Mission_handler_landing::default_config();
00332 
00333     conf.ahrs_ekf_config = AHRS_ekf::default_config();
00334     conf.qfilter_config = AHRS_qfilter::default_config();
00335 
00336     conf.ins_complementary_config = INS_complementary::default_config();
00337 
00338     conf.manual_control_config = Manual_control::default_config();
00339 
00340     conf.remote_config = remote_default_config();
00341 
00342     conf.safety_geofence_config     = Geofence_cylinder::default_config();
00343     conf.emergency_geofence_config  = Geofence_cylinder::default_config();
00344     conf.emergency_geofence_config.radius = 1000.0f;
00345     conf.emergency_geofence_config.height = 500.0f;
00346 
00347     /* Mavlink communication config */
00348     conf.mavlink_communication_config                        = Mavlink_communication::default_config(sysid);
00349     conf.mavlink_communication_config.handler.debug          = false;
00350     conf.mavlink_communication_config.parameters.debug       = true;
00351     conf.mavlink_communication_config.mavlink_stream.debug   = false;
00352 
00353     return conf;
00354 };
00355 
00356 
00357 MAV::conf_t MAV::dronedome_config(uint8_t sysid)
00358 {
00359     conf_t conf                                                = {};
00360 
00361     conf = MAV::default_config(sysid);
00362 
00363     // adapt gains for the drone dome
00364     conf.ins_complementary_config.kp_baro_alt = 0.0f;
00365     conf.ins_complementary_config.kp_baro_vel = 0.0f;
00366     conf.ins_complementary_config.kp_sonar_alt = 0.0f;
00367     conf.ins_complementary_config.kp_sonar_vel = 0.0f;
00368 
00369     conf.mission_handler_landing_config.desc_to_ground_altitude = -1.0f;
00370     conf.mission_planner_config.safe_altitude                   =  -3.0f;
00371     conf.mission_planner_config.critical_landing_altitude       =  -2.0f;
00372     conf.mission_planner_config.takeoff_altitude                =  -2.0f;
00373 
00374     return conf;
00375 }
00376 
00377 #endif /* MAV_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines