Auterion App SDK
Auterion SDK is a library that can be used by AuterionOS apps to communicate with the system.
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 
38 namespace auterion {
43 class ParameterManager;
44 
50 struct RunOptions {
59  skip_flight_controller_connection_check = skip;
60  return *this;
61  }
62 
77  skip_message_compatibility_check = skip;
78  return *this;
79  }
80 
81  void validate() const {
82  if (skip_flight_controller_connection_check && !skip_message_compatibility_check) {
84  "Cannot check message compatibility if connection check is skipped. "
85  "Enable flight controller connection check, or disable message compatibility "
86  "check.");
87  }
88  }
89 
90  bool skip_flight_controller_connection_check{false};
91  bool skip_message_compatibility_check{true};
92 };
93 
97 class SDK {
98  public:
99  SDK(int argc, char** argv, const std::string& node_name);
100 
101  ~SDK();
102 
103  [[deprecated("Use run(RunOptions) instead, this method will be removed in a future release.")]]
109  void
111  _skip_flight_controller_connection_check = skip;
112  }
113 
114  void run(RunOptions options = RunOptions());
115 
116  rclcpp::Node::SharedPtr defaultRosHandle() const { return _default_ros_node; }
117 
118  rclcpp::Time now() const { return _default_ros_node->now(); }
119 
120  void addNode(rclcpp::Node::SharedPtr node) { _executor->add_node(node); }
121  void removeNode(rclcpp::Node::SharedPtr node) { _executor->remove_node(node); }
122 
123  const rclcpp::executors::SingleThreadedExecutor::UniquePtr& executor() const {
124  return _executor;
125  }
126 
127  // Function to verify supported parameter types at compile time
128  template <typename T>
129  static constexpr void verify_param() {
130  static_assert(std::is_same<T, bool>::value || std::is_same<T, int64_t>::value ||
131  std::is_same<T, int>::value || std::is_same<T, float>::value ||
132  std::is_same<T, double>::value || std::is_same<T, std::string>::value ||
133  std::is_same<T, std::vector<bool>>::value ||
134  std::is_same<T, std::vector<int64_t>>::value ||
135  std::is_same<T, std::vector<int>>::value ||
136  std::is_same<T, std::vector<float>>::value ||
137  std::is_same<T, std::vector<double>>::value ||
138  std::is_same<T, std::vector<std::string>>::value,
139  "Unsupported parameter data type");
140  }
141 
153  template <typename T>
154  T getParameter(const std::string& name,
155  std::function<void(const T&)> on_change_callback = nullptr) {
156  verify_param<T>();
157  }
158 
169  template <typename T>
170  void setParameter(const std::string& name, const T& value) {
171  verify_param<T>();
172  }
173 
174  private:
175  rclcpp::Node::SharedPtr _default_ros_node;
176  rclcpp::executors::SingleThreadedExecutor::UniquePtr _executor;
177  bool _skip_flight_controller_connection_check{false}; // deprecating, use RunOptions
178  std::unique_ptr<ParameterManager> _parameter_manager;
179 };
180 
181 template <>
182 bool SDK::getParameter(const std::string& name,
183  std::function<void(const bool&)> on_change_callback);
184 
185 template <>
186 int64_t SDK::getParameter(const std::string& name,
187  std::function<void(const int64_t&)> on_change_callback);
188 
189 template <>
190 int SDK::getParameter(const std::string& name, std::function<void(const int&)> on_change_callback);
191 
192 template <>
193 float SDK::getParameter(const std::string& name,
194  std::function<void(const float&)> on_change_callback);
195 
196 template <>
197 double SDK::getParameter(const std::string& name,
198  std::function<void(const double&)> on_change_callback);
199 
200 template <>
201 std::string SDK::getParameter(const std::string& name,
202  std::function<void(const std::string&)> on_change_callback);
203 
204 template <>
205 std::vector<bool> SDK::getParameter(
206  const std::string& name, std::function<void(const std::vector<bool>&)> on_change_callback);
207 
208 template <>
209 std::vector<int64_t> SDK::getParameter(
210  const std::string& name, std::function<void(const std::vector<int64_t>&)> on_change_callback);
211 
212 template <>
213 std::vector<int> SDK::getParameter(const std::string& name,
214  std::function<void(const std::vector<int>&)> on_change_callback);
215 
216 template <>
217 std::vector<float> SDK::getParameter(
218  const std::string& name, std::function<void(const std::vector<float>&)> on_change_callback);
219 
220 template <>
221 std::vector<double> SDK::getParameter(
222  const std::string& name, std::function<void(const std::vector<double>&)> on_change_callback);
223 
224 template <>
225 std::vector<std::string> SDK::getParameter(
226  const std::string& name,
227  std::function<void(const std::vector<std::string>&)> on_change_callback);
228 
229 template <>
230 void SDK::setParameter(const std::string& name, const bool& value);
231 
232 template <>
233 void SDK::setParameter(const std::string& name, const int64_t& value);
234 
235 template <>
236 void SDK::setParameter(const std::string& name, const int& value);
237 
238 template <>
239 void SDK::setParameter(const std::string& name, const float& value);
240 
241 template <>
242 void SDK::setParameter(const std::string& name, const double& value);
243 
244 template <>
245 void SDK::setParameter(const std::string& name, const std::string& value);
246 
247 template <>
248 void SDK::setParameter(const std::string& name, const std::vector<bool>& value);
249 
250 template <>
251 void SDK::setParameter(const std::string& name, const std::vector<int64_t>& value);
252 
253 template <>
254 void SDK::setParameter(const std::string& name, const std::vector<int>& value);
255 
256 template <>
257 void SDK::setParameter(const std::string& name, const std::vector<float>& value);
258 
259 template <>
260 void SDK::setParameter(const std::string& name, const std::vector<double>& value);
261 
262 template <>
263 void SDK::setParameter(const std::string& name, const std::vector<std::string>& value);
264 
266 } // 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:97
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:154
void setParameter(const std::string &name, const T &value)
Set a parameter.
Definition: auterion.hpp:170
void setSkipFlightControllerConnectionCheck(bool skip)
Definition: auterion.hpp:110
Options for SDK run() method.
Definition: auterion.hpp:50
RunOptions & withSkipMessageCompatibilityCheck(bool skip)
Skip the check for message compatibility.
Definition: auterion.hpp:76
RunOptions & withSkipFlightControllerConnectionCheck(bool skip)
Skip the check for flight controller connection.
Definition: auterion.hpp:58