teamspatzenhirn / rig_reconfigure

GUI tool for editing ROS 2 parameters at runtime
MIT License
79 stars 8 forks source link

The logic of the checkbox for bool type parameters is inverted #26

Closed Alondruck closed 1 year ago

Alondruck commented 1 year ago

When the GUI shows the checkbox marked, in the code I get the value of false and vice versa.

ottojo commented 1 year ago

Hi! I can't seem to reproduce this, do you have an example node that shows this behavior? Also, which ROS version and which rig_reconfigure version do you use? (ros2 pkg xml rig_reconfigure)

Alondruck commented 1 year ago

I have implemented, as nav2, a way to add a callback every time a parameter of the node is set:

rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
std::mutex dynamic_params_lock_;
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters);

dyn_params_handler_ = node->add_on_set_parameters_callback(
        std::bind(&SomeNode::dynamicParametersCallback, this, _1));

rcl_interfaces::msg::SetParametersResult
SomeNode::dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters)
{
    rcl_interfaces::msg::SetParametersResult result;

    for (auto parameter : parameters)
    {
        const auto &type = parameter.get_type();
        const auto &name = parameter.get_name();

        // If we are trying to change the parameter of a plugin we can just skip it at this point
        // as they handle parameter changes themselves and don't need to lock the mutex
        if (name.find('.') != std::string::npos)
        {
            continue;
        }

        if (!dynamic_params_lock_.try_lock())
        {
            RCLCPP_WARN(
                get_logger(),
                "Unable to dynamically change Parameters while the controller is currently running");
            result.successful = false;
            result.reason =
                "Unable to dynamically change Parameters while the controller is currently running";
            return result;
        }

        if (type == rclcpp::ParameterType::PARAMETER_INTEGER)
        {
            if (name == "loop_rate_ms")
            {
                loop_rate_ms_ = parameter.as_int();
                RCLCPP_INFO(get_logger(), "Name: %s, value: %d", name.c_str(), loop_rate_ms_);
            }
        }
        else if (type == rclcpp::ParameterType::PARAMETER_STRING)
        {
            if (name == "global_frame")
            {
                global_frame_ = parameter.as_string();
                RCLCPP_INFO(get_logger(), "Name: %s, value: %s", name.c_str(), global_frame_.c_str());
            }
            else if (name == "base_frame")
            {
                base_frame_ = parameter.as_string();
                RCLCPP_INFO(get_logger(), "Name: %s, value: %s", name.c_str(), base_frame_.c_str());
            }
        }
        else if (type == rclcpp::ParameterType::PARAMETER_DOUBLE)
        {
            if (name == "test_double")
            {
                test_double_ = get_parameter("test_double").as_double();
                RCLCPP_INFO(get_logger(), "Name: %s, Value: %f", name.c_str(), test_double_);
            }
        }
        else if (type == rclcpp::ParameterType::PARAMETER_BOOL)
        {
            if (name == "test_bool")
            {
                test_bool_ = get_parameter("test_bool").as_bool();
                RCLCPP_INFO(get_logger(), "Name: %s, Value: %s", name.c_str(), test_bool_ ? "True" : "False");
            }
        }

        dynamic_params_lock_.unlock();
    }

    result.successful = true;
    return result;
}

I used the following command to install the program: sudo apt install ros-humble-rig-reconfigure and it installed version 1.2.0.

I cloned the repository but it won't let me compile it in a workspace just using colcon build, what tool could I use to compile it?

ottojo commented 1 year ago

I can try your code tomorrow, but I believe the get_parameter calls in the callback return the old values here. They are only updated after the callback returns (I guess the callback could reject parameter changes if validation fails). Please try printing the parameter values in the "parameters" function argument, I suspect those will contain the proper values.

ottojo commented 1 year ago

https://github.com/ros2/ros2_documentation/blob/rolling/source/Concepts/Basic/About-Parameters.rst#id25 the ros2 docs seem to be down rn, but here are the docs about the different available callbacks

edit: The site is back up: https://docs.ros.org/en/iron/Concepts/Basic/About-Parameters.html#parameter-callbacks

Alondruck commented 1 year ago

Sorry, it was my mistake, it works just as you say, thanks for the help.