PX4 Firmware
PX4 Autopilot Software http://px4.io
Commander.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 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 #ifndef COMMANDER_HPP_
35 #define COMMANDER_HPP_
36 
37 #include "state_machine_helper.h"
39 
41 #include <lib/mathlib/mathlib.h>
42 #include <px4_platform_common/module.h>
43 #include <px4_platform_common/module_params.h>
45 
46 // publications
47 #include <uORB/Publication.hpp>
55 #include <uORB/topics/test_motor.h>
56 
57 // subscriptions
58 #include <uORB/Subscription.hpp>
59 #include <uORB/topics/airspeed.h>
70 #include <uORB/topics/esc_status.h>
71 
72 using math::constrain;
73 
74 using namespace time_literals;
75 
76 class Commander : public ModuleBase<Commander>, public ModuleParams
77 {
78 public:
79  Commander();
80 
81  /** @see ModuleBase */
82  static int task_spawn(int argc, char *argv[]);
83 
84  /** @see ModuleBase */
85  static Commander *instantiate(int argc, char *argv[]);
86 
87  /** @see ModuleBase */
88  static int custom_command(int argc, char *argv[]);
89 
90  /** @see ModuleBase */
91  static int print_usage(const char *reason = nullptr);
92 
93  /** @see ModuleBase::run() */
94  void run() override;
95 
96  void enable_hil();
97 
98  // TODO: only temporarily static until low priority thread is removed
99  static bool preflight_check(bool report);
100 
101  void get_circuit_breaker_params();
102 
103 private:
104 
106 
107  (ParamInt<px4::params::NAV_DLL_ACT>) _param_nav_dll_act,
108  (ParamInt<px4::params::COM_DL_LOSS_T>) _param_com_dl_loss_t,
109 
110  (ParamInt<px4::params::COM_HLDL_LOSS_T>) _param_com_hldl_loss_t,
111  (ParamInt<px4::params::COM_HLDL_REG_T>) _param_com_hldl_reg_t,
112 
113  (ParamInt<px4::params::NAV_RCL_ACT>) _param_nav_rcl_act,
114  (ParamFloat<px4::params::COM_RC_LOSS_T>) _param_com_rc_loss_t,
115 
116  (ParamFloat<px4::params::COM_HOME_H_T>) _param_com_home_h_t,
117  (ParamFloat<px4::params::COM_HOME_V_T>) _param_com_home_v_t,
118 
119  (ParamFloat<px4::params::COM_POS_FS_EPH>) _param_com_pos_fs_eph,
120  (ParamFloat<px4::params::COM_POS_FS_EPV>) _param_com_pos_fs_epv, /*Not realy used for now*/
121  (ParamFloat<px4::params::COM_VEL_FS_EVH>) _param_com_vel_fs_evh,
122  (ParamInt<px4::params::COM_POSCTL_NAVL>) _param_com_posctl_navl, /* failsafe response to loss of navigation accuracy */
123 
124  (ParamInt<px4::params::COM_POS_FS_DELAY>) _param_com_pos_fs_delay,
125  (ParamInt<px4::params::COM_POS_FS_PROB>) _param_com_pos_fs_prob,
126  (ParamInt<px4::params::COM_POS_FS_GAIN>) _param_com_pos_fs_gain,
127 
128  (ParamInt<px4::params::COM_LOW_BAT_ACT>) _param_com_low_bat_act,
129  (ParamFloat<px4::params::COM_DISARM_LAND>) _param_com_disarm_land,
130  (ParamFloat<px4::params::COM_DISARM_PRFLT>) _param_com_disarm_preflight,
131 
132  (ParamInt<px4::params::COM_OBS_AVOID>) _param_com_obs_avoid,
133  (ParamInt<px4::params::COM_OA_BOOT_T>) _param_com_oa_boot_t,
134 
135  (ParamInt<px4::params::COM_FLT_PROFILE>) _param_com_flt_profile,
136 
137 
138  (ParamFloat<px4::params::COM_OF_LOSS_T>) _param_com_of_loss_t,
139  (ParamInt<px4::params::COM_OBL_ACT>) _param_com_obl_act,
140  (ParamInt<px4::params::COM_OBL_RC_ACT>) _param_com_obl_rc_act,
141 
142  (ParamInt<px4::params::COM_PREARM_MODE>) _param_com_prearm_mode,
143  (ParamInt<px4::params::COM_MOT_TEST_EN>) _param_com_mot_test_en,
144 
145  (ParamFloat<px4::params::COM_KILL_DISARM>) _param_com_kill_disarm,
146 
147  (ParamInt<px4::params::CBRK_SUPPLY_CHK>) _param_cbrk_supply_chk,
148  (ParamInt<px4::params::CBRK_USB_CHK>) _param_cbrk_usb_chk,
149  (ParamInt<px4::params::CBRK_AIRSPD_CHK>) _param_cbrk_airspd_chk,
150  (ParamInt<px4::params::CBRK_ENGINEFAIL>) _param_cbrk_enginefail,
151  (ParamInt<px4::params::CBRK_GPSFAIL>) _param_cbrk_gpsfail,
152  (ParamInt<px4::params::CBRK_FLIGHTTERM>) _param_cbrk_flightterm,
153  (ParamInt<px4::params::CBRK_VELPOSERR>) _param_cbrk_velposerr
154  )
155 
156  enum class PrearmedMode {
157  DISABLED = 0,
158  SAFETY_BUTTON = 1,
159  ALWAYS = 2
160  };
161 
162  const int64_t POSVEL_PROBATION_MIN = 1_s; /**< minimum probation duration (usec) */
163  const int64_t POSVEL_PROBATION_MAX = 100_s; /**< maximum probation duration (usec) */
164 
165  hrt_abstime _last_gpos_fail_time_us{0}; /**< Last time that the global position validity recovery check failed (usec) */
166  hrt_abstime _last_lpos_fail_time_us{0}; /**< Last time that the local position validity recovery check failed (usec) */
167  hrt_abstime _last_lvel_fail_time_us{0}; /**< Last time that the local velocity validity recovery check failed (usec) */
168 
169  // Probation times for position and velocity validity checks to pass if failed
170  hrt_abstime _gpos_probation_time_us = POSVEL_PROBATION_MIN;
171  hrt_abstime _lpos_probation_time_us = POSVEL_PROBATION_MIN;
172  hrt_abstime _lvel_probation_time_us = POSVEL_PROBATION_MIN;
173 
174  /* class variables used to check for navigation failure after takeoff */
175  hrt_abstime _time_at_takeoff{0}; /**< last time we were on the ground */
176  hrt_abstime _time_last_innov_pass{0}; /**< last time velocity or position innovations passed */
177  bool _nav_test_passed{false}; /**< true if the post takeoff navigation test has passed */
178  bool _nav_test_failed{false}; /**< true if the post takeoff navigation test has failed */
179 
180  bool _geofence_loiter_on{false};
181  bool _geofence_rtl_on{false};
182  bool _geofence_warning_action_on{false};
183  bool _geofence_violated_prev{false};
184 
186  bool _flight_termination_triggered{false};
187 
188  bool handle_command(vehicle_status_s *status, const vehicle_command_s &cmd, actuator_armed_s *armed,
189  uORB::PublicationQueued<vehicle_command_ack_s> &command_ack_pub, bool *changed);
190 
191  unsigned handle_command_motor_test(const vehicle_command_s &cmd);
192 
193  bool set_home_position();
194  bool set_home_position_alt_only();
195 
196  // Set the main system state based on RC and override device inputs
197  transition_result_t set_main_state(const vehicle_status_s &status, bool *changed);
198 
199  // Enable override (manual reversion mode) on the system
200  transition_result_t set_main_state_override_on(const vehicle_status_s &status, bool *changed);
201 
202  // Set the system main state based on the current RC inputs
203  transition_result_t set_main_state_rc(const vehicle_status_s &status, bool *changed);
204 
205  void update_control_mode();
206 
207  void check_valid(const hrt_abstime &timestamp, const hrt_abstime &timeout, const bool valid_in, bool *valid_out,
208  bool *changed);
209 
210  bool check_posvel_validity(const bool data_valid, const float data_accuracy, const float required_accuracy,
211  const hrt_abstime &data_timestamp_us, hrt_abstime *last_fail_time_us, hrt_abstime *probation_time_us, bool *valid_state,
212  bool *validity_changed);
213 
214  void reset_posvel_validity(bool *changed);
215 
216  void mission_init();
217 
218  void estimator_check(bool *status_changed);
219 
220  void offboard_control_update(bool &status_changed);
221 
222  void battery_status_check();
223 
224  void esc_status_check(const esc_status_s &esc_status);
225 
226  /**
227  * Checks the status of all available data links and handles switching between different system telemetry states.
228  */
229  void data_link_check(bool &status_changed);
230 
231  uORB::Subscription _telemetry_status_sub{ORB_ID(telemetry_status)};
232 
233  hrt_abstime _datalink_last_heartbeat_gcs{0};
234 
235  hrt_abstime _datalink_last_heartbeat_onboard_controller{0};
236  bool _onboard_controller_lost{false};
237 
238  hrt_abstime _datalink_last_heartbeat_avoidance_system{0};
239  bool _avoidance_system_lost{false};
240 
241  bool _avoidance_system_status_change{false};
242  uint8_t _datalink_last_status_avoidance_system{telemetry_status_s::MAV_STATE_UNINIT};
243 
244  uORB::Subscription _iridiumsbd_status_sub{ORB_ID(iridiumsbd_status)};
245 
246  hrt_abstime _high_latency_datalink_heartbeat{0};
247  hrt_abstime _high_latency_datalink_lost{0};
248 
249  int _last_esc_online_flags{-1};
250 
252  uint8_t _battery_warning{battery_status_s::BATTERY_WARNING_NONE};
253  float _battery_current{0.0f};
254 
255  systemlib::Hysteresis _auto_disarm_landed{false};
256  systemlib::Hysteresis _auto_disarm_killed{false};
257 
258  bool _print_avoidance_msg_once{false};
259 
260  // Subscriptions
261  uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)};
262  uORB::Subscription _vehicle_acceleration_sub{ORB_ID(vehicle_acceleration)};
263 
265  uORB::SubscriptionData<estimator_status_s> _estimator_status_sub{ORB_ID(estimator_status)};
266  uORB::SubscriptionData<mission_result_s> _mission_result_sub{ORB_ID(mission_result)};
267  uORB::SubscriptionData<offboard_control_mode_s> _offboard_control_mode_sub{ORB_ID(offboard_control_mode)};
268  uORB::SubscriptionData<vehicle_global_position_s> _global_position_sub{ORB_ID(vehicle_global_position)};
269  uORB::SubscriptionData<vehicle_local_position_s> _local_position_sub{ORB_ID(vehicle_local_position)};
270 
271  // Publications
272  uORB::Publication<vehicle_control_mode_s> _control_mode_pub{ORB_ID(vehicle_control_mode)};
273  uORB::Publication<vehicle_status_s> _status_pub{ORB_ID(vehicle_status)};
274  uORB::Publication<actuator_armed_s> _armed_pub{ORB_ID(actuator_armed)};
275  uORB::Publication<commander_state_s> _commander_state_pub{ORB_ID(commander_state)};
276  uORB::Publication<vehicle_status_flags_s> _vehicle_status_flags_pub{ORB_ID(vehicle_status_flags)};
277  uORB::Publication<test_motor_s> _test_motor_pub{ORB_ID(test_motor)};
278 
280 
281 };
282 
283 #endif /* COMMANDER_HPP_ */
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Definition: Limits.hpp:66
FailureDetector _failure_detector
Definition: Commander.hpp:185
Base class for failure detection logic based on vehicle states for failsafe triggering.
static struct vehicle_status_s status
Definition: Commander.cpp:138
State machine helper functions definitions.
static int _battery_sub
Definition: messages.cpp:60
static void print_usage()
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
static struct actuator_armed_s armed
Definition: Commander.cpp:139
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Hysteresis of a boolean value.
Controller library code.
DEFINE_PARAMETERS((ParamInt< px4::params::NAV_DLL_ACT >) _param_nav_dll_act,(ParamInt< px4::params::COM_DL_LOSS_T >) _param_com_dl_loss_t,(ParamInt< px4::params::COM_HLDL_LOSS_T >) _param_com_hldl_loss_t,(ParamInt< px4::params::COM_HLDL_REG_T >) _param_com_hldl_reg_t,(ParamInt< px4::params::NAV_RCL_ACT >) _param_nav_rcl_act,(ParamFloat< px4::params::COM_RC_LOSS_T >) _param_com_rc_loss_t,(ParamFloat< px4::params::COM_HOME_H_T >) _param_com_home_h_t,(ParamFloat< px4::params::COM_HOME_V_T >) _param_com_home_v_t,(ParamFloat< px4::params::COM_POS_FS_EPH >) _param_com_pos_fs_eph,(ParamFloat< px4::params::COM_POS_FS_EPV >) _param_com_pos_fs_epv,(ParamFloat< px4::params::COM_VEL_FS_EVH >) _param_com_vel_fs_evh,(ParamInt< px4::params::COM_POSCTL_NAVL >) _param_com_posctl_navl,(ParamInt< px4::params::COM_POS_FS_DELAY >) _param_com_pos_fs_delay,(ParamInt< px4::params::COM_POS_FS_PROB >) _param_com_pos_fs_prob,(ParamInt< px4::params::COM_POS_FS_GAIN >) _param_com_pos_fs_gain,(ParamInt< px4::params::COM_LOW_BAT_ACT >) _param_com_low_bat_act,(ParamFloat< px4::params::COM_DISARM_LAND >) _param_com_disarm_land,(ParamFloat< px4::params::COM_DISARM_PRFLT >) _param_com_disarm_preflight,(ParamInt< px4::params::COM_OBS_AVOID >) _param_com_obs_avoid,(ParamInt< px4::params::COM_OA_BOOT_T >) _param_com_oa_boot_t,(ParamInt< px4::params::COM_FLT_PROFILE >) _param_com_flt_profile,(ParamFloat< px4::params::COM_OF_LOSS_T >) _param_com_of_loss_t,(ParamInt< px4::params::COM_OBL_ACT >) _param_com_obl_act,(ParamInt< px4::params::COM_OBL_RC_ACT >) _param_com_obl_rc_act,(ParamInt< px4::params::COM_PREARM_MODE >) _param_com_prearm_mode,(ParamInt< px4::params::COM_MOT_TEST_EN >) _param_com_mot_test_en,(ParamFloat< px4::params::COM_KILL_DISARM >) _param_com_kill_disarm,(ParamInt< px4::params::CBRK_SUPPLY_CHK >) _param_cbrk_supply_chk,(ParamInt< px4::params::CBRK_USB_CHK >) _param_cbrk_usb_chk,(ParamInt< px4::params::CBRK_AIRSPD_CHK >) _param_cbrk_airspd_chk,(ParamInt< px4::params::CBRK_ENGINEFAIL >) _param_cbrk_enginefail,(ParamInt< px4::params::CBRK_GPSFAIL >) _param_cbrk_gpsfail,(ParamInt< px4::params::CBRK_FLIGHTTERM >) _param_cbrk_flightterm,(ParamInt< px4::params::CBRK_VELPOSERR >) _param_cbrk_velposerr) enum class PrearmedMode
Definition: Commander.hpp:105
transition_result_t
static int _airspeed_sub
Definition: messages.cpp:64