Firmware
state_machine_helper.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013, 2014 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 
42 #ifndef STATE_MACHINE_HELPER_H_
43 #define STATE_MACHINE_HELPER_H_
44 
45 #include <drivers/drv_hrt.h>
46 
47 #include <uORB/uORB.h>
48 #include <uORB/topics/vehicle_status.h>
49 #include <uORB/topics/battery_status.h>
50 #include <uORB/topics/actuator_armed.h>
51 #include <uORB/topics/safety.h>
52 #include <uORB/topics/commander_state.h>
53 #include <uORB/topics/vehicle_status_flags.h>
54 
55 typedef enum {
56  TRANSITION_DENIED = -1,
57  TRANSITION_NOT_CHANGED = 0,
58  TRANSITION_CHANGED
59 } transition_result_t;
60 
61 enum class link_loss_actions_t {
62  DISABLED = 0,
63  AUTO_LOITER = 1, // Hold mode
64  AUTO_RTL = 2, // Return mode
65  AUTO_LAND = 3, // Land mode
66  AUTO_RECOVER = 4, // Data Link Auto Recovery (CASA Outback Challenge rules)
67  TERMINATE = 5, // Turn off all controllers and set PWM outputs to failsafe value
68  LOCKDOWN = 6, // Kill the motors, same result as kill switch
69 };
70 
71 typedef enum {
72  ARM_REQ_NONE = 0,
73  ARM_REQ_MISSION_BIT = (1 << 0),
74  ARM_REQ_ARM_AUTH_BIT = (1 << 1),
75  ARM_REQ_GPS_BIT = (1 << 2),
76 } arm_requirements_t;
77 
78 extern const char *const arming_state_names[];
79 
80 bool is_safe(const safety_s &safety, const actuator_armed_s &armed);
81 
82 transition_result_t
83 arming_state_transition(vehicle_status_s *status, const safety_s &safety, const arming_state_t new_arming_state,
84  actuator_armed_s *armed, const bool fRunPreArmChecks, orb_advert_t *mavlink_log_pub,
85  vehicle_status_flags_s *status_flags, const uint8_t arm_requirements, const hrt_abstime &time_since_boot);
86 
87 transition_result_t
88 main_state_transition(const vehicle_status_s &status, const main_state_t new_main_state,
89  const vehicle_status_flags_s &status_flags, commander_state_s *internal_state);
90 
91 void enable_failsafe(vehicle_status_s *status, bool old_failsafe, orb_advert_t *mavlink_log_pub, const char *reason);
92 
93 bool set_nav_state(vehicle_status_s *status, actuator_armed_s *armed, commander_state_s *internal_state,
94  orb_advert_t *mavlink_log_pub, const link_loss_actions_t data_link_loss_act, const bool mission_finished,
95  const bool stay_in_failsafe, const vehicle_status_flags_s &status_flags, bool landed,
96  const link_loss_actions_t rc_loss_act, const int offb_loss_act, const int offb_loss_rc_act,
97  const int posctl_nav_loss_act);
98 
99 /*
100  * Checks the validty of position data aaainst the requirements of the current navigation
101  * mode and switches mode if position data required is not available.
102  */
103 bool check_invalid_pos_nav_state(vehicle_status_s *status, bool old_failsafe, orb_advert_t *mavlink_log_pub,
104  const vehicle_status_flags_s &status_flags, const bool use_rc, const bool using_global_pos);
105 
106 bool prearm_check(orb_advert_t *mavlink_log_pub, const vehicle_status_flags_s &status_flags, const safety_s &safety,
107  const uint8_t arm_requirements);
108 
109 
110 // COM_LOW_BAT_ACT parameter values
111 typedef enum LOW_BAT_ACTION {
112  WARNING = 0, // Warning
113  RETURN = 1, // Return mode
114  LAND = 2, // Land mode
115  RETURN_OR_LAND = 3 // Return mode at critically low level, Land mode at current position if reaching dangerously low levels
116 } low_battery_action_t;
117 
118 void battery_failsafe(orb_advert_t *mavlink_log_pub, const vehicle_status_s &status,
119  const vehicle_status_flags_s &status_flags, commander_state_s *internal_state, const uint8_t battery_warning,
120  const low_battery_action_t low_bat_action);
121 
122 #endif /* STATE_MACHINE_HELPER_H_ */
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
High-resolution timer with callouts and timekeeping.
API for the uORB lightweight object broker.
void enable_failsafe(vehicle_status_s *status, bool old_failsafe, orb_advert_t *mavlink_log_pub, const char *reason)
Enable failsafe and report to user.
Definition: state_machine_helper.cpp:380
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
bool set_nav_state(vehicle_status_s *status, actuator_armed_s *armed, commander_state_s *internal_state, orb_advert_t *mavlink_log_pub, const link_loss_actions_t data_link_loss_act, const bool mission_finished, const bool stay_in_failsafe, const vehicle_status_flags_s &status_flags, bool landed, const link_loss_actions_t rc_loss_act, const int offb_loss_act, const int offb_loss_rc_act, const int posctl_nav_loss_act)
Check failsafe and main status and set navigation status for navigator accordingly.
Definition: state_machine_helper.cpp:392