Go to the documentation of this file.
14 #ifndef MOTION_COMMON__MOTION_COMMON_HPP_
15 #define MOTION_COMMON__MOTION_COMMON_HPP_
18 #include <autoware_auto_planning_msgs/msg/trajectory.hpp>
19 #include <autoware_auto_system_msgs/msg/control_diagnostic.hpp>
20 #include <autoware_auto_vehicle_msgs/msg/vehicle_control_command.hpp>
21 #include <autoware_auto_vehicle_msgs/msg/vehicle_kinematic_state.hpp>
22 #include <geometry_msgs/msg/transform_stamped.hpp>
24 #include <tf2/LinearMath/Quaternion.h>
25 #include <tf2/utils.h>
26 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
37 using Real = decltype(autoware_auto_planning_msgs::msg::TrajectoryPoint::longitudinal_velocity_mps);
39 using Diagnostic = autoware_auto_system_msgs::msg::ControlDiagnostic;
44 using Index = decltype(Trajectory::points)::size_type;
45 using Point = decltype(Trajectory::points)::value_type;
52 const Point & state,
const Point & current_pt,
const Point & next_pt) noexcept;
58 template<
typename IsPastPo
intF>
66 if (traj.points.empty()) {
69 auto next_idx = start_idx;
70 for (
auto idx = start_idx; idx < traj.points.size() - 1U; ++idx) {
71 const auto current_pt = traj.points[idx];
72 const auto next_pt = traj.points[idx + 1U];
110 template<
typename RealT>
113 static_assert(std::is_floating_point<RealT>::value,
"angle must be floating point");
114 tf2::Quaternion quat;
115 quat.setRPY(roll, pitch,
yaw);
116 return tf2::toMsg(quat);
123 template<
typename RealT>
134 throw std::domain_error{
"max < min"};
136 return std::min(max, std::max(min, val));
140 template<
typename T,
typename RealT =
double>
143 static_assert(std::is_floating_point<RealT>::value,
"t must be floating point");
144 t =
clamp(t, RealT{0.0}, RealT{1.0});
145 const auto del = b - a;
146 return static_cast<T
>(t *
static_cast<RealT
>(del)) + a;
153 template<
typename SlerpF>
156 Point ret{rosidl_runtime_cpp::MessageInitialization::ALL};
162 ret.pose.position.x =
interpolate(a.pose.position.x, b.pose.position.x, t);
163 ret.pose.position.y =
interpolate(a.pose.position.y, b.pose.position.y, t);
164 ret.pose.orientation = slerp_fn(a.pose.orientation, b.pose.orientation, t);
165 ret.longitudinal_velocity_mps =
166 interpolate(a.longitudinal_velocity_mps, b.longitudinal_velocity_mps, t);
167 ret.lateral_velocity_mps =
interpolate(a.lateral_velocity_mps, b.lateral_velocity_mps, t);
168 ret.acceleration_mps2 =
interpolate(a.acceleration_mps2, b.acceleration_mps2, t);
169 ret.heading_rate_rps =
interpolate(a.heading_rate_rps, b.heading_rate_rps, t);
170 ret.front_wheel_angle_rad =
interpolate(a.front_wheel_angle_rad, b.front_wheel_angle_rad, t);
171 ret.rear_wheel_angle_rad =
interpolate(a.rear_wheel_angle_rad, b.rear_wheel_angle_rad, t);
180 template<
typename SlerpF>
184 std::chrono::nanoseconds period,
188 out.header = in.header;
189 if (in.points.empty()) {
194 const auto last_time =
from_message(in.points.back().time_from_start);
195 auto count_ = last_time / period;
197 using SizeT =
typename decltype(in.points)::size_type;
199 static_cast<SizeT
>(std::min(count_,
static_cast<decltype(count_)
>(in.CAPACITY)));
200 out.points.reserve(count);
202 out.points.push_back(in.points.front());
204 auto ref_duration = period;
205 auto after_current_ref_idx = SizeT{1};
206 for (
auto idx = SizeT{1}; idx < count; ++idx) {
208 for (
auto jdx = after_current_ref_idx; jdx < in.points.size(); ++jdx) {
209 const auto & pt = in.points[jdx];
211 after_current_ref_idx = jdx;
217 const auto & curr_pt = in.points[after_current_ref_idx - 1U];
218 const auto & next_pt = in.points[after_current_ref_idx];
219 const auto dt = std::chrono::duration_cast<std::chrono::duration<Real>>(
221 const auto dt_ = std::chrono::duration_cast<std::chrono::duration<Real>>(
223 const auto t = dt_.count() / dt.count();
224 out.points.push_back(
interpolate(curr_pt, next_pt, t, slerp_fn));
226 ref_duration += period;
231 MOTION_COMMON_PUBLIC
void sample(
234 std::chrono::nanoseconds period);
247 MOTION_COMMON_PUBLIC Quaternion
operator+(Quaternion a, Quaternion b) noexcept;
249 MOTION_COMMON_PUBLIC Quaternion
operator-(Quaternion a) noexcept;
251 MOTION_COMMON_PUBLIC Quaternion
operator-(Quaternion a, Quaternion b) noexcept;
254 #endif // MOTION_COMMON__MOTION_COMMON_HPP_
MOTION_COMMON_PUBLIC Quaternion operator+(Quaternion a, Quaternion b) noexcept
Addition operator.
Definition: motion_common.cpp:164
decltype(autoware_auto_planning_msgs::msg::TrajectoryPoint::longitudinal_velocity_mps) Real
Definition: motion_common.hpp:37
TIME_UTILS_PUBLIC builtin_interfaces::msg::Time to_message(std::chrono::system_clock::time_point t)
Convert from std::chrono::time_point to time message.
Definition: time_utils.cpp:67
MOTION_COMMON_PUBLIC void error(const Point &state, const Point &ref, Diagnostic &out) noexcept
Diagnostic header stuff.
Definition: motion_common.cpp:135
autoware_auto_vehicle_msgs::msg::VehicleControlCommand VehicleControlCommand
Definition: pure_pursuit.hpp:41
Orientation from_angles(RealT roll, RealT pitch, RealT yaw) noexcept
Converts angles into a corresponding Orientation.
Definition: motion_common.hpp:111
T clamp(T val, T min, T max)
Standard clamp implementation.
Definition: motion_common.hpp:131
Definition: motion_common.hpp:242
decltype(Orientation::x) Double
Definition: motion_common.hpp:43
Definition: trajectory_common.hpp:37
autoware_auto_vehicle_msgs::msg::VehicleControlCommand Command
Definition: motion_common.hpp:38
Orientation from_angle(RealT angle) noexcept
Converts a heading angle into a corresponding Orientation.
Definition: motion_common.hpp:124
MOTION_COMMON_PUBLIC Orientation slerp(const Orientation &a, const Orientation &b, const Real t)
Spherical linear interpolation.
Definition: motion_common.cpp:112
autoware_auto_vehicle_msgs::msg::VehicleKinematicState State
Definition: motion_common.hpp:40
void sample(const Trajectory &in, Trajectory &out, std::chrono::nanoseconds period, SlerpF slerp_fn)
Sample a trajectory using interpolation; does not extrapolate.
Definition: motion_common.hpp:181
TIME_UTILS_PUBLIC std::chrono::nanoseconds interpolate(std::chrono::nanoseconds a, std::chrono::nanoseconds b, float t) noexcept
Standard interpolation.
Definition: time_utils.cpp:96
T interpolate(T a, T b, RealT t)
Standard linear interpolation.
Definition: motion_common.hpp:141
Index update_reference_index(const Trajectory &traj, const State &state, Index start_idx, IsPastPointF is_past_point)
Advance to the first trajectory point past state according to criterion is_past_point.
Definition: motion_common.hpp:59
MOTION_COMMON_PUBLIC Quaternion operator-(Quaternion a) noexcept
Unary minus.
Definition: motion_common.cpp:172
MOTION_COMMON_PUBLIC bool heading_ok(const Trajectory &traj)
Check that all heading values in a trajectory are normalized 2D quaternions.
Definition: motion_common.cpp:68
geometry_msgs::msg::TransformStamped TransformStamped
Definition: motion_testing_publisher.hpp:37
TIME_UTILS_PUBLIC std::chrono::system_clock::time_point from_message(builtin_interfaces::msg::Time t) noexcept
Convert from std::chrono::time_point from time message.
Definition: time_utils.cpp:80
DifferentialState x
Definition: kinematic_bicycle.snippet.hpp:28
decltype(Trajectory::points)::value_type Point
Definition: motion_common.hpp:45
autoware_auto_system_msgs::msg::ControlDiagnostic Diagnostic
Definition: motion_common.hpp:39
MOTION_COMMON_PUBLIC Double to_angle(Orientation orientation) noexcept
Converts 3D quaternion to simple heading representation.
Definition: motion_common.cpp:106
decltype(Trajectory::points)::size_type Index
Definition: motion_common.hpp:44
Definition: controller_base.hpp:31
geometry_msgs::msg::Quaternion Orientation
Definition: motion_common.hpp:42
autoware_auto_planning_msgs::msg::Trajectory Trajectory
Definition: motion_common.hpp:41
geometry_msgs::msg::Point32 Point
Definition: cluster2d.cpp:28
MOTION_COMMON_PUBLIC void doTransform(const Point &t_in, Point &t_out, const geometry_msgs::msg::TransformStamped &transform) noexcept
Apply transform to TrajectoryPoint.
Definition: motion_common.cpp:82
DifferentialState yaw
Definition: kinematic_bicycle.snippet.hpp:28
autoware_auto_vehicle_msgs::msg::VehicleKinematicState VehicleKinematicState
Definition: trajectory_spoofer.hpp:42
MOTION_COMMON_PUBLIC bool is_past_point(const Point &state, const Point &pt) noexcept
Check if a state is past a given trajectory point, assuming heading is correct.
Definition: motion_common.cpp:30