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 
38 namespace auterion {
43 enum class SubscriptionType {
44  LOCAL_POSITION,
45  GLOBAL_POSITION,
46  ATTITUDE,
47  ANGULAR_RATES,
48  ODOMETRY,
49  ARMED,
50  HOME_POSITION,
51  BATTERY,
52  VTOL_STATE,
53  MANUAL_INPUT,
54  LAND_DETECTED,
55 };
56 
63 struct LocalPosition {
64  Eigen::Vector3f position_enu_m;
65  Eigen::Vector3f velocity_enu_m;
66  Eigen::Vector3f acceleration_enu_m;
68  double heading;
69  float dist_bottom;
76 };
77 
85  double latitude_deg;
86  double longitude_deg;
87  double altitude_amsl_m;
94  inline Eigen::Vector3d toEigenVector() const {
95  return Eigen::Vector3d{latitude_deg, longitude_deg, altitude_amsl_m};
96  }
97 };
98 
105 struct Odometry {
106  Eigen::Quaternionf attitude;
107  Eigen::Vector3f angular_velocity;
108  Eigen::Vector3f position;
109  Eigen::Vector3f velocity;
110 };
111 
118 struct AngularRates {
119  Eigen::Vector3f angular_velocity_flu_rad_s;
122 };
123 
130 struct HomePosition {
131  Eigen::Vector3f local_position_enu_m;
133  double yaw;
140 };
141 
149  float voltage_v;
150  float current_a;
153  float remaining;
156  Eigen::VectorXf cell_voltage_v;
157 };
158 
165 enum VtolState {
166  UNDEFINED,
167  TRANSITION_TO_FIXED_WING,
168  TRANSITION_TO_MULTICOPTER,
169  MULTICOPTER,
170  FIXED_WING
171 };
172 
179 struct ManualInput {
180  float roll;
181  float pitch;
182  float yaw;
183  float throttle;
185  Eigen::Matrix<float, 6, 1> aux;
188 };
189 
196 struct LandDetected {
197  bool landed;
198  bool in_descend;
199 };
200 
202 class SDK;
203 
204 template <SubscriptionType S, typename DataType>
205 class SubscriptionImpl;
206 
207 class SystemStateImpl;
209 
219 template <SubscriptionType S, typename DataType>
221  public:
222  Subscription(const std::shared_ptr<SubscriptionImpl<S, DataType>>& impl);
223 
232  void onUpdate(std::function<void(DataType)> callback);
233 
242  void subscribe(std::function<void(DataType)> callback);
243 
249  DataType last() const;
250 
256  bool isLastValid() const;
257 
258  private:
259  std::shared_ptr<SubscriptionImpl<S, DataType>> _impl;
260 };
261 
292 class SystemState {
293  public:
294  SystemState(SDK& sdk);
295  ~SystemState() = default;
296 
308 
309  SystemState& subscribeLocalPosition(std::function<void(LocalPosition)> callback = nullptr) {
310  localPosition().subscribe(callback);
311  return *this;
312  }
313 
314  SystemState& subscribeGlobalPosition(std::function<void(GlobalPosition)> callback = nullptr) {
315  globalPosition().subscribe(callback);
316  return *this;
317  }
318 
319  SystemState& subscribeAttitude(std::function<void(Eigen::Quaternionf)> callback = nullptr) {
320  attitude().subscribe(callback);
321  return *this;
322  }
323 
324  SystemState& subscribeOdometry(std::function<void(Odometry)> callback = nullptr) {
325  odometry().subscribe(callback);
326  return *this;
327  }
328 
329  SystemState& subscribeAngularRates(std::function<void(AngularRates)> callback = nullptr) {
330  angularRates().subscribe(callback);
331  return *this;
332  }
333 
334  SystemState& subscribeArmed(std::function<void(bool)> callback = nullptr) {
335  armed().subscribe(callback);
336  return *this;
337  }
338 
339  SystemState& subscribeHomePosition(std::function<void(HomePosition)> callback = nullptr) {
340  homePosition().subscribe(callback);
341  return *this;
342  }
343 
344  SystemState& subscribeBattery(std::function<void(BatteryStatus)> callback = nullptr) {
345  battery().subscribe(callback);
346  return *this;
347  }
348 
349  SystemState& subscribeVtolState(std::function<void(VtolState)> callback = nullptr) {
350  vtolState().subscribe(callback);
351  return *this;
352  }
353 
354  SystemState& subscribeManualInput(std::function<void(ManualInput)> callback = nullptr) {
355  manualInput().subscribe(callback);
356  return *this;
357  }
358 
359  SystemState& subscribeLandDetected(std::function<void(LandDetected)> callback = nullptr) {
360  landDetected().subscribe(callback);
361  return *this;
362  }
363 
364  private:
365  std::shared_ptr<SystemStateImpl> _impl;
366 };
367 
369 } // namespace auterion
SDK execution class. All callbacks are called on the same thread.
Definition: auterion.hpp:47
A template class for managing subscriptions to data coming from the vehicle.
Definition: system_state.hpp:220
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:292
VtolState
Represents the VTOL state.
Definition: system_state.hpp:165
Represents angular rates and accelerations in the FLU body frame.
Definition: system_state.hpp:118
Eigen::Vector3f angular_acceleration_flu_rad_s
Definition: system_state.hpp:120
Eigen::Vector3f angular_velocity_flu_rad_s
Definition: system_state.hpp:119
Represents the status of the battery.
Definition: system_state.hpp:148
float discharged_mah
Definition: system_state.hpp:152
float remaining
Definition: system_state.hpp:153
float voltage_v
Definition: system_state.hpp:149
int cell_count
Definition: system_state.hpp:155
Eigen::VectorXf cell_voltage_v
Definition: system_state.hpp:156
float current_a
Definition: system_state.hpp:150
Represents global position using latitude, longitude, and altitude.
Definition: system_state.hpp:84
double longitude_deg
Definition: system_state.hpp:86
Eigen::Vector3d toEigenVector() const
Converts the global position to an Eigen vector.
Definition: system_state.hpp:94
double altitude_amsl_m
Definition: system_state.hpp:87
double latitude_deg
Definition: system_state.hpp:85
Represents the home position in both local ENU and global coordinates.
Definition: system_state.hpp:130
bool was_set_manually
Definition: system_state.hpp:139
double yaw
Definition: system_state.hpp:133
bool global_position_valid
Definition: system_state.hpp:136
bool local_position_valid
Definition: system_state.hpp:137
bool altitude_valid
Definition: system_state.hpp:135
Eigen::Vector3f local_position_enu_m
Definition: system_state.hpp:131
GlobalPosition global_position
Definition: system_state.hpp:132
Represents landed state of the vehicle.
Definition: system_state.hpp:196
bool in_descend
Definition: system_state.hpp:198
Represents local position, velocity, and acceleration in the ENU coordinate system.
Definition: system_state.hpp:63
bool horizontal_position_valid
Definition: system_state.hpp:71
bool horizontal_velocity_valid
Definition: system_state.hpp:73
bool vertical_velocity_valid
Definition: system_state.hpp:74
double heading
Definition: system_state.hpp:68
Eigen::Vector3f velocity_enu_m
Definition: system_state.hpp:65
Eigen::Vector3f acceleration_enu_m
Definition: system_state.hpp:66
Eigen::Vector3f position_enu_m
Definition: system_state.hpp:64
bool dist_bottom_valid
Definition: system_state.hpp:75
bool vertical_position_valid
Definition: system_state.hpp:72
Represents manual input from a user.
Definition: system_state.hpp:179
float pitch
Definition: system_state.hpp:181
float roll
Definition: system_state.hpp:180
Eigen::Matrix< float, 6, 1 > aux
Definition: system_state.hpp:185
float throttle
Definition: system_state.hpp:183
bool sticks_moving
Definition: system_state.hpp:187
float yaw
Definition: system_state.hpp:182
Represents odometry data including attitude and angular rates.
Definition: system_state.hpp:105
Eigen::Vector3f velocity
Definition: system_state.hpp:109
Eigen::Vector3f position
Definition: system_state.hpp:108
Eigen::Vector3f angular_velocity
Definition: system_state.hpp:107
Eigen::Quaternionf attitude
Definition: system_state.hpp:106