Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filters as ROS2 components #758

Open
wants to merge 4 commits into
base: humble-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ endif()
find_package(ament_cmake REQUIRED)
find_package(angles REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(diagnostic_msgs REQUIRED)
find_package(diagnostic_updater REQUIRED)
find_package(geographic_msgs REQUIRED)
Expand Down Expand Up @@ -64,6 +65,7 @@ include_directories(

add_library(
${library_name}
SHARED
src/ekf.cpp
src/ukf.cpp
src/filter_base.cpp
Expand All @@ -72,7 +74,8 @@ add_library(
src/robot_localization_estimator.cpp
src/ros_filter.cpp
src/ros_filter_utilities.cpp
src/ros_robot_localization_listener.cpp)
src/ros_robot_localization_listener.cpp
src/ros_components.cpp)

rosidl_get_typesupport_target(cpp_typesupport_target "${PROJECT_NAME}" "rosidl_typesupport_cpp")
target_link_libraries(${library_name} "${cpp_typesupport_target}")
Expand Down Expand Up @@ -163,6 +166,16 @@ ament_target_dependencies(
rclcpp
)

rclcpp_components_register_nodes(
${library_name}
"robot_localization::UkfFilter"
)

rclcpp_components_register_nodes(
${library_name}
"robot_localization::EkfFilter"
)

if(BUILD_TESTING)
find_package(ament_cmake_gtest REQUIRED)
find_package(ament_cmake_cppcheck REQUIRED)
Expand Down
52 changes: 52 additions & 0 deletions include/robot_localization/ros_components.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022, Patrick Roncagliolo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ROBOT_LOCALIZATION__ROS_COMPONENTS_HPP_
#define ROBOT_LOCALIZATION__ROS_COMPONENTS_HPP_

#include "robot_localization/ros_filter_types.hpp"

namespace robot_localization
{

class EkfFilter : public RosEkf{
public:
explicit EkfFilter(const rclcpp::NodeOptions & options);
};

class UkfFilter : public RosUkf{
public:
explicit UkfFilter(const rclcpp::NodeOptions & options);
};

}
#endif // ROBOT_LOCALIZATION__ROS_COMPONENTS_HPP_
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<depend>angles</depend>
<build_depend>rclcpp</build_depend>
<build_depend>rmw_implementation</build_depend>
<depend>rclcpp_components</depend>
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>
<depend>std_srvs</depend>
Expand Down
66 changes: 66 additions & 0 deletions src/ros_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022, Patrick Roncagliolo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <robot_localization/ros_components.hpp>

namespace robot_localization{
rclcpp::NodeOptions _option_helper(std::string node_name, const rclcpp::NodeOptions & options){
rclcpp::NodeOptions o = options;
std::vector<std::string> a;
a = o.arguments();
a.insert(a.begin(), node_name);
o.arguments(a);
return o;
}

EkfFilter::EkfFilter(const rclcpp::NodeOptions & options)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not just register the EKF / UKF files? https://github.com/cra-ros-pkg/robot_localization/blob/humble-devel/src/ukf.cpp If the alpha/beta/kappa params are stuck into the UKF file itself, there shouldn't be a need for these separate objects for componentizing

Copy link
Collaborator

Choose a reason for hiding this comment

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

Especially if we don't need a shared pointer that you changed to this (assuming that works)

Copy link
Author

Choose a reason for hiding this comment

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

Hi, happy to share my findings... Of course I tried in a first iteration not to define the classes you saw, but just exposing existing ones.

One problem there is that the RosFilter<> extracts the node name in the constructor (thus, I needed to inject it manually, since component instantiation is a standardized procedure).

One other problem is that the usage of shared_from_this() lead to bad_weak_ptr exceptions due to the fact that component instantiation seems not to create the node as a shared pointer.

Third, I wanted to mimic the behaviour of the Ekf and Ukf nodes initialization.

Let me know if you need further demonstration of the first two points, I can make some branches demonstrating the various approaches I tried.

Copy link
Collaborator

Choose a reason for hiding this comment

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

extracts the node name in the constructor

@ayrton04 are you against just having the names default to filter_node or something so that this isn't a problem?

Yeah, I understand the shared pointer issue well, those changes would be needed either way

Copy link
Author

@roncapat roncapat May 23, 2022

Choose a reason for hiding this comment

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

Yeah, changing the way node name is set in the mainline code would be a simplification.

One last thing: to expose RosFilter as a component, the risk is to have a component named "robot_localization::RosFilter<robot_localization::Ekf>" since I tried in a lot of ways (failing) to use the typedef "RosEkf". I think that typedefs are not visible at linking/loading scope, as a symbol. I digged with "nm" tool in the shared library to confirm my theory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Believe that would be true

Copy link
Author

Choose a reason for hiding this comment

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

So, are there requested changes at the end?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, all above :-)

Copy link
Author

Choose a reason for hiding this comment

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

Well, from the discussion I though we ruled out some initial comments from being implemented

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're still needing a shared pointer in the constructor, even if you call it in another function. The scope of the constructor is not completed yet so there's no reference counter on the shared pointer. Thus, this still has the same issues as before. If you're finding that changing to this works, then that would work too for the componentization of the EKF/UKF themselves without wrappers.

If we change the name to some logical default, that deals with that issue.

robot_localization::RosFilter<robot_localization::Ekf> is not super clean, I will admit, but I think if you register the type def instead of the object, that should work. Either way, that would still be fine semantically

: RosEkf(_option_helper("ekf_filter_node", options)){
initialize();
}

UkfFilter::UkfFilter(const rclcpp::NodeOptions & options)
: RosUkf(_option_helper("ukf_filter_node", options)){
double alpha = declare_parameter("alpha", 0.001);
double kappa = declare_parameter("kappa", 0.0);
double beta = declare_parameter("beta", 2.0);
getFilter().setConstants(alpha, kappa, beta);
initialize();
}
}

#include "rclcpp_components/register_node_macro.hpp"

// Register the component with class_loader.
// This acts as a sort of entry point, allowing the component to be discoverable when its library
// is being loaded into a running process.
RCLCPP_COMPONENTS_REGISTER_NODE(robot_localization::EkfFilter)
RCLCPP_COMPONENTS_REGISTER_NODE(robot_localization::UkfFilter)
4 changes: 2 additions & 2 deletions src/ros_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,11 +1941,11 @@ template<typename T>
void RosFilter<T>::initialize()
{
diagnostic_updater_ = std::make_unique<diagnostic_updater::Updater>(
shared_from_this());
this);
diagnostic_updater_->setHardwareID("none");

world_transform_broadcaster_ = std::make_shared<tf2_ros::TransformBroadcaster>(
shared_from_this());
this);

loadParams();

Expand Down