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

feat: add remapping argument to MoveItPy initialization #3367

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
21 changes: 19 additions & 2 deletions moveit_py/src/moveit/moveit_ros/moveit_cpp/moveit_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
/* Author: Peter David Fagan */

#include "moveit_cpp.hpp"
#include <pybind11/pytypes.h>
#include <moveit/utils/logger.hpp>
#include <string>

namespace moveit_py
{
Expand All @@ -62,7 +64,7 @@ void initMoveitPy(py::module& m)

.def(py::init([](const std::string& node_name, const std::string& name_space,
const std::vector<std::string>& launch_params_filepaths, const py::object& config_dict,
bool provide_planning_service) {
bool provide_planning_service, const py::object& remappings) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just use a std::map as argument here. This simplifies casting code below.
Wrapping the map into std::optional allows to handle None values.

Suggested change
bool provide_planning_service, const py::object& remappings) {
bool provide_planning_service, const std::optional<std::map<std::string, std::string>>& remappings) {

// This section is used to load the appropriate node parameters before spinning a moveit_cpp instance
// Priority is given to parameters supplied directly via a config_dict, followed by launch parameters
// and finally no supplied parameters.
Expand All @@ -85,6 +87,21 @@ void initMoveitPy(py::module& m)
}
}

if (py::isinstance<py::list>(remappings))
{
for (auto item : remappings)
{
py::tuple item_tuple = item.cast<py::tuple>();
if (item_tuple.size() != 2)
{
throw std::runtime_error("Remapping must be a list of tuples with 2 elements");
}
launch_arguments.push_back("--remap");
launch_arguments.push_back(item_tuple[0].cast<std::string>() +
":=" + item_tuple[1].cast<std::string>());
}
}

Comment on lines +90 to +104
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (py::isinstance<py::list>(remappings))
{
for (auto item : remappings)
{
py::tuple item_tuple = item.cast<py::tuple>();
if (item_tuple.size() != 2)
{
throw std::runtime_error("Remapping must be a list of tuples with 2 elements");
}
launch_arguments.push_back("--remap");
launch_arguments.push_back(item_tuple[0].cast<std::string>() +
":=" + item_tuple[1].cast<std::string>());
}
}
if (remappings.has_value())
{
for (const auto &[key, value]: remappings)
{
launch_arguments.push_back("--remap");
launch_arguments.push_back(key + ":=" + value);
}
}

// Initialize ROS, pass launch arguments with rclcpp::init()
if (!rclcpp::ok())
{
Expand Down Expand Up @@ -138,7 +155,7 @@ void initMoveitPy(py::module& m)
py::arg("launch_params_filepaths") =
utils.attr("get_launch_params_filepaths")().cast<std::vector<std::string>>(),
py::arg("config_dict") = py::none(), py::arg("provide_planning_service") = true,
py::return_value_policy::take_ownership,
py::arg("remappings") = py::none(), py::return_value_policy::take_ownership,
R"(
Initialize moveit_cpp node and the planning scene service.
)")
Expand Down
Loading