@@ -207,8 +207,7 @@ void AdaptiveCruiseController::insertAdaptiveCruiseVelocity(
207
207
208
208
const double current_velocity = current_odometry_ptr->twist .twist .linear .x ;
209
209
double col_point_distance;
210
- double point_velocity;
211
- bool success_estimate_vel = false ;
210
+ std::optional<double > point_velocity;
212
211
/*
213
212
* calc distance to collision point
214
213
*/
@@ -225,25 +224,19 @@ void AdaptiveCruiseController::insertAdaptiveCruiseVelocity(
225
224
* estimate velocity of collision point
226
225
*/
227
226
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);
232
229
}
233
230
234
231
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);
239
233
}
240
234
241
- if (param_.use_rough_est_vel && !success_estimate_vel ) {
235
+ if (param_.use_rough_est_vel && !point_velocity ) {
242
236
point_velocity = estimateRoughPointVelocity (current_velocity);
243
- success_estimate_vel = true ;
244
237
}
245
238
246
- if (!success_estimate_vel ) {
239
+ if (!point_velocity ) {
247
240
// if failed to estimate velocity, need to stop
248
241
RCLCPP_DEBUG_THROTTLE (
249
242
node_->get_logger (), *node_->get_clock (), std::chrono::milliseconds (1000 ).count (),
@@ -257,7 +250,7 @@ void AdaptiveCruiseController::insertAdaptiveCruiseVelocity(
257
250
258
251
// calculate max(target) velocity of self
259
252
const double upper_velocity =
260
- calcUpperVelocity (col_point_distance, point_velocity, current_velocity);
253
+ calcUpperVelocity (col_point_distance, point_velocity. value () , current_velocity);
261
254
pub_debug_->publish (debug_values_);
262
255
263
256
if (upper_velocity <= param_.thresh_vel_to_stop ) {
@@ -405,45 +398,47 @@ double AdaptiveCruiseController::calcTrajYaw(
405
398
return tf2::getYaw (trajectory.at (collision_point_idx).pose .orientation );
406
399
}
407
400
408
- bool AdaptiveCruiseController::estimatePointVelocityFromObject (
401
+ std::optional< double > AdaptiveCruiseController::estimatePointVelocityFromObject (
409
402
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)
411
404
{
412
405
geometry_msgs::msg::Point nearest_collision_p_ros;
413
406
nearest_collision_p_ros.x = nearest_collision_point.x ;
414
407
nearest_collision_p_ros.y = nearest_collision_point.y ;
415
408
nearest_collision_p_ros.z = nearest_collision_point.z ;
416
409
417
410
/* 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;
421
412
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
+ }
438
432
}
439
433
}
440
434
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;
445
440
} else {
446
- return false ;
441
+ return std::nullopt ;
447
442
}
448
443
}
449
444
@@ -467,9 +462,9 @@ void AdaptiveCruiseController::calculateProjectedVelocityFromObject(
467
462
debug_values_.data .at (DBGVAL::ESTIMATED_VEL_OBJ) = *velocity;
468
463
}
469
464
470
- bool AdaptiveCruiseController::estimatePointVelocityFromPcl (
465
+ std::optional< double > AdaptiveCruiseController::estimatePointVelocityFromPcl (
471
466
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)
473
468
{
474
469
geometry_msgs::msg::Point nearest_collision_p_ros;
475
470
nearest_collision_p_ros.x = nearest_collision_point.x ;
@@ -487,7 +482,7 @@ bool AdaptiveCruiseController::estimatePointVelocityFromPcl(
487
482
prev_collision_point_time_ = nearest_collision_point_time;
488
483
prev_collision_point_ = nearest_collision_point;
489
484
prev_collision_point_valid_ = true ;
490
- return false ;
485
+ return std::nullopt ;
491
486
}
492
487
const double p_dx = nearest_collision_point.x - prev_collision_point_.x ;
493
488
const double p_dy = nearest_collision_point.y - prev_collision_point_.y ;
@@ -501,22 +496,22 @@ bool AdaptiveCruiseController::estimatePointVelocityFromPcl(
501
496
prev_collision_point_ = nearest_collision_point;
502
497
prev_collision_point_valid_ = true ;
503
498
est_vel_que_.clear ();
504
- return false ;
499
+ return std::nullopt ;
505
500
}
506
501
507
502
// append new velocity and remove old velocity from que
508
503
registerQueToVelocity (est_velocity, nearest_collision_point_time);
509
504
}
510
505
511
506
// 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;
514
509
515
510
prev_collision_point_time_ = nearest_collision_point_time;
516
511
prev_collision_point_ = nearest_collision_point;
517
- prev_target_velocity_ = * velocity;
512
+ prev_target_velocity_ = velocity;
518
513
prev_collision_point_valid_ = true ;
519
- return true ;
514
+ return velocity ;
520
515
}
521
516
522
517
double AdaptiveCruiseController::estimateRoughPointVelocity (double current_vel)
0 commit comments