36 #ifndef SNAV_FCI_FLIGHT_CONTROL_INTERFACE_HPP_
37 #define SNAV_FCI_FLIGHT_CONTROL_INTERFACE_HPP_
40 #include <condition_variable>
46 #include <Eigen/Geometry>
48 #include "snav/snapdragon_navigator.h"
50 #include "api/waypoint.hpp"
51 #include "api/config/landing_config.hpp"
52 #include "api/config/rx_config.hpp"
53 #include "api/config/takeoff_config.hpp"
54 #include "api/config/tx_config.hpp"
55 #include "api/config/waypoint_config.hpp"
56 #include "imp/inc/snav_cached_data_thread_safe.hpp"
57 #include "imp/inc/state_info.hpp"
58 #include "imp/inc/planner.hpp"
59 #include "imp/inc/transform.hpp"
60 #include "imp/inc/tx_command_thread_safe.hpp"
704 const Eigen::Vector3f& t_pw);
724 Eigen::Vector3f& t_pw) const;
816 const
float yaw_rate,
RcCommand& rc_command);
861 inline static bool ok()
863 return rx_ok_ && tx_ok_;
888 static void tx_loop();
889 static void update_waypoint_frame_parent_transform();
890 static void send_tx_command();
891 static void rx_loop();
892 static void check_tx_conditions();
893 static float constrain_min_max(
float input,
const float min,
const float max);
894 static bool has_closed_loop_position_control(SnRcCommandType);
895 static void print_error(
const std::string& message);
896 static void print_error(
const std::string& origin,
const std::string& message);
897 static void print_warning(
const std::string& message);
898 static void print_warning(
const std::string& origin,
const std::string& message);
899 static void print_info(
const std::string& message);
900 static void print_message(
const std::string& prefix,
const std::string& origin,
901 const std::string& message);
902 static void enter_action(
Action action);
903 static void exit_action(
const Return& ret_code);
908 void enforce_init_precondition()
const;
909 void enforce_connected_precondition()
const;
910 void enforce_write_precondition()
const;
911 void enforce_in_flight_precondition()
const;
912 void enforce_no_action_in_progress_precondition()
const;
913 void enforce_tx_connection_invariant()
const;
914 int infer_metadata_from_mode(
const SnMode,
const bool, TxCommand::Mode&, SnInputCommandType&,
916 void enforce_props_state_constraint(
const SnPropsState);
917 void do_not_enforce_props_state_constraint();
918 void update_tx_command_pos_ctrl();
921 Return go_to_waypoint_impl(std::unique_lock<std::mutex>& lock,
const Waypoint& wp);
922 Return execute_mission_impl(std::unique_lock<std::mutex>& lock,
const double t_start);
923 Return execute_planner(std::unique_lock<std::mutex>& lock,
const double t_start = 0);
926 void takeoff_safe() noexcept;
928 void land_safe() noexcept;
930 void go_to_waypoint_safe(const
Waypoint&) noexcept;
931 void execute_mission_safe(const
double t_start) noexcept;
936 static std::mutex connection_mutex_;
937 static std::mutex command_mutex_;
939 static
bool configured_tx_;
941 static
bool configured_rx_;
942 static std::condition_variable configured_rx_cv_;
943 static TxCommandThreadSafe tcts_;
944 static TransformThreadSafe tf_ep_;
945 static TransformThreadSafe tf_pw_;
946 static TransformThreadSafe tf_ev_;
949 static SnavCachedDataThreadSafe scdts_;
950 static StateInfoThreadSafe sits_;
951 static std::thread tx_thread_;
952 static std::atomic_bool tx_ok_;
953 static std::thread rx_thread_;
954 static std::atomic_bool rx_ok_;
955 static
bool write_access_taken_;
956 static
int num_connections_;
957 static std::atomic_bool verbose_;
967 static std::chrono::time_point<std::chrono::system_clock> t0_;
968 static std::mutex time_mutex_;
969 static Planner planner_;
970 static std::thread action_thread_;
971 static std::atomic<
Action> current_action_;
972 static std::atomic<
Action> last_action_;
973 static std::atomic<
Return> last_action_result_;
974 static std::atomic_bool kill_current_action_;
975 static std::condition_variable action_cv_;
976 static std::mutex action_mutex_;
981 std::atomic_bool connected_;
988 #endif // SNAV_FCI_FLIGHT_CONTROL_INTERFACE_HPP_
~FlightControlInterface() noexcept
Destructor of FlightControlInterface.
Return stop_props()
Blocking call to stop the propellers.
Structure containing options for the Planner.
Definition: planner_config.hpp:44
Return compute_trajectory(StateVector starting_state)
Compute a trajectory through the given waypoints.
Return go_to_waypoint_nb(const Waypoint &waypoint)
Non-blocking call to go to specified waypoint.
Interface to the Qualcomm Navigator Flight Controller.
Definition: flight_control_interface.hpp:80
static std::string get_return_as_string(Return ret_code)
Convert a Return code to a string.
Return convert_velocity_to_rc_command(const std::array< float, 3 > velocity, const float yaw_rate, RcCommand &rc_command)
Convert metric velocity command into an RcCommand suitable for sending to the flight controller based...
Structure containing the inputs to the sn_send_rc_command() Qualcomm Navigator API function...
Definition: rc_command.hpp:51
Return go_to_waypoint(const Waypoint &waypoint)
Blocking call to go to specified waypoint.
Return set_tx_command(const TxCommand &command)
Directly set the command being sent to the flight controller.
void wait_on_action() const noexcept
Wait for the currently-executing action to complete.
static bool ok()
Whether or not read and write access to Qualcomm Navigator is ok.
Definition: flight_control_interface.hpp:861
void wait_for_configure() const noexcept
Block until FlightControlInterface has been configured.
Definition: tx_command.hpp:41
Return disconnect() noexcept
End connection to Qualcomm Navigator.
SnavCachedData get_snav_cached_data() const
Get a copy of the latest data from the cache.
float get_trajectory_time() const
Get the current trajectory time.
Return get_optimized_waypoints(std::vector< Waypoint > &waypoints) const
Get the optimized waypoints.
Return get_last_action_result() const noexcept
Get the result of the most recently executed action.
Return configure_planner(const PlannerConfig &config)
Specify the configuration options for the planner.
Return start_props()
Blocking call to start the propellers.
Return get_waypoints(std::vector< Waypoint > &waypoints) const
Get the input waypoints.
static void set_verbosity(bool verbose)
Set verbosity of FlightControlInterface.
Definition: flight_control_interface.hpp:837
Return get_current_trajectory(std::vector< snav_traj_gen::SnavTrajectory > &traj) const
Get the current trajectory.
Return execute_mission(const double t_start=0)
Execute the trajectory-following mission.
StateVector get_desired_state(const SnavCachedData &snav_data) const
Get the desired state as a StateVector with respect to ReferenceFrame::WAYPOINT from the SnavCachedDa...
Return preload_waypoints(const std::vector< Waypoint > &waypoints)
Load a vector of waypoints.
static void preempt_current_action()
Send preempt signal to currently-executing action.
Return execute_mission_nb(const double t_start=0)
Non-blocking call to execute the trajectory-following mission.
Action get_last_action() const noexcept
Get the most recently executed action.
Structure containing position and yaw along with their derivatives and some metadata.
Definition: waypoint.hpp:52
Return get_waypoint_frame_tf(Eigen::Quaternionf &q_pw, Eigen::Vector3f &t_pw) const
Get the transform between ReferenceFrame::WAYPOINT and its parent.
Structure containing position and yaw along with their derivatives.
Definition: state_vector.hpp:50
Return connect()
Establish connection to Qualcomm Navigator.
Action get_current_action() const noexcept
Get the currently-executing action.
Return configure_tx(const TxConfig &tx_config)
Specify configuration options for sending commands to Qualcomm Navigator.
Action
Enum used for possible actions.
Definition: flight_control_interface.hpp:138
StateVector get_estimated_state(const SnavCachedData &snav_data) const
Get the estimated state as a StateVector with respect to ReferenceFrame::WAYPOINT from the SnavCached...
FlightControlInterface(const Permissions &requested_access)
Constructs an instance of FlightControlInterface.
static bool tx_ok()
Whether or not write access to Qualcomm Navigator is ok.
Definition: flight_control_interface.hpp:845
Return set_waypoint_frame_tf(const Eigen::Quaternionf &q_pw, const Eigen::Vector3f &t_pw)
Updates the transform between ReferenceFrame::WAYPOINT and its parent.
Planner::Status get_planner_status() const
Get the current status of the Planner.
Permissions get_permissions() const noexcept
Return the permissions of this object.
Return configure_rx(const RxConfig &rx_config)
Specify configuration options for receiving data from Qualcomm Navigator.
Structure containing options for takeoff.
Definition: takeoff_config.hpp:46
Structure containing options for the TX thread.
Definition: tx_config.hpp:46
Structure containing options for the RX thread.
Definition: rx_config.hpp:44
Structure containing options for landing.
Definition: landing_config.hpp:46
Permissions
Defines the permissions of an instance of FlightControlInterface.
Definition: flight_control_interface.hpp:100
Return get_last_optimized_trajectory(std::vector< snav_traj_gen::SnavTrajectory > &traj) const
Get the last optimized trajectory.
static bool rx_ok()
Whether or not read access to Qualcomm Navigator is ok.
Definition: flight_control_interface.hpp:853
Return
Return codes for FlightControlInterface.
Definition: flight_control_interface.hpp:116