move_base_msgs/MoveBaseActionGoal

发布时间 2023-04-04 00:05:46作者: 黑逍逍

是用于向move_base节点发送目标点位置信息的消息类型,包含目标点的位置和姿态信息。当move_base节点收到该消息后,会规划机器人的路径并执行导航任务,到达目标点

 

#!/usr/bin/env python
import rospy
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib

def move_to_goal(x, y):
    # Initialize node
    rospy.init_node('move_to_goal', anonymous=False)

    # Create a MoveBaseAction object
    move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)

    # Wait for the action server to come up
    while not move_base.wait_for_server(rospy.Duration(5)):
        rospy.loginfo("Waiting for move_base action server...")

    # Define a goal
    goal = MoveBaseGoal()
    goal.target_pose.header.frame_id = "map"
    goal.target_pose.pose.position.x = x
    goal.target_pose.pose.position.y = y
    goal.target_pose.pose.orientation.w = 1.0

    # Send the goal and wait for completion
    move_base.send_goal(goal)
    move_base.wait_for_result()

    # Print the result
    if move_base.get_state() == actionlib.GoalStatus.SUCCEEDED:
        rospy.loginfo("Goal reached!")
        return True
    else:
        rospy.loginfo("Failed to reach goal.")
        return False

if __name__ == '__main__':
    try:
        move_to_goal(1.0, 2.0) # Set the goal position x and y coordinates
    except rospy.ROSInterruptException:
        pass

  它接收x和y坐标参数,并将这些坐标作为目标位置发送给move_base节点。具体来说,我们通过创建一个MoveBaseAction对象,然后在其中设置我们的目标位置信息。接下来,我们使用move_base.send_goal函数将目标位置发送给move_base节点。等待机器人完成目标位置时,我们使用move_base.wait_for_result函数,等待move_base节点返回一个结果。最后,我们检查返回的结果来确认机器人是否到达目标位置