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 main
receive 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:
- Modify the function
uwb_visualization_node.py
in the scriptmain
to 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.py
the 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.launch
file 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_string
parameter 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.launch
place the file in the correct ROS package directory.
Run the launch file:
-
$ roslaunch uwb_visualization uwb_visualization.launch input_string:="your_input_string"
Here,
your_input_string
replace 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.