Robot that moves with putting in X,Y coordinates of a U shaped field

44 views Asked by At

So i just started a project for an employer. One of the things they want is a robot that runs on Python and can be controlled from anywhere with a internet connection and a laptop. They hired another person before me who convinced the company to buy a DJI Robomaster EP corethis. This robot is programmed in the robomasters app you can download from their website. Now one of the things i need to do is to make it possible to use pycharm to code the robot. So far i havent been able to do that. Secondly i need to get the robot to move by giving it an X and Y coordinate (which must work from distance). I have been able to get a sort of code for the moving to X,Y and another function which returns it to the starting position(X=0,Y=0), but I haven't tested that yet as I need to figure something out to use pycharm to program the robot. The second problem with the code is that the area it needs to drive in is U shaped, and it cant hit the object in the middle as it is very expensive.

The code 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()

I'm not a programmer so i dont even know if the above code is correct. All help is apreciated!

1

There are 1 answers

0
Mason Peterson On

This sounds like a nice robot path-planning problem. A few options:

  • Discretize the space (break it up into a grid) and make some cells "untraversable" and use something like A* (A Star) to plan a shortest path through the grid cells. Then iteratively feed each grid cell location to your go_to_coordinates function.
  • Use a random sampling planner like RRT which generates a tree of randomly sampled points in a space to eventually reach a goal location without passing through obstacles (open source RRT code).
  • Use a trajectory optimization approach to create a sequence of timestamped trajectory points to reach the goal while avoiding the obstacle in the middle.