Auterion App SDK
Auterion SDK is a library that can be used by AuterionOS apps to communicate with the system.
system_state.hpp
1 /****************************************************************************
2  *
3  * Copyright 2023 Auterion AG. 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 are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  ****************************************************************************/
32 
33 #pragma once
34 
35 #include <Eigen/Eigen>
36 #include <memory>
37 #include <optional>
38 
39 namespace auterion {
44 enum class SubscriptionType {
45  LOCAL_POSITION,
46  GLOBAL_POSITION,
47  ATTITUDE,
48  ANGULAR_RATES,
49  ODOMETRY,
50  ARMED,
51  HOME_POSITION,
52  BATTERY,
53  VTOL_STATE,
54  MANUAL_INPUT,
55  LAND_DETECTED,
56  WIND,
57  AIRSPEED
58 };
59 
66 struct LocalPosition {
67  Eigen::Vector3f position_enu_m;
68  Eigen::Vector3f velocity_enu_m;
69  Eigen::Vector3f acceleration_enu_m;
71  double heading;
72  float dist_bottom;
79 };
80 
88  double latitude_deg;
89  double longitude_deg;
90  double altitude_amsl_m;
97  inline Eigen::Vector3d toEigenVector() const {
98  return Eigen::Vector3d{latitude_deg, longitude_deg, altitude_amsl_m};
99  }
100 };
101 
108 struct Odometry {
109  Eigen::Quaternionf attitude;
110  Eigen::Vector3f angular_velocity;
111  Eigen::Vector3f position;
112  Eigen::Vector3f velocity;
113 };
114 
121 struct AngularRates {
122  Eigen::Vector3f angular_velocity_flu_rad_s;
125 };
126 
133 struct HomePosition {
134  Eigen::Vector3f local_position_enu_m;
136  double yaw;
143 };
144 
152  float voltage_v;
153  float current_a;
156  float remaining;
159  Eigen::VectorXf cell_voltage_v;
160 };
161 
168 enum VtolState {
169  UNDEFINED,
170  TRANSITION_TO_FIXED_WING,
171  TRANSITION_TO_MULTICOPTER,
172  MULTICOPTER,
173  FIXED_WING
174 };
175 
182 struct ManualInput {
183  float roll;
184  float pitch;
185  float yaw;
186  float throttle;
188  Eigen::Matrix<float, 6, 1> aux;
191 };
192 
199 struct LandDetected {
200  bool landed;
201  bool in_descend;
202 };
203 
210 struct Wind {
213  std::optional<float> variance_north;
215  std::optional<float> variance_east;
217 };
218 
225 struct Airspeed {
226  std::optional<float>
228  std::optional<float> calibrated_airspeed_m_s;
230  std::optional<float> true_airspeed_m_s;
231 };
232 
234 class SDK;
235 
236 template <SubscriptionType S, typename DataType>
237 class SubscriptionImpl;
238 
239 class SystemStateImpl;
241 
251 template <SubscriptionType S, typename DataType>
253  public:
254  Subscription(const std::shared_ptr<SubscriptionImpl<S, DataType>>& impl);
255 
264  void onUpdate(std::function<void(DataType)> callback);
265 
274  void subscribe(std::function<void(DataType)> callback);
275 
281  DataType last() const;
282 
288  bool isLastValid() const;
289 
290  private:
291  std::shared_ptr<SubscriptionImpl<S, DataType>> _impl;
292 };
293 
324 class SystemState {
325  public:
326  SystemState(SDK& sdk);
327  ~SystemState() = default;
328 
342 
343  SystemState& subscribeLocalPosition(std::function<void(LocalPosition)> callback = nullptr) {
344  localPosition().subscribe(callback);
345  return *this;
346  }
347 
348  SystemState& subscribeGlobalPosition(std::function<void(GlobalPosition)> callback = nullptr) {
349  globalPosition().subscribe(callback);
350  return *this;
351  }
352 
353  SystemState& subscribeAttitude(std::function<void(Eigen::Quaternionf)> callback = nullptr) {
354  attitude().subscribe(callback);
355  return *this;
356  }
357 
358  SystemState& subscribeOdometry(std::function<void(Odometry)> callback = nullptr) {
359  odometry().subscribe(callback);
360  return *this;
361  }
362 
363  SystemState& subscribeAngularRates(std::function<void(AngularRates)> callback = nullptr) {
364  angularRates().subscribe(callback);
365  return *this;
366  }
367 
368  SystemState& subscribeArmed(std::function<void(bool)> callback = nullptr) {
369  armed().subscribe(callback);
370  return *this;
371  }
372 
373  SystemState& subscribeHomePosition(std::function<void(HomePosition)> callback = nullptr) {
374  homePosition().subscribe(callback);
375  return *this;
376  }
377 
378  SystemState& subscribeBattery(std::function<void(BatteryStatus)> callback = nullptr) {
379  battery().subscribe(callback);
380  return *this;
381  }
382 
383  SystemState& subscribeVtolState(std::function<void(VtolState)> callback = nullptr) {
384  vtolState().subscribe(callback);
385  return *this;
386  }
387 
388  SystemState& subscribeManualInput(std::function<void(ManualInput)> callback = nullptr) {
389  manualInput().subscribe(callback);
390  return *this;
391  }
392 
393  SystemState& subscribeLandDetected(std::function<void(LandDetected)> callback = nullptr) {
394  landDetected().subscribe(callback);
395  return *this;
396  }
397 
398  SystemState& subscribeWind(std::function<void(Wind)> callback = nullptr) {
399  wind().subscribe(callback);
400  return *this;
401  }
402 
403  SystemState& subscribeAirspeed(std::function<void(Airspeed)> callback = nullptr) {
404  airspeed().subscribe(callback);
405  return *this;
406  }
407 
408  private:
409  std::shared_ptr<SystemStateImpl> _impl;
410 };
411 
413 } // namespace auterion
SDK execution class. All callbacks are called on the same thread.
Definition: auterion.hpp:97
A template class for managing subscriptions to data coming from the vehicle.
Definition: system_state.hpp:252
void subscribe(std::function< void(DataType)> callback)
Activates the subscription process and registers a callback function if provided.
bool isLastValid() const
Checks if the last received data is valid.
DataType last() const
Retrieves the last received data.
void onUpdate(std::function< void(DataType)> callback)
Regsiters a callback function to be called when the data is updated.
Provides access to the system's state, including flight controller telemetry.
Definition: system_state.hpp:324
VtolState
Represents the VTOL state.
Definition: system_state.hpp:168
Represents validated airspeed.
Definition: system_state.hpp:225
std::optional< float > true_airspeed_m_s
Definition: system_state.hpp:230
std::optional< float > calibrated_airspeed_m_s
Definition: system_state.hpp:228
std::optional< float > indicated_airspeed_m_s
Definition: system_state.hpp:227
Represents angular rates and accelerations in the FLU body frame.
Definition: system_state.hpp:121
Eigen::Vector3f angular_acceleration_flu_rad_s
Definition: system_state.hpp:123
Eigen::Vector3f angular_velocity_flu_rad_s
Definition: system_state.hpp:122
Represents the status of the battery.
Definition: system_state.hpp:151
float discharged_mah
Definition: system_state.hpp:155
float remaining
Definition: system_state.hpp:156
float voltage_v
Definition: system_state.hpp:152
int cell_count
Definition: system_state.hpp:158
Eigen::VectorXf cell_voltage_v
Definition: system_state.hpp:159
float current_a
Definition: system_state.hpp:153
Represents global position using latitude, longitude, and altitude.
Definition: system_state.hpp:87
double longitude_deg
Definition: system_state.hpp:89
Eigen::Vector3d toEigenVector() const
Converts the global position to an Eigen vector.
Definition: system_state.hpp:97
double altitude_amsl_m
Definition: system_state.hpp:90
double latitude_deg
Definition: system_state.hpp:88
Represents the home position in both local ENU and global coordinates.
Definition: system_state.hpp:133
bool was_set_manually
Definition: system_state.hpp:142
double yaw
Definition: system_state.hpp:136
bool global_position_valid
Definition: system_state.hpp:139
bool local_position_valid
Definition: system_state.hpp:140
bool altitude_valid
Definition: system_state.hpp:138
Eigen::Vector3f local_position_enu_m
Definition: system_state.hpp:134
GlobalPosition global_position
Definition: system_state.hpp:135
Represents landed state of the vehicle.
Definition: system_state.hpp:199
bool in_descend
Definition: system_state.hpp:201
Represents local position, velocity, and acceleration in the ENU coordinate system.
Definition: system_state.hpp:66
bool horizontal_position_valid
Definition: system_state.hpp:74
bool horizontal_velocity_valid
Definition: system_state.hpp:76
bool vertical_velocity_valid
Definition: system_state.hpp:77
double heading
Definition: system_state.hpp:71
Eigen::Vector3f velocity_enu_m
Definition: system_state.hpp:68
Eigen::Vector3f acceleration_enu_m
Definition: system_state.hpp:69
Eigen::Vector3f position_enu_m
Definition: system_state.hpp:67
bool dist_bottom_valid
Definition: system_state.hpp:78
bool vertical_position_valid
Definition: system_state.hpp:75
Represents manual input from a user.
Definition: system_state.hpp:182
float pitch
Definition: system_state.hpp:184
float roll
Definition: system_state.hpp:183
Eigen::Matrix< float, 6, 1 > aux
Definition: system_state.hpp:188
float throttle
Definition: system_state.hpp:186
bool sticks_moving
Definition: system_state.hpp:190
float yaw
Definition: system_state.hpp:185
Represents odometry data including attitude and angular rates.
Definition: system_state.hpp:108
Eigen::Vector3f velocity
Definition: system_state.hpp:112
Eigen::Vector3f position
Definition: system_state.hpp:111
Eigen::Vector3f angular_velocity
Definition: system_state.hpp:110
Eigen::Quaternionf attitude
Definition: system_state.hpp:109
Represents wind speed and direction.
Definition: system_state.hpp:210
float windspeed_north
Definition: system_state.hpp:211
std::optional< float > variance_east
Definition: system_state.hpp:215
float windspeed_east
Definition: system_state.hpp:212
std::optional< float > variance_north
Definition: system_state.hpp:213