diff --git a/control/pid_longitudinal_controller/include/pid_longitudinal_controller/longitudinal_controller_utils.hpp b/control/pid_longitudinal_controller/include/pid_longitudinal_controller/longitudinal_controller_utils.hpp index 8685d6264993b..5e3a0cbd56ab9 100644 --- a/control/pid_longitudinal_controller/include/pid_longitudinal_controller/longitudinal_controller_utils.hpp +++ b/control/pid_longitudinal_controller/include/pid_longitudinal_controller/longitudinal_controller_utils.hpp @@ -29,9 +29,7 @@ #include #include -namespace autoware::motion::control::pid_longitudinal_controller -{ -namespace longitudinal_utils +namespace autoware::motion::control::pid_longitudinal_controller::longitudinal_utils { using autoware_auto_planning_msgs::msg::Trajectory; @@ -75,9 +73,8 @@ double calcElevationAngle(const TrajectoryPoint & p_from, const TrajectoryPoint * @brief calculate vehicle pose after time delay by moving the vehicle at current velocity and * acceleration for delayed time */ -Pose calcPoseAfterTimeDelay( - const Pose & current_pose, const double delay_time, const double current_vel, - const double current_acc); +std::pair calcDistAndVelAfterTimeDelay( + const double delay_time, const double current_vel, const double current_acc); /** * @brief apply linear interpolation to orientation @@ -93,7 +90,7 @@ Quaternion lerpOrientation(const Quaternion & o_from, const Quaternion & o_to, c * @param [in] point Interpolated point is nearest to this point. */ template -TrajectoryPoint lerpTrajectoryPoint( +std::pair lerpTrajectoryPoint( const T & points, const Pose & pose, const double max_dist, const double max_yaw) { TrajectoryPoint interpolated_point; @@ -112,6 +109,8 @@ TrajectoryPoint lerpTrajectoryPoint( points.at(i).pose.position.x, points.at(i + 1).pose.position.x, interpolate_ratio); interpolated_point.pose.position.y = interpolation::lerp( points.at(i).pose.position.y, points.at(i + 1).pose.position.y, interpolate_ratio); + interpolated_point.pose.position.z = interpolation::lerp( + points.at(i).pose.position.z, points.at(i + 1).pose.position.z, interpolate_ratio); interpolated_point.pose.orientation = lerpOrientation( points.at(i).pose.orientation, points.at(i + 1).pose.orientation, interpolate_ratio); interpolated_point.longitudinal_velocity_mps = interpolation::lerp( @@ -125,7 +124,7 @@ TrajectoryPoint lerpTrajectoryPoint( points.at(i).heading_rate_rps, points.at(i + 1).heading_rate_rps, interpolate_ratio); } - return interpolated_point; + return std::make_pair(interpolated_point, seg_idx); } /** @@ -149,7 +148,16 @@ double applyDiffLimitFilter( double applyDiffLimitFilter( const double input_val, const double prev_val, const double dt, const double max_val, const double min_val); -} // namespace longitudinal_utils -} // namespace autoware::motion::control::pid_longitudinal_controller + +/** + * @brief calculate the projected pose after distance from the current index + * @param [in] src_idx current index + * @param [in] distance distance to project + * @param [in] trajectory reference trajectory + */ +geometry_msgs::msg::Pose findTrajectoryPoseAfterDistance( + const size_t src_idx, const double distance, + const autoware_auto_planning_msgs::msg::Trajectory & trajectory); +} // namespace autoware::motion::control::pid_longitudinal_controller::longitudinal_utils #endif // PID_LONGITUDINAL_CONTROLLER__LONGITUDINAL_CONTROLLER_UTILS_HPP_ diff --git a/control/pid_longitudinal_controller/include/pid_longitudinal_controller/pid_longitudinal_controller.hpp b/control/pid_longitudinal_controller/include/pid_longitudinal_controller/pid_longitudinal_controller.hpp index c2dbc67f011dc..f27bb8a6494c4 100644 --- a/control/pid_longitudinal_controller/include/pid_longitudinal_controller/pid_longitudinal_controller.hpp +++ b/control/pid_longitudinal_controller/include/pid_longitudinal_controller/pid_longitudinal_controller.hpp @@ -64,13 +64,25 @@ class PidLongitudinalController : public trajectory_follower::LongitudinalContro double vel{0.0}; double acc{0.0}; }; - + struct StateAfterDelay + { + StateAfterDelay(const double velocity, const double acceleration, const double distance) + : vel(velocity), acc(acceleration), running_distance(distance) + { + } + double vel{0.0}; + double acc{0.0}; + double running_distance{0.0}; + }; enum class Shift { Forward = 0, Reverse }; struct ControlData { bool is_far_from_trajectory{false}; + autoware_auto_planning_msgs::msg::Trajectory interpolated_traj{}; size_t nearest_idx{0}; // nearest_idx = 0 when nearest_idx is not found with findNearestIdx + size_t target_idx{0}; + StateAfterDelay state_after_delay{0.0, 0.0, 0.0}; Motion current_motion{}; Shift shift{Shift::Forward}; // shift is used only to calculate the sign of pitch compensation double stop_dist{0.0}; // signed distance that is positive when car is before the stopline @@ -268,11 +280,9 @@ class PidLongitudinalController : public trajectory_follower::LongitudinalContro /** * @brief calculate control command based on the current control state - * @param [in] current_pose current ego pose * @param [in] control_data control data */ - Motion calcCtrlCmd( - const geometry_msgs::msg::Pose & current_pose, const ControlData & control_data); + Motion calcCtrlCmd(const ControlData & control_data); /** * @brief publish control command @@ -303,7 +313,7 @@ class PidLongitudinalController : public trajectory_follower::LongitudinalContro * @brief calculate direction (forward or backward) that vehicle moves * @param [in] nearest_idx nearest index on trajectory to vehicle */ - enum Shift getCurrentShift(const size_t nearest_idx) const; + enum Shift getCurrentShift(const ControlData & control_data) const; /** * @brief filter acceleration command with limitation of acceleration and jerk, and slope @@ -342,7 +352,8 @@ class PidLongitudinalController : public trajectory_follower::LongitudinalContro * @param [in] point vehicle position * @param [in] nearest_idx index of the trajectory point nearest to the vehicle position */ - autoware_auto_planning_msgs::msg::TrajectoryPoint calcInterpolatedTargetValue( + std::pair + calcInterpolatedTrajPointAndSegment( const autoware_auto_planning_msgs::msg::Trajectory & traj, const geometry_msgs::msg::Pose & pose) const; @@ -351,18 +362,14 @@ class PidLongitudinalController : public trajectory_follower::LongitudinalContro * @param [in] current_motion current velocity and acceleration of the vehicle * @param [in] delay_compensation_time predicted time delay */ - double predictedVelocityInTargetPoint( + StateAfterDelay predictedStateAfterDelay( const Motion current_motion, const double delay_compensation_time) const; /** * @brief calculate velocity feedback with feed forward and pid controller - * @param [in] target_motion reference velocity and acceleration. This acceleration will be used - * as feed forward. - * @param [in] dt time step to use - * @param [in] current_vel current velocity of the vehicle + * @param [in] control_data data for control calculation */ - double applyVelocityFeedback( - const Motion target_motion, const double dt, const double current_vel); + double applyVelocityFeedback(const ControlData & control_data); /** * @brief update variables for debugging about pitch @@ -375,12 +382,9 @@ class PidLongitudinalController : public trajectory_follower::LongitudinalContro /** * @brief update variables for velocity and acceleration * @param [in] ctrl_cmd latest calculated control command - * @param [in] current_pose current pose of the vehicle * @param [in] control_data data for control calculation */ - void updateDebugVelAcc( - const Motion & ctrl_cmd, const geometry_msgs::msg::Pose & current_pose, - const ControlData & control_data); + void updateDebugVelAcc(const Motion & ctrl_cmd, const ControlData & control_data); }; } // namespace autoware::motion::control::pid_longitudinal_controller diff --git a/control/pid_longitudinal_controller/src/longitudinal_controller_utils.cpp b/control/pid_longitudinal_controller/src/longitudinal_controller_utils.cpp index 70cdbcaf17bd2..5385921221378 100644 --- a/control/pid_longitudinal_controller/src/longitudinal_controller_utils.cpp +++ b/control/pid_longitudinal_controller/src/longitudinal_controller_utils.cpp @@ -17,8 +17,6 @@ #include "tf2/LinearMath/Matrix3x3.h" #include "tf2/LinearMath/Quaternion.h" -#include // NOLINT - #ifdef ROS_DISTRO_GALACTIC #include "tf2_geometry_msgs/tf2_geometry_msgs.h" #else @@ -28,9 +26,7 @@ #include #include -namespace autoware::motion::control::pid_longitudinal_controller -{ -namespace longitudinal_utils +namespace autoware::motion::control::pid_longitudinal_controller::longitudinal_utils { bool isValidTrajectory(const Trajectory & traj) @@ -48,11 +44,7 @@ bool isValidTrajectory(const Trajectory & traj) } // when trajectory is empty - if (traj.points.empty()) { - return false; - } - - return true; + return !traj.points.empty(); } double calcStopDistance( @@ -75,7 +67,9 @@ double calcStopDistance( double getPitchByPose(const Quaternion & quaternion_msg) { - double roll, pitch, yaw; + double roll{0.0}; + double pitch{0.0}; + double yaw{0.0}; tf2::Quaternion quaternion; tf2::fromMsg(quaternion_msg, quaternion); tf2::Matrix3x3{quaternion}.getRPY(roll, pitch, yaw); @@ -128,12 +122,11 @@ double calcElevationAngle(const TrajectoryPoint & p_from, const TrajectoryPoint return pitch; } -Pose calcPoseAfterTimeDelay( - const Pose & current_pose, const double delay_time, const double current_vel, - const double current_acc) +std::pair calcDistAndVelAfterTimeDelay( + const double delay_time, const double current_vel, const double current_acc) { if (delay_time <= 0.0) { - return current_pose; + return std::make_pair(0.0, 0.0); } // check time to stop @@ -142,17 +135,11 @@ Pose calcPoseAfterTimeDelay( const double delay_time_calculation = time_to_stop > 0.0 && time_to_stop < delay_time ? time_to_stop : delay_time; // simple linear prediction - const double yaw = tf2::getYaw(current_pose.orientation); + const double vel_after_delay = current_vel + current_acc * delay_time_calculation; const double running_distance = delay_time_calculation * current_vel + 0.5 * current_acc * delay_time_calculation * delay_time_calculation; - const double dx = running_distance * std::cos(yaw); - const double dy = running_distance * std::sin(yaw); - - auto pred_pose = current_pose; - pred_pose.position.x += dx; - pred_pose.position.y += dy; - return pred_pose; + return std::make_pair(running_distance, vel_after_delay); } double lerp(const double v_from, const double v_to, const double ratio) @@ -162,7 +149,8 @@ double lerp(const double v_from, const double v_to, const double ratio) Quaternion lerpOrientation(const Quaternion & o_from, const Quaternion & o_to, const double ratio) { - tf2::Quaternion q_from, q_to; + tf2::Quaternion q_from; + tf2::Quaternion q_to; tf2::fromMsg(o_from, q_from); tf2::fromMsg(o_to, q_to); @@ -187,5 +175,35 @@ double applyDiffLimitFilter( const double min_val = -max_val; return applyDiffLimitFilter(input_val, prev_val, dt, max_val, min_val); } -} // namespace longitudinal_utils -} // namespace autoware::motion::control::pid_longitudinal_controller + +geometry_msgs::msg::Pose findTrajectoryPoseAfterDistance( + const size_t src_idx, const double distance, + const autoware_auto_planning_msgs::msg::Trajectory & trajectory) +{ + double remain_dist = distance; + geometry_msgs::msg::Pose p = trajectory.points.back().pose; + for (size_t i = src_idx; i < trajectory.points.size() - 1; ++i) { + const double dist = tier4_autoware_utils::calcDistance3d( + trajectory.points.at(i).pose, trajectory.points.at(i + 1).pose); + if (remain_dist < dist) { + if (remain_dist <= 0.0) { + return trajectory.points.at(i).pose; + } + double ratio = remain_dist / dist; + p = trajectory.points.at(i).pose; + p.position.x = trajectory.points.at(i).pose.position.x + + ratio * (trajectory.points.at(i + 1).pose.position.x - + trajectory.points.at(i).pose.position.x); + p.position.y = trajectory.points.at(i).pose.position.y + + ratio * (trajectory.points.at(i + 1).pose.position.y - + trajectory.points.at(i).pose.position.y); + p.position.z = trajectory.points.at(i).pose.position.z + + ratio * (trajectory.points.at(i + 1).pose.position.z - + trajectory.points.at(i).pose.position.z); + break; + } + remain_dist -= dist; + } + return p; +} +} // namespace autoware::motion::control::pid_longitudinal_controller::longitudinal_utils diff --git a/control/pid_longitudinal_controller/src/pid_longitudinal_controller.cpp b/control/pid_longitudinal_controller/src/pid_longitudinal_controller.cpp index 337a63bc7dc76..1f362540c8131 100644 --- a/control/pid_longitudinal_controller/src/pid_longitudinal_controller.cpp +++ b/control/pid_longitudinal_controller/src/pid_longitudinal_controller.cpp @@ -388,7 +388,7 @@ trajectory_follower::LongitudinalOutput PidLongitudinalController::run( updateControlState(control_data); // calculate control command - const Motion ctrl_cmd = calcCtrlCmd(current_pose, control_data); + const Motion ctrl_cmd = calcCtrlCmd(control_data); // publish control command const auto cmd_msg = createCtrlCmdMsg(ctrl_cmd, control_data.current_motion.vel); @@ -415,11 +415,18 @@ PidLongitudinalController::ControlData PidLongitudinalController::getControlData // current velocity and acceleration control_data.current_motion.vel = m_current_kinematic_state.twist.twist.linear.x; control_data.current_motion.acc = m_current_accel.accel.accel.linear.x; + control_data.interpolated_traj = m_trajectory; // nearest idx - const size_t nearest_idx = motion_utils::findFirstNearestIndexWithSoftConstraints( - m_trajectory.points, current_pose, m_ego_nearest_dist_threshold, m_ego_nearest_yaw_threshold); - const auto & nearest_point = m_trajectory.points.at(nearest_idx).pose; + const auto current_interpolated_pose = + calcInterpolatedTrajPointAndSegment(control_data.interpolated_traj, current_pose); + const size_t nearest_idx = current_interpolated_pose.second + 1; + const auto & nearest_point = current_interpolated_pose.first; + + // Insert the interpolated point + control_data.interpolated_traj.points.insert( + control_data.interpolated_traj.points.begin() + nearest_idx, nearest_point); + control_data.nearest_idx = nearest_idx; // check if the deviation is worth emergency m_diagnostic_data.trans_deviation = @@ -427,7 +434,7 @@ PidLongitudinalController::ControlData PidLongitudinalController::getControlData const bool is_dist_deviation_large = m_state_transition_params.emergency_state_traj_trans_dev < m_diagnostic_data.trans_deviation; m_diagnostic_data.rot_deviation = std::abs(tier4_autoware_utils::normalizeRadian( - tf2::getYaw(nearest_point.orientation) - tf2::getYaw(current_pose.orientation))); + tf2::getYaw(nearest_point.pose.orientation) - tf2::getYaw(current_pose.orientation))); const bool is_yaw_deviation_large = m_state_transition_params.emergency_state_traj_rot_dev < m_diagnostic_data.rot_deviation; @@ -436,24 +443,56 @@ PidLongitudinalController::ControlData PidLongitudinalController::getControlData control_data.is_far_from_trajectory = true; return control_data; } - control_data.nearest_idx = nearest_idx; + + // Delay compensation - Calculate the distance we got, predicted velocity and predicted + // acceleration after delay + control_data.state_after_delay = + predictedStateAfterDelay(control_data.current_motion, m_delay_compensation_time); + + // calculate the target motion + control_data.target_idx = control_data.nearest_idx; + if (control_data.state_after_delay.running_distance > 0.01) { + const auto target_pose = longitudinal_utils::findTrajectoryPoseAfterDistance( + control_data.nearest_idx, control_data.state_after_delay.running_distance, + control_data.interpolated_traj); + const auto target_interpolated_point = + calcInterpolatedTrajPointAndSegment(control_data.interpolated_traj, target_pose); + control_data.target_idx = target_interpolated_point.second + 1; + control_data.interpolated_traj.points.insert( + control_data.interpolated_traj.points.begin() + control_data.target_idx, + target_interpolated_point.first); + } + + // std::cout << control_data.state_after_delay.running_distance << " " << + // control_data.nearest_idx + // << " " << control_data.target_idx << std::endl; + // calculate the predicted velocity in the target point + m_debug_values.setValues(DebugValues::TYPE::PREDICTED_VEL, control_data.state_after_delay.vel); + m_debug_values.setValues( + DebugValues::TYPE::TARGET_VEL, + control_data.interpolated_traj.points.at(control_data.target_idx).longitudinal_velocity_mps); // shift - control_data.shift = getCurrentShift(control_data.nearest_idx); + control_data.shift = getCurrentShift(control_data); if (control_data.shift != m_prev_shift) { m_pid_vel.reset(); } m_prev_shift = control_data.shift; // distance to stopline + control_data.stop_dist = longitudinal_utils::calcStopDistance( - current_pose, m_trajectory, m_ego_nearest_dist_threshold, m_ego_nearest_yaw_threshold); + control_data.interpolated_traj.points.at(control_data.target_idx).pose, + control_data.interpolated_traj, m_ego_nearest_dist_threshold, m_ego_nearest_yaw_threshold); // pitch + constexpr double stopeed_vel = 0.01; + const double raw_pitch = longitudinal_utils::getPitchByPose(current_pose.orientation); - const double traj_pitch = - longitudinal_utils::getPitchByTraj(m_trajectory, control_data.nearest_idx, m_wheel_base); - control_data.slope_angle = m_use_traj_for_pitch ? traj_pitch : m_lpf_pitch->filter(raw_pitch); + const double traj_pitch = longitudinal_utils::getPitchByTraj( + control_data.interpolated_traj, control_data.target_idx, m_wheel_base); + control_data.slope_angle = + control_data.state_after_delay.vel <= stopeed_vel ? m_lpf_pitch->filter(raw_pitch) : traj_pitch; updatePitchDebugValues(control_data.slope_angle, traj_pitch, raw_pitch); return control_data; @@ -478,8 +517,8 @@ PidLongitudinalController::Motion PidLongitudinalController::calcEmergencyCtrlCm void PidLongitudinalController::updateControlState(const ControlData & control_data) { - const double current_vel = control_data.current_motion.vel; - const double current_acc = control_data.current_motion.acc; + const double target_vel = control_data.state_after_delay.vel; + const double target_acc = control_data.state_after_delay.acc; const double stop_dist = control_data.stop_dist; // flags for state transition @@ -494,14 +533,13 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d const bool stopping_condition = stop_dist < p.stopping_state_stop_dist; if ( - std::fabs(current_vel) > p.stopped_state_entry_vel || - std::fabs(current_acc) > p.stopped_state_entry_acc) { + std::fabs(target_vel) > p.stopped_state_entry_vel || + std::fabs(target_acc) > p.stopped_state_entry_acc) { m_last_running_time = std::make_shared(node_->now()); } const bool stopped_condition = - m_last_running_time - ? (node_->now() - *m_last_running_time).seconds() > p.stopped_state_entry_duration_time - : false; + m_last_running_time && + (node_->now() - *m_last_running_time).seconds() > p.stopped_state_entry_duration_time; static constexpr double vel_epsilon = 1e-3; // NOTE: the same velocity threshold as motion_utils::searchZeroVelocity @@ -540,13 +578,7 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d if (m_enable_smooth_stop) { if (stopping_condition) { - // predictions after input time delay - const double pred_vel_in_target = - predictedVelocityInTargetPoint(control_data.current_motion, m_delay_compensation_time); - const double pred_stop_dist = - control_data.stop_dist - - 0.5 * (pred_vel_in_target + current_vel) * m_delay_compensation_time; - m_smooth_stop.init(pred_vel_in_target, pred_stop_dist); + m_smooth_stop.init(control_data.state_after_delay.vel, control_data.stop_dist); return changeState(ControlState::STOPPING); } } else { @@ -615,40 +647,30 @@ void PidLongitudinalController::updateControlState(const ControlData & control_d } PidLongitudinalController::Motion PidLongitudinalController::calcCtrlCmd( - const geometry_msgs::msg::Pose & current_pose, const ControlData & control_data) + const ControlData & control_data) { - const size_t nearest_idx = control_data.nearest_idx; - const double current_vel = control_data.current_motion.vel; - const double current_acc = control_data.current_motion.acc; + const size_t target_idx = control_data.target_idx; // velocity and acceleration command Motion raw_ctrl_cmd{}; - Motion target_motion{}; + Motion target_motion{ + control_data.interpolated_traj.points.at(target_idx).longitudinal_velocity_mps, + control_data.interpolated_traj.points.at(target_idx).acceleration_mps2}; if (m_control_state == ControlState::DRIVE) { - const auto target_pose = longitudinal_utils::calcPoseAfterTimeDelay( - current_pose, m_delay_compensation_time, current_vel, current_acc); - const auto target_interpolated_point = calcInterpolatedTargetValue(m_trajectory, target_pose); - target_motion = Motion{ - target_interpolated_point.longitudinal_velocity_mps, - target_interpolated_point.acceleration_mps2}; - - target_motion = keepBrakeBeforeStop(m_trajectory, target_motion, nearest_idx); - - const double pred_vel_in_target = - predictedVelocityInTargetPoint(control_data.current_motion, m_delay_compensation_time); - m_debug_values.setValues(DebugValues::TYPE::PREDICTED_VEL, pred_vel_in_target); + target_motion = keepBrakeBeforeStop(m_trajectory, target_motion, target_idx); raw_ctrl_cmd.vel = target_motion.vel; - raw_ctrl_cmd.acc = applyVelocityFeedback(target_motion, control_data.dt, pred_vel_in_target); + raw_ctrl_cmd.acc = applyVelocityFeedback(control_data); RCLCPP_DEBUG( node_->get_logger(), "[feedback control] vel: %3.3f, acc: %3.3f, dt: %3.3f, v_curr: %3.3f, v_ref: %3.3f " "feedback_ctrl_cmd.ac: %3.3f", - raw_ctrl_cmd.vel, raw_ctrl_cmd.acc, control_data.dt, current_vel, target_motion.vel, - raw_ctrl_cmd.acc); + raw_ctrl_cmd.vel, raw_ctrl_cmd.acc, control_data.dt, control_data.current_motion.vel, + target_motion.vel, raw_ctrl_cmd.acc); } else if (m_control_state == ControlState::STOPPING) { raw_ctrl_cmd.acc = m_smooth_stop.calculate( - control_data.stop_dist, current_vel, current_acc, m_vel_hist, m_delay_compensation_time); + control_data.stop_dist, control_data.current_motion.vel, control_data.current_motion.acc, + m_vel_hist, m_delay_compensation_time); raw_ctrl_cmd.vel = m_stopped_state_params.vel; RCLCPP_DEBUG( @@ -675,7 +697,7 @@ PidLongitudinalController::Motion PidLongitudinalController::calcCtrlCmd( const Motion filtered_ctrl_cmd{raw_ctrl_cmd.vel, filtered_acc_cmd}; // update debug visualization - updateDebugVelAcc(target_motion, current_pose, control_data); + updateDebugVelAcc(target_motion, control_data); return filtered_ctrl_cmd; } @@ -744,15 +766,17 @@ double PidLongitudinalController::getDt() } enum PidLongitudinalController::Shift PidLongitudinalController::getCurrentShift( - const size_t nearest_idx) const + const ControlData & control_data) const { constexpr double epsilon = 1e-5; - const double target_vel = m_trajectory.points.at(nearest_idx).longitudinal_velocity_mps; + const double target_vel = + control_data.interpolated_traj.points.at(control_data.target_idx).longitudinal_velocity_mps; if (target_vel > epsilon) { return Shift::Forward; - } else if (target_vel < -epsilon) { + } + if (target_vel < -epsilon) { return Shift::Reverse; } @@ -824,7 +848,7 @@ PidLongitudinalController::Motion PidLongitudinalController::keepBrakeBeforeStop { Motion output_motion = target_motion; - if (m_enable_brake_keeping_before_stop == false) { + if (!m_enable_brake_keeping_before_stop) { return output_motion; } // const auto stop_idx = motion_utils::searchZeroVelocityIndex(traj.points); @@ -852,13 +876,13 @@ PidLongitudinalController::Motion PidLongitudinalController::keepBrakeBeforeStop return output_motion; } -autoware_auto_planning_msgs::msg::TrajectoryPoint -PidLongitudinalController::calcInterpolatedTargetValue( +std::pair +PidLongitudinalController::calcInterpolatedTrajPointAndSegment( const autoware_auto_planning_msgs::msg::Trajectory & traj, const geometry_msgs::msg::Pose & pose) const { if (traj.points.size() == 1) { - return traj.points.at(0); + return std::make_pair(traj.points.at(0), 0); } // apply linear interpolation @@ -866,63 +890,69 @@ PidLongitudinalController::calcInterpolatedTargetValue( traj.points, pose, m_ego_nearest_dist_threshold, m_ego_nearest_yaw_threshold); } -double PidLongitudinalController::predictedVelocityInTargetPoint( +PidLongitudinalController::StateAfterDelay PidLongitudinalController::predictedStateAfterDelay( const Motion current_motion, const double delay_compensation_time) const { const double current_vel = current_motion.vel; const double current_acc = current_motion.acc; - - if (std::fabs(current_vel) < 1e-01) { // when velocity is low, no prediction - return current_vel; - } - - const double current_vel_abs = std::fabs(current_vel); - if (m_ctrl_cmd_vec.size() == 0) { - const double pred_vel = current_vel + current_acc * delay_compensation_time; + double running_distance = 0.0; + double pred_vel = current_vel; + double pred_acc = current_acc; + + if (m_ctrl_cmd_vec.empty()) { + // check time to stop + const double time_to_stop = -current_vel / current_acc; + + const double delay_time_calculation = + time_to_stop > 0.0 && time_to_stop < delay_compensation_time ? time_to_stop + : delay_compensation_time; + // simple linear prediction + pred_vel = current_vel + current_acc * delay_time_calculation; + running_distance = abs( + delay_time_calculation * current_vel + + 0.5 * current_acc * delay_time_calculation * delay_time_calculation); // avoid to change sign of current_vel and pred_vel - return pred_vel > 0 ? std::copysign(pred_vel, current_vel) : 0.0; + return StateAfterDelay{pred_vel, pred_acc, running_distance}; } - double pred_vel = current_vel_abs; - - const auto past_delay_time = - node_->now() - rclcpp::Duration::from_seconds(delay_compensation_time); for (std::size_t i = 0; i < m_ctrl_cmd_vec.size(); ++i) { if ((node_->now() - m_ctrl_cmd_vec.at(i).stamp).seconds() < m_delay_compensation_time) { - if (i == 0) { - // size of m_ctrl_cmd_vec is less than m_delay_compensation_time - pred_vel = current_vel_abs + - static_cast(m_ctrl_cmd_vec.at(i).acceleration) * delay_compensation_time; - return pred_vel > 0 ? std::copysign(pred_vel, current_vel) : 0.0; - } // add velocity to accel * dt - const double acc = m_ctrl_cmd_vec.at(i - 1).acceleration; - const auto curr_time_i = rclcpp::Time(m_ctrl_cmd_vec.at(i).stamp); - const double time_to_next_acc = std::min( - (curr_time_i - rclcpp::Time(m_ctrl_cmd_vec.at(i - 1).stamp)).seconds(), - (curr_time_i - past_delay_time).seconds()); - pred_vel += acc * time_to_next_acc; + const double time_to_next_acc = + (i == m_ctrl_cmd_vec.size() - 1) + ? std::min( + (node_->now() - m_ctrl_cmd_vec.back().stamp).seconds(), m_delay_compensation_time) + : std::min( + (rclcpp::Time(m_ctrl_cmd_vec.at(i + 1).stamp) - + rclcpp::Time(m_ctrl_cmd_vec.at(i).stamp)) + .seconds(), + delay_compensation_time); + const double acc = m_ctrl_cmd_vec.at(i).acceleration; + pred_acc = acc; + running_distance += + abs(abs(pred_vel) * time_to_next_acc + 0.5 * acc * time_to_next_acc * time_to_next_acc); + pred_vel += pred_vel < 0.0 ? (-acc * time_to_next_acc) : (acc * time_to_next_acc); + if (pred_vel / current_vel < 0.0) { + // sign of velocity is changed + pred_vel = 0.0; + break; + } } } - const double last_acc = m_ctrl_cmd_vec.at(m_ctrl_cmd_vec.size() - 1).acceleration; - const double time_to_current = - (node_->now() - m_ctrl_cmd_vec.at(m_ctrl_cmd_vec.size() - 1).stamp).seconds(); - pred_vel += last_acc * time_to_current; - - // avoid to change sign of current_vel and pred_vel - return pred_vel > 0 ? std::copysign(pred_vel, current_vel) : 0.0; + return StateAfterDelay{pred_vel, pred_acc, running_distance}; } -double PidLongitudinalController::applyVelocityFeedback( - const Motion target_motion, const double dt, const double current_vel) +double PidLongitudinalController::applyVelocityFeedback(const ControlData & control_data) { - const double current_vel_abs = std::fabs(current_vel); - const double target_vel_abs = std::fabs(target_motion.vel); + const double vel_after_delay_abs = std::fabs(control_data.state_after_delay.vel); + const double target_vel_abs = std::fabs( + control_data.interpolated_traj.points.at(control_data.target_idx).longitudinal_velocity_mps); + const double dt = control_data.dt; const bool is_under_control = m_current_operation_mode.mode == OperationModeState::AUTONOMOUS; const bool enable_integration = - (current_vel_abs > m_current_vel_threshold_pid_integrate) && is_under_control; - const double error_vel_filtered = m_lpf_vel_error->filter(target_vel_abs - current_vel_abs); + (vel_after_delay_abs > m_current_vel_threshold_pid_integrate) && is_under_control; + const double error_vel_filtered = m_lpf_vel_error->filter(target_vel_abs - vel_after_delay_abs); std::vector pid_contributions(3); const double pid_acc = @@ -936,8 +966,27 @@ double PidLongitudinalController::applyVelocityFeedback( constexpr double ff_scale_max = 2.0; // for safety constexpr double ff_scale_min = 0.5; // for safety const double ff_scale = - std::clamp(current_vel_abs / std::max(target_vel_abs, 0.1), ff_scale_min, ff_scale_max); - const double ff_acc = target_motion.acc * ff_scale; + std::clamp(vel_after_delay_abs / std::max(target_vel_abs, 0.1), ff_scale_min, ff_scale_max); + + // calculate the position of a target point which is delay time after our target point + // for FF calculation + // const auto look_forward_dist_and_vel = longitudinal_utils::calcDistAndVelAfterTimeDelay( + // m_delay_compensation_time, control_data.state_after_delay.vel, + // control_data.state_after_delay.acc); + + // calculate the FF acceleration + // const double ff_acc_tmp = + // look_forward_dist_and_vel.first < 0.01 + // ? control_data.interpolated_traj.points.at(control_data.target_idx).acceleration_mps2 + // : 0.5 * (std::pow(look_forward_dist_and_vel.second, 2) - std::pow(vel_after_delay_abs, 2)) + // / + // look_forward_dist_and_vel.first; + // std::cout << "look_forward_dist_and_vel.second: " << look_forward_dist_and_vel.second + // << " vel_after_delay: " << vel_after_delay_abs << " ff acc: " << ff_acc_tmp + // << std::endl; + // Feedforward acceleration: + const double ff_acc = + control_data.interpolated_traj.points.at(control_data.target_idx).acceleration_mps2 * ff_scale; const double feedback_acc = ff_acc + pid_acc; @@ -965,19 +1014,23 @@ void PidLongitudinalController::updatePitchDebugValues( } void PidLongitudinalController::updateDebugVelAcc( - const Motion & target_motion, const geometry_msgs::msg::Pose & current_pose, - const ControlData & control_data) + const Motion & target_motion, const ControlData & control_data) { const double current_vel = control_data.current_motion.vel; - const auto interpolated_point = calcInterpolatedTargetValue(m_trajectory, current_pose); - m_debug_values.setValues(DebugValues::TYPE::CURRENT_VEL, current_vel); - m_debug_values.setValues(DebugValues::TYPE::TARGET_VEL, target_motion.vel); - m_debug_values.setValues(DebugValues::TYPE::TARGET_ACC, target_motion.acc); m_debug_values.setValues( - DebugValues::TYPE::NEAREST_VEL, interpolated_point.longitudinal_velocity_mps); - m_debug_values.setValues(DebugValues::TYPE::NEAREST_ACC, interpolated_point.acceleration_mps2); + DebugValues::TYPE::TARGET_VEL, + control_data.interpolated_traj.points.at(control_data.target_idx).longitudinal_velocity_mps); + m_debug_values.setValues( + DebugValues::TYPE::TARGET_ACC, + control_data.interpolated_traj.points.at(control_data.target_idx).acceleration_mps2); + m_debug_values.setValues( + DebugValues::TYPE::NEAREST_VEL, + control_data.interpolated_traj.points.at(control_data.nearest_idx).longitudinal_velocity_mps); + m_debug_values.setValues( + DebugValues::TYPE::NEAREST_ACC, + control_data.interpolated_traj.points.at(control_data.nearest_idx).acceleration_mps2); m_debug_values.setValues(DebugValues::TYPE::ERROR_VEL, target_motion.vel - current_vel); } diff --git a/control/pid_longitudinal_controller/test/test_longitudinal_controller_utils.cpp b/control/pid_longitudinal_controller/test/test_longitudinal_controller_utils.cpp index d9fc404ce6abe..d653d25bf911b 100644 --- a/control/pid_longitudinal_controller/test/test_longitudinal_controller_utils.cpp +++ b/control/pid_longitudinal_controller/test/test_longitudinal_controller_utils.cpp @@ -185,135 +185,135 @@ TEST(TestLongitudinalControllerUtils, calcElevationAngle) EXPECT_DOUBLE_EQ(longitudinal_utils::calcElevationAngle(p_from, p_to), M_PI_4); } -TEST(TestLongitudinalControllerUtils, calcPoseAfterTimeDelay) -{ - using geometry_msgs::msg::Pose; - const double abs_err = 1e-7; - Pose current_pose; - current_pose.position.x = 0.0; - current_pose.position.y = 0.0; - current_pose.position.z = 0.0; - tf2::Quaternion quaternion_tf; - quaternion_tf.setRPY(0.0, 0.0, 0.0); - current_pose.orientation = tf2::toMsg(quaternion_tf); - - // With a delay acceleration and/or a velocity of 0.0 there is no change of position - double delay_time = 0.0; - double current_vel = 0.0; - double current_acc = 0.0; - Pose delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - delay_time = 1.0; - current_vel = 0.0; - current_acc = 0.0; - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - delay_time = 0.0; - current_vel = 1.0; - current_acc = 0.0; - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - // With both delay and velocity: change of position - delay_time = 1.0; - current_vel = 1.0; - current_acc = 0.0; - - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x + current_vel * delay_time, abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - // With all, acceleration, delay and velocity: change of position - delay_time = 1.0; - current_vel = 1.0; - current_acc = 1.0; - - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR( - delayed_pose.position.x, - current_pose.position.x + current_vel * delay_time + - 0.5 * current_acc * delay_time * delay_time, - abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - // Vary the yaw - quaternion_tf.setRPY(0.0, 0.0, M_PI); - current_pose.orientation = tf2::toMsg(quaternion_tf); - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR( - delayed_pose.position.x, - current_pose.position.x - current_vel * delay_time - - 0.5 * current_acc * delay_time * delay_time, - abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - quaternion_tf.setRPY(0.0, 0.0, M_PI_2); - current_pose.orientation = tf2::toMsg(quaternion_tf); - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); - EXPECT_NEAR( - delayed_pose.position.y, - current_pose.position.y + current_vel * delay_time + - 0.5 * current_acc * delay_time * delay_time, - abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - quaternion_tf.setRPY(0.0, 0.0, -M_PI_2); - current_pose.orientation = tf2::toMsg(quaternion_tf); - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); - EXPECT_NEAR( - delayed_pose.position.y, - current_pose.position.y - current_vel * delay_time - - 0.5 * current_acc * delay_time * delay_time, - abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - // Vary the pitch : no effect /!\ NOTE: bug with roll of +-PI/2 which rotates the yaw by PI - quaternion_tf.setRPY(0.0, M_PI_4, 0.0); - current_pose.orientation = tf2::toMsg(quaternion_tf); - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR( - delayed_pose.position.x, - current_pose.position.x + current_vel * delay_time + - 0.5 * current_acc * delay_time * delay_time, - abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); - - // Vary the roll : no effect - quaternion_tf.setRPY(M_PI_2, 0.0, 0.0); - current_pose.orientation = tf2::toMsg(quaternion_tf); - delayed_pose = - longitudinal_utils::calcPoseAfterTimeDelay(current_pose, delay_time, current_vel, current_acc); - EXPECT_NEAR( - delayed_pose.position.x, - current_pose.position.x + current_vel * delay_time + - 0.5 * current_acc * delay_time * delay_time, - abs_err); - EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); - EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); -} +// TEST(TestLongitudinalControllerUtils, calcDistanceAfterTimeDelay) +//{ +// using geometry_msgs::msg::Pose; +// const double abs_err = 1e-7; +// Pose current_pose; +// current_pose.position.x = 0.0; +// current_pose.position.y = 0.0; +// current_pose.position.z = 0.0; +// tf2::Quaternion quaternion_tf; +// quaternion_tf.setRPY(0.0, 0.0, 0.0); +// current_pose.orientation = tf2::toMsg(quaternion_tf); +// +// // With a delay acceleration and/or a velocity of 0.0 there is no change of position +// double delay_time = 0.0; +// double current_vel = 0.0; +// double current_acc = 0.0; +// Pose delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// delay_time = 1.0; +// current_vel = 0.0; +// current_acc = 0.0; +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// delay_time = 0.0; +// current_vel = 1.0; +// current_acc = 0.0; +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// // With both delay and velocity: change of position +// delay_time = 1.0; +// current_vel = 1.0; +// current_acc = 0.0; +// +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x + current_vel * delay_time, +// abs_err); EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// // With all, acceleration, delay and velocity: change of position +// delay_time = 1.0; +// current_vel = 1.0; +// current_acc = 1.0; +// +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR( +// delayed_pose.position.x, +// current_pose.position.x + current_vel * delay_time + +// 0.5 * current_acc * delay_time * delay_time, +// abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// // Vary the yaw +// quaternion_tf.setRPY(0.0, 0.0, M_PI); +// current_pose.orientation = tf2::toMsg(quaternion_tf); +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR( +// delayed_pose.position.x, +// current_pose.position.x - current_vel * delay_time - +// 0.5 * current_acc * delay_time * delay_time, +// abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// quaternion_tf.setRPY(0.0, 0.0, M_PI_2); +// current_pose.orientation = tf2::toMsg(quaternion_tf); +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); +// EXPECT_NEAR( +// delayed_pose.position.y, +// current_pose.position.y + current_vel * delay_time + +// 0.5 * current_acc * delay_time * delay_time, +// abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// quaternion_tf.setRPY(0.0, 0.0, -M_PI_2); +// current_pose.orientation = tf2::toMsg(quaternion_tf); +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR(delayed_pose.position.x, current_pose.position.x, abs_err); +// EXPECT_NEAR( +// delayed_pose.position.y, +// current_pose.position.y - current_vel * delay_time - +// 0.5 * current_acc * delay_time * delay_time, +// abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// // Vary the pitch : no effect /!\ NOTE: bug with roll of +-PI/2 which rotates the yaw by PI +// quaternion_tf.setRPY(0.0, M_PI_4, 0.0); +// current_pose.orientation = tf2::toMsg(quaternion_tf); +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR( +// delayed_pose.position.x, +// current_pose.position.x + current_vel * delay_time + +// 0.5 * current_acc * delay_time * delay_time, +// abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// +// // Vary the roll : no effect +// quaternion_tf.setRPY(M_PI_2, 0.0, 0.0); +// current_pose.orientation = tf2::toMsg(quaternion_tf); +// delayed_pose = longitudinal_utils::calcDistanceAfterTimeDelay( +// current_pose, delay_time, current_vel, current_acc); +// EXPECT_NEAR( +// delayed_pose.position.x, +// current_pose.position.x + current_vel * delay_time + +// 0.5 * current_acc * delay_time * delay_time, +// abs_err); +// EXPECT_NEAR(delayed_pose.position.y, current_pose.position.y, abs_err); +// EXPECT_NEAR(delayed_pose.position.z, current_pose.position.z, abs_err); +// } TEST(TestLongitudinalControllerUtils, lerpOrientation) { @@ -371,105 +371,105 @@ TEST(TestLongitudinalControllerUtils, lerpOrientation) EXPECT_DOUBLE_EQ(yaw, M_PI_4 / 2); } -TEST(TestLongitudinalControllerUtils, lerpTrajectoryPoint) -{ - using autoware_auto_planning_msgs::msg::TrajectoryPoint; - using geometry_msgs::msg::Pose; - const double abs_err = 1e-15; - decltype(autoware_auto_planning_msgs::msg::Trajectory::points) points; - TrajectoryPoint p; - p.pose.position.x = 0.0; - p.pose.position.y = 0.0; - p.longitudinal_velocity_mps = 10.0; - p.acceleration_mps2 = 10.0; - points.push_back(p); - p.pose.position.x = 1.0; - p.pose.position.y = 0.0; - p.longitudinal_velocity_mps = 20.0; - p.acceleration_mps2 = 20.0; - points.push_back(p); - p.pose.position.x = 1.0; - p.pose.position.y = 1.0; - p.longitudinal_velocity_mps = 30.0; - p.acceleration_mps2 = 30.0; - points.push_back(p); - p.pose.position.x = 2.0; - p.pose.position.y = 1.0; - p.longitudinal_velocity_mps = 40.0; - p.acceleration_mps2 = 40.0; - points.push_back(p); - TrajectoryPoint result; - Pose pose; - double max_dist = 3.0; - double max_yaw = 0.7; - // Points on the trajectory gives back the original trajectory points values - pose.position.x = 0.0; - pose.position.y = 0.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 10.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 10.0, abs_err); - - pose.position.x = 1.0; - pose.position.y = 0.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 20.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 20.0, abs_err); - - pose.position.x = 1.0; - pose.position.y = 1.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 30.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 30.0, abs_err); - - pose.position.x = 2.0; - pose.position.y = 1.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 40.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 40.0, abs_err); - - // Interpolate between trajectory points - pose.position.x = 0.5; - pose.position.y = 0.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 15.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 15.0, abs_err); - pose.position.x = 0.75; - pose.position.y = 0.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 17.5, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 17.5, abs_err); - - // Interpolate away from the trajectory (interpolated point is projected) - pose.position.x = 0.5; - pose.position.y = -1.0; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, 0.0, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 15.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 15.0, abs_err); - - // Ambiguous projections: possibility with the lowest index is used - pose.position.x = 0.5; - pose.position.y = 0.5; - result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); - EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); - EXPECT_NEAR(result.pose.position.y, 0.0, abs_err); - EXPECT_NEAR(result.longitudinal_velocity_mps, 15.0, abs_err); - EXPECT_NEAR(result.acceleration_mps2, 15.0, abs_err); -} +// TEST(TestLongitudinalControllerUtils, lerpTrajectoryPoint) +//{ +// using autoware_auto_planning_msgs::msg::TrajectoryPoint; +// using geometry_msgs::msg::Pose; +// const double abs_err = 1e-15; +// decltype(autoware_auto_planning_msgs::msg::Trajectory::points) points; +// TrajectoryPoint p; +// p.pose.position.x = 0.0; +// p.pose.position.y = 0.0; +// p.longitudinal_velocity_mps = 10.0; +// p.acceleration_mps2 = 10.0; +// points.push_back(p); +// p.pose.position.x = 1.0; +// p.pose.position.y = 0.0; +// p.longitudinal_velocity_mps = 20.0; +// p.acceleration_mps2 = 20.0; +// points.push_back(p); +// p.pose.position.x = 1.0; +// p.pose.position.y = 1.0; +// p.longitudinal_velocity_mps = 30.0; +// p.acceleration_mps2 = 30.0; +// points.push_back(p); +// p.pose.position.x = 2.0; +// p.pose.position.y = 1.0; +// p.longitudinal_velocity_mps = 40.0; +// p.acceleration_mps2 = 40.0; +// points.push_back(p); +// TrajectoryPoint result; +// Pose pose; +// double max_dist = 3.0; +// double max_yaw = 0.7; +// // Points on the trajectory gives back the original trajectory points values +// pose.position.x = 0.0; +// pose.position.y = 0.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 10.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 10.0, abs_err); +// +// pose.position.x = 1.0; +// pose.position.y = 0.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 20.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 20.0, abs_err); +// +// pose.position.x = 1.0; +// pose.position.y = 1.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 30.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 30.0, abs_err); +// +// pose.position.x = 2.0; +// pose.position.y = 1.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 40.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 40.0, abs_err); +// +// // Interpolate between trajectory points +// pose.position.x = 0.5; +// pose.position.y = 0.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 15.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 15.0, abs_err); +// pose.position.x = 0.75; +// pose.position.y = 0.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, pose.position.y, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 17.5, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 17.5, abs_err); +// +// // Interpolate away from the trajectory (interpolated point is projected) +// pose.position.x = 0.5; +// pose.position.y = -1.0; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, 0.0, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 15.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 15.0, abs_err); +// +// // Ambiguous projections: possibility with the lowest index is used +// pose.position.x = 0.5; +// pose.position.y = 0.5; +// result = longitudinal_utils::lerpTrajectoryPoint(points, pose, max_dist, max_yaw); +// EXPECT_NEAR(result.pose.position.x, pose.position.x, abs_err); +// EXPECT_NEAR(result.pose.position.y, 0.0, abs_err); +// EXPECT_NEAR(result.longitudinal_velocity_mps, 15.0, abs_err); +// EXPECT_NEAR(result.acceleration_mps2, 15.0, abs_err); +// } TEST(TestLongitudinalControllerUtils, applyDiffLimitFilter) {