The python version of the ROS node takes a string as a parameter input and configures it in the launch file

Let the main function of the python version of the ROS node be input as a string, and use it as a parameter in the launch file

ChatGPT

If you want to mainreceive a string as an input in the function of the Python version of the ROS node and pass it as a parameter in the launch file, you can follow the steps below:

  1. Modify the function uwb_visualization_node.pyin the script mainto accept a string parameter:
def main(input_string):
    # 在这里使用输入字符串进行逻辑处理
    # ...
    rospy.init_node("uwb_visualization_node")

    # 创建一个发布者,发布可视化消息
    marker_pub = rospy.Publisher("uwb_trajectory", Marker, queue_size=10)

    # 创建一个订阅者,订阅nlink_linktrack_tagframe0话题
    rospy.Subscriber("nlink_linktrack_tagframe0", LinktrackTagframe0, tagframe0_callback)

    rospy.spin()

Modify uwb_visualization_node.pythe last few lines in the script to receive and use the parameters passed in the launch file:

if __name__ == "__main__":
    # 从launch文件中获取参数
    input_string = rospy.get_param('~input_string')

    # 调用主函数并传递输入字符串作为参数
    main(input_string)

Create a uwb_visualization.launchfile called launch and add the following to the file:

<launch>
  <arg name="input_string" default="default_input" />

  <node pkg="uwb_visualization" type="uwb_visualization_node.py" name="uwb_visualization_node" output="screen">
    <param name="input_string" value="$(arg input_string)" />
  </node>
</launch>

In this launch file, we define a input_stringparameter called and set its default value to "default_input". We then <param>pass that parameter to the node using the label.

Make sure to uwb_visualization.launchplace the file in the correct ROS package directory.

Run the launch file:

  1. $ roslaunch uwb_visualization uwb_visualization.launch input_string:="your_input_string"
    

    Here, your_input_stringreplace with the actual input string you wish to pass to the node.

Through the above steps, you can receive a string as input in the Python version of the ROS node, and pass the parameter to the node through the launch file.

Guess you like

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