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.
port_drayage_plugin.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-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 port_drayage_plugin
19{
20 namespace std_ph = std::placeholders;
21
22 PortDrayagePlugin::PortDrayagePlugin(const rclcpp::NodeOptions &options)
23 : carma_ros2_utils::CarmaLifecycleNode(options),
24 pdw_(get_node_logging_interface(), get_clock(),
25 std::bind(&PortDrayagePlugin::publishMobilityOperation, this, std_ph::_1),
26 std::bind(&PortDrayagePlugin::publishUIInstructions, this, std_ph::_1),
27 std::bind(&PortDrayagePlugin::callSetActiveRouteClient, this, std_ph::_1))
28 {
29 // Create initial config
30 config_ = Config();
31
32 // Declare parameters
33 config_.cmv_id = declare_parameter<std::string>("vehicle_id", config_.cmv_id);
34 config_.cargo_id = declare_parameter<std::string>("cargo_id", config_.cargo_id);
35 config_.enable_port_drayage = declare_parameter<bool>("enable_port_drayage", config_.enable_port_drayage);
36 config_.starting_at_staging_area = declare_parameter<bool>("starting_at_staging_area", config_.starting_at_staging_area);
37 }
38
39 rcl_interfaces::msg::SetParametersResult PortDrayagePlugin::parameter_update_callback(const std::vector<rclcpp::Parameter> &parameters)
40 {
41 auto error = update_params<std::string>({
42 {"vehicle_id", config_.cmv_id},
43 {"cargo_id", config_.cargo_id}}, parameters);
44 auto error_2 = update_params<bool>({
45 {"enable_port_drayage", config_.enable_port_drayage},
46 {"starting_at_staging_area", config_.starting_at_staging_area}}, parameters);
47
48 rcl_interfaces::msg::SetParametersResult result;
49
50 result.successful = !error && !error_2;
51
52 if (result.successful) {
53 // Set PortDrayageWorker parameters
58 }
59
60 return result;
61 }
62
63 carma_ros2_utils::CallbackReturn PortDrayagePlugin::handle_on_configure(const rclcpp_lifecycle::State &)
64 {
65 RCLCPP_INFO_STREAM(get_logger(), "port_drayage_plugin trying to configure");
66
67 // Reset config
68 config_ = Config();
69
70 // Load parameters
71 get_parameter<std::string>("vehicle_id", config_.cmv_id);
72 get_parameter<std::string>("cargo_id", config_.cargo_id);
73 get_parameter<bool>("enable_port_drayage", config_.enable_port_drayage);
74 get_parameter<bool>("starting_at_staging_area", config_.starting_at_staging_area);
75
76 RCLCPP_INFO_STREAM(get_logger(), "Loaded params: " << config_);
77
78 // Register runtime parameter update callback
79 add_on_set_parameters_callback(std::bind(&PortDrayagePlugin::parameter_update_callback, this, std_ph::_1));
80
81 // Setup subscribers
82 pose_subscriber_ = create_subscription<geometry_msgs::msg::PoseStamped>("current_pose", 5,
83 std::bind(&PortDrayageWorker::onNewPose, &pdw_, std_ph::_1));
84 inbound_mobility_operation_subscriber_ = create_subscription<carma_v2x_msgs::msg::MobilityOperation>("incoming_mobility_operation", 5,
85 std::bind(&PortDrayageWorker::onInboundMobilityOperation, &pdw_, std_ph::_1));
86 guidance_state_subscriber_ = create_subscription<carma_planning_msgs::msg::GuidanceState> ("guidance_state", 5,
87 std::bind(&PortDrayageWorker::onGuidanceState, &pdw_, std_ph::_1));
88 route_event_subscriber_ = create_subscription<carma_planning_msgs::msg::RouteEvent>("route_event", 5,
89 std::bind(&PortDrayageWorker::onRouteEvent, &pdw_, std_ph::_1));
90 georeference_subscriber_ = create_subscription<std_msgs::msg::String>("georeference", 1,
91 std::bind(&PortDrayageWorker::onNewGeoreference, &pdw_, std_ph::_1));
92
93 // Setup publishers
94 outbound_mobility_operations_publisher_ = create_publisher<carma_v2x_msgs::msg::MobilityOperation>("outgoing_mobility_operation", 5);
95 ui_instructions_publisher_ = create_publisher<carma_msgs::msg::UIInstructions>("ui_instructions", 5);
96
97 // Setup service clients
98 set_active_route_client_ = create_client<carma_planning_msgs::srv::SetActiveRoute>("set_active_route");
99
100 // Set PortDrayageWorker variables
105
106 // Return success if everything initialized successfully
107 return CallbackReturn::SUCCESS;
108 }
109
110 bool PortDrayagePlugin::callSetActiveRouteClient(std::shared_ptr<carma_planning_msgs::srv::SetActiveRoute::Request> req)
111 {
112 // Send request to set_active_route service
113 auto route_result = set_active_route_client_->async_send_request(req);
114
115 // Wait for response from service call
116 auto future_status = route_result.wait_for(std::chrono::milliseconds(100));
117
118 if (future_status == std::future_status::ready) {
119 if(route_result.get()->error_status == carma_planning_msgs::srv::SetActiveRoute::Response::NO_ERROR){
120 RCLCPP_DEBUG_STREAM(get_logger(), "Route Generation succeeded for Set Active Route service call.");
121 return true;
122 }
123 else{
124 RCLCPP_DEBUG_STREAM(get_logger(), "Route Generation failed for Set Active Route service call.");
125 return false;
126 }
127 }
128 else {
129 // No response was received after making the service call
130 RCLCPP_DEBUG_STREAM(get_logger(), "Set Active Route service call was not successful.");
131 return false;
132 }
133
134 }
135
136 void PortDrayagePlugin::publishMobilityOperation(const carma_v2x_msgs::msg::MobilityOperation& msg)
137 {
139 }
140
141 void PortDrayagePlugin::publishUIInstructions(const carma_msgs::msg::UIInstructions& msg)
142 {
143 ui_instructions_publisher_->publish(msg);
144 }
145
146} // port_drayage_plugin
147
148#include "rclcpp_components/register_node_macro.hpp"
149
150// Register the component with class_loader
151RCLCPP_COMPONENTS_REGISTER_NODE(port_drayage_plugin::PortDrayagePlugin)
bool callSetActiveRouteClient(std::shared_ptr< carma_planning_msgs::srv::SetActiveRoute::Request > req)
Calls the /guidance/set_active_route service client to set an active route.
carma_ros2_utils::PubPtr< carma_v2x_msgs::msg::MobilityOperation > outbound_mobility_operations_publisher_
carma_ros2_utils::SubPtr< std_msgs::msg::String > georeference_subscriber_
carma_ros2_utils::PubPtr< carma_msgs::msg::UIInstructions > ui_instructions_publisher_
carma_ros2_utils::SubPtr< geometry_msgs::msg::PoseStamped > pose_subscriber_
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &prev_state)
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::GuidanceState > guidance_state_subscriber_
carma_ros2_utils::SubPtr< carma_v2x_msgs::msg::MobilityOperation > inbound_mobility_operation_subscriber_
void publishMobilityOperation(const carma_v2x_msgs::msg::MobilityOperation &msg)
Publishes a Mobility Operation message to the outgoing mobility operation topic.
PortDrayagePlugin(const rclcpp::NodeOptions &)
PortDrayagePlugin constructor.
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::RouteEvent > route_event_subscriber_
void publishUIInstructions(const carma_msgs::msg::UIInstructions &msg)
Publishes a UI Instructions message to the outgoing UI Instructions topic.
rcl_interfaces::msg::SetParametersResult parameter_update_callback(const std::vector< rclcpp::Parameter > &parameters)
Callback for dynamic parameter updates.
carma_ros2_utils::ClientPtr< carma_planning_msgs::srv::SetActiveRoute > set_active_route_client_
void setVehicleID(const std::string &cmv_id)
Setter function to set this object's cmv_id_ string.
void setStartingAtStagingAreaFlag(bool starting_at_staging_area)
Setter function to set this object's starting_at_staging_area flag.
void onRouteEvent(const carma_planning_msgs::msg::RouteEvent::UniquePtr msg)
Callback to process each Route Event.
void setEnablePortDrayageFlag(bool enable_port_drayage)
Setter function to set this object's enable_port_drayage flag.
void onNewPose(geometry_msgs::msg::PoseStamped::UniquePtr msg)
Callback for the pose subscriber. The pose will be converted into lat/lon and stored locally.
void onGuidanceState(const carma_planning_msgs::msg::GuidanceState::UniquePtr msg)
Callback to process the current status of the guidance state machine.
void onInboundMobilityOperation(carma_v2x_msgs::msg::MobilityOperation::UniquePtr msg)
Callback to process a received MobilityOperation message.
void setCargoID(const std::string &cargo_id)
Setter function to set this object's cargo_id_ string.
void onNewGeoreference(std_msgs::msg::String::UniquePtr msg)
Callback for map projection string to define lat/lon <--> map conversion.
Struct containing the algorithm configuration values for port_drayage_plugin.