Skip to content

Commit 5a87868

Browse files
authored
Merge branch 'main' into feature/smooth_ndt_map_update
2 parents bed7b54 + b2cd2aa commit 5a87868

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

planning/obstacle_stop_planner/include/obstacle_stop_planner/adaptive_cruise_control.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <pcl_conversions/pcl_conversions.h>
2727
#include <tf2/utils.h>
2828

29+
#include <optional>
2930
#include <vector>
3031

3132
namespace motion_planning
@@ -190,12 +191,12 @@ class AdaptiveCruiseController
190191
const rclcpp::Time & nearest_collision_point_time, double * distance,
191192
const std_msgs::msg::Header & trajectory_header);
192193
double calcTrajYaw(const TrajectoryPoints & trajectory, const int collision_point_idx);
193-
bool estimatePointVelocityFromObject(
194+
std::optional<double> estimatePointVelocityFromObject(
194195
const autoware_auto_perception_msgs::msg::PredictedObjects::ConstSharedPtr object_ptr,
195-
const double traj_yaw, const pcl::PointXYZ & nearest_collision_point, double * velocity);
196-
bool estimatePointVelocityFromPcl(
196+
const double traj_yaw, const pcl::PointXYZ & nearest_collision_point);
197+
std::optional<double> estimatePointVelocityFromPcl(
197198
const double traj_yaw, const pcl::PointXYZ & nearest_collision_point,
198-
const rclcpp::Time & nearest_collision_point_time, double * velocity);
199+
const rclcpp::Time & nearest_collision_point_time);
199200
void calculateProjectedVelocityFromObject(
200201
const PredictedObject & object, const double traj_yaw, double * velocity);
201202
double estimateRoughPointVelocity(double current_vel);

planning/obstacle_stop_planner/src/adaptive_cruise_control.cpp

+43-48
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ void AdaptiveCruiseController::insertAdaptiveCruiseVelocity(
207207

208208
const double current_velocity = current_odometry_ptr->twist.twist.linear.x;
209209
double col_point_distance;
210-
double point_velocity;
211-
bool success_estimate_vel = false;
210+
std::optional<double> point_velocity;
212211
/*
213212
* calc distance to collision point
214213
*/
@@ -225,25 +224,19 @@ void AdaptiveCruiseController::insertAdaptiveCruiseVelocity(
225224
* estimate velocity of collision point
226225
*/
227226
if (param_.use_pcl_to_est_vel) {
228-
if (estimatePointVelocityFromPcl(
229-
traj_yaw, nearest_collision_point, nearest_collision_point_time, &point_velocity)) {
230-
success_estimate_vel = true;
231-
}
227+
point_velocity =
228+
estimatePointVelocityFromPcl(traj_yaw, nearest_collision_point, nearest_collision_point_time);
232229
}
233230

234231
if (param_.use_object_to_est_vel) {
235-
if (estimatePointVelocityFromObject(
236-
object_ptr, traj_yaw, nearest_collision_point, &point_velocity)) {
237-
success_estimate_vel = true;
238-
}
232+
point_velocity = estimatePointVelocityFromObject(object_ptr, traj_yaw, nearest_collision_point);
239233
}
240234

241-
if (param_.use_rough_est_vel && !success_estimate_vel) {
235+
if (param_.use_rough_est_vel && !point_velocity) {
242236
point_velocity = estimateRoughPointVelocity(current_velocity);
243-
success_estimate_vel = true;
244237
}
245238

246-
if (!success_estimate_vel) {
239+
if (!point_velocity) {
247240
// if failed to estimate velocity, need to stop
248241
RCLCPP_DEBUG_THROTTLE(
249242
node_->get_logger(), *node_->get_clock(), std::chrono::milliseconds(1000).count(),
@@ -257,7 +250,7 @@ void AdaptiveCruiseController::insertAdaptiveCruiseVelocity(
257250

258251
// calculate max(target) velocity of self
259252
const double upper_velocity =
260-
calcUpperVelocity(col_point_distance, point_velocity, current_velocity);
253+
calcUpperVelocity(col_point_distance, point_velocity.value(), current_velocity);
261254
pub_debug_->publish(debug_values_);
262255

263256
if (upper_velocity <= param_.thresh_vel_to_stop) {
@@ -405,45 +398,47 @@ double AdaptiveCruiseController::calcTrajYaw(
405398
return tf2::getYaw(trajectory.at(collision_point_idx).pose.orientation);
406399
}
407400

408-
bool AdaptiveCruiseController::estimatePointVelocityFromObject(
401+
std::optional<double> AdaptiveCruiseController::estimatePointVelocityFromObject(
409402
const autoware_auto_perception_msgs::msg::PredictedObjects::ConstSharedPtr object_ptr,
410-
const double traj_yaw, const pcl::PointXYZ & nearest_collision_point, double * velocity)
403+
const double traj_yaw, const pcl::PointXYZ & nearest_collision_point)
411404
{
412405
geometry_msgs::msg::Point nearest_collision_p_ros;
413406
nearest_collision_p_ros.x = nearest_collision_point.x;
414407
nearest_collision_p_ros.y = nearest_collision_point.y;
415408
nearest_collision_p_ros.z = nearest_collision_point.z;
416409

417410
/* get object velocity, and current yaw */
418-
bool get_obj = false;
419-
double obj_vel_norm;
420-
double obj_vel_yaw;
411+
std::optional<std::pair<double, double>> obj_vel_norm_yaw = std::nullopt;
421412
const Point collision_point_2d = convertPointRosToBoost(nearest_collision_p_ros);
422-
for (const auto & obj : object_ptr->objects) {
423-
const Polygon obj_poly = getPolygon(
424-
obj.kinematics.initial_pose_with_covariance.pose, obj.shape.dimensions, 0.0,
425-
param_.object_polygon_length_margin, param_.object_polygon_width_margin);
426-
if (boost::geometry::distance(obj_poly, collision_point_2d) <= 0) {
427-
obj_vel_norm = std::hypot(
428-
obj.kinematics.initial_twist_with_covariance.twist.linear.x,
429-
obj.kinematics.initial_twist_with_covariance.twist.linear.y);
430-
431-
const double obj_yaw =
432-
tf2::getYaw(obj.kinematics.initial_pose_with_covariance.pose.orientation);
433-
obj_vel_yaw = obj_yaw + std::atan2(
434-
obj.kinematics.initial_twist_with_covariance.twist.linear.y,
435-
obj.kinematics.initial_twist_with_covariance.twist.linear.x);
436-
get_obj = true;
437-
break;
413+
if (object_ptr) {
414+
for (const auto & obj : object_ptr->objects) {
415+
const Polygon obj_poly = getPolygon(
416+
obj.kinematics.initial_pose_with_covariance.pose, obj.shape.dimensions, 0.0,
417+
param_.object_polygon_length_margin, param_.object_polygon_width_margin);
418+
if (boost::geometry::distance(obj_poly, collision_point_2d) <= 0) {
419+
const double obj_vel_norm = std::hypot(
420+
obj.kinematics.initial_twist_with_covariance.twist.linear.x,
421+
obj.kinematics.initial_twist_with_covariance.twist.linear.y);
422+
423+
const double obj_yaw =
424+
tf2::getYaw(obj.kinematics.initial_pose_with_covariance.pose.orientation);
425+
const double obj_vel_yaw =
426+
obj_yaw + std::atan2(
427+
obj.kinematics.initial_twist_with_covariance.twist.linear.y,
428+
obj.kinematics.initial_twist_with_covariance.twist.linear.x);
429+
obj_vel_norm_yaw = std::make_pair(obj_vel_norm, obj_vel_yaw);
430+
break;
431+
}
438432
}
439433
}
440434

441-
if (get_obj) {
442-
*velocity = obj_vel_norm * std::cos(obj_vel_yaw - traj_yaw);
443-
debug_values_.data.at(DBGVAL::ESTIMATED_VEL_OBJ) = *velocity;
444-
return true;
435+
if (obj_vel_norm_yaw) {
436+
const auto [obj_vel_norm, obj_vel_yaw] = obj_vel_norm_yaw.value();
437+
const double velocity = obj_vel_norm * std::cos(obj_vel_yaw - traj_yaw);
438+
debug_values_.data.at(DBGVAL::ESTIMATED_VEL_OBJ) = velocity;
439+
return velocity;
445440
} else {
446-
return false;
441+
return std::nullopt;
447442
}
448443
}
449444

@@ -467,9 +462,9 @@ void AdaptiveCruiseController::calculateProjectedVelocityFromObject(
467462
debug_values_.data.at(DBGVAL::ESTIMATED_VEL_OBJ) = *velocity;
468463
}
469464

470-
bool AdaptiveCruiseController::estimatePointVelocityFromPcl(
465+
std::optional<double> AdaptiveCruiseController::estimatePointVelocityFromPcl(
471466
const double traj_yaw, const pcl::PointXYZ & nearest_collision_point,
472-
const rclcpp::Time & nearest_collision_point_time, double * velocity)
467+
const rclcpp::Time & nearest_collision_point_time)
473468
{
474469
geometry_msgs::msg::Point nearest_collision_p_ros;
475470
nearest_collision_p_ros.x = nearest_collision_point.x;
@@ -487,7 +482,7 @@ bool AdaptiveCruiseController::estimatePointVelocityFromPcl(
487482
prev_collision_point_time_ = nearest_collision_point_time;
488483
prev_collision_point_ = nearest_collision_point;
489484
prev_collision_point_valid_ = true;
490-
return false;
485+
return std::nullopt;
491486
}
492487
const double p_dx = nearest_collision_point.x - prev_collision_point_.x;
493488
const double p_dy = nearest_collision_point.y - prev_collision_point_.y;
@@ -501,22 +496,22 @@ bool AdaptiveCruiseController::estimatePointVelocityFromPcl(
501496
prev_collision_point_ = nearest_collision_point;
502497
prev_collision_point_valid_ = true;
503498
est_vel_que_.clear();
504-
return false;
499+
return std::nullopt;
505500
}
506501

507502
// append new velocity and remove old velocity from que
508503
registerQueToVelocity(est_velocity, nearest_collision_point_time);
509504
}
510505

511506
// calc average(median) velocity from que
512-
*velocity = getMedianVel(est_vel_que_);
513-
debug_values_.data.at(DBGVAL::ESTIMATED_VEL_PCL) = *velocity;
507+
const double velocity = getMedianVel(est_vel_que_);
508+
debug_values_.data.at(DBGVAL::ESTIMATED_VEL_PCL) = velocity;
514509

515510
prev_collision_point_time_ = nearest_collision_point_time;
516511
prev_collision_point_ = nearest_collision_point;
517-
prev_target_velocity_ = *velocity;
512+
prev_target_velocity_ = velocity;
518513
prev_collision_point_valid_ = true;
519-
return true;
514+
return velocity;
520515
}
521516

522517
double AdaptiveCruiseController::estimateRoughPointVelocity(double current_vel)

0 commit comments

Comments
 (0)