Skip to content

Nacho/cleanup get transform util#4181

Merged
SteveMacenski merged 15 commits intoros-navigation:mainfrom
nachovizzo:nacho/cleanup_get_transform_util
Apr 2, 2024
Merged

Nacho/cleanup get transform util#4181
SteveMacenski merged 15 commits intoros-navigation:mainfrom
nachovizzo:nacho/cleanup_get_transform_util

Conversation

@nachovizzo
Copy link
Contributor

@nachovizzo nachovizzo commented Mar 14, 2024

Basic Info

Info Please fill out this column
Ticket(s) this addresses None
Primary OS tested on Ubuntu
Robotic platform tested on Simulatin
Does this PR contain AI generated software? NO

Description of contribution in a few bullet points

The following pattern is widely used in ROS applications, and (far from ideal) I'd like to extend the nav2_utils::getTransform to also spit TransformStamped messages. I know, these util functions should be in the tf library but here seemed like a lower-hanging fruit to bite:

try {
  // Obtaining the transform to get data from source to target frame
  return tf_buffer->lookupTransform(
    target_frame_id, source_frame_id,
    tf2::TimePointZero, transform_timeout);
} catch (tf2::TransformException & e) {
  RCLCPP_ERROR(
    rclcpp::get_logger("getTransform"),
    "Failed to get \"%s\"->\"%s\" frame transform: %s",
    source_frame_id.c_str(), target_frame_id.c_str(), e.what());
  return std::nullopt;
}

Some comments:

  • Open discussion PR. Not necessary to pretend that 100% gets merged
  • This PR introduces a hard dependency in C++17 on the nav2_util pkg. I'm happy to express this in the build system but I'm not sure if this is a common practice in nav2. Do you think this might bring a problem
  • I tried to be specific commit-wise. So the changes can be reviewed in the commits and not just the entire diff
  • I renamed some bits to follow more the traditional names from tf2 to avoid confusing users
  • I introduced a BREAKIN change by swapping the soruce and frame id from the getTransform function. I'm not sure why it was "inverted" (compared to tf2) in the first place. But I found it very confusing to read this:
// tf2
geometry_msgs::msg::TransformStamped
lookupTransform(const std::string & target_frame, const std::string & source_frame, ...)

// nav2
bool getTransform(const std::string & source_frame_id,   const std::string & target_frame_id, ...)

/// Function call
lookupTransform(target, source)
  • I know it's not common in ROS, but I'm trying to introduce a silly pattern that I think it's much better than returning booleans on functions, and forcing the user to provide an output argument:
    Before:
OutputType out_variable;
bool res = FunctionCall(/*const*/ arg1,/*const*/ arg2, ..., /* non-const */ out_variable);
if(res) {
 // use out_variable
} else {
return;
}

For a more modern and easier to read + safer approach:

auto out_variable = FunctionCall(/*const*/ arg1,/*const*/ arg2);
if(out_variable.has_value()) {
 // use out_variable
} else {
return;
}

This improves the code (on my opinion) from:

 tf2::Transform tf_transform;
  if (base_shift_correction_) {
    // Obtaining the transform to get data from source frame and time where it was received
    // to the base frame and current time
    if (
      !nav2_util::getTransform(
        data_->header.frame_id, data_->header.stamp,
        base_frame_id_, curr_time, global_frame_id_,
        transform_tolerance_, tf_buffer_, tf_transform))
    {
      return false;
    }
  } else {
    // Obtaining the transform to get data from source frame to base frame without time shift
    // considered. Less accurate but much more faster option not dependent on state estimation
    // frames.
    if (
      !nav2_util::getTransform(
        data_->header.frame_id, base_frame_id_,
        transform_tolerance_, tf_buffer_, tf_transform))
    {
      return false;
    }
  }

, to:

  const auto tf_transform = [&]() -> std::optional<tf2::Transform> {
      // Obtaining the transform to get data from source frame and time where it was received to the
      // base frame and current time
      if (base_shift_correction_) {
        return nav2_util::getTransform(
          base_frame_id_, curr_time, data_->header.frame_id,
          data_->header.stamp, global_frame_id_, transform_timeout_,
          tf_buffer_);
      }

      // Obtaining the transform to get data from source frame to base frame without time shift
      // considered. Less accurate but much more faster option not dependent on state estimation
      // frames.
      return nav2_util::getTransform(
        base_frame_id_, data_->header.frame_id, transform_timeout_,
        tf_buffer_);
    }();

  if (!tf_transform.has_value()) {return false;}

Future work that may be required in bullet points

  • Make sure we aren't using the "bare" pattern without calling nav2_utils::getTransform

For Maintainers:

  • Check that any new parameters added are updated in navigation.ros.org
  • Check that any significant change is added to the migration guide
  • Check that any new features OR changes to existing behaviors are reflected in the tuning guide
  • Check that any new functions have Doxygen added
  • Check that any new features have test coverage
  • Check that any new plugins is added to the plugins page
  • If BT Node, Additionally: add to BT's XML index of nodes for groot, BT package's readme table, and BT library lists

@mergify
Copy link
Contributor

mergify bot commented Mar 14, 2024

@nachovizzo, your PR has failed to build. Please check CI outputs and resolve issues.
You may need to rebase or pull in main due to API changes (or your contribution genuinely fails).

@SteveMacenski
Copy link
Member

This PR introduces a hard dependency in C++17 on the nav2_util pkg. I'm happy to express this in the build system but I'm not sure if this is a common practice in nav2. Do you think this might bring a problem

This is fine!

Copy link
Member

@SteveMacenski SteveMacenski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heed the linter

nachovizzo added a commit to botsandus/navigation2 that referenced this pull request Mar 14, 2024
@mergify
Copy link
Contributor

mergify bot commented Mar 14, 2024

@nachovizzo, your PR has failed to build. Please check CI outputs and resolve issues.
You may need to rebase or pull in main due to API changes (or your contribution genuinely fails).

@SteveMacenski
Copy link
Member

@nachovizzo waddup here? 💯

@nachovizzo
Copy link
Contributor Author

@nachovizzo waddup here? 💯

Done!

@mergify
Copy link
Contributor

mergify bot commented Mar 28, 2024

@nachovizzo, your PR has failed to build. Please check CI outputs and resolve issues.
You may need to rebase or pull in main due to API changes (or your contribution genuinely fails).

@SteveMacenski
Copy link
Member

2 things:

  • Pull in main, CI should run again
  • Do the DCO bits

Otherwise, love it, super clean, I love abstracting out nonsense

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Otherwise is very confusing for any user who is user to the
tf2::Buffer::lookupTransform method

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
I find this "transform tolerance" to specify something else. Once
again, I believe that sticking to tf2 name conventions is better and
improve readability for users already used to the tf2 API

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
This allow us to also query for the TransformStamped message from the
lookupTransform method.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
This reverts commit ca7d06b.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
@nachovizzo nachovizzo force-pushed the nacho/cleanup_get_transform_util branch from 2bd3588 to d450502 Compare April 2, 2024 07:38
@nachovizzo
Copy link
Contributor Author

@SteveMacenski done with this, I pulled main but the CI still fails to run 😢

@SteveMacenski SteveMacenski merged commit 911555a into ros-navigation:main Apr 2, 2024
@SteveMacenski
Copy link
Member

It ran (i.e. built) there are some new test failures that popped up while our CI was down. I just haven't had a chance to look at them yet but not caused by this PR (probably; or at least in whole)

ajtudela pushed a commit to grupo-avispa/navigation2 that referenced this pull request Apr 19, 2024
* remove unused header

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* First step. Return an optional value instead of user provided output.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Second step, update the consumers of this utility function

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Third step: swap source/target to make it consistent with tf lookups

Otherwise is very confusing for any user who is user to the
tf2::Buffer::lookupTransform method

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* transform tolerance -> transform timeout

I find this "transform tolerance" to specify something else. Once
again, I believe that sticking to tf2 name conventions is better and
improve readability for users already used to the tf2 API

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Last step, convert functions to template functions

This allow us to also query for the TransformStamped message from the
lookupTransform method.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add nodiscard to avoid confusiong API calls

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Update docs

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Revert "transform tolerance -> transform timeout"

This reverts commit ca7d06b.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix linter 🤦

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* reset to main

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add 2 new utils functions to query transforms using messages

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Move utility function to base class to reuse implementation

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix Typo

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

---------

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
enricosutera pushed a commit to enricosutera/navigation2 that referenced this pull request May 19, 2024
* remove unused header

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* First step. Return an optional value instead of user provided output.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Second step, update the consumers of this utility function

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Third step: swap source/target to make it consistent with tf lookups

Otherwise is very confusing for any user who is user to the
tf2::Buffer::lookupTransform method

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* transform tolerance -> transform timeout

I find this "transform tolerance" to specify something else. Once
again, I believe that sticking to tf2 name conventions is better and
improve readability for users already used to the tf2 API

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Last step, convert functions to template functions

This allow us to also query for the TransformStamped message from the
lookupTransform method.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add nodiscard to avoid confusiong API calls

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Update docs

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Revert "transform tolerance -> transform timeout"

This reverts commit ca7d06b.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix linter 🤦

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* reset to main

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add 2 new utils functions to query transforms using messages

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Move utility function to base class to reuse implementation

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix Typo

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

---------

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: enricosutera <enricosutera@outlook.com>
Marc-Morcos pushed a commit to Marc-Morcos/navigation2 that referenced this pull request Jul 4, 2024
* remove unused header

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* First step. Return an optional value instead of user provided output.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Second step, update the consumers of this utility function

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Third step: swap source/target to make it consistent with tf lookups

Otherwise is very confusing for any user who is user to the
tf2::Buffer::lookupTransform method

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* transform tolerance -> transform timeout

I find this "transform tolerance" to specify something else. Once
again, I believe that sticking to tf2 name conventions is better and
improve readability for users already used to the tf2 API

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Last step, convert functions to template functions

This allow us to also query for the TransformStamped message from the
lookupTransform method.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add nodiscard to avoid confusiong API calls

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Update docs

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Revert "transform tolerance -> transform timeout"

This reverts commit ca7d06b.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix linter 🤦

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* reset to main

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add 2 new utils functions to query transforms using messages

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Move utility function to base class to reuse implementation

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix Typo

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

---------

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Manos-G pushed a commit to Manos-G/navigation2 that referenced this pull request Aug 1, 2024
* remove unused header

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* First step. Return an optional value instead of user provided output.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Second step, update the consumers of this utility function

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Third step: swap source/target to make it consistent with tf lookups

Otherwise is very confusing for any user who is user to the
tf2::Buffer::lookupTransform method

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* transform tolerance -> transform timeout

I find this "transform tolerance" to specify something else. Once
again, I believe that sticking to tf2 name conventions is better and
improve readability for users already used to the tf2 API

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Last step, convert functions to template functions

This allow us to also query for the TransformStamped message from the
lookupTransform method.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add nodiscard to avoid confusiong API calls

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Update docs

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Revert "transform tolerance -> transform timeout"

This reverts commit ca7d06b.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix linter 🤦

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* reset to main

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add 2 new utils functions to query transforms using messages

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Move utility function to base class to reuse implementation

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix Typo

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

---------

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
stevedanomodolor pushed a commit to stevedanomodolor/navigation2 that referenced this pull request Apr 29, 2025
* remove unused header

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* First step. Return an optional value instead of user provided output.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Second step, update the consumers of this utility function

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Third step: swap source/target to make it consistent with tf lookups

Otherwise is very confusing for any user who is user to the
tf2::Buffer::lookupTransform method

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* transform tolerance -> transform timeout

I find this "transform tolerance" to specify something else. Once
again, I believe that sticking to tf2 name conventions is better and
improve readability for users already used to the tf2 API

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Last step, convert functions to template functions

This allow us to also query for the TransformStamped message from the
lookupTransform method.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add nodiscard to avoid confusiong API calls

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Update docs

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Revert "transform tolerance -> transform timeout"

This reverts commit ca7d06b.

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix linter 🤦

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* reset to main

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Add 2 new utils functions to query transforms using messages

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Move utility function to base class to reuse implementation

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

* Fix Typo

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>

---------

Signed-off-by: Ignacio Vizzo <ignacio@dexory.com>
Signed-off-by: stevedanomodolor <stevedan.o.omodolor@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants