In the C++ version of the ROS node function, configure bool parameters through the launch file

In the main function of the C++ version ROS node, configure bool parameters through the launch file

ChatGPT

To mainconfigure parameters through the launch file in the function of the C++ version of the ROS node bool, you can use roslaunchthe parameter server to achieve. Here is a sample code:

#include <ros/ros.h>

int main(int argc, char** argv)
{
    
    
  // 初始化ROS节点
  ros::init(argc, argv, "your_node_name");

  // 创建一个节点句柄
  ros::NodeHandle nh("~");  // 使用私有命名空间

  // 从launch文件中读取bool型参数,参数名为"your_param_name",默认值为false
  bool your_param;
  nh.param("your_param_name", your_param, false);

  // 使用读取到的参数进行相应的逻辑处理
  if (your_param)
  {
    
    
    ROS_INFO("Your parameter is true");
    // 执行相关操作
  }
  else
  {
    
    
    ROS_INFO("Your parameter is false");
    // 执行其他操作
  }

  // 进入循环等待
  ros::spin();

  return 0;
}

In the above example, nh.param()the function is used to read the parameter named from the parameter server your_param_nameand store it into your_parama variable. If this parameter is not configured in the launch file, the default value will be used false. Then, you can perform corresponding logical processing according to the read parameters.

In the launch file, you can use <param>tags to configure this parameter:

<node pkg="your_package" type="your_node_executable" name="your_node_name" output="screen">
  <param name="your_param_name" value="true"/>  <!-- 或者 value="false" -->
</node>

Please your_packagereplace with your package name, your_node_executablewith your executable name, your_node_namewith your node name, your_param_namewith your parameter name, and set the value of the parameter as needed.

Through the above code and launch file configuration, you can configure type parameters through the launch file in the C++ version of the ROS node bool, and use these parameters to perform corresponding operations in the node. Please ensure that the launch file is specified correctly when running the node, and that the relevant ROS dependencies are properly installed and configured.

Guess you like

Origin blog.csdn.net/NICAI001/article/details/131521073