Skip to content

Latest commit

 

History

History
136 lines (105 loc) · 7.2 KB

airsim_obstacleavoidance.md

File metadata and controls

136 lines (105 loc) · 7.2 KB
Imagine you are helping me interact with the AirSim simulator for drones. At any given point of time, you have the following abilities. You are also required to output code for some of the requests.

Question - Ask me a clarification question Reason - Explain why you did something the way you did it. Code - Output a code command that achieves the desired goal.

The simulator contains a drone, along with several objects. Apart from the drone, none of the objects are movable. Within the code, we have the following commands available to us. You are not to use any other hypothetical functions.

get_position(object_name): Takes a string as input indicating the name of an object of interest, and returns a vector of 3 floats indicating its X,Y,Z coordinates.

fly_to(position): Takes a vector of 3 floats as input indicating X,Y,Z coordinates and commands the drone to fly there.

fly_path(positions): Takes a list of X,Y,Z positions indicating waypoints along a path and flies the drone along that path.

set_yaw(angle): Set the yaw angle for the drone (in degrees)

Are you ready?


Yes, I am ready. How can I help you with AirSim?


Assume I give you some XYZ coordinates as a goal. I want you to control the drone such that it moves towards the goal while avoiding obstacles in front of it. Instead of flying directly to the goal, the drone should take small steps while reassessing the obstacle situation at every step.

The drone is equipped with a distance sensor that returns the distance to the closest obstacle directly in front of the drone. You can access it by calling get_distance().

I want you to make sure to fly only if there is at least 10 meters of distance in front of the drone. If not, you should rotate the drone until there is at least 10 meters of clearance in the front and then take a step in the direction that the drone is currently facing. Don't forget to face the goal again after taking a step. Got it?


Yes, I got it. Here's how I would approach the problem.

First, we need to get the drone's current position and set the yaw angle towards the goal. Then, we can start a loop that runs until the drone reaches the goal position. Inside the loop, we can check if there is at least 10 meters of distance in front of the drone using the get_distance() function. If the distance is less than 10 meters, we can set the yaw angle to rotate the drone until there is at least 10 meters of clearance in the front. Once we have enough clearance, we can take a step towards the goal while maintaining the yaw angle. Here's the code that achieves this:

# Get the drone's current position and set the yaw angle towards the goal
current_pos = get_position('drone')
goal_pos = [x_goal, y_goal, z_goal]
yaw = math.atan2(goal_pos[1]-current_pos[1], goal_pos[0]-current_pos[0]) * 180 / math.pi
set_yaw(yaw)

# Start a loop that runs until the drone reaches the goal position
while np.linalg.norm(np.array(current_pos) - np.array(goal_pos)) > 0.5:
    # Check if there is at least 10 meters of distance in front of the drone
    distance = get_distance()
    while distance < 10:
        # Set the yaw angle to rotate the drone until there is at least 10 meters of clearance in the front
        set_yaw(yaw+30) # rotate 30 degrees at a time
        time.sleep(1) # wait for a second to measure the new distance
        distance = get_distance()

    # Take a step towards the goal while maintaining the yaw angle
    step_size = 5 # take steps of 5 meters
    step_vector = [step_size * math.cos(yaw), step_size * math.sin(yaw), 0]
    new_pos = [current_pos[i]+step_vector[i] for i in range(3)]
    fly_to(new_pos)
    current_pos = get_position('drone')

Note that this code assumes that the goal position is given as x_goal, y_goal, and z_goal. It also assumes that the drone is named 'drone' and the distance sensor is facing straight ahead. You may need to make some adjustments based on your specific setup.


After you take a step, you need to face the goal again.


You're right. After taking a step, we need to recalculate the yaw angle to face the goal again. Here's the modified code that includes this step:

# Get the drone's current position and set the yaw angle towards the goal
current_pos = get_position('drone')
goal_pos = [x_goal, y_goal, z_goal]
yaw = math.atan2(goal_pos[1]-current_pos[1], goal_pos[0]-current_pos[0]) * 180 / math.pi
set_yaw(yaw)

# Start a loop that runs until the drone reaches the goal position
while np.linalg.norm(np.array(current_pos) - np.array(goal_pos)) > 0.5:
    # Check if there is at least 10 meters of distance in front of the drone
    distance = get_distance()
    while distance < 10:
        # Set the yaw angle to rotate the drone until there is at least 10 meters of clearance in the front
        set_yaw(yaw+30) # rotate 30 degrees at a time
        time.sleep(1) # wait for a second to measure the new distance
        distance = get_distance()

    # Take a step towards the goal while maintaining the yaw angle
    step_size = 5 # take steps of 5 meters
    step_vector = [step_size * math.cos(yaw), step_size * math.sin(yaw), 0]
    new_pos = [current_pos[i]+step_vector[i] for i in range(3)]
    fly_to(new_pos)
    
    # Face the goal again by calculating the new yaw angle
    current_pos = get_position('drone')
    yaw = math.atan2(goal_pos[1]-current_pos[1], goal_pos[0]-current_pos[0]) * 180 / math.pi
    set_yaw(yaw)

This code should ensure that the drone faces the goal after taking a step, and continues moving towards the goal while avoiding obstacles in front of it.


You are not using the current yaw angle when computing the step vector. Remember there is a get_yaw() function.


You're right, I missed that. We can use the get_yaw() function to get the current yaw angle and use it to compute the step vector. Here's the updated code that includes this change:

# Get the drone's current position and set the yaw angle towards the goal
current_pos = get_position('drone')
goal_pos = [x_goal, y_goal, z_goal]
yaw = math.atan2(goal_pos[1]-current_pos[1], goal_pos[0]-current_pos[0]) * 180 / math.pi
set_yaw(yaw)

# Start a loop that runs until the drone reaches the goal position
while np.linalg.norm(np.array(current_pos) - np.array(goal_pos)) > 0.5:
    # Check if there is at least 10 meters of distance in front of the drone
    distance = get_distance()
    while distance < 10:
        # Set the yaw angle to rotate the drone until there is at least 10 meters of clearance in the front
        set_yaw(yaw+30) # rotate 30 degrees at a time
        time.sleep(1) # wait for a second to measure the new distance
        distance = get_distance()

    # Take a step towards the goal while maintaining the yaw angle
    step_size = 5 # take steps of 5 meters
    current_yaw = get_yaw()
    step_vector = [step_size * math.cos(current_yaw), step_size * math.sin(current_yaw), 0]
    new_pos = [current_pos[i]+step_vector[i] for i in range(3)]
    fly_to(new_pos)
    
    # Face the goal again by calculating the new yaw angle
    current_pos = get_position('drone')
    yaw = math.atan2(goal_pos[1]-current_pos[1], goal_pos[0]-current_pos[0]) * 180 / math.pi
    set_yaw(yaw)

This code should ensure that the drone takes steps while facing the goal and avoiding obstacles in front of it.