Search code examples
c++ros

Why can't I get the param from the launchfile in ROS?


I'm trying to make a ros package with two nodes, one developed on python and the other in c++, and i would like to read some parameters with a launchfile.

The problem comes when I try yo get a parameter defined in a launchfile similar to this one:

<?xml version="1.0" encoding="UTF-8"?>
<launch>

<!--#### MODIFY THESE VALUES ####-->

  <arg name="print_detect" default="true"/>

<!--#############################-->

  <!-- Nodos Python -->

  <!-- Nodos C++ -->
  <node name="yolo_result_processor" pkg="yolov8_inference" type="yolo_result_processor"  output="screen">
    <param name="print_detect" value="$(arg print_detect)"/>
    <!-- Configurar aquí los parámetros específicos para el nodo C++ -->
  </node>

</launch>

And I´m trying to do the reading of the parameter as follows:

#INCLUDES#
#include <ros/ros.h>
bool printDetect = false;
int main(int argc, char** argv)
{
    ros::init(argc, argv, "yolo_result_processor");
    ros::NodeHandle nh;

    // Obtener el valor del parámetro "print_detect"
    if (nh.getParam("print_detect", printDetect)){
         std::cout << "printDetect = " << printDetect << std::endl;
    }else{
         ROS_ERROR("No se pudo obtener el valor del parametro 'print_detect'. Usando valor predeterminado.");
    }

    std::cout << "Running" << std::endl;

    // Suscribirse al mensaje YoloResult
    ros::Subscriber sub = nh.subscribe("yolo_result", 1, yoloResultCallback);

    ros::spin();

    return 0;
}

Everything compiles correctly, but when I launch the node using:

roslaunch <pkg_name> <launch_file>

It enters the else statement and prints the error message as if it cant get the parameter.


Solution

  • Fixed it by removing all lines corresponding to the parameter I was trying to read in the launchfile, and just adding this one before initializing the nodes:

    <param name="print_detect" type="bool" value="true"/>
    

    So the file would look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <launch>
    
    <!--#### MODIFY THESE VALUES ####-->
    
      <param name="print_detect" type="bool" value="true"/>
    
    <!--#############################-->
    
      <!-- Nodos Python -->
    
      <!-- Nodos C++ -->
      <node name="yolo_result_processor" pkg="yolov8_inference" type="yolo_result_processor"  output="screen">
      </node>