-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.py
190 lines (164 loc) · 5.02 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pybullet as pb
import gym
import numpy as np
from gym import spaces
from gym_pybullet_drones.envs.BaseAviary import (
DroneModel,
Physics,
ImageType,
BaseAviary,
)
import time
class Environment(BaseAviary):
def __init__(
self,
drone_model=DroneModel.CF2X,
num_drones: int = 1,
initial_xyzs=np.array([[0, 0, 1]]),
initial_rpys=None,
physics: Physics = Physics.PYB,
freq: int = 60,
aggregate_phy_steps: int = 1,
gui=False,
record=False,
obstacles=False,
user_debug_gui=False,
visualize=False,
):
self.max_XYZ = [5.0, 5.0, 5.0]
self.min_XYZ = [-5.0, -5.0, 0.1]
self.obstacle_type = 0
self.EPISODE_LEN_SEC = 5
self.maxEpisodeSteps = self.EPISODE_LEN_SEC * freq
self.target_point = np.array([0, 0, 3])
self.columnIDs = []
self.margin = 0.3
self.VISUALIZE = visualize
super().__init__(
drone_model=drone_model,
num_drones=num_drones,
initial_xyzs=initial_xyzs,
initial_rpys=initial_rpys,
physics=physics,
freq=freq,
aggregate_phy_steps=aggregate_phy_steps,
gui=gui,
record=record,
obstacles=obstacles,
user_debug_gui=user_debug_gui,
vision_attributes=True,
)
def _actionSpace(self):
action_lower = np.array([-1.0, -1.0, -1.0, -1.0])
action_upper = np.array([1.0, 1.0, 1.0, 1.0])
return spaces.Box(low=action_lower, high=action_upper, dtype=np.float32)
def _observationSpace(self):
#### Observation vector ### X Y Z Q1 Q2 Q3 Q4 R P Y VX VY VZ WX WY WZ P0 P1 P2 P3
obs_lower_bound = np.array(
[
## position
-np.inf,
-np.inf,
-np.inf,
## diff
-np.inf,
-np.inf,
-np.inf,
## Quaternions
-1.0,
-1.0,
-1.0,
-1.0,
## Linear Vel
-np.inf,
-np.inf,
-np.inf,
## Angular Vel
-np.inf,
-np.inf,
-np.inf,
]
)
obs_upper_bound = np.array(
[
np.inf,
np.inf,
np.inf,
np.inf,
np.inf,
np.inf,
1.0,
1.0,
1.0,
1.0,
np.inf,
np.inf,
np.inf,
np.inf,
np.inf,
np.inf,
]
)
return spaces.Dict(
{
"state": spaces.Box(
low=obs_lower_bound, high=obs_upper_bound, dtype=np.float32
)
}
)
def _computeObs(self):
state = self._getDroneStateVector(0)
target = self.target_point
pos = state[0:3]
diff = [pos[0] - target[0], pos[1] - target[1], pos[2] - target[2]]
quaternions = state[3:7] # 4
lin_vel = state[10:13] # 3
ang_vel = state[13:16] # 3
state_combined = [
pos,
diff,
quaternions,
lin_vel,
ang_vel,
]
state_new = []
for i in state_combined:
for j in i:
state_new.append(j)
obs = {
"state": state_new,
}
return obs
def _preprocessAction(self, action):
clipped_action = np.zeros((1, 4))
for k, v in action.items():
clipped_action[int(k), :] = np.clip(np.array(v), 0, self.MAX_RPM)
return clipped_action
def _computeReward(self):
state = self._getDroneStateVector(0)
target = self.target_point
pos = state[0:3]
diff = [pos[0] - target[0], pos[1] - target[1], pos[2] - target[2]]
reward = 0
dist = np.linalg.norm(diff) ** 2
reward = -np.log(2.5*dist+1)
if pos[2] < 0.1:
reward -= 1
return reward
def _computeDone(self):
z_pos = self._getDroneStateVector(0)[2]
if self.step_counter / self.SIM_FREQ > self.EPISODE_LEN_SEC:
return True
else:
return False
def _computeInfo(self):
return {}
def _addObstacles(self):
self.createTarget()
print("Target: ", self.target_point)
def createTarget(self):
drone_pos = self._getDroneStateVector(0)[0:3]
new_x = np.clip(drone_pos[0] + np.random.uniform(-2, 2), -5, 5)
new_y = np.clip(drone_pos[1] + np.random.uniform(-2, 2), -5, 5)
new_z = np.clip(drone_pos[2] + np.random.uniform(-0.5, 3), 0.5, 5)
self.target_point = np.array([new_x, new_y, new_z])