Autoware.Auto
controller_base_node.hpp
Go to the documentation of this file.
1 // Copyright 2019 Christopher Ho
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef CONTROLLER_COMMON_NODES__CONTROLLER_BASE_NODE_HPP_
15 #define CONTROLLER_COMMON_NODES__CONTROLLER_BASE_NODE_HPP_
16 
18 #include <autoware_auto_system_msgs/msg/control_diagnostic.hpp>
19 #include <autoware_auto_planning_msgs/msg/trajectory.hpp>
20 #include <autoware_auto_vehicle_msgs/msg/vehicle_kinematic_state.hpp>
22 #include <tf2/buffer_core.h>
23 #include <tf2_msgs/msg/tf_message.hpp>
24 
25 #include <rclcpp/rclcpp.hpp>
26 
27 #include <exception>
28 #include <memory>
29 #include <list>
30 #include <string>
31 
32 namespace motion
33 {
34 namespace control
35 {
36 namespace controller_common_nodes
37 {
44 using ControllerPtr = std::unique_ptr<controller_common::ControllerBase>;
45 
46 class CONTROLLER_COMMON_NODES_PUBLIC ControllerBaseNode : public rclcpp::Node
47 {
48 public:
50  ControllerBaseNode(const std::string & name, const std::string & ns);
53  const std::string & name,
54  const std::string & ns,
55  const std::string & command_topic,
56  const std::string & state_topic,
57  const std::string & tf_topic,
58  const std::string & trajectory_topic,
59  const std::string & diagnostic_topic,
60  const std::string & static_tf_topic = "static_tf");
61 
62  virtual ~ControllerBaseNode() noexcept = default;
63 
64 protected:
66  void set_controller(ControllerPtr && controller) noexcept;
69  virtual void on_bad_trajectory(std::exception_ptr eptr);
72  virtual void on_bad_compute(std::exception_ptr eptr);
74  void publish(const Command & msg);
75 
76 private:
77  // Common initialization
78  CONTROLLER_COMMON_NODES_LOCAL void init(
79  const std::string & command_topic,
80  const std::string & state_topic,
81  const std::string & tf_topic,
82  const std::string & static_tf_topic,
83  const std::string & trajectory_topic,
84  const std::string & diagnostic_topic);
85  // Callbacks, note passing smart pointers by ref is fine if you're not using ownership
86  // semantics:
87  // stackoverflow.com/questions/3310737/
88  // should-we-pass-a-shared-ptr-by-reference-or-by-value/8741626
89  CONTROLLER_COMMON_NODES_LOCAL void on_tf(const TFMessage::SharedPtr & msg);
90  CONTROLLER_COMMON_NODES_LOCAL void on_static_tf(const TFMessage::SharedPtr & msg);
91  CONTROLLER_COMMON_NODES_LOCAL void on_trajectory(const Trajectory::SharedPtr & msg);
92  CONTROLLER_COMMON_NODES_LOCAL void on_state(const State::SharedPtr & msg);
93  // Main computation, false if failure (due to missing tf?)
94  CONTROLLER_COMMON_NODES_LOCAL bool try_compute(const State & state);
95  // Try to compute control commands from old states in the context of new trajectories and tfs
96  CONTROLLER_COMMON_NODES_LOCAL void retry_compute();
97 
98 
99  rclcpp::Subscription<State>::SharedPtr m_state_sub{};
100  rclcpp::Subscription<TFMessage>::SharedPtr m_tf_sub{};
101  rclcpp::Subscription<TFMessage>::SharedPtr m_static_tf_sub{};
102  rclcpp::Subscription<Trajectory>::SharedPtr m_trajectory_sub{};
103  rclcpp::Publisher<Command>::SharedPtr m_command_pub{};
104  rclcpp::Publisher<Diagnostic>::SharedPtr m_diagnostic_pub{};
105  tf2::BufferCore m_tf_buffer{tf2::BUFFER_CORE_DEFAULT_CACHE_TIME};
106  // TODO(c.ho) diagnostics
107  ControllerPtr m_controller{nullptr};
108  std::list<State> m_uncomputed_states{};
109 }; // class ControllerBaseNode
110 } // namespace controller_common_nodes
111 } // namespace control
112 } // namespace motion
113 
114 #endif // CONTROLLER_COMMON_NODES__CONTROLLER_BASE_NODE_HPP_
motion::control::controller_common_nodes::ControllerBaseNode
Definition: controller_base_node.hpp:46
motion::motion_testing_nodes::TFMessage
tf2_msgs::msg::TFMessage TFMessage
Definition: motion_testing_publisher.hpp:34
motion::motion_common::Command
autoware_auto_vehicle_msgs::msg::VehicleControlCommand Command
Definition: motion_common.hpp:38
motion::motion_common::State
autoware_auto_vehicle_msgs::msg::VehicleKinematicState State
Definition: motion_common.hpp:40
controller_base.hpp
motion::motion_testing_nodes::TransformStamped
geometry_msgs::msg::TransformStamped TransformStamped
Definition: motion_testing_publisher.hpp:37
setup.name
name
Definition: setup.py:6
motion::motion_common::Diagnostic
autoware_auto_system_msgs::msg::ControlDiagnostic Diagnostic
Definition: motion_common.hpp:39
visibility_control.hpp
motion
Definition: controller_base.hpp:31
motion::motion_common::Trajectory
autoware_auto_planning_msgs::msg::Trajectory Trajectory
Definition: motion_common.hpp:41
motion::control::controller_common_nodes::ControllerPtr
std::unique_ptr< controller_common::ControllerBase > ControllerPtr
Definition: controller_base_node.hpp:44