You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python将路径规划坐标数组转为Arduino适配的AGV控制格式

AGV路径坐标格式转换Python实现

需求说明

需要将路径规划得到的坐标数组:

path = [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3)]

转换为适合发送给Arduino的字符串格式:{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 5.2, 5.3}

实现代码

# 原始路径坐标
path = [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3)]

# 转换每个坐标为x.y格式的字符串
formatted_points = [f"{x}.{y}" for x, y in path]

# 拼接成目标格式字符串
result = "{" + ", ".join(formatted_points) + "}"

print(result)

代码解释

  • 列表推导式[f"{x}.{y}" for x, y in path]遍历每个坐标元组,将x和y用小数点连接成字符串(比如(0,1)转为"0.1")。
  • ", ".join(formatted_points)把所有转换后的字符串用, 连接成连续序列。
  • 最后在序列首尾添加{和},得到符合要求的格式。

运行结果

输出:

{0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 5.2, 5.3}

内容的提问来源于stack exchange,提问作者41_ Andi Yuda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:35:18