44#include " software/geom/algorithms/contains.h"
55#include " software/geom/algorithms/distance.h"
66
7+
78TrajectoryPlanner::TrajectoryPlanner ()
89 : relative_sub_destinations(getRelativeSubDestinations())
910{
@@ -109,8 +110,9 @@ std::optional<TrajectoryPath> TrajectoryPlanner::findTrajectory(
109110 break ;
110111 }
111112
112- TrajectoryPathWithCost full_traj_with_cost = getTrajectoryWithCost (
113- traj_path_to_dest, obstacles, sub_trajectory, connection_time);
113+ TrajectoryPathWithCost full_traj_with_cost =
114+ getTrajectoryWithCost (traj_path_to_dest, obstacles, sub_trajectory,
115+ connection_time, best_traj_with_cost.cost );
114116 full_traj_with_cost.cost += cost_offset;
115117 if (full_traj_with_cost.cost < best_traj_with_cost.cost )
116118 {
@@ -133,58 +135,47 @@ std::optional<TrajectoryPath> TrajectoryPlanner::findTrajectory(
133135 }
134136 }
135137
136- return best_traj_with_cost.traj_path ;
138+ // In move primitive, a stop primitive is created when trajectory path is null.
139+ // Check if there is an unavoidable collision, and return a null opt if such
140+ // collision exist on best path
141+ double collision_velocity =
142+ best_traj_with_cost.traj_path
143+ .getVelocity (best_traj_with_cost.first_collision_time_s )
144+ .length ();
145+ if (best_traj_with_cost.collides () &&
146+ best_traj_with_cost.first_collision_time_s <
147+ UNAVOIDABLE_COLLISION_TIME_THRESHOLD_S &&
148+ collision_velocity > UNAVOIDABLE_COLLISION_VELOCITY_THRESHOLD_M_S )
149+ {
150+ return std::nullopt ;
151+ }
152+ else
153+ {
154+ return best_traj_with_cost.traj_path ;
155+ }
137156}
138157
139158TrajectoryPathWithCost TrajectoryPlanner::getDirectTrajectoryWithCost (
140159 const Point &start, const Point &destination, const Vector &initial_velocity,
141160 const KinematicConstraints &constraints, const std::vector<ObstaclePtr> &obstacles)
142161{
162+ // Calculate full new cost regardless by passing in maximum max cost
143163 return getTrajectoryWithCost (
144164 TrajectoryPath (std::make_shared<BangBangTrajectory2D>(
145165 start, destination, initial_velocity, constraints),
146166 BangBangTrajectory2D::generator),
147- obstacles, std::nullopt , std::nullopt );
167+ obstacles, std::nullopt , std::nullopt , std::numeric_limits< double >:: max () );
148168}
149169
150170TrajectoryPathWithCost TrajectoryPlanner::getTrajectoryWithCost (
151171 const TrajectoryPath &trajectory, const std::vector<ObstaclePtr> &obstacles,
152172 const std::optional<TrajectoryPathWithCost> &sub_traj_with_cost,
153- const std::optional<double > sub_traj_duration_s)
173+ const std::optional<double > sub_traj_duration_s, double max_cost )
154174{
155175 CollisionEvaluator evaluator (obstacles);
156- TrajectoryPathWithCost traj_with_cost (
157- evaluator.evaluate (trajectory, sub_traj_with_cost, sub_traj_duration_s));
158- traj_with_cost.cost = calculateCost (traj_with_cost);
176+ TrajectoryPathWithCost traj_with_cost (evaluator.evaluate (
177+ trajectory, sub_traj_with_cost, sub_traj_duration_s, max_cost));
159178
160- return traj_with_cost;
161- }
162179
163- double TrajectoryPlanner::calculateCost (
164- const TrajectoryPathWithCost &traj_with_cost) const
165- {
166- double total_cost = traj_with_cost.traj_path .getTotalTime ();
167-
168- // Add a large cost if the trajectory collides with an obstacle
169- // Note that this ignores collisions that may be in at the
170- // start of the trajectory as those are unavoidable by all trajectories.
171- if (traj_with_cost.colliding_obstacle != nullptr )
172- {
173- total_cost += 6.0 ;
174- }
175-
176- // The closer the collision is to the destination, the lower its cost will be
177- Point first_collision_position =
178- traj_with_cost.traj_path .getPosition (traj_with_cost.first_collision_time_s );
179- Point destination = traj_with_cost.traj_path .getDestination ();
180- total_cost += (first_collision_position - destination).length ();
181-
182- total_cost += std::max (
183- 0.0 , (MAX_FUTURE_COLLISION_CHECK_SEC - traj_with_cost.first_collision_time_s ));
184-
185- total_cost += 3 * traj_with_cost.collision_duration_front_s ;
186-
187- total_cost += 1 * traj_with_cost.collision_duration_back_s ;
188-
189- return total_cost;
180+ return traj_with_cost;
190181}
0 commit comments