Skip to content

Commit 705a70d

Browse files
authored
feat(tracking_object_merger): check motion direction and yaw direction to prevent unintended direction update (#5853)
* fix: add function to check object direction before merging object states Signed-off-by: yoshiri <yoshiyoshidetteiu@gmail.com> * refactor: add warning in update whole tracked object Signed-off-by: yoshiri <yoshiyoshidetteiu@gmail.com> --------- Signed-off-by: yoshiri <yoshiyoshidetteiu@gmail.com>
1 parent c3dcd81 commit 705a70d

File tree

2 files changed

+99
-6
lines changed
  • perception/tracking_object_merger

2 files changed

+99
-6
lines changed

perception/tracking_object_merger/include/tracking_object_merger/utils/utils.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define TRACKING_OBJECT_MERGER__UTILS__UTILS_HPP_
1919

2020
// #include <tier4_autoware_utils/tier4_autoware_utils.hpp>
21+
#include "tier4_autoware_utils/geometry/geometry.hpp"
22+
2123
#include <rclcpp/rclcpp.hpp>
2224

2325
#include <autoware_auto_perception_msgs/msg/object_classification.hpp>

perception/tracking_object_merger/src/utils/utils.cpp

+97-6
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,68 @@ double mean(const double a, const double b)
253253
return (a + b) / 2.0;
254254
}
255255

256+
/**
257+
* @brief compare two tracked objects motion direction is same or not
258+
*
259+
* @param main_obj
260+
* @param sub_obj
261+
* @return true
262+
* @return false
263+
*/
264+
bool objectsHaveSameMotionDirections(const TrackedObject & main_obj, const TrackedObject & sub_obj)
265+
{
266+
// get yaw
267+
const auto main_yaw = tf2::getYaw(main_obj.kinematics.pose_with_covariance.pose.orientation);
268+
const auto sub_yaw = tf2::getYaw(sub_obj.kinematics.pose_with_covariance.pose.orientation);
269+
// get velocity
270+
const auto main_vx = main_obj.kinematics.twist_with_covariance.twist.linear.x;
271+
const auto main_vy = main_obj.kinematics.twist_with_covariance.twist.linear.y;
272+
const auto sub_vx = sub_obj.kinematics.twist_with_covariance.twist.linear.x;
273+
const auto sub_vy = sub_obj.kinematics.twist_with_covariance.twist.linear.y;
274+
// calc velocity direction
275+
const auto main_vyaw = std::atan2(main_vy, main_vx);
276+
const auto sub_vyaw = std::atan2(sub_vy, sub_vx);
277+
// get motion yaw angle
278+
const auto main_motion_yaw = main_yaw + main_vyaw;
279+
const auto sub_motion_yaw = sub_yaw + sub_vyaw;
280+
// diff of motion yaw angle
281+
const auto motion_yaw_diff = std::fabs(main_motion_yaw - sub_motion_yaw);
282+
const auto normalized_motion_yaw_diff =
283+
tier4_autoware_utils::normalizeRadian(motion_yaw_diff); // -pi ~ pi
284+
// evaluate if motion yaw angle is same
285+
constexpr double yaw_threshold = M_PI / 4.0; // 45 deg
286+
if (std::abs(normalized_motion_yaw_diff) < yaw_threshold) {
287+
return true;
288+
} else {
289+
return false;
290+
}
291+
}
292+
293+
/**
294+
* @brief compare two tracked objects yaw is reverted or not
295+
*
296+
* @param main_obj
297+
* @param sub_obj
298+
* @return true
299+
* @return false
300+
*/
301+
bool objectsYawIsReverted(const TrackedObject & main_obj, const TrackedObject & sub_obj)
302+
{
303+
// get yaw
304+
const auto main_yaw = tf2::getYaw(main_obj.kinematics.pose_with_covariance.pose.orientation);
305+
const auto sub_yaw = tf2::getYaw(sub_obj.kinematics.pose_with_covariance.pose.orientation);
306+
// calc yaw diff
307+
const auto yaw_diff = std::fabs(main_yaw - sub_yaw);
308+
const auto normalized_yaw_diff = tier4_autoware_utils::normalizeRadian(yaw_diff); // -pi ~ pi
309+
// evaluate if yaw is reverted
310+
constexpr double yaw_threshold = M_PI / 2.0; // 90 deg
311+
if (std::abs(normalized_yaw_diff) >= yaw_threshold) {
312+
return true;
313+
} else {
314+
return false;
315+
}
316+
}
317+
256318
// object kinematics merger
257319
// currently only support velocity fusion
258320
autoware_auto_perception_msgs::msg::TrackedObjectKinematics objectKinematicsVXMerger(
@@ -261,18 +323,29 @@ autoware_auto_perception_msgs::msg::TrackedObjectKinematics objectKinematicsVXMe
261323
autoware_auto_perception_msgs::msg::TrackedObjectKinematics output_kinematics;
262324
// copy main object at first
263325
output_kinematics = main_obj.kinematics;
326+
auto sub_obj_ = sub_obj;
327+
// do not merge reverse direction objects
328+
if (!objectsHaveSameMotionDirections(main_obj, sub_obj)) {
329+
return output_kinematics;
330+
} else if (objectsYawIsReverted(main_obj, sub_obj)) {
331+
// revert velocity (revert pose is not necessary because it is not used in tracking)
332+
sub_obj_.kinematics.twist_with_covariance.twist.linear.x *= -1.0;
333+
sub_obj_.kinematics.twist_with_covariance.twist.linear.y *= -1.0;
334+
}
335+
336+
// currently only merge vx
264337
if (policy == MergePolicy::SKIP) {
265338
return output_kinematics;
266339
} else if (policy == MergePolicy::OVERWRITE) {
267340
output_kinematics.twist_with_covariance.twist.linear.x =
268-
sub_obj.kinematics.twist_with_covariance.twist.linear.x;
341+
sub_obj_.kinematics.twist_with_covariance.twist.linear.x;
269342
return output_kinematics;
270343
} else if (policy == MergePolicy::FUSION) {
271344
const auto main_vx = main_obj.kinematics.twist_with_covariance.twist.linear.x;
272-
const auto sub_vx = sub_obj.kinematics.twist_with_covariance.twist.linear.x;
345+
const auto sub_vx = sub_obj_.kinematics.twist_with_covariance.twist.linear.x;
273346
// inverse weight
274347
const auto main_vx_cov = main_obj.kinematics.twist_with_covariance.covariance[0];
275-
const auto sub_vx_cov = sub_obj.kinematics.twist_with_covariance.covariance[0];
348+
const auto sub_vx_cov = sub_obj_.kinematics.twist_with_covariance.covariance[0];
276349
double main_vx_weight, sub_vx_weight;
277350
if (main_vx_cov == 0.0) {
278351
main_vx_weight = 1.0 * 1e6;
@@ -380,9 +453,21 @@ autoware_auto_perception_msgs::msg::Shape shapeMerger(
380453

381454
void updateExceptVelocity(TrackedObject & main_obj, const TrackedObject & sub_obj)
382455
{
383-
const auto vx_temp = main_obj.kinematics.twist_with_covariance.twist.linear.x;
384-
main_obj = sub_obj;
385-
main_obj.kinematics.twist_with_covariance.twist.linear.x = vx_temp;
456+
if (!objectsHaveSameMotionDirections(main_obj, sub_obj)) {
457+
// do not update velocity
458+
return;
459+
} else if (objectsYawIsReverted(main_obj, sub_obj)) {
460+
// revert velocity (revert pose is not necessary because it is not used in tracking)
461+
const auto vx_temp = sub_obj.kinematics.twist_with_covariance.twist.linear.x;
462+
main_obj = sub_obj;
463+
main_obj.kinematics.twist_with_covariance.twist.linear.x = -vx_temp;
464+
return;
465+
} else {
466+
// update velocity
467+
const auto vx_temp = sub_obj.kinematics.twist_with_covariance.twist.linear.x;
468+
main_obj = sub_obj;
469+
main_obj.kinematics.twist_with_covariance.twist.linear.x = vx_temp;
470+
}
386471
}
387472

388473
void updateOnlyObjectVelocity(TrackedObject & main_obj, const TrackedObject & sub_obj)
@@ -397,6 +482,12 @@ void updateOnlyClassification(TrackedObject & main_obj, const TrackedObject & su
397482

398483
void updateWholeTrackedObject(TrackedObject & main_obj, const TrackedObject & sub_obj)
399484
{
485+
if (!objectsHaveSameMotionDirections(main_obj, sub_obj)) {
486+
// warning
487+
std::cerr << "[object_tracking_merger]: motion direction is different in "
488+
"updateWholeTrackedObject function."
489+
<< std::endl;
490+
}
400491
main_obj = sub_obj;
401492
}
402493

0 commit comments

Comments
 (0)