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(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 carma_planning_msgs::msg::ManeuverPlan cur_plan = it->first;
46
47 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "START");
48
49 for (auto mvr : cur_plan.maneuvers)
50 {
51 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "Printing cur_plan: mvr: "<< (int)mvr.type);
52 }
53
54 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "PRINT END");
55
56 auto plan_duration = rclcpp::Duration(0, 0); // zero duration
57
58 // If we're not at the root, plan_duration is nonzero (our plan should have maneuvers)
59 if (!cur_plan.maneuvers.empty())
60 {
61 // get plan duration
63 }
64
65 // save longest if none of the plans have enough target duration
66 if (plan_duration > longest_plan_duration)
67 {
68 longest_plan_duration = plan_duration;
69 longest_plan = cur_plan;
70 }
71
72 // Evaluate plan_duration is sufficient do not expand more
73 if (plan_duration >= target_plan_duration_)
74 {
75 final_open_list.push_back((*it));
76 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "Has enough duration, skipping that which has following mvrs..:");
77 for (auto mvr : it->first.maneuvers)
78 {
79 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "Printing mvr: "<< (int)mvr.type);
80 }
81 continue;
82 }
83
84 // Expand it, and reprioritize
85 std::vector<carma_planning_msgs::msg::ManeuverPlan> children = neighbor_generator_->generate_neighbors(cur_plan, start_state);
86
87 // Compute cost for each child and store in open list
88 for (auto child = children.begin(); child != children.end(); child++)
89 {
90
91 if (child->maneuvers.empty())
92 {
93 RCLCPP_WARN_STREAM(rclcpp::get_logger("arbitrator"), "Child was empty for id: " << std::string(child->maneuver_plan_id));
94 continue;
95 }
96
97 temp_open_list.push_back(std::make_pair(*child, cost_function_->compute_cost_per_unit_distance(*child)));
98 }
99 }
100
101 open_list_to_evaluate = temp_open_list;
102 }
103
104 if (final_open_list.empty())
105 {
106 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...");
107 throw std::runtime_error("None of the strategic plugins generated any valid plans! Please check if any is turned and returning valid maneuvers...");
108 }
109
110 final_open_list = search_strategy_->prioritize_plans(final_open_list);
111
112 // now every plan has enough duration if possible and prioritized
113 for (auto pair : final_open_list)
114 {
115 // Pop the first element off the open list
116 carma_planning_msgs::msg::ManeuverPlan cur_plan = pair.first;
117 rclcpp::Duration plan_duration(0,0); // zero duration
118
119 // get plan duration
121
122 // Evaluate plan_duration is sufficient do not expand more
123 if (plan_duration >= target_plan_duration_)
124 {
125 return cur_plan;
126 }
127 }
128
129 // If no perfect match is found, return the longest plan that fit the criteria
130 return longest_plan;
131 }
132}
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.
Struct defining the vehicle state required for maneuver planning.