输入U形场的X、Y坐标即可移动的机器人

问题描述 投票:0回答:1

所以我刚刚为雇主启动了一个项目。他们想要的东西之一是一个在 Python 上运行的机器人,并且可以通过互联网连接和笔记本电脑从任何地方进行控制。他们在我之前雇佣了另一个人,说服公司购买 DJI Robomaster EP 核心这个。 该机器人在 robomasters 应用程序中进行编程,您可以从他们的网站下载。现在我需要做的一件事是让使用 pycharm 来对机器人进行编码成为可能。到目前为止我还无法做到这一点。 其次,我需要通过给机器人一个 X 和 Y 坐标(必须在远处工作)来让机器人移动。我已经能够获得一种用于移动到 X,Y 的代码和另一个将其返回到起始位置(X=0,Y=0)的函数,但我还没有测试过,因为我需要计算想用pycharm来给机器人编程。代码的第二个问题是它需要驱动的区域是U形的,并且它不能击中中间的物体,因为它非常昂贵。

atm 代码:

import time
from dji_robomaster_sdk import RoboMaster
from dji_robomaster_sdk import robot
from dji_robomaster_sdk import armor
from dji_robomaster_sdk import chassis

# Initialize the robot
robo = RoboMaster()
robo.initialize()

# Define the starting position
starting_position = (0, 0)

# Define the U-shaped area
area_width = 100
area_height = 50

# Function to make the robot go back to the starting position
def go_to_starting_position():
    global starting_position
    chassis.move(0, 0, 0, 0).wait_for_completed()
    chassis.move(-starting_position[0], 0, 0, 0).wait_for_completed()
    chassis.move(0, 0, 0, 0).wait_for_completed()

# Function to check if the given coordinates are within the U-shaped area
def is_within_area(x, y):
    global area_width, area_height
    return -area_width / 2 <= x <= area_width / 2 and -area_height / 2 <= y <= area_height / 2

# Function to make the robot go to the given coordinates
def go_to_coordinates(x, y):
    global starting_position
    if not is_within_area(x, y):
        print("The given coordinates are outside the U-shaped area.")
        return
    if x < 0:
        x = 0
    if y < 0:
        y = 0
    if x > area_width:
        x = area_width
    if y > area_height:
        y = area_height
    chassis.move(x - starting_position[0], y - starting_position[1], 0, 0).wait_for_completed()
    starting_position = (x, y)

# Example usage
go_to_coordinates(50, 0)
time.sleep(2)
go_to_coordinates(0, 25)
time.sleep(2)
go_to_coordinates(-50, 0)
time.sleep(2)
go_to_coordinates(0, -25)
time.sleep(2)
go_to_starting_position()

# Shut down the robot
robo.close()

我不是程序员,所以我什至不知道上面的代码是否正确。 感谢所有帮助!

python robotics dji-sdk
1个回答
0
投票

这听起来像是一个很好的机器人路径规划问题。几个选项:

  • 离散空间(将其分解为网格)并使某些单元格“不可遍历”,并使用类似A*(一颗星)之类的东西来规划穿过网格单元格的最短路径。然后迭代地将每个网格单元位置提供给您的
    go_to_coordinates
    函数。
  • 使用像 RRT 这样的随机采样规划器,它会在空间中生成一棵随机采样点树,最终在不穿过障碍物的情况下到达目标位置(开源 RRT 代码)。
  • 使用轨迹优化方法创建一系列带时间戳的轨迹点以到达目标,同时避开中间的障碍物。
© www.soinside.com 2019 - 2024. All rights reserved.