ROS学习篇之发生目标(九)-通过程序发生目标点

下面是通过发生四元素作为目标点

x,y,z坐标
及表示姿态的w

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 上面两行不可省略,第一行是:告诉操作系统执行这个脚本的时候,调用 /usr/bin 下的 python 解释器。第二行是:定义编码格式 "UTF-8-" 支持中文from actionlib.action_client import GoalManager
import rospy 
import actionlib
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
​
def send_goals_python():
    client = actionlib.SimpleActionClient('move_base',MoveBaseAction)
    client.wait_for_server()
    #定义四个发送目标点的对象
    goal0 = MoveBaseGoal()
    goal1 = MoveBaseGoal() 
    goal2 = MoveBaseGoal() 
    goal3 = MoveBaseGoal()
    # 初始化四个目标点在 map 坐标系下的坐标,数据来源于《采集的目标点.docx》
    goal0.target_pose.pose.position.x = 1.84500110149
    goal0.target_pose.pose.position.y = -0.883078575134
    goal0.target_pose.pose.orientation.z = -0.306595935327
    goal0.target_pose.pose.orientation.w = 0.951839761956
    
    goal1.target_pose.pose.position.x = 3.24358606339
    goal1.target_pose.pose.position.y = 0.977679371834
    goal1.target_pose.pose.orientation.z = 0.647871240469
    goal1.target_pose.pose.orientation.w = 0.761749864308
    
    goal2.target_pose.pose.position.x = 2.41693687439
    goal2.target_pose.pose.position.y = 1.64631867409
    goal2.target_pose.pose.orientation.z = 0.988149484601
    goal2.target_pose.pose.orientation.w = 0.153494612555
    
    goal3.target_pose.pose.position.x = -0.970185279846
    goal3.target_pose.pose.position.y = 0.453477025032
    goal3.target_pose.pose.orientation.z = 0.946238058267
    goal3.target_pose.pose.orientation.w = -0.323471076121
    
    goal_lists=[goal0, goal1, goal2, goal3]       # 采用 python 中的列表方式,替代实现C/C++ 中的数组概念 
    goal_number = 4     # total is 4 goals
    while(goal_number):      
        if(4 - goal_number ==0):
            goal_lists[4-goal_number].target_pose.header.frame_id = "map"
            goal_lists[4-goal_number].target_pose.header.stamp = rospy.Time.now()
            client.send_goal( goal_lists[4-goal_number])
            str_log = "Send NO. %s Goal !!!" %str(4-goal_number)
            rospy.loginfo(str_log)
        elif(4 - goal_number ==1):
            goal_lists[4-goal_number].target_pose.header.frame_id = "map"
            goal_lists[4-goal_number].target_pose.header.stamp = rospy.Time.now()
            client.send_goal( goal_lists[4-goal_number])
            str_log = "Send NO. %s Goal !!!" %str(4-goal_number)
            rospy.loginfo(str_log)
        elif(4 - goal_number ==2):
            goal_lists[4-goal_number].target_pose.header.frame_id = "map"
            goal_lists[4-goal_number].target_pose.header.stamp = rospy.Time.now()
            client.send_goal( goal_lists[4-goal_number])
            str_log = "Send NO. %s Goal !!!" %str(4-goal_number)
            rospy.loginfo(str_log)
        elif(4 - goal_number ==3):
            goal_lists[4-goal_number].target_pose.header.frame_id = "map"
            goal_lists[4-goal_number].target_pose.header.stamp = rospy.Time.now()
            client.send_goal( goal_lists[4-goal_number])
            str_log = "Send NO. %s Goal !!!" %str(4-goal_number)
            rospy.loginfo(str_log)
​
        wait = client.wait_for_result(rospy.Duration.from_sec(30.0))  # 发送完毕目标点之后,根据action 的机制,等待反馈执行的状态,等待时长是:30 s.
        if not wait:
            str_log="The NO. %s Goal Planning Failed for some reasons" %str(4-goal_number)
            rospy.loginfo(str_log)
            goal_number = goal_number - 1  # 等待超时之后 准备发送下一个目标点,action server 会自动执行最新的请求.
            continue
        else:
            str_log="The NO. %s Goal achieved success !!!" %str(4-goal_number)
            rospy.loginfo(str_log)
            goal_number = goal_number - 1
    return "Mission Finished."if __name__ == '__main__':
        rospy.init_node('send_goals_python',anonymous=True)    # python 语言方式下的 初始化 ROS 节点,
        result = send_goals_python()
        rospy.loginfo(result)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值