PX4 Firmware
PX4 Autopilot Software http://px4.io
land_detector_main.cpp
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 land_detector_main.cpp
36  * Land detection algorithm
37  *
38  * @author Johan Jansen <jnsn.johan@gmail.com>
39  * @author Lorenz Meier <lorenz@px4.io>
40  */
41 
42 #include <drivers/drv_hrt.h>
43 #include <px4_platform_common/px4_config.h>
44 #include <px4_platform_common/defines.h>
45 #include <px4_platform_common/posix.h>
46 #include <px4_platform_common/tasks.h>
47 
48 #include "FixedwingLandDetector.h"
50 #include "RoverLandDetector.h"
51 #include "VtolLandDetector.h"
52 
53 
54 namespace land_detector
55 {
56 
57 extern "C" __EXPORT int land_detector_main(int argc, char *argv[]);
58 
59 static char _currentMode[12];
60 
61 int LandDetector::task_spawn(int argc, char *argv[])
62 {
63  if (argc < 2) {
64  print_usage();
65  return PX4_ERROR;
66  }
67 
68  LandDetector *obj = nullptr;
69 
70  if (strcmp(argv[1], "fixedwing") == 0) {
71  obj = new FixedwingLandDetector();
72 
73  } else if (strcmp(argv[1], "multicopter") == 0) {
74  obj = new MulticopterLandDetector();
75 
76  } else if (strcmp(argv[1], "vtol") == 0) {
77  obj = new VtolLandDetector();
78 
79  } else if (strcmp(argv[1], "rover") == 0) {
80  obj = new RoverLandDetector();
81 
82  } else {
83  print_usage("unknown mode");
84  return PX4_ERROR;
85  }
86 
87  if (obj == nullptr) {
88  PX4_ERR("alloc failed");
89  return PX4_ERROR;
90  }
91 
92  // Remember current active mode
93  strncpy(_currentMode, argv[1], sizeof(_currentMode) - 1);
94  _currentMode[sizeof(_currentMode) - 1] = '\0';
95 
96  _object.store(obj);
97  _task_id = task_id_is_work_queue;
98 
99  obj->start();
100 
101  return PX4_OK;
102 }
103 
105 {
106  PX4_INFO("running (%s)", _currentMode);
107  return 0;
108 }
109 int LandDetector::print_usage(const char *reason)
110 {
111  if (reason != nullptr) {
112  PX4_ERR("%s\n", reason);
113  }
114 
115  PRINT_MODULE_DESCRIPTION(
116  R"DESCR_STR(
117 ### Description
118 Module to detect the freefall and landed state of the vehicle, and publishing the `vehicle_land_detected` topic.
119 Each vehicle type (multirotor, fixedwing, vtol, ...) provides its own algorithm, taking into account various
120 states, such as commanded thrust, arming state and vehicle motion.
121 
122 ### Implementation
123 Every type is implemented in its own class with a common base class. The base class maintains a state (landed,
124 maybe_landed, ground_contact). Each possible state is implemented in the derived classes. A hysteresis and a fixed
125 priority of each internal state determines the actual land_detector state.
126 
127 #### Multicopter Land Detector
128 **ground_contact**: thrust setpoint and velocity in z-direction must be below a defined threshold for time
129 GROUND_CONTACT_TRIGGER_TIME_US. When ground_contact is detected, the position controller turns off the thrust setpoint
130 in body x and y.
131 
132 **maybe_landed**: it requires ground_contact together with a tighter thrust setpoint threshold and no velocity in the
133 horizontal direction. The trigger time is defined by MAYBE_LAND_TRIGGER_TIME. When maybe_landed is detected, the
134 position controller sets the thrust setpoint to zero.
135 
136 **landed**: it requires maybe_landed to be true for time LAND_DETECTOR_TRIGGER_TIME_US.
137 
138 The module runs periodically on the HP work queue.
139 )DESCR_STR");
140 
141  PRINT_MODULE_USAGE_NAME("land_detector", "system");
142  PRINT_MODULE_USAGE_COMMAND_DESCR("start", "Start the background task");
143  PRINT_MODULE_USAGE_ARG("fixedwing|multicopter|vtol|rover", "Select vehicle type", false);
144  PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
145  return 0;
146 }
147 
148 
149 int land_detector_main(int argc, char *argv[])
150 {
151  return LandDetector::main(argc, argv);
152 }
153 
154 } // namespace land_detector
int main(int argc, char **argv)
Definition: main.cpp:3
Definition: I2C.hpp:51
Land detector implementation for fixedwing.
High-resolution timer with callouts and timekeeping.
Land detection implementation for multicopters.
__EXPORT int land_detector_main(int argc, char *argv[])
static int print_usage(const char *reason=nullptr)
Land detection implementation for VTOL also called hybrids.
Land detection implementation for VTOL also called hybrids.
static int task_spawn(int argc, char *argv[])
static char _currentMode[12]
void start()
Get the work queue going.