Skip to content

Unexpected client behaviour - doesn't follow python instructions #3707

@AndreasKaratzas

Description

@AndreasKaratzas

Question



What's your question?


I'm trying to execute a piece of python code, and the Multirotor client won't even take off. Is there something wrong with my code?

import os
import time
import numpy
import random
import setup_path
import airsim


def interpret_action(action, momentum, v_decay_counter):
    if action == 0:
        quad_offset = (2, 0, 0)
    elif action == 1:
        quad_offset = (0, 2, 0)
    elif action == 2:
        quad_offset = (0, 0, 2)
    elif action == 3:
        quad_offset = (-2, 0, 0)
    elif action == 4:
        quad_offset = (0, -2, 0)
    else:
        quad_offset = (0, 0, -2)

    if v_decay_counter >= 4:
        v_decay_counter = 0
    for i in range(v_decay_counter):
        quad_offset = tuple([momentum*x for x in quad_offset])
    v_decay_counter += 1
    return quad_offset, v_decay_counter


def do_action(client, momentum, v_decay_counter, clockspeed):
    quad_offset, v_decay_counter = interpret_action(
        random.randint(0, 5), 
        momentum, 
        v_decay_counter
    )
    
    client.moveByVelocityAsync(
        quad_offset[0],
        quad_offset[1],
        quad_offset[2],
        2,
        drivetrain=airsim.DrivetrainType.ForwardOnly,
        yaw_mode=airsim.YawMode(False, 0)
    ).join()
    time.sleep(2 / clockspeed)
    client.moveByVelocityAsync(0, 0, 0, 1).join()
    client.hoverAsync().join()

    print("Position: \n %s \n" % 
        client.
        getMultirotorState().
        kinematics_estimated.
        position.
        to_numpy_array().
        astype(int))

    return v_decay_counter


def main():
    v_decay_counter = 0
    momentum = 0.6
    clockspeed = 1000

    client = airsim.MultirotorClient()
    client.confirmConnection()
    client.enableApiControl(True)
    client.armDisarm(True)
    client.reset()
    client.simRunConsoleCommand("t.MaxFPS 5")
    
    client.moveToZAsync(z=-20, velocity=10, timeout_sec=10).join()
    time.sleep(10 / clockspeed)
    collision = client.simGetCollisionInfo().has_collided
    
    while not collision:
        v_decay_counter = do_action(client, momentum, v_decay_counter, clockspeed)
        collision = client.simGetCollisionInfo().has_collided
    
    client.armDisarm(False)
    client.reset()
    client.enableApiControl(False)


if __name__ == "__main__":
    main()


Include context on what you are trying to achieve


The ultimate purpose is to instruct the drone by calling the interpret_action method in a higher clockspeed rate and lower FPS. I've overcome the inaccuracy of the moveToPositionAsync instruction by using moveByVelocityAsync. I would like to find a solution with minimum changes to the code attached above.



Context details


AirSim version: 1.4

Python version: 3.8

UE: 4.26.1

OS: Windows 10

I use the CityEnviron environment attached under the AirSim 1.4 assets for Windows, and I boot it by executing a batch file (run.bat). The instruction is:

start CityEnviron -ResX=640 -ResY=480 -windowed


Settings


{
  "SeeDocsAt": "https://github.com/Microsoft/AirSim/blob/master/docs/settings.md",
  "SettingsVersion": 1.2,
  "SimMode": "Multirotor",
  "ClockType": "SteppableClock",
  "ClockSpeed": 1000.0,
  "ViewMode": "FlyWithMe" 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions