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.
tree_planner.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
17#include "tree_planner.hpp"
18#include "arbitrator_utils.hpp"
19#include <vector>
20#include <map>
21#include <limits>
22
23namespace arbitrator
24{
25 carma_planning_msgs::msg::ManeuverPlan TreePlanner::generate_plan(const VehicleState& start_state)
26 {
27 carma_planning_msgs::msg::ManeuverPlan root;
28 std::vector<std::pair<carma_planning_msgs::msg::ManeuverPlan, double>> open_list_to_evaluate;
29 std::vector<std::pair<carma_planning_msgs::msg::ManeuverPlan, double>> final_open_list;
30
31 const double INF = std::numeric_limits<double>::infinity();
32 open_list_to_evaluate.push_back(std::make_pair(root, INF));
33
34 carma_planning_msgs::msg::ManeuverPlan longest_plan = root; // Track longest plan in case target length is never reached
35 rclcpp::Duration longest_plan_duration = rclcpp::Duration::from_nanoseconds(0);
36
37
38 while (!open_list_to_evaluate.empty())
39 {
40 std::vector<std::pair<carma_planning_msgs::msg::ManeuverPlan, double>> temp_open_list;
41
42 for (auto it = open_list_to_evaluate.begin(); it != open_list_to_evaluate.end(); it++)
43 {
44 // Pop the first element off the open list
45 auto& cur_plan = it->first;
46
47 if (cur_plan.maneuver_plan_id == "")
48 {
49 // Generate a new unique id for the maneuver plan
50 cur_plan.maneuver_plan_id =
51 boost::uuids::to_string(boost::uuids::random_generator()());
52 }
53
54 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"),
55 "Start printing newly requested maneuver plan for debugging:");
56
57 for (auto mvr : cur_plan.maneuvers)
58 {
59 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"),
60 "Maneuver: "<< maneuver_type_to_string(mvr.type));
61 }
62
63 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"),
64 "Ended printing newly requested maneuver plan for debugging");
65
66 auto plan_duration = rclcpp::Duration(0, 0); // zero duration
67
68 // If we're not at the root, plan_duration is nonzero (our plan should have maneuvers)
69 if (!cur_plan.maneuvers.empty())
70 {
71 // get plan duration
73 }
74
75 // save longest if none of the plans have enough target duration
76 if (plan_duration > longest_plan_duration)
77 {
78 longest_plan_duration = plan_duration;
79 longest_plan = cur_plan;
80 }
81
82 // Evaluate plan_duration is sufficient do not expand more
83 if (plan_duration >= target_plan_duration_)
84 {
85 final_open_list.push_back((*it));
86 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "Has enough duration, skipping that which has following mvrs..:");
87 for (auto mvr : it->first.maneuvers)
88 {
89 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"),
90 "Printing successful mvr: " << maneuver_type_to_string(mvr.type));
91 }
92 continue;
93 }
94
95 // Expand it, and reprioritize
96 std::vector<carma_planning_msgs::msg::ManeuverPlan> children = neighbor_generator_->generate_neighbors(cur_plan, start_state);
97 RCLCPP_DEBUG_STREAM(
98 rclcpp::get_logger("arbitrator"),
99 "Arbitrator received total of " << children.size()
100 << " response from strategic_plugins for plan_id: "
101 << std::string(cur_plan.maneuver_plan_id)
102 );
103 // Compute cost for each child and store in open list
104 for (auto child = children.begin(); child != children.end(); child++)
105 {
106 if (child->maneuvers.empty())
107 {
108 RCLCPP_DEBUG_STREAM(
109 rclcpp::get_logger("arbitrator"),
110 "Arbitrator didn't get successful maneuver plan for plan_id: "
111 << std::string(cur_plan.maneuver_plan_id)
112 <<", from one of the strategic plugin, which so far had maneuvers: "
113 );
114 for (auto mvr : cur_plan.maneuvers)
115 {
116 RCLCPP_DEBUG_STREAM(
117 rclcpp::get_logger("arbitrator"),
118 "Maneuver type: " << maneuver_type_to_string(mvr.type);
119 );
120 }
121 continue;
122 }
123
124 temp_open_list.push_back(std::make_pair(*child, cost_function_->compute_cost_per_unit_distance(*child)));
125 }
126 }
127
128 open_list_to_evaluate = temp_open_list;
129 }
130
131 if (final_open_list.empty())
132 {
133 RCLCPP_ERROR_STREAM(rclcpp::get_logger("arbitrator"), "None of the strategic plugins generated any valid plans! Please check if any is turned and returning valid maneuvers...");
134 throw std::runtime_error("None of the strategic plugins generated any valid plans! Please check if any is turned and returning valid maneuvers...");
135 }
136
137 final_open_list = search_strategy_->prioritize_plans(final_open_list);
138
139 // now every plan has enough duration if possible and prioritized
140 for (auto pair : final_open_list)
141 {
142 // Pop the first element off the open list
143 carma_planning_msgs::msg::ManeuverPlan cur_plan = pair.first;
144 rclcpp::Duration plan_duration(0,0); // zero duration
145
146 // get plan duration
148
149 // Evaluate plan_duration is sufficient do not expand more
150 if (plan_duration >= target_plan_duration_)
151 {
152 return cur_plan;
153 }
154 }
155
156 // If no perfect match is found, return the longest plan that fit the criteria
157 return longest_plan;
158 }
159}
rclcpp::Duration target_plan_duration_
carma_planning_msgs::msg::ManeuverPlan generate_plan(const VehicleState &start_state)
Utilize the configured cost function, neighbor generator, and search strategy, to generate a plan by ...
std::shared_ptr< SearchStrategy > search_strategy_
std::shared_ptr< NeighborGenerator > neighbor_generator_
std::shared_ptr< CostFunction > cost_function_
rclcpp::Time get_plan_start_time(const carma_planning_msgs::msg::ManeuverPlan &)
Get the start time of the first maneuver in the plan.
rclcpp::Time get_plan_end_time(const carma_planning_msgs::msg::ManeuverPlan &)
Get the end time of the first maneuver in the plan.
std::string maneuver_type_to_string(uint8_t type)
Convert maneuver type enum to string representation.
auto to_string(const UtmZone &zone) -> std::string
Definition: utm_zone.cpp:21
Struct defining the vehicle state required for maneuver planning.