32 const std::string& config_dir,
const std::string& name,
33 const std::shared_ptr<DependencyInjector>& injector) {
38 if (!Decider::LoadConfig<OpenSpaceFallBackDeciderParkConfig>(&config_)) {
39 AERROR <<
"Failed to load config file";
42 open_space_fallback_collision_distance_ =
48bool OpenSpaceFallbackDeciderPark::QuardraticFormulaLowerSolution(
const double a,
54 const double kEpsilon = 1e-6;
56 if (std::abs(a) < kEpsilon) {
60 double tmp = b * b - 4 * a * c;
64 double sol1 = (-b + std::sqrt(tmp)) / (2.0 * a);
65 double sol2 = (-b - std::sqrt(tmp)) / (2.0 * a);
67 *sol = std::abs(std::min(sol1, sol2));
68 ADEBUG <<
"QuardraticFormulaLowerSolution finished with sol: " << *sol
69 <<
"sol1: " << sol1 <<
", sol2: " << sol2 <<
"a: " << a <<
"b: " << b
74Status OpenSpaceFallbackDeciderPark::Process(
Frame* frame) {
75 AINFO <<
"frame->open_space_info().chosen_partitioned_trajectory(): " <<
76 frame->open_space_info().chosen_partitioned_trajectory().first.size();
77 AINFO <<
"frame->open_space_info().optimizer_trajectory_data(): " <<
78 frame->open_space_info().optimizer_trajectory_data().size();
80 std::vector<std::vector<common::math::Box2d>> predicted_bounding_rectangles;
81 std::vector<Obstacle*> static_obstacles;
82 int first_collision_index = 0;
83 int fallback_start_index = 0;
85 bool is_collision_with_static_obstacle =
false;
86 bool is_collision_with_dynamic_obstacle =
false;
90 Vec2d(frame->vehicle_state().x(), frame->vehicle_state().y()),
92 predicted_bounding_rectangles,
97 Vec2d(frame->vehicle_state().x(), frame->vehicle_state().y()),
101 ADEBUG <<
"Numbers of obstsacles are: " << frame->obstacles().size();
102 ADEBUG <<
"Numbers of predicted bounding rectangles of dynamic obstacle are: "
103 << predicted_bounding_rectangles[0].size()
104 <<
" and : " << predicted_bounding_rectangles.size()
105 <<
"Numbers of static obstsacles are:" << static_obstacles.size();
107 if (frame->open_space_info().chosen_partitioned_trajectory().first.empty()) {
108 AERROR <<
"No trajectory is generated for fallback";
111 static constexpr double relative_stop_time = 0.1;
113 auto fallback_tra_pair =
115 mutable_chosen_partitioned_trajectory();
116 fallback_tra_pair->first.clear();
117 auto current_speed =
injector_->vehicle_state()->
118 vehicle_state().linear_velocity();
119 AINFO <<
"open_space_fallback_collision_distance_: " << open_space_fallback_collision_distance_;
120 double stop_deceleration_for_static_collision =
121 std::max(std::min(-1 * current_speed * current_speed /
122 (2 * (open_space_fallback_collision_distance_ - 1e-12)),
125 AINFO <<
"stop_deceleration_for_static_collision: " << stop_deceleration_for_static_collision;
127 fallback_trajectory_pair_candidate.second =
129 CalculateFallbackTrajectory(fallback_trajectory_pair_candidate,
130 stop_deceleration_for_static_collision,
131 relative_stop_time, fallback_tra_pair);
132 open_space_fallback_collision_distance_ = std::max (1e-6,
133 open_space_fallback_collision_distance_ - fabs(fallback_tra_pair->first[1].path_point().s()));
134 AINFO <<
"open_space_fallback_collision_ditance_: " << open_space_fallback_collision_distance_;
139 fallback_trajectory, predicted_bounding_rectangles,
140 static_obstacles, &fallback_start_index, &first_collision_index,
141 is_collision_with_static_obstacle,
142 is_collision_with_dynamic_obstacle,
144 AINFO <<
" fallback trajectory still has collision with obstacles";
146 CalculateFallbackTrajectory(fallback_trajectory, -4.0,
147 relative_stop_time, fallback_tra_pair);
153 open_space_fallback_collision_distance_ =
157 frame->open_space_info().chosen_partitioned_trajectory(),
158 predicted_bounding_rectangles, static_obstacles,
159 &fallback_start_index, &first_collision_index,
160 is_collision_with_static_obstacle,
161 is_collision_with_dynamic_obstacle,
163 AINFO <<
"trajectory still has collision with obstacles";
167 frame->open_space_info().chosen_partitioned_trajectory();
170 const auto future_collision_point =
171 fallback_trajectory_pair_candidate.first[first_collision_index];
174 auto fallback_start_point =
175 fallback_trajectory_pair_candidate.first[fallback_start_index];
176 const auto& vehicle_state =
injector_->vehicle_state()->vehicle_state();
179 double stop_distance =
181 ? std::max(future_collision_point.path_point().s() -
184 :
std::min(future_collision_point.path_point().s() -
185 fallback_start_point.path_point().s() + config_.distance_to_obs(),
188 ADEBUG <<
"stop distance : " << stop_distance;
190 const auto& vehicle_param =
191 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param();
192 const double max_adc_stop_speed =
193 vehicle_param.max_abs_speed_when_stopped();
196 if (is_collision_with_static_obstacle) {
197 AINFO <<
"collision with static obstacle";
200 mutable_optimizer_trajectory_data()->clear();
202 mutable_path_planning_trajectory_result()->clear();
204 mutable_interpolated_trajectory_result()->clear();
206 mutable_partitioned_trajectories()->clear();
208 mutable_chosen_partitioned_trajectory()->first.clear();
211 const auto& open_space_fallback_collision_distance =
213 static constexpr double relative_stop_time = 0.1;
215 double relative_time = 0.0;
216 double current_s_distance = 0.0;
217 auto fallback_tra_pair =
219 mutable_chosen_partitioned_trajectory();
220 fallback_tra_pair->first.clear();
221 double stop_deceleration_for_static_collision = 0;
223 auto current_speed = vehicle_state.linear_velocity();
224 AINFO <<
"open_space_fallback_collision_distance: "
225 << open_space_fallback_collision_distance
226 <<
"stop_distance: " << stop_distance;
227 if (open_space_fallback_collision_distance > std::abs(stop_distance)) {
228 AINFO <<
"The collision distance with static obstacle is too small, "
230 << stop_distance <<
"!";
232 stop_deceleration_for_static_collision = -4.0;
234 AINFO <<
"The collision distance with static obstacle is large, stop "
235 "with set distance is :"
236 << open_space_fallback_collision_distance <<
"!";
238 stop_deceleration_for_static_collision =
239 std::max(-1 * current_speed * current_speed /
240 (2 * (open_space_fallback_collision_distance - 1e-6)),
242 AINFO <<
"current_speed: " << current_speed
243 <<
"open_space_fallback_collision_distance: "
244 << open_space_fallback_collision_distance;
246 AINFO <<
"stop deceleration for static collision is : "
247 << stop_deceleration_for_static_collision
248 <<
"relative_stop_time: " << relative_stop_time;
250 CalculateFallbackTrajectoryWithTrajectoryPose(
251 fallback_trajectory_pair_candidate,
252 fallback_start_point, future_collision_point, stop_distance,
253 relative_stop_time, fallback_tra_pair);
255 CalculateFallbackTrajectory(fallback_trajectory_pair_candidate,
256 open_space_fallback_collision_distance,
257 relative_stop_time, fallback_tra_pair);
259 AINFO <<
"fallback trajectory has collision with obstacles";
263 AINFO <<
"fallback trajectory has collision with obstacles";
265 fallback_trajectory, predicted_bounding_rectangles,
266 static_obstacles, &fallback_start_index, &first_collision_index,
267 is_collision_with_static_obstacle,
268 is_collision_with_dynamic_obstacle,
270 AINFO <<
" fallback trajectory still has collision with obstacles";
272 CalculateFallbackTrajectory(fallback_trajectory, -4.0,
273 relative_stop_time, fallback_tra_pair);
278 if (is_collision_with_dynamic_obstacle) {
279 auto fallback_tra_pair =
281 mutable_chosen_partitioned_trajectory();
282 fallback_tra_pair->first.clear();
284 fallback_start_point.set_v(vehicle_state.linear_velocity());
287 future_collision_point;
290 double min_stop_distance =
291 0.5 * fallback_start_point.v() * fallback_start_point.v() / 4.0;
295 const double vehicle_max_acc = 4.0;
296 const double vehicle_max_dec =
299 double stop_deceleration = 0.0;
301 if (fallback_trajectory_pair_candidate.second ==
304 std::min(fallback_start_point.v() * fallback_start_point.v() /
305 (2.0 * (stop_distance + 1e-6)),
307 stop_distance = std::min(-1 * min_stop_distance, stop_distance);
310 std::max(-fallback_start_point.v() * fallback_start_point.v() /
311 (2.0 * (stop_distance + 1e-6)),
313 stop_distance = std::max(min_stop_distance, stop_distance);
316 AINFO <<
"stop_deceleration: " << stop_deceleration;
319 int stop_index = fallback_start_index;
321 for (
int i = fallback_start_index;
322 i < fallback_trajectory_pair_candidate.first.NumOfPoints(); ++i) {
324 fallback_trajectory_pair_candidate.first[i].path_point().s()) >=
325 std::abs(fallback_start_point.path_point().s() + stop_distance)) {
331 AINFO <<
"stop index before is: " << stop_index
332 <<
"; fallback_start index before is: " << fallback_start_index;
334 for (
int i = 0; i < fallback_start_index; ++i) {
335 fallback_trajectory_pair_candidate.first[i].set_v(
336 fallback_start_point.v());
337 fallback_trajectory_pair_candidate.first[i].set_a(stop_deceleration);
344 if (fallback_start_index >= stop_index) {
346 AINFO <<
"Stop distance within safety buffer, stop now!";
347 fallback_start_point.set_v(0.0);
348 fallback_start_point.set_a(0.0);
349 fallback_trajectory_pair_candidate.first[stop_index].set_v(0.0);
350 fallback_trajectory_pair_candidate.first[stop_index].set_a(0.0);
353 fallback_trajectory_pair_candidate.first.erase(
354 fallback_trajectory_pair_candidate.first.begin() + stop_index + 1,
355 fallback_trajectory_pair_candidate.first.end());
358 for (
int i = 0; i < 20; ++i) {
359 common::TrajectoryPoint trajectory_point(
360 fallback_trajectory_pair_candidate.first[stop_index]);
361 trajectory_point.set_relative_time(
363 fallback_trajectory_pair_candidate.first[stop_index]
365 fallback_trajectory_pair_candidate.first.AppendTrajectoryPoint(
370 fallback_trajectory_pair_candidate;
375 AINFO <<
"before change, size : "
376 << fallback_trajectory_pair_candidate.first.size()
377 <<
", first index information : "
378 << fallback_trajectory_pair_candidate.first[0].DebugString()
379 <<
", second index information : "
380 << fallback_trajectory_pair_candidate.first[1].DebugString();
384 for (
int i = fallback_start_index; i <= stop_index; ++i) {
385 double new_relative_time = 0.0;
388 -2.0 * fallback_trajectory_pair_candidate.first[i].path_point().s();
390 if (QuardraticFormulaLowerSolution(stop_deceleration,
391 2.0 * fallback_start_point.v(), c,
392 &new_relative_time) &&
394 fallback_trajectory_pair_candidate.first[i].path_point().s()) <=
395 std::abs(stop_distance)) {
396 ADEBUG <<
"new_relative_time" << new_relative_time;
398 fallback_start_point.v() + stop_deceleration * new_relative_time;
400 if (std::abs(temp_v) < 1.0) {
401 fallback_trajectory_pair_candidate.first[i].set_v(temp_v);
403 fallback_trajectory_pair_candidate.first[i].set_v(
404 temp_v / std::abs(temp_v) * 1.0);
406 fallback_trajectory_pair_candidate.first[i].set_a(stop_deceleration);
408 fallback_trajectory_pair_candidate.first[i].set_relative_time(
412 fallback_trajectory_pair_candidate.first[i]
413 .mutable_path_point()
414 ->CopyFrom(fallback_trajectory_pair_candidate.first[i - 1]
416 fallback_trajectory_pair_candidate.first[i].set_v(0.0);
417 fallback_trajectory_pair_candidate.first[i].set_a(0.0);
418 fallback_trajectory_pair_candidate.first[i].set_relative_time(
419 fallback_trajectory_pair_candidate.first[i - 1]
423 fallback_trajectory_pair_candidate.first[i].set_v(0.0);
424 fallback_trajectory_pair_candidate.first[i].set_a(0.0);
429 ADEBUG <<
"fallback start point after changes: "
430 << fallback_start_point.DebugString();
432 ADEBUG <<
"stop index: " << stop_index;
433 ADEBUG <<
"fallback start index: " << fallback_start_index;
436 fallback_trajectory_pair_candidate.first.erase(
437 fallback_trajectory_pair_candidate.first.begin() + stop_index + 1,
438 fallback_trajectory_pair_candidate.first.end());
441 for (
int i = 0; i < 20; ++i) {
442 common::TrajectoryPoint trajectory_point(
443 fallback_trajectory_pair_candidate.first[stop_index]);
444 trajectory_point.set_relative_time(
446 fallback_trajectory_pair_candidate.first[stop_index]
448 fallback_trajectory_pair_candidate.first.AppendTrajectoryPoint(
452 mutable_chosen_partitioned_trajectory()) =
453 fallback_trajectory_pair_candidate;
459bool OpenSpaceFallbackDeciderPark::IsCollisionFreeEgoBox() {
462 double x = vehicle_state.
x();
463 double y = vehicle_state.y();
464 double heading = vehicle_state.heading();
466 const auto& vehicle_config =
467 common::VehicleConfigHelper::Instance()->GetConfig();
468 double ego_length = vehicle_config.vehicle_param().length();
469 double ego_width = vehicle_config.vehicle_param().width();
470 Box2d ego_box({x, y}, heading, ego_length, ego_width);
471 Polygon2d ego_polygon = Polygon2d(ego_box);
473 for (
const Obstacle* obstacle :
frame_->obstacles()) {
474 if (obstacle->IsVirtual() || !obstacle->IsStatic() ||
475 obstacle->Perception().width() < 0.2 ||
476 obstacle->Perception().length() < 0.2) {
479 Polygon2d obstacle_polygon = obstacle->PerceptionPolygon();
480 if (ego_polygon.HasOverlap(obstacle_polygon)) {
487void OpenSpaceFallbackDeciderPark::CalculateFallbackTrajectory(
488 const TrajGearPair& ChosenPartitionedTrajectory,
const double& deceleration,
489 const double& relative_time_interval,
TrajGearPair* traj_gear_pair) {
491 traj_gear_pair->first.clear();
494 double pre_speed = current_speed;
495 double decelerate = fabs(deceleration);
497 double current_s_distance = 0;
498 double relative_time = 0;
499 const auto& vehicle_config =
500 common::VehicleConfigHelper::Instance()->GetConfig();
510 vehicle_config.vehicle_param().max_abs_speed_when_stopped()) {
511 TrajectoryPoint point;
512 static constexpr double kSpeedEpsilon = 1e-9;
515 current_s_distance * alpha *
516 std::cos(moving_heading));
518 current_s_distance * alpha *
519 std::sin(moving_heading));
521 point.mutable_path_point()->set_s(current_s_distance * alpha);
522 point.mutable_path_point()->set_kappa(0.0);
523 point.set_relative_time(relative_time);
524 point.set_v(current_speed* alpha);
525 point.set_a(decelerate* -1 * alpha);
526 traj_gear_pair->first.emplace_back(point);
527 relative_time += relative_time_interval;
528 pre_speed = current_speed;
529 current_speed -= relative_time_interval * decelerate;
530 current_s_distance +=
531 (pre_speed + current_speed) / 2 * relative_time_interval;
532 if (pre_speed < kSpeedEpsilon)
break;
535 if (traj_gear_pair->first.empty()) {
536 for (
int i = 0; i < 20; i++) {
537 TrajectoryPoint point;
541 point.mutable_path_point()->set_s(0);
542 point.mutable_path_point()->set_kappa(0.0);
543 point.set_relative_time(relative_time);
545 point.set_a(-1.0 * alpha);
546 traj_gear_pair->first.emplace_back(point);
547 relative_time += relative_time_interval;
553void OpenSpaceFallbackDeciderPark::CalculateFallbackTrajectoryWithTrajectoryPose(
554 const TrajGearPair& ChosenPartitionedTrajectory,
const TrajectoryPoint start_point,
const TrajectoryPoint collision_point,
const double& stop_distance,
555 const double& relative_time_interval,
TrajGearPair* traj_gear_pair) {
556 static int count = 0;
557 AINFO <<
"stop_distance:" << stop_distance;
558 if (std::fabs(stop_distance) >= 1e-3) {
560 int alpha = stop_distance < 0 ? -1 : 1;
562 double t = std::fabs(stop_distance) / v_max * 2;
563 double a = v_max / t;
564 Vec2d unit_vec(collision_point.path_point().x() - start_point.path_point().x(), collision_point.path_point().y() - start_point.path_point().y());
565 Vec2d start_vec(start_point.path_point().x(), start_point.path_point().y());
566 unit_vec.Normalize();
567 for (
double relative_t = 0; relative_t < t; relative_t += relative_time_interval){
568 TrajectoryPoint point;
569 double point_v, point_a, point_s;
570 point_v = a * (t - relative_t);
572 point_s = std::fabs(stop_distance) - 0.5 * point_v * (t - relative_t);
573 Vec2d point_vec = start_vec + unit_vec * point_s;
574 point.mutable_path_point()->set_x(point_vec.x());
575 point.mutable_path_point()->set_y(point_vec.y());
576 point.mutable_path_point()->set_theta(start_point.path_point().theta());
577 point.mutable_path_point()->set_s(point_s * alpha);
578 point.mutable_path_point()->set_kappa(0.0);
579 point.set_relative_time(relative_t - 0.1 * count);
580 point.set_v(point_v * alpha);
581 point.set_a(point_a * alpha);
582 AINFO <<
"point: " << point.DebugString();
583 traj_gear_pair->first.emplace_back(point);
585 TrajectoryPoint point;
586 Vec2d point_vec = start_vec + unit_vec * std::fabs(stop_distance);
587 point.mutable_path_point()->set_x(point_vec.x());
588 point.mutable_path_point()->set_y(point_vec.y());
589 point.mutable_path_point()->set_theta(start_point.path_point().theta());
590 point.mutable_path_point()->set_s(stop_distance);
591 point.mutable_path_point()->set_kappa(0.0);
592 point.set_relative_time(t - 0.1 * count);
595 AINFO <<
"point: " << point.DebugString();
596 traj_gear_pair->first.emplace_back(point);
597 traj_gear_pair->second = ChosenPartitionedTrajectory.second;
599 if (traj_gear_pair->first.empty()) {
600 double relative_time = 0;
604 for (
int i = 0; i < 20; i++) {
605 TrajectoryPoint point;
609 point.mutable_path_point()->set_s(0);
610 point.mutable_path_point()->set_kappa(0.0);
611 point.set_relative_time(relative_time);
613 point.set_a(-1.0 * alpha);
614 traj_gear_pair->first.emplace_back(point);
615 relative_time += relative_time_interval;
A general class to denote the return status of an API call.
static Status OK()
generate a success status.
Rectangular (undirected) bounding box in 2-D.
The class of polygon in 2-D.
Implements a class of 2-dimensional vectors.
const OpenSpaceInfo & open_space_info() const
const LocalView & local_view() const
const common::VehicleState & vehicle_state() const
OpenSpaceInfo * mutable_open_space_info()
bool Init(const std::string &config_dir, const std::string &name, const std::shared_ptr< DependencyInjector > &injector) override
static bool IsCollisionFreeTrajectory(const TrajGearPair &trajectory_gear_pair, const std::vector< std::vector< common::math::Box2d > > &predicted_bounding_rectangles, const std::vector< Obstacle * > &static_obstacles, int *current_index, int *first_collision_index, bool &is_collision_with_static_obstacle, bool &is_collision_with_dynamic_obstacle, const double &collilsion_time_buffer)
static void BuildPredictedEnvironment(const common::math::Vec2d ego_pos, const std::vector< const Obstacle * > &obstacles, std::vector< std::vector< common::math::Box2d > > &predicted_bounding_rectangles, const double &time_period, const double &build_range)
static void BuildStaticObstacleEnvironment(const common::math::Vec2d ego_pos, const std::vector< const Obstacle * > &obstacles, std::vector< Obstacle * > &static_obstacles, const double &build_range)
common::TrajectoryPoint * mutable_future_collision_point()
const TrajGearPair & chosen_partitioned_trajectory() const
TrajGearPair * mutable_fallback_trajectory()
std::shared_ptr< DependencyInjector > injector_
virtual bool Init(const std::string &config_dir, const std::string &name, const std::shared_ptr< DependencyInjector > &injector)
Planning module main class.
std::pair< DiscretizedTrajectory, canbus::Chassis::GearPosition > TrajGearPair
optional double linear_velocity
std::shared_ptr< canbus::Chassis > chassis
optional double distance_to_obs
optional double open_space_fallback_collision_distance
optional double open_space_prediction_time_period
optional double open_space_fallback_collision_time_buffer
optional bool is_use_trajectory_pose
optional double collision_check_range