NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
ff_flight.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef FF_UTIL_FF_FLIGHT_H_
20 #define FF_UTIL_FF_FLIGHT_H_
21 
22 // ROS includes
23 #include <ros/ros.h>
24 
25 // General information
26 #include <geometry_msgs/InertiaStamped.h>
27 
28 // Message includes
29 #include <ff_msgs/EkfState.h>
30 #include <ff_msgs/ControlState.h>
31 #include <ff_msgs/FlightMode.h>
32 
33 // Eigen includes
34 #include <Eigen/Dense>
35 #include <Eigen/Geometry>
36 
37 // STL includes
38 #include <vector>
39 #include <string>
40 #include <map>
41 
42 namespace ff_util {
43 
44 // Convenience declarations
45 typedef ff_msgs::ControlState Setpoint;
46 typedef ff_msgs::EkfState Estimate;
47 typedef std::vector<Setpoint> Segment;
48 
49 class State {
50  public:
51  // Empty state
52  State();
53 
54  // Create a state from a ff_msgs::ControlState message
55  explicit State(Setpoint const& msg);
56 
57  // Create a state from a ff_msgs::ControlState message
58  explicit State(Estimate const& msg);
59 
60  // Destructor
61  virtual ~State();
62 
63  // Assignment operator
64  void operator=(State const& right);
65 
66  // Minus operator
67  State operator-(State const& right);
68 
69  // Convert the state to a setpoint
71 
72  // Return the magnitude of a rotation
73  double QuaternionMagnitude();
74 
75  // Useful helper functions
76  static double QuaternionMagnitude(Eigen::Quaterniond const& iq);
77  static double VectorMagnitude(Eigen::Vector3d const& iv);
78 
79  public:
80  double t; // Time
81  Eigen::Quaterniond q; // Orientation
82  Eigen::Vector3d p; // Position
83  Eigen::Vector3d w; // Omega
84  Eigen::Vector3d v; // Velocity
85  Eigen::Vector3d b; // Alpha
86  Eigen::Vector3d a; // Acceleration
87 };
88 
90 // SEGMENT CHECKING UTILITY FUNCTIONS //
92 
93 // Segment check bitmask
94 enum SegmentCheckMask : uint32_t {
95  CHECK_NONE = 0x000,
107  CHECK_ALL = 0x800
108 };
109 // Segment result bitmask
110 enum SegmentResult : int32_t {
111  SUCCESS = 0,
120 };
121 
122 // Class to read flight mode information
123 class FlightUtil {
124  public:
125  // Vector of flight modes
126  typedef std::map<std::string, ff_msgs::FlightMode> FlightModeMap;
127 
128  // Minimum acceptable control rate
129  static constexpr double MIN_CONTROL_RATE = 1.0;
130 
131  // Get all inertia configuration (LUA config file is queried only once)
132  static bool GetInertiaConfig(geometry_msgs::InertiaStamped & data);
133 
134  // Get data for a given flight mode (leave empty for default value)
135  static bool GetFlightMode(
136  ff_msgs::FlightMode &data, std::string const& name = "");
137 
138  // Get the intial flight mode
139  static bool GetInitialFlightMode(ff_msgs::FlightMode &data);
140 
141  // Validate if an upper limit has been breached
142  static double ValidateUpperLimit(const double limit, double val);
143 
144  // Validate if a lower limit has been breached
145  static double ValidateLowerLimit(const double limit, double val);
146 
147  // Perform checks on a segment for validity, based on a flight mode
148  static SegmentResult Check(SegmentCheckMask mask, Segment const& segment,
149  ff_msgs::FlightMode const& flight_mode, bool faceforward);
150 
151  // Resample a segment to the minimum control frequency
152  static SegmentResult Resample(Segment const& in, Segment & out,
153  double rate = -1.0);
154 
155  // Print a human-readable description of the error code
156  static std::string GetDescription(SegmentResult result);
157 
158  // Logical comparison of two setpoints within defined EPSILON value
159  static bool Equal(Setpoint const& left, Setpoint const& right,
160  size_t degree = 2);
161 
162  // Check if two poses are within position and attitude tolerance
163  static bool WithinTolerance(geometry_msgs::Pose const& a,
164  geometry_msgs::Pose const& b, double tolerance_pos, double tolerance_att);
165 
166  // Check if two poses are within tolerance of a flight mode
167  static bool WithinTolerance(ff_msgs::FlightMode const& fm,
168  geometry_msgs::Pose const& a, geometry_msgs::Pose const& b);
169 };
170 
171 } // end namespace ff_util
172 
173 #endif // FF_UTIL_FF_FLIGHT_H_
ff_util::SUCCESS
@ SUCCESS
Definition: ff_flight.h:111
ff_util::FlightUtil::GetInertiaConfig
static bool GetInertiaConfig(geometry_msgs::InertiaStamped &data)
Definition: ff_flight.cc:123
ff_util::State::operator-
State operator-(State const &right)
Definition: ff_flight.cc:73
ff_util::ERROR_STATIONARY_ENDPOINT
@ ERROR_STATIONARY_ENDPOINT
Definition: ff_flight.h:113
ff_util::CHECK_NONE
@ CHECK_NONE
Definition: ff_flight.h:95
ff_util::State::w
Eigen::Vector3d w
Definition: ff_flight.h:83
ff_util::State::VectorMagnitude
static double VectorMagnitude(Eigen::Vector3d const &iv)
Definition: ff_flight.cc:114
ff_util::FlightUtil::WithinTolerance
static bool WithinTolerance(geometry_msgs::Pose const &a, geometry_msgs::Pose const &b, double tolerance_pos, double tolerance_att)
Definition: ff_flight.cc:504
ff_util::State::~State
virtual ~State()
Definition: ff_flight.cc:61
ff_util::Estimate
ff_msgs::EkfState Estimate
Definition: ff_flight.h:46
ff_util::ERROR_LIMITS_ACCEL
@ ERROR_LIMITS_ACCEL
Definition: ff_flight.h:117
ff_util::FlightUtil::GetFlightMode
static bool GetFlightMode(ff_msgs::FlightMode &data, std::string const &name="")
Definition: ff_flight.cc:151
ff_util::CHECK_MINIMUM_NUM_SETPOINTS
@ CHECK_MINIMUM_NUM_SETPOINTS
Definition: ff_flight.h:99
ff_util::State::ToSetpoint
Setpoint ToSetpoint()
Definition: ff_flight.cc:86
ff_util::ERROR_LIMITS_OMEGA
@ ERROR_LIMITS_OMEGA
Definition: ff_flight.h:118
ff_util::Segment
std::vector< Setpoint > Segment
Definition: ff_flight.h:47
mask
uint8_t mask
Definition: signal_lights.h:72
ff_util::ERROR_TIME_RUNS_BACKWARDS
@ ERROR_TIME_RUNS_BACKWARDS
Definition: ff_flight.h:115
ff_util::State::p
Eigen::Vector3d p
Definition: ff_flight.h:82
ff_util::FlightUtil::MIN_CONTROL_RATE
static constexpr double MIN_CONTROL_RATE
Definition: ff_flight.h:129
ff_util::FlightUtil::ValidateUpperLimit
static double ValidateUpperLimit(const double limit, double val)
Definition: ff_flight.cc:237
ff_util::State::v
Eigen::Vector3d v
Definition: ff_flight.h:84
name
std::string name
Definition: eps_simulator.cc:48
ff_util::FlightUtil::GetInitialFlightMode
static bool GetInitialFlightMode(ff_msgs::FlightMode &data)
Definition: ff_flight.cc:226
ff_util::State::State
State()
Definition: ff_flight.cc:41
point_cloud_common::Vector3d
Eigen::Vector3d Vector3d(const PointType &point)
Definition: utilities.h:328
ff_util::FlightUtil::Equal
static bool Equal(Setpoint const &left, Setpoint const &right, size_t degree=2)
Definition: ff_flight.cc:466
ff_util::FlightUtil
Definition: ff_flight.h:123
ff_util::CHECK_STATIONARY_ENDPOINT
@ CHECK_STATIONARY_ENDPOINT
Definition: ff_flight.h:97
ff_util::State::t
double t
Definition: ff_flight.h:80
ff_util::CHECK_MINIMUM_FREQUENCY
@ CHECK_MINIMUM_FREQUENCY
Definition: ff_flight.h:96
ff_util::State::b
Eigen::Vector3d b
Definition: ff_flight.h:85
ff_util::ERROR_LIMITS_VEL
@ ERROR_LIMITS_VEL
Definition: ff_flight.h:116
ff_util::CHECK_ALL
@ CHECK_ALL
Definition: ff_flight.h:107
ff_util::CHECK_ALL_BASIC
@ CHECK_ALL_BASIC
Definition: ff_flight.h:104
ff_util::CHECK_LIMITS_ALPHA
@ CHECK_LIMITS_ALPHA
Definition: ff_flight.h:103
ff_util::ERROR_MINIMUM_FREQUENCY
@ ERROR_MINIMUM_FREQUENCY
Definition: ff_flight.h:112
ff_util::ERROR_MINIMUM_NUM_SETPOINTS
@ ERROR_MINIMUM_NUM_SETPOINTS
Definition: ff_flight.h:114
ff_util::FlightUtil::FlightModeMap
std::map< std::string, ff_msgs::FlightMode > FlightModeMap
Definition: ff_flight.h:126
ff_util::State::a
Eigen::Vector3d a
Definition: ff_flight.h:86
ff_util::CHECK_ALL_LIMITS
@ CHECK_ALL_LIMITS
Definition: ff_flight.h:105
ff_util::CHECK_FIRST_TIMESTAMP_IN_PAST
@ CHECK_FIRST_TIMESTAMP_IN_PAST
Definition: ff_flight.h:98
ff_util::FlightUtil::GetDescription
static std::string GetDescription(SegmentResult result)
Definition: ff_flight.cc:441
ff_util::FlightUtil::ValidateLowerLimit
static double ValidateLowerLimit(const double limit, double val)
Definition: ff_flight.cc:246
ff_util::State::operator=
void operator=(State const &right)
Definition: ff_flight.cc:63
ff_util::FlightUtil::Check
static SegmentResult Check(SegmentCheckMask mask, Segment const &segment, ff_msgs::FlightMode const &flight_mode, bool faceforward)
Definition: ff_flight.cc:255
ff_util::CHECK_LIMITS_OMEGA
@ CHECK_LIMITS_OMEGA
Definition: ff_flight.h:102
ff_util::SegmentResult
SegmentResult
Definition: ff_flight.h:110
ff_util::CHECK_LIMITS_VEL
@ CHECK_LIMITS_VEL
Definition: ff_flight.h:100
ff_util
Definition: config_client.h:31
ff_util::SegmentCheckMask
SegmentCheckMask
Definition: ff_flight.h:94
ff_util::CHECK_ALL_CONSISTENCY
@ CHECK_ALL_CONSISTENCY
Definition: ff_flight.h:106
ff_util::State
Definition: ff_flight.h:49
ff_util::ERROR_LIMITS_ALPHA
@ ERROR_LIMITS_ALPHA
Definition: ff_flight.h:119
ff_util::FlightUtil::Resample
static SegmentResult Resample(Segment const &in, Segment &out, double rate=-1.0)
Definition: ff_flight.cc:320
ff_util::Setpoint
ff_msgs::ControlState Setpoint
Definition: ff_flight.h:45
ff_util::State::QuaternionMagnitude
double QuaternionMagnitude()
Definition: ff_flight.cc:99
ff_util::State::q
Eigen::Quaterniond q
Definition: ff_flight.h:81
ff_util::CHECK_LIMITS_ACCEL
@ CHECK_LIMITS_ACCEL
Definition: ff_flight.h:101