AirSim icon indicating copy to clipboard operation
AirSim copied to clipboard

how to reset environment?

Open akshaysw opened this issue 9 years ago • 9 comments

how to reset position of drone while simulation is running and start a new episode just like reset function from OpenAI gym ''env.reset()". @sytelus #331

akshaysw avatar Sep 27 '17 18:09 akshaysw

Basically simSetPose() does the trick. There is an issue though.

I am using the PythonClient Example code:

client = AirSimClient('127.0.0.1')
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)
pos = client.getPosition()
ori = client.getOrientation()

while crash_count < number_episodes:

    client.simSetPose(Vector3r(pos.x_val, pos.y_val, pos.z_val),Quaternionr(ori.w_val, ori.x_val, ori.y_val, ori.z_val))
        
    client.armDisarm(True)    
    client.moveToZ(z, 3)  
.
.
.

Kjell-K avatar Sep 27 '17 20:09 Kjell-K

We still need to add API for reset. Unfortunately that warrents some refactoring so its taking time...

sytelus avatar Sep 28 '17 09:09 sytelus

Workaround SOLVED: I want to go to my home position from any position where I crashed right now.

My Workaround due to:

  1. simSetPose() takes direct and physical path --> collides with obstacles
  • Since I am working with an environment with open sky, I escape to the top (-30) and move from there
  1. simSetPose() is very unreliable in actually performing the reset
  • I have to do multiple attempts until the copter really sets the position. There fore the while and step by step if loops.
home_pos = client.getPosition()

def reset_env():
    
    reset = False

    while reset != True:
    
        now = client.getPosition()
        client.simSetPose(Vector3r(now.x_val, now.y_val, -30),Quaternionr(home_ori.w_val, home_ori.x_val, home_ori.y_val, home_ori.z_val))
        now = client.getPosition()
        
        if (now.z_val - (-30)) == 0:
            client.simSetPose(Vector3r(home_pos.x_val, home_pos.y_val, -30),Quaternionr(home_ori.w_val, home_ori.x_val, home_ori.y_val, home_ori.z_val))
            now = client.getPosition()
            
            if (now.x_val - home_pos.x_val) == 0 and (now.y_val - home_pos.y_val) == 0 and (now.z_val - (-30)) == 0 :
                client.simSetPose(Vector3r(home_pos.x_val, home_pos.y_val, home_pos.z_val),Quaternionr(home_ori.w_val, home_ori.x_val, home_ori.y_val, home_ori.z_val))
                now = client.getPosition()
                
                if (now.x_val - home_pos.x_val) == 0 and (now.y_val - home_pos.y_val) == 0 and (now.z_val - home_pos.z_val) == 0:
                    reset = True
                    client.moveByVelocity(0, 0, 0, 1)
                    time.sleep(1)

Kjell-K avatar Sep 28 '17 10:09 Kjell-K

FYI - I've implemented reset API in car. I'm planning to add it to multirotor as well but that would be coming later.

sytelus avatar Oct 04 '17 06:10 sytelus

@sytelus I tested the new simSetPose() in my test environment. After I run some episodes, it shows that the ignore_collison feature does not work properly.

home = client.simGetPose()
.
.
client.simSetPose(home, True);

Here the attempt of a reset with simSetPose():

capture

The copter still crashes on obstacles in direct path. Reopen or open a new issue?

Kjell-K avatar Oct 06 '17 10:10 Kjell-K

Hi! @Kjell-K first of all thank you for making this repository that interacts with the sim like an open ai gym environment. I've been testing it - when I run that code you suggested as a workaround I get

"msgpackrpc.error.RPCError: b"rpclib: function 'simSetPose' (called with 2 arg(s)) threw an exception. The exception contained this information: bad cast."

cangokalp avatar Oct 12 '17 21:10 cangokalp

and then when I use it with one argument -say home coordinates - it would just start from beginning and go to the last location. Regardless of what I give to simSetPose - it takes me from beginning to where I was at before... simGetPose() does the same thing too. I don't think they are working properly

cangokalp avatar Oct 12 '17 21:10 cangokalp

@sytelus is the reset API to multirotor coming soon?

cangokalp avatar Oct 12 '17 21:10 cangokalp

@cangokalp My posted code here is not longer valid after some updates. Refer to the new issue #532

Kjell-K avatar Oct 12 '17 21:10 Kjell-K