本页介绍了如何通过调用接口将机器的复合地图保存在本地


本页内容





运行环境准备

  • 软件平台

  • 硬件平台

          (以下任选其一)

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

例程下载

GetStcmMapdemo.py


编译运行

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

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


  3. 在终端中传参运行并在代码中指定的文件夹下保存地图成功

       

   
 

      

       



代码描述

  • 如下代码实现了将机器人的复合地图保存在./map文件夹下,并命名为map.stcm
#!/usr/bin/python
# -*- coding:utf-8 -*-
import requests
import sys
import os

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

ip = sys.argv[1]
url = f"http://{ip}:1448/api/core/slam/v1/maps/stcm"
headers = {'Content-Type': 'application/octet-stream'}

# 获取数据
try:
    response = requests.get(url, headers=headers, timeout=30)
    response.raise_for_status()  # 如果状态码是4xx或5xx,会抛出HTTPError异常
except requests.exceptions.RequestException as e:
    print(f"请求失败: {e}")
    sys.exit(1)

if response.status_code in [200, 204, 203, 201]:
    print("接口调用成功, 地图内容如下:")
    print(response.text + "\n")
else:
    print(f"请求状态码异常: {response.status_code}")

# 确保保存目录存在
save_dir = "./map"
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 保存文件
try:
    with open(os.path.join(save_dir, "map.stcm"), "wb") as file:
        file.write(response.content)
    print("地图已成功保存到本地")
except IOError as e:
    print(f"保存文件失败: {e}")
    sys.exit(1)

   


  • No labels