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.
guidance_worker.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-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 */
17
18namespace guidance
19{
20 namespace std_ph = std::placeholders;
21
22 GuidanceWorker::GuidanceWorker(const rclcpp::NodeOptions &options)
23 : carma_ros2_utils::CarmaLifecycleNode(options),
24 gsm_(this->get_node_logging_interface())
25 {
26 // Create initial config
27 config_ = Config();
28
29 // Declare parameters
30 config_.spin_rate_hz = declare_parameter<double>("spin_rate_hz", config_.spin_rate_hz);
31 }
32
33 rcl_interfaces::msg::SetParametersResult GuidanceWorker::parameter_update_callback(const std::vector<rclcpp::Parameter> &parameters)
34 {
35 auto error = update_params<double>({{"spin_rate_hz", config_.spin_rate_hz}}, parameters);
36
37 rcl_interfaces::msg::SetParametersResult result;
38
39 result.successful = !error;
40
41 return result;
42 }
43
44 carma_ros2_utils::CallbackReturn GuidanceWorker::handle_on_configure(const rclcpp_lifecycle::State &)
45 {
46 RCLCPP_INFO_STREAM(get_logger(), "GuidanceWorker trying to configure");
47
48 // Reset config
49 config_ = Config();
50
51 // Load parameters
52 get_parameter<double>("spin_rate_hz", config_.spin_rate_hz);
53
54 RCLCPP_INFO_STREAM(get_logger(), "Loaded params: " << config_);
55
56 // Register runtime parameter update callback
57 add_on_set_parameters_callback(std::bind(&GuidanceWorker::parameter_update_callback, this, std_ph::_1));
58
59 // Setup subscribers
60 robot_status_subscriber_ = create_subscription<carma_driver_msgs::msg::RobotEnabled>("robot_status", 5,
61 std::bind(&GuidanceWorker::robot_status_cb, this, std_ph::_1));
62 route_event_subscriber_ = create_subscription<carma_planning_msgs::msg::RouteEvent>("route_event", 5,
63 std::bind(&GuidanceWorker::route_event_cb, this, std_ph::_1));
64 vehicle_status_subscriber_ = create_subscription<autoware_msgs::msg::VehicleStatus>("vehicle_status", 5,
65 std::bind(&GuidanceWorker::vehicle_status_cb, this, std_ph::_1));
66
67 // Setup publishers
68 state_publisher_ = create_publisher<carma_planning_msgs::msg::GuidanceState>("state", 5);
69
70 // Setup service clients
71 enable_client_ = create_client<carma_driver_msgs::srv::SetEnableRobotic>("enable_robotic");
72
73 // Setup service servers
74 guidance_activate_service_server_ = create_service<carma_planning_msgs::srv::SetGuidanceActive>("set_guidance_active",
75 std::bind(&GuidanceWorker::guidance_activation_cb, this, std_ph::_1, std_ph::_2, std_ph::_3));
76
77 // Return success if everything initialized successfully
78 return CallbackReturn::SUCCESS;
79 }
80
81 carma_ros2_utils::CallbackReturn GuidanceWorker::handle_on_activate(const rclcpp_lifecycle::State &prev_state)
82 {
83 // Setup timer
84 auto guidance_spin_period_ms = int ((1 / config_.spin_rate_hz) * 1000); // Conversion rom frequency (Hz) to milliseconds time period
85 spin_timer_ = create_timer(
86 get_clock(),
87 std::chrono::milliseconds(guidance_spin_period_ms),
88 std::bind(&GuidanceWorker::spin_cb, this));
89
90 // Update Guidance State Machine since node activation by the lifecycle system indicates that all required drivers are ready
92
93 return CallbackReturn::SUCCESS;
94 }
95
96 carma_ros2_utils::CallbackReturn GuidanceWorker::handle_on_shutdown(const rclcpp_lifecycle::State &prev_state)
97 {
98 // Update Guidance State Machine with SHUTDOWN signal
100 }
101
102 bool GuidanceWorker::guidance_activation_cb(const std::shared_ptr<rmw_request_id_t>,
103 const std::shared_ptr<carma_planning_msgs::srv::SetGuidanceActive::Request> req,
104 std::shared_ptr<carma_planning_msgs::srv::SetGuidanceActive::Response> resp)
105 {
106 // Translate message type from GuidanceActiveRequest to SetEnableRobotic
107 if(!req->guidance_active)
108 {
109 auto enable_robotic_request = std::make_shared<carma_driver_msgs::srv::SetEnableRobotic::Request>();
110 enable_robotic_request->set = carma_driver_msgs::srv::SetEnableRobotic::Request::DISABLE;
111 enable_client_->async_send_request(enable_robotic_request);
112 }
113
114 gsm_.onSetGuidanceActive(req->guidance_active);
115 resp->guidance_status = (gsm_.getCurrentState() == GuidanceStateMachine::ACTIVE);
116 return true;
117 }
118
120 {
122 auto req = std::make_shared<carma_driver_msgs::srv::SetEnableRobotic::Request>();
123 req->set = carma_driver_msgs::srv::SetEnableRobotic::Request::ENABLE;
124 enable_client_->async_send_request(req);
125 }
126
127 carma_planning_msgs::msg::GuidanceState state;
128 state.state = gsm_.getCurrentState();
129 state_publisher_->publish(state);
130 return true;
131 }
132
133 void GuidanceWorker::route_event_cb(carma_planning_msgs::msg::RouteEvent::UniquePtr msg)
134 {
135 gsm_.onRouteEvent(move(msg));
136 }
137
138 void GuidanceWorker::robot_status_cb(carma_driver_msgs::msg::RobotEnabled::UniquePtr msg)
139 {
140 gsm_.onRoboticStatus(move(msg));
141 }
142
143 void GuidanceWorker::vehicle_status_cb(autoware_msgs::msg::VehicleStatus::UniquePtr msg)
144 {
145 gsm_.onVehicleStatus(move(msg));
146 }
147
148} // guidance
149
150#include "rclcpp_components/register_node_macro.hpp"
151
152// Register the component with class_loader
153RCLCPP_COMPONENTS_REGISTER_NODE(guidance::GuidanceWorker)
void onGuidanceShutdown()
Updates Guidance State Machine with SHUTDOWN signal.
void onRoboticStatus(carma_driver_msgs::msg::RobotEnabled::UniquePtr msg)
Handle robotic_status message from ROS network.
uint8_t getCurrentState()
Get current state machine status.
void onVehicleStatus(autoware_msgs::msg::VehicleStatus::UniquePtr msg)
Handle vehicle status message from ROS network.
void onRouteEvent(carma_planning_msgs::msg::RouteEvent::UniquePtr msg)
Handle route event message.
void onGuidanceInitialized()
Updates Guidance State Machine with INITIALIZED signal.
bool shouldCallSetEnableRobotic()
Indicate if SetEnableRobotic needs to be called in ACTIVE state.
void onSetGuidanceActive(bool msg)
Handle set_guidance_active service call from ROS network.
Worker class for the Guidance node.
bool spin_cb()
Timer callback.
void route_event_cb(carma_planning_msgs::msg::RouteEvent::UniquePtr msg)
Callback for route event messages.
rclcpp::TimerBase::SharedPtr spin_timer_
carma_ros2_utils::SubPtr< autoware_msgs::msg::VehicleStatus > vehicle_status_subscriber_
carma_ros2_utils::CallbackReturn handle_on_activate(const rclcpp_lifecycle::State &)
carma_ros2_utils::SubPtr< carma_driver_msgs::msg::RobotEnabled > robot_status_subscriber_
carma_ros2_utils::ServicePtr< carma_planning_msgs::srv::SetGuidanceActive > guidance_activate_service_server_
GuidanceStateMachine gsm_
carma_ros2_utils::CallbackReturn handle_on_shutdown(const rclcpp_lifecycle::State &prev_state)
GuidanceWorker(const rclcpp::NodeOptions &)
GuidanceWorker constructor.
void robot_status_cb(carma_driver_msgs::msg::RobotEnabled::UniquePtr msg)
Callback for robot enabled messages.
rcl_interfaces::msg::SetParametersResult parameter_update_callback(const std::vector< rclcpp::Parameter > &parameters)
Callback for dynamic parameter updates.
carma_ros2_utils::ClientPtr< carma_driver_msgs::srv::SetEnableRobotic > enable_client_
carma_ros2_utils::PubPtr< carma_planning_msgs::msg::GuidanceState > state_publisher_
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &prev_state)
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::RouteEvent > route_event_subscriber_
void vehicle_status_cb(const autoware_msgs::msg::VehicleStatus::UniquePtr msg)
Callback for vehicle status messages.
bool guidance_activation_cb(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_planning_msgs::srv::SetGuidanceActive::Request > req, std::shared_ptr< carma_planning_msgs::srv::SetGuidanceActive::Response > resp)
Set_guidance_active service callback. User can attempt to activate the guidance system by calling thi...
Stuct containing the algorithm configuration values for GuidanceWorker.