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  ARMED,
49  HOME_POSITION,
50  BATTERY,
51  VTOL_STATE,
52  MANUAL_INPUT,
53  LAND_DETECTED,
54 };
55 
62 struct LocalPosition {
63  Eigen::Vector3f position_enu_m;
64  Eigen::Vector3f velocity_enu_m;
65  Eigen::Vector3f acceleration_enu_m;
67  double heading;
68  float dist_bottom;
75 };
76 
84  double latitude_deg;
85  double longitude_deg;
86  double altitude_amsl_m;
93  inline Eigen::Vector3d toEigenVector() const {
94  return Eigen::Vector3d{latitude_deg, longitude_deg, altitude_amsl_m};
95  }
96 };
97 
104 struct AngularRates {
105  Eigen::Vector3f angular_velocity_enu_rad_s;
108 };
109 
116 struct HomePosition {
117  Eigen::Vector3f local_position_enu_m;
119  double yaw;
126 };
127 
135  float voltage_v;
136  float current_a;
139  float remaining;
142  Eigen::VectorXf cell_voltage_v;
143 };
144 
151 enum VtolState {
152  UNDEFINED,
153  TRANSITION_TO_FIXED_WING,
154  TRANSITION_TO_MULTICOPTER,
155  MULTICOPTER,
156  FIXED_WING
157 };
158 
165 struct ManualInput {
166  float roll;
167  float pitch;
168  float yaw;
169  float throttle;
171  Eigen::Matrix<float, 6, 1> aux;
174 };
175 
182 struct LandDetected {
183  bool landed;
184  bool in_descend;
185 };
186 
188 class SDK;
189 
190 template <SubscriptionType S, typename DataType>
191 class SubscriptionImpl;
192 
193 class SystemStateImpl;
195 
205 template <SubscriptionType S, typename DataType>
207  public:
208  Subscription(const std::shared_ptr<SubscriptionImpl<S, DataType>>& impl);
209 
218  void onUpdate(std::function<void(DataType)> callback);
219 
228  void subscribe(std::function<void(DataType)> callback);
229 
235  DataType last() const;
236 
242  bool isLastValid() const;
243 
244  private:
245  std::shared_ptr<SubscriptionImpl<S, DataType>> _impl;
246 };
247 
278 class SystemState {
279  public:
280  SystemState(SDK& sdk);
281  ~SystemState() = default;
282 
293 
294  SystemState& subscribeLocalPosition(std::function<void(LocalPosition)> callback = nullptr) {
295  localPosition().subscribe(callback);
296  return *this;
297  }
298 
299  SystemState& subscribeGlobalPosition(std::function<void(GlobalPosition)> callback = nullptr) {
300  globalPosition().subscribe(callback);
301  return *this;
302  }
303 
304  SystemState& subscribeAttitude(std::function<void(Eigen::Quaternionf)> callback = nullptr) {
305  attitude().subscribe(callback);
306  return *this;
307  }
308 
309  SystemState& subscribeAngularRates(std::function<void(AngularRates)> callback = nullptr) {
310  angularRates().subscribe(callback);
311  return *this;
312  }
313 
314  SystemState& subscribeArmed(std::function<void(bool)> callback = nullptr) {
315  armed().subscribe(callback);
316  return *this;
317  }
318 
319  SystemState& subscribeHomePosition(std::function<void(HomePosition)> callback = nullptr) {
320  homePosition().subscribe(callback);
321  return *this;
322  }
323 
324  SystemState& subscribeBattery(std::function<void(BatteryStatus)> callback = nullptr) {
325  battery().subscribe(callback);
326  return *this;
327  }
328 
329  SystemState& subscribeVtolState(std::function<void(VtolState)> callback = nullptr) {
330  vtolState().subscribe(callback);
331  return *this;
332  }
333 
334  SystemState& subscribeManualInput(std::function<void(ManualInput)> callback = nullptr) {
335  manualInput().subscribe(callback);
336  return *this;
337  }
338 
339  SystemState& subscribeLandDetected(std::function<void(LandDetected)> callback = nullptr) {
340  landDetected().subscribe(callback);
341  return *this;
342  }
343 
344  private:
345  std::shared_ptr<SystemStateImpl> _impl;
346 };
347 
349 } // namespace auterion
SDK execution class. All callbacks are called on the same thread.
Definition: auterion.hpp:45
A template class for managing subscriptions to data coming from the vehicle.
Definition: system_state.hpp:206
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:278
VtolState
Represents the VTOL state.
Definition: system_state.hpp:151
Represents angular rates and accelerations in the ENU coordinate system.
Definition: system_state.hpp:104
Eigen::Vector3f angular_velocity_enu_rad_s
Definition: system_state.hpp:105
Eigen::Vector3f angular_acceleration_enu_rad_s
Definition: system_state.hpp:106
Represents the status of the battery.
Definition: system_state.hpp:134
float discharged_mah
Definition: system_state.hpp:138
float remaining
Definition: system_state.hpp:139
float voltage_v
Definition: system_state.hpp:135
int cell_count
Definition: system_state.hpp:141
Eigen::VectorXf cell_voltage_v
Definition: system_state.hpp:142
float current_a
Definition: system_state.hpp:136
Represents global position using latitude, longitude, and altitude.
Definition: system_state.hpp:83
double longitude_deg
Definition: system_state.hpp:85
Eigen::Vector3d toEigenVector() const
Converts the global position to an Eigen vector.
Definition: system_state.hpp:93
double altitude_amsl_m
Definition: system_state.hpp:86
double latitude_deg
Definition: system_state.hpp:84
Represents the home position in both local ENU and global coordinates.
Definition: system_state.hpp:116
bool was_set_manually
Definition: system_state.hpp:125
double yaw
Definition: system_state.hpp:119
bool global_position_valid
Definition: system_state.hpp:122
bool local_position_valid
Definition: system_state.hpp:123
bool altitude_valid
Definition: system_state.hpp:121
Eigen::Vector3f local_position_enu_m
Definition: system_state.hpp:117
GlobalPosition global_position
Definition: system_state.hpp:118
Represents landed state of the vehicle.
Definition: system_state.hpp:182
bool in_descend
Definition: system_state.hpp:184
Represents local position, velocity, and acceleration in the ENU coordinate system.
Definition: system_state.hpp:62
bool horizontal_position_valid
Definition: system_state.hpp:70
bool horizontal_velocity_valid
Definition: system_state.hpp:72
bool vertical_velocity_valid
Definition: system_state.hpp:73
double heading
Definition: system_state.hpp:67
Eigen::Vector3f velocity_enu_m
Definition: system_state.hpp:64
Eigen::Vector3f acceleration_enu_m
Definition: system_state.hpp:65
Eigen::Vector3f position_enu_m
Definition: system_state.hpp:63
bool dist_bottom_valid
Definition: system_state.hpp:74
bool vertical_position_valid
Definition: system_state.hpp:71
Represents manual input from a user.
Definition: system_state.hpp:165
float pitch
Definition: system_state.hpp:167
float roll
Definition: system_state.hpp:166
Eigen::Matrix< float, 6, 1 > aux
Definition: system_state.hpp:171
float throttle
Definition: system_state.hpp:169
bool sticks_moving
Definition: system_state.hpp:173
float yaw
Definition: system_state.hpp:168