You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 30 Next »

本页介绍了move_to_spot的用法, 自由导航模式下运动到目标点的过程。


本页内容





运行环境准备

  • 软件平台

  • 硬件平台

          (以下任选其一)

      • Slamware SDP mini 
      • Slamware 套装 (基于Slamware导航方案的用户机器人系统)
      • Apollo/Ares/Athena/Hermes等底盘系统

例程下载

MoveTodemo.py


编译运行

        如下说明使用的是以PyCharm作为demo

  1. 下载例程到本地文件夹中,并使用PyCharm打开此文件夹
  2. 在PyCharm中配置好 Python 解释器


  3. 在终端中传参运行并连上Robostudio查看机器人在地图上运行

       

   
     

      

       



代码描述

  • 如下代码实现的是机器人自由导航模式下运动到目标点的过程


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84


    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    import requests
    import json
    import sys
    import time

    url1 = "http://{ip}:1448/api/core/motion/v1/actions"
    url2 = "http://{ip}:1448/api/core/motion/v1/actions/{action_id}"
    headers = {'Content-Type': 'application/json'}

    action_options = {
    "action_name": "slamtec.agent.actions.MoveToAction",
    "options": {
    "target": {
    "x": 0.42,
    "y": 0.81,
    "z": 0
    },
    "move_options": {
    "mode": 0,
    "flags": [],
    "yaw": 0,
    "acceptable_precision": 0,
    "fail_retry_count": 0
    }
    }
    }

    def send_post_request(url, headers, json_data):
    try:
    response = requests.post(url, headers=headers, json=json_data, timeout=30)
    response.raise_for_status() # 如果状态码是 4xx 或 5xx,抛出 HTTPError 异常
    return response
    except requests.exceptions.RequestException as e:
    print(f"POST 请求失败: {e}")
    sys.exit(1)

    def send_get_request(url, headers):
    try:
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response
    except requests.exceptions.RequestException as e:
    print(f"GET 请求失败: {e}")
    return None

    def main(ip):
    # 发送移动指令
    print("机器人开始移动到点")
    url = url1.format(ip=ip)
    rq1 = send_post_request(url, headers, action_options)
    print(f"接口调用成功: {rq1.text}\n")

    # 获取 action_id
    actionid = json.loads(rq1.text).get("action_id")
    if not actionid:
    print("无法获取 action_id")
    sys.exit(1)
    print(f"action_id : {actionid}")

    # 等待任务结束
    print("************等待到点任务结束************")
    url = url2.format(ip=ip, action_id=actionid)

    while True:
    rq2 = send_get_request(url, headers)
    if rq2 is None:
    continue

    print(f"查询 action 状态为: {rq2.text}")
    if not json.loads(rq2.text).get("action_name"):
    print("任务结束")
    break

    time.sleep(2) # 避免频繁查询,稍作延时

    if __name__ == "__main__":
    if len(sys.argv) < 2:
    print("请提供IP地址")
    sys.exit(1)

    main(sys.argv[1])
  • No labels