Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
plugin_base_node.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16#include <functional>
17#include <rclcpp/create_publisher.hpp>
18
20
22{
23 namespace std_ph = std::placeholders;
24
25 PluginBaseNode::PluginBaseNode(const rclcpp::NodeOptions &options)
26 : carma_ros2_utils::CarmaLifecycleNode(options)
27 {
28
29 // Setup discovery timer to publish onto the plugin_discovery_pub
30 discovery_timer_ = create_timer(
31 get_clock(),
32 std::chrono::milliseconds(500), // 2 Hz frequency to account for 1Hz maneuver planning frequency
34 }
35
37 {
38 if (wm_listener_)
39 return; // Already initialized
40
41
42 wm_listener_ = std::make_shared<carma_wm::WMListener>(
43 this->get_node_base_interface(), this->get_node_logging_interface(),
44 this->get_node_topics_interface(), this->get_node_parameters_interface()
45 );
46
47 wm_ = wm_listener_->getWorldModel();
48 }
49
51 {
52 return std::string(get_name());
53 }
54
56 {
57 return std::string(get_namespace()) + "/" + std::string(get_name());
58 }
59
60 std::shared_ptr<carma_wm::WMListener> PluginBaseNode::get_world_model_listener()
61 {
63 return wm_listener_;
64 }
65
67 {
69 return wm_;
70 }
71
73 // Determine the plugin activation state by checking which lifecycle state we are in.
74 // If we are active then the plugin is active otherwise the plugin is inactive
75 return get_current_state().id() == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE;
76 }
77
78 carma_ros2_utils::CallbackReturn PluginBaseNode::on_activate_plugin()
79 {
80 return carma_ros2_utils::CallbackReturn::SUCCESS;
81 }
82
83 carma_ros2_utils::CallbackReturn PluginBaseNode::on_deactivate_plugin()
84 {
85 return carma_ros2_utils::CallbackReturn::SUCCESS;
86 }
87
88 carma_ros2_utils::CallbackReturn PluginBaseNode::on_cleanup_plugin()
89 {
90 return carma_ros2_utils::CallbackReturn::SUCCESS;
91 }
92
93 carma_ros2_utils::CallbackReturn PluginBaseNode::on_shutdown_plugin()
94 {
95 return carma_ros2_utils::CallbackReturn::SUCCESS;
96 }
97
98 carma_ros2_utils::CallbackReturn PluginBaseNode::on_error_plugin(const std::string &)
99 {
100 // On error should default to failure so user must explicitly implement error handling to get any other behavior
101 return carma_ros2_utils::CallbackReturn::FAILURE;
102 }
103
104 carma_ros2_utils::CallbackReturn PluginBaseNode::handle_on_configure(const rclcpp_lifecycle::State &)
105 {
106 return on_configure_plugin();
107 }
108
109 carma_ros2_utils::CallbackReturn PluginBaseNode::handle_on_activate(const rclcpp_lifecycle::State &)
110 {
111 return on_activate_plugin();
112 }
113
114 carma_ros2_utils::CallbackReturn PluginBaseNode::handle_on_deactivate(const rclcpp_lifecycle::State &)
115 {
116 return on_deactivate_plugin();
117 }
118
119 carma_ros2_utils::CallbackReturn PluginBaseNode::handle_on_cleanup(const rclcpp_lifecycle::State &)
120 {
121 return on_cleanup_plugin();
122 }
123
124 carma_ros2_utils::CallbackReturn PluginBaseNode::handle_on_shutdown(const rclcpp_lifecycle::State &)
125 {
126 return on_shutdown_plugin();
127 }
128
129 carma_ros2_utils::CallbackReturn PluginBaseNode::handle_on_error(const rclcpp_lifecycle::State &, const std::string &exception_string)
130 {
131 return on_error_plugin(exception_string);
132 }
133
135 {
136 // Base class returns unknown.
137 // Its expected that 2nd level extending classes (strategic, tactical, control) will return correct type by overriding
138 return carma_planning_msgs::msg::Plugin::UNKNOWN;
139 }
140
142 {
143
144 if (!plugin_discovery_pub_) // If the publisher has not been initalized then initialize it.
145 {
146 // Setup plugin publisher discovery which should bypass lifecycle behavior to ensure plugins are found
147 // prior to them needing to be activated.
148 // NOTE: Any other topics which need to be setup in the future should use handle_on_configure and the default this->create_publisher method to get a lifecycle publisher instead
149 auto this_shared = shared_from_this(); // Usage of shared_from_this() means this cannot be done in the constructor thus it is delegated to the timer callback
150 plugin_discovery_pub_ = rclcpp::create_publisher<carma_planning_msgs::msg::Plugin>(this_shared, "plugin_discovery", 1);
151
152 }
153
154 carma_planning_msgs::msg::Plugin msg;
155 msg.name = get_plugin_name_and_ns();
156 msg.version_id = get_version_id();
157 msg.type = get_type();
158 msg.available = get_availability();
159 msg.activated = get_activation_status();
160 msg.capability = get_capability();
161
162 plugin_discovery_pub_->publish(msg);
163 }
164
165} // carma_guidance_plugins
166
virtual uint8_t get_type()
Returns the type of this plugin according to the carma_planning_msgs::Plugin type enum....
virtual bool get_activation_status() final
Returns the activation status of this plugin. The plugins API callbacks will only be triggered when t...
virtual carma_ros2_utils::CallbackReturn on_deactivate_plugin()
Method which is triggered when this plugin is moved from the ACTIVE to INACTIVE states....
carma_ros2_utils::CallbackReturn handle_on_deactivate(const rclcpp_lifecycle::State &) override
carma_ros2_utils::CallbackReturn handle_on_error(const rclcpp_lifecycle::State &, const std::string &exception_string) override
carma_wm::WorldModelConstPtr wm_
virtual std::string get_capability()=0
Get the capability string representing this plugins capabilities Method must be overriden by extendin...
carma_ros2_utils::CallbackReturn handle_on_cleanup(const rclcpp_lifecycle::State &) override
std::string get_plugin_name() const
Return the name of this plugin.
virtual carma_wm::WorldModelConstPtr get_world_model() final
Method to return the default world model provided as a convience by this base class If this method or...
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &) override
virtual carma_ros2_utils::CallbackReturn on_cleanup_plugin()
Method which is triggered when this plugin is moved from the INACTIVE to UNCONFIGURED states....
virtual bool get_availability()=0
Get the availability status of this plugin based on the current operating environment....
PluginBaseNode(const rclcpp::NodeOptions &)
PluginBaseNode constructor.
virtual carma_ros2_utils::CallbackReturn on_configure_plugin()=0
Method which is triggered when this plugin is moved from the UNCONFIGURED to INACTIVE states....
rclcpp::Publisher< carma_planning_msgs::msg::Plugin >::SharedPtr plugin_discovery_pub_
Publisher for plugin discovery. This publisher will begin publishing.
void lazy_wm_initialization()
Helper function for lazy initialization of wm_listener_. If already initialized method returns (ie....
std::shared_ptr< carma_wm::WMListener > wm_listener_
virtual std::shared_ptr< carma_wm::WMListener > get_world_model_listener() final
Method to return the default world model listener provided as a convience by this base class If this ...
virtual carma_ros2_utils::CallbackReturn on_shutdown_plugin()
Method which is triggered when this plugin is moved from any state to FINALIZED This method should be...
carma_ros2_utils::CallbackReturn handle_on_activate(const rclcpp_lifecycle::State &) override
std::string get_plugin_name_and_ns() const
Return the name of this plugin with namespace. NOTE: If only the name of the plugin is required,...
virtual carma_ros2_utils::CallbackReturn on_error_plugin(const std::string &exception_string)
Method which is triggered when an unhandled exception occurs in this plugin This method should be use...
virtual std::string get_version_id()=0
Returns the version id of this plugin.
void discovery_timer_callback()
Callback for the plugin discovery timer which will publish the plugin discovery message.
carma_ros2_utils::CallbackReturn handle_on_shutdown(const rclcpp_lifecycle::State &) override
virtual carma_ros2_utils::CallbackReturn on_activate_plugin()
Method which is triggered when this plugin is moved from the INACTIVE to ACTIVE states....
rclcpp::TimerBase::SharedPtr discovery_timer_
Timer to trigger publication of the plugin discovery message at a fixed frequency.
std::shared_ptr< const WorldModel > WorldModelConstPtr
Definition: WorldModel.hpp:452