-
Notifications
You must be signed in to change notification settings - Fork 9
/
env.py
148 lines (115 loc) · 4.63 KB
/
env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/env python
"""Environment for Microsoft AirSim Unity Quadrotor using AirSim python API
- Author: Subin Yang
- Contact: subinlab.yang@gmail.com
- Date: 2019.06.20.
"""
import csv
import math
import pprint
import time
import torch
from PIL import Image
import numpy as np
import airsim
#import setup_path
MOVEMENT_INTERVAL = 1
class DroneEnv(object):
"""Drone environment class using AirSim python API"""
def __init__(self, useDepth=False):
self.client = airsim.MultirotorClient()
self.last_dist = self.get_distance(self.client.getMultirotorState().kinematics_estimated.position)
self.quad_offset = (0, 0, 0)
self.useDepth = useDepth
def step(self, action):
"""Step"""
#print("new step ------------------------------")
self.quad_offset = self.interpret_action(action)
#print("quad_offset: ", self.quad_offset)
quad_vel = self.client.getMultirotorState().kinematics_estimated.linear_velocity
self.client.moveByVelocityAsync(
quad_vel.x_val + self.quad_offset[0],
quad_vel.y_val + self.quad_offset[1],
quad_vel.z_val + self.quad_offset[2],
MOVEMENT_INTERVAL
).join()
collision = self.client.simGetCollisionInfo().has_collided
time.sleep(0.5)
quad_state = self.client.getMultirotorState().kinematics_estimated.position
quad_vel = self.client.getMultirotorState().kinematics_estimated.linear_velocity
if quad_state.z_val < - 7.3:
self.client.moveToPositionAsync(quad_state.x_val, quad_state.y_val, -7, 1).join()
result, done = self.compute_reward(quad_state, quad_vel, collision)
state, image = self.get_obs()
return state, result, done, image
def reset(self):
self.client.reset()
self.last_dist = self.get_distance(self.client.getMultirotorState().kinematics_estimated.position)
self.client.enableApiControl(True)
self.client.armDisarm(True)
self.client.takeoffAsync().join()
quad_state = self.client.getMultirotorState().kinematics_estimated.position
self.client.moveToPositionAsync(quad_state.x_val, quad_state.y_val, -7, 1).join()
obs, image = self.get_obs()
return obs, image
def get_obs(self):
if self.useDepth:
# get depth image
responses = self.client.simGetImages(
[airsim.ImageRequest(0, airsim.ImageType.DepthPlanner, pixels_as_float=True)])
response = responses[0]
img1d = np.array(response.image_data_float, dtype=np.float)
img1d = img1d * 3.5 + 30
img1d[img1d > 255] = 255
image = np.reshape(img1d, (responses[0].height, responses[0].width))
image_array = Image.fromarray(image).resize((84, 84)).convert("L")
else:
# Get rgb image
responses = self.client.simGetImages(
[airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]
)
response = responses[0]
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8)
image = img1d.reshape(response.height, response.width, 3)
image_array = Image.fromarray(image).resize((84, 84)).convert("L")
obs = np.array(image_array)
return obs, image
def get_distance(self, quad_state):
"""Get distance between current state and goal state"""
pts = np.array([3, -76, -7])
quad_pt = np.array(list((quad_state.x_val, quad_state.y_val, quad_state.z_val)))
dist = np.linalg.norm(quad_pt - pts)
return dist
def compute_reward(self, quad_state, quad_vel, collision):
"""Compute reward"""
reward = -1
if collision:
reward = -50
else:
dist = self.get_distance(quad_state)
diff = self.last_dist - dist
if dist < 10:
reward = 500
else:
reward += diff
self.last_dist = dist
done = 0
if reward <= -10:
done = 1
time.sleep(1)
elif reward > 499:
done = 1
time.sleep(1)
return reward, done
def interpret_action(self, action):
"""Interprete action"""
scaling_factor = 3
if action == 0:
self.quad_offset = (scaling_factor, 0, 0)
elif action == 1:
self.quad_offset = (-scaling_factor, 0, 0)
elif action == 2:
self.quad_offset = (0, scaling_factor, 0)
elif action == 3:
self.quad_offset = (0, -scaling_factor, 0)
return self.quad_offset