PX4 Firmware
PX4 Autopilot Software http://px4.io
LandDetector.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-2017 PX4 Development Team. 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
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file LandDetector.h
36  * Land detector interface for multicopter, fixedwing and VTOL implementations.
37  *
38  * @author Johan Jansen <jnsn.johan@gmail.com>
39  * @author Julian Oes <julian@oes.ch>
40  * @author Lorenz Meier <lorenz@px4.io>
41  */
42 
43 #pragma once
44 
45 #include <float.h>
47 #include <math.h>
48 #include <perf/perf_counter.h>
49 #include <px4_platform_common/px4_config.h>
50 #include <px4_platform_common/defines.h>
51 #include <px4_platform_common/module.h>
52 #include <px4_platform_common/module_params.h>
53 #include <px4_platform_common/module.h>
54 #include <px4_platform_common/module_params.h>
55 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
56 #include <uORB/Subscription.hpp>
57 #include <uORB/Publication.hpp>
64 
65 using namespace time_literals;
66 
67 namespace land_detector
68 {
69 
70 class LandDetector : public ModuleBase<LandDetector>, ModuleParams, px4::ScheduledWorkItem
71 {
72 public:
73 
74  LandDetector();
75  virtual ~LandDetector();
76 
77 
78  /** @see ModuleBase */
79  static int custom_command(int argc, char *argv[])
80  {
81  return print_usage("unknown command");
82  }
83 
84  /** @see ModuleBase */
85  static int print_usage(const char *reason = nullptr);
86 
87  /** @see ModuleBase::print_status() */
88  int print_status() override;
89 
90  /**
91  * Get the work queue going.
92  */
93  void start();
94 
95  static int task_spawn(int argc, char *argv[]);
96 
97 protected:
98 
99  /**
100  * Updates parameters.
101  */
102  virtual void _update_params();
103 
104  /**
105  * Updates subscribed uORB topics.
106  */
107  virtual void _update_topics();
108 
109  /**
110  * @return true if UAV is in a landed state.
111  */
112  virtual bool _get_landed_state() = 0;
113 
114  /**
115  * @return true if UAV is in almost landed state
116  */
117  virtual bool _get_maybe_landed_state() { return false; }
118 
119  /**
120  * @return true if UAV is touching ground but not landed
121  */
122  virtual bool _get_ground_contact_state() { return false; }
123 
124  /**
125  * @return true if UAV is in free-fall state.
126  */
127  virtual bool _get_freefall_state() { return false; }
128 
129  /**
130  * @return maximum altitude that can be reached
131  */
132  virtual float _get_max_altitude() { return INFINITY; }
133 
134  /**
135  * @return true if vehicle could be in ground effect (close to ground)
136  */
137  virtual bool _get_ground_effect_state() { return false; }
138 
139  /** Run main land detector loop at this interval. */
140  static constexpr uint32_t LAND_DETECTOR_UPDATE_INTERVAL = 20_ms;
141 
142  systemlib::Hysteresis _freefall_hysteresis{false};
143  systemlib::Hysteresis _landed_hysteresis{true};
144  systemlib::Hysteresis _maybe_landed_hysteresis{true};
145  systemlib::Hysteresis _ground_contact_hysteresis{true};
146  systemlib::Hysteresis _ground_effect_hysteresis{false};
147 
148  actuator_armed_s _actuator_armed{};
149  vehicle_acceleration_s _vehicle_acceleration{};
150 
151  vehicle_land_detected_s _land_detected = {
152  .timestamp = 0,
153  .alt_max = -1.0f,
154  .freefall = false,
155  .ground_contact = true,
156  .maybe_landed = true,
157  .landed = true,
158  };
159 
160  vehicle_local_position_s _vehicle_local_position{};
161 
162  uORB::Publication<vehicle_land_detected_s> _vehicle_land_detected_pub{ORB_ID(vehicle_land_detected)};
163 
164 private:
165 
166  void Run() override;
167 
168  void _update_state();
169 
170  void _update_total_flight_time();
171 
172  bool _previous_armed_state{false}; ///< stores the previous actuator_armed.armed state
173 
174  hrt_abstime _takeoff_time{0};
175  hrt_abstime _total_flight_time{0}; ///< total vehicle flight time in microseconds
176 
177  perf_counter_t _cycle_perf{perf_alloc(PC_ELAPSED, "land_detector_cycle")};
178 
179  uORB::Subscription _actuator_armed_sub{ORB_ID(actuator_armed)};
180  uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)};
181  uORB::Subscription _vehicle_acceleration_sub{ORB_ID(vehicle_acceleration)};
182  uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)};
183 
184  DEFINE_PARAMETERS_CUSTOM_PARENT(
185  ModuleParams,
186  (ParamInt<px4::params::LND_FLIGHT_T_HI>) _param_total_flight_time_high,
187  (ParamInt<px4::params::LND_FLIGHT_T_LO>) _param_total_flight_time_low
188  );
189 };
190 
191 } // namespace land_detector
virtual float _get_max_altitude()
Definition: LandDetector.h:132
measure the time elapsed performing an event
Definition: perf_counter.h:56
void print_status()
Definition: Commander.cpp:517
static void print_usage()
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
Header common to all counters.
#define perf_alloc(a, b)
Definition: px4io.h:59
virtual bool _get_ground_contact_state()
Definition: LandDetector.h:122
static int custom_command(int argc, char *argv[])
Definition: LandDetector.h:79
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
static int start()
Definition: dataman.cpp:1452
Hysteresis of a boolean value.
virtual bool _get_ground_effect_state()
Definition: LandDetector.h:137
virtual bool _get_maybe_landed_state()
Definition: LandDetector.h:117
virtual bool _get_freefall_state()
Definition: LandDetector.h:127
Performance measuring tools.