PX4 Firmware
PX4 Autopilot Software http://px4.io
FailureDetector.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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 FailureDetector.cpp
36 *
37 * @author Mathieu Bresciani <brescianimathieu@gmail.com>
38 *
39 */
40 
41 #include "FailureDetector.hpp"
42 
43 FailureDetector::FailureDetector(ModuleParams *parent) :
44  ModuleParams(parent)
45 {
46 }
47 
49 {
50  bool status_changed = _status != FAILURE_NONE;
52 
53  return status_changed;
54 }
55 
56 bool
58 {
59  bool updated(false);
60 
61  if (isAttitudeStabilized(vehicle_status)) {
62  updated = updateAttitudeStatus();
63 
64  } else {
65  updated = resetStatus();
66  }
67 
68  return updated;
69 }
70 
71 bool
73 {
74  bool attitude_is_stabilized{false};
75  const uint8_t vehicle_type = vehicle_status.vehicle_type;
76  const uint8_t nav_state = vehicle_status.nav_state;
77 
78  if (vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROTARY_WING) {
79  attitude_is_stabilized = nav_state != vehicle_status_s::NAVIGATION_STATE_ACRO &&
80  nav_state != vehicle_status_s::NAVIGATION_STATE_RATTITUDE;
81 
82  } else if (vehicle_type == vehicle_status_s::VEHICLE_TYPE_FIXED_WING) {
83  attitude_is_stabilized = nav_state != vehicle_status_s::NAVIGATION_STATE_MANUAL &&
84  nav_state != vehicle_status_s::NAVIGATION_STATE_ACRO &&
85  nav_state != vehicle_status_s::NAVIGATION_STATE_RATTITUDE;
86  }
87 
88  return attitude_is_stabilized;
89 }
90 
91 bool
93 {
94  bool updated(false);
96 
97  if (_sub_vehicule_attitude.update(&attitude)) {
98 
99  const matrix::Eulerf euler(matrix::Quatf(attitude.q));
100  const float roll(euler.phi());
101  const float pitch(euler.theta());
102 
103  const float max_roll_deg = _param_fd_fail_r.get();
104  const float max_pitch_deg = _param_fd_fail_p.get();
105  const float max_roll(fabsf(math::radians(max_roll_deg)));
106  const float max_pitch(fabsf(math::radians(max_pitch_deg)));
107 
108  const bool roll_status = (max_roll > 0.0f) && (fabsf(roll) > max_roll);
109  const bool pitch_status = (max_pitch > 0.0f) && (fabsf(pitch) > max_pitch);
110 
111  hrt_abstime time_now = hrt_absolute_time();
112 
113  // Update hysteresis
114  _roll_failure_hysteresis.set_hysteresis_time_from(false, (hrt_abstime)(1e6f * _param_fd_fail_r_ttri.get()));
115  _pitch_failure_hysteresis.set_hysteresis_time_from(false, (hrt_abstime)(1e6f * _param_fd_fail_p_ttri.get()));
116  _roll_failure_hysteresis.set_state_and_update(roll_status, time_now);
117  _pitch_failure_hysteresis.set_state_and_update(pitch_status, time_now);
118 
119  // Update bitmask
121 
124  }
125 
128  }
129 
130  updated = true;
131  }
132 
133  return updated;
134 }
#define VEHICLE_TYPE_FIXED_WING
Base class for failure detection logic based on vehicle states for failsafe triggering.
Type theta() const
Definition: Euler.hpp:132
systemlib::Hysteresis _pitch_failure_hysteresis
Type phi() const
Definition: Euler.hpp:128
Quaternion class.
Definition: Dcm.hpp:24
void set_state_and_update(const bool new_state, const hrt_abstime &now_us)
Definition: hysteresis.cpp:57
bool get_state() const
Definition: hysteresis.h:60
constexpr T radians(T degrees)
Definition: Limits.hpp:85
Euler angles class.
Definition: AxisAngle.hpp:18
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
DEFINE_PARAMETERS((ParamInt< px4::params::FD_FAIL_P >) _param_fd_fail_p,(ParamInt< px4::params::FD_FAIL_R >) _param_fd_fail_r,(ParamFloat< px4::params::FD_FAIL_R_TTRI >) _param_fd_fail_r_ttri,(ParamFloat< px4::params::FD_FAIL_P_TTRI >) _param_fd_fail_p_ttri) uORB uint8_t _status
void set_hysteresis_time_from(const bool from_state, const hrt_abstime new_hysteresis_time_us)
Definition: hysteresis.cpp:46
bool isAttitudeStabilized(const vehicle_status_s &vehicle_status)
FailureDetector(ModuleParams *parent)
bool update(const vehicle_status_s &vehicle_status)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
systemlib::Hysteresis _roll_failure_hysteresis