PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
Loading...
Searching...
No Matches
setpoint_base.hpp
1/****************************************************************************
2 * Copyright (c) 2023 PX4 Development Team.
3 * SPDX-License-Identifier: BSD-3-Clause
4 ****************************************************************************/
5
6#pragma once
7
8#include <cstdint>
9#include <functional>
10#include <memory>
11#include <px4_msgs/msg/setpoint_config.hpp>
12#include <px4_msgs/msg/setpoint_config_reply.hpp>
13#include <rclcpp/rclcpp.hpp>
14
15#include "context.hpp"
16#include "exception.hpp"
17
18namespace px4_ros2 {
19
20class SetpointBase : public std::enable_shared_from_this<SetpointBase> {
21 public:
22 using ShouldActivateCB = std::function<void()>;
23 using SetpointType = decltype(px4_msgs::msg::SetpointConfig::type);
24
25 explicit SetpointBase(Context& context) { context.addSetpointType(this); }
26
27 virtual ~SetpointBase() = default;
28
29 std::shared_ptr<SetpointBase> getSharedPtr()
30 {
31 try {
32 return shared_from_this();
33 } catch (const std::bad_weak_ptr& exception) {
34 throw Exception("Setpoint must be instantiated with std::make_shared<>");
35 }
36 return {};
37 }
38
42 virtual SetpointType getSetpointType() = 0;
43
51 virtual void clearOptionalRequirements(px4_msgs::msg::SetpointConfigReply& setpoint_config_reply)
52 {
53 }
54
55 virtual float desiredUpdateRateHz() { return 50.f; }
56
57 void setShouldActivateCallback(const ShouldActivateCB& should_activate_cb)
58 {
59 _should_activate_cb = should_activate_cb;
60 }
61 void setActive(bool active) { _active = active; }
62
63 protected:
64 void onUpdate()
65 {
66 if (!_active && _should_activate_cb) {
67 _should_activate_cb();
68 }
69 }
70
71 private:
72 ShouldActivateCB _should_activate_cb;
73 bool _active{false};
74};
75
76} /* namespace px4_ros2 */
Definition context.hpp:18
Definition exception.hpp:14
Definition setpoint_base.hpp:20
virtual void clearOptionalRequirements(px4_msgs::msg::SetpointConfigReply &setpoint_config_reply)
Definition setpoint_base.hpp:51
virtual SetpointType getSetpointType()=0