Auterion App SDK
Auterion SDK is a library that can be used by AuterionOS apps to communicate with the system.
Loading...
Searching...
No Matches
auterion.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 <auterion_sdk/exception/exception.hpp>
36#include <rclcpp/rclcpp.hpp>
37
38namespace auterion {
43class ParameterManager;
44
50struct RunOptions {
59 skip_flight_controller_connection_check = skip;
60 return *this;
61 }
62
77 skip_message_compatibility_check = skip;
78 return *this;
79 }
80
92 RunOptions& withFlightControllerConnectionTimeout(std::chrono::seconds timeout) {
93 if (timeout.count() <= 0) {
95 "flight_controller_connection_timeout must be positive (got: " +
96 std::to_string(timeout.count()) + "s)."};
97 }
98 flight_controller_connection_timeout = timeout;
99 return *this;
100 }
101
102 void validate() const {
103 if (skip_flight_controller_connection_check && !skip_message_compatibility_check) {
105 "Cannot check message compatibility if connection check is skipped. "
106 "Enable flight controller connection check, or disable message compatibility "
107 "check.");
108 }
109 }
110
111 bool skip_flight_controller_connection_check{false};
112 bool skip_message_compatibility_check{true};
113 std::chrono::seconds flight_controller_connection_timeout{15};
114};
115
119class SDK {
120 public:
121 SDK(int argc, char** argv, const std::string& node_name);
122
123 ~SDK();
124
125 [[deprecated("Use run(RunOptions) instead, this method will be removed in a future release.")]]
131 void
133 _skip_flight_controller_connection_check = skip;
134 }
135
136 void run(RunOptions options = RunOptions());
137
138 rclcpp::Node::SharedPtr defaultRosHandle() const { return _default_ros_node; }
139
140 rclcpp::Time now() const { return _default_ros_node->now(); }
141
142 void addNode(rclcpp::Node::SharedPtr node) { _executor->add_node(node); }
143 void removeNode(rclcpp::Node::SharedPtr node) { _executor->remove_node(node); }
144
145 const rclcpp::executors::SingleThreadedExecutor::UniquePtr& executor() const {
146 return _executor;
147 }
148
149 // Function to verify supported parameter types at compile time
150 template <typename T>
151 static constexpr void verify_param() {
152 static_assert(std::is_same<T, bool>::value || std::is_same<T, int64_t>::value ||
153 std::is_same<T, int>::value || std::is_same<T, float>::value ||
154 std::is_same<T, double>::value || std::is_same<T, std::string>::value ||
155 std::is_same<T, std::vector<bool>>::value ||
156 std::is_same<T, std::vector<int64_t>>::value ||
157 std::is_same<T, std::vector<int>>::value ||
158 std::is_same<T, std::vector<float>>::value ||
159 std::is_same<T, std::vector<double>>::value ||
160 std::is_same<T, std::vector<std::string>>::value,
161 "Unsupported parameter data type");
162 }
163
175 template <typename T>
176 T getParameter(const std::string& name,
177 std::function<void(const T&)> on_change_callback = nullptr) {
178 verify_param<T>();
179 }
180
191 template <typename T>
192 void setParameter(const std::string& name, const T& value) {
193 verify_param<T>();
194 }
195
196 private:
197 rclcpp::Node::SharedPtr _default_ros_node;
198 rclcpp::executors::SingleThreadedExecutor::UniquePtr _executor;
199 bool _skip_flight_controller_connection_check{false}; // deprecating, use RunOptions
200 std::unique_ptr<ParameterManager> _parameter_manager;
201};
202
203template <>
204bool SDK::getParameter(const std::string& name,
205 std::function<void(const bool&)> on_change_callback);
206
207template <>
208int64_t SDK::getParameter(const std::string& name,
209 std::function<void(const int64_t&)> on_change_callback);
210
211template <>
212int SDK::getParameter(const std::string& name, std::function<void(const int&)> on_change_callback);
213
214template <>
215float SDK::getParameter(const std::string& name,
216 std::function<void(const float&)> on_change_callback);
217
218template <>
219double SDK::getParameter(const std::string& name,
220 std::function<void(const double&)> on_change_callback);
221
222template <>
223std::string SDK::getParameter(const std::string& name,
224 std::function<void(const std::string&)> on_change_callback);
225
226template <>
227std::vector<bool> SDK::getParameter(
228 const std::string& name, std::function<void(const std::vector<bool>&)> on_change_callback);
229
230template <>
231std::vector<int64_t> SDK::getParameter(
232 const std::string& name, std::function<void(const std::vector<int64_t>&)> on_change_callback);
233
234template <>
235std::vector<int> SDK::getParameter(const std::string& name,
236 std::function<void(const std::vector<int>&)> on_change_callback);
237
238template <>
239std::vector<float> SDK::getParameter(
240 const std::string& name, std::function<void(const std::vector<float>&)> on_change_callback);
241
242template <>
243std::vector<double> SDK::getParameter(
244 const std::string& name, std::function<void(const std::vector<double>&)> on_change_callback);
245
246template <>
247std::vector<std::string> SDK::getParameter(
248 const std::string& name,
249 std::function<void(const std::vector<std::string>&)> on_change_callback);
250
251template <>
252void SDK::setParameter(const std::string& name, const bool& value);
253
254template <>
255void SDK::setParameter(const std::string& name, const int64_t& value);
256
257template <>
258void SDK::setParameter(const std::string& name, const int& value);
259
260template <>
261void SDK::setParameter(const std::string& name, const float& value);
262
263template <>
264void SDK::setParameter(const std::string& name, const double& value);
265
266template <>
267void SDK::setParameter(const std::string& name, const std::string& value);
268
269template <>
270void SDK::setParameter(const std::string& name, const std::vector<bool>& value);
271
272template <>
273void SDK::setParameter(const std::string& name, const std::vector<int64_t>& value);
274
275template <>
276void SDK::setParameter(const std::string& name, const std::vector<int>& value);
277
278template <>
279void SDK::setParameter(const std::string& name, const std::vector<float>& value);
280
281template <>
282void SDK::setParameter(const std::string& name, const std::vector<double>& value);
283
284template <>
285void SDK::setParameter(const std::string& name, const std::vector<std::string>& value);
286
288} // namespace auterion
Exception thrown for invalid arguments.
Definition exception.hpp:82
SDK execution class. All callbacks are called on the same thread.
Definition auterion.hpp:119
T getParameter(const std::string &name, std::function< void(const T &)> on_change_callback=nullptr)
Get a parameter value and optionally register a callback for changes.
Definition auterion.hpp:176
void setParameter(const std::string &name, const T &value)
Set a parameter.
Definition auterion.hpp:192
void setSkipFlightControllerConnectionCheck(bool skip)
Definition auterion.hpp:132
Options for SDK run() method.
Definition auterion.hpp:50
RunOptions & withSkipFlightControllerConnectionCheck(bool skip)
Skip the check for flight controller connection.
Definition auterion.hpp:58
RunOptions & withFlightControllerConnectionTimeout(std::chrono::seconds timeout)
Set the timeout for the flight controller connection check.
Definition auterion.hpp:92
RunOptions & withSkipMessageCompatibilityCheck(bool skip)
Skip the check for message compatibility.
Definition auterion.hpp:76