-
Notifications
You must be signed in to change notification settings - Fork 1
/
uniform_gust_environment.py
374 lines (281 loc) · 12.1 KB
/
uniform_gust_environment.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import math
import numpy as np
import gym
from gym import spaces
from gym.utils import seeding
import torch as T
import torch.nn as nn
import math
import time
import serial
import random
import nidaqmx
from nidaqmx.constants import TerminalConfiguration
class mfc_gust_Env(gym.Env):
def __init__(self, start_volt = 2.5, goal_position=0, steps_per_ep = 200, seed = 0):
T.manual_seed(seed)
np.random.seed(seed)
# Set up ni communication for pressure taps and MFCs
self.press_taps = nidaqmx.Task()
self.press_taps.ai_channels.add_ai_voltage_chan("Dev2/ai0", terminal_config = TerminalConfiguration.RSE)
self.press_taps.ai_channels.add_ai_voltage_chan("Dev2/ai1", terminal_config = TerminalConfiguration.RSE)
self.press_taps.ai_channels.add_ai_voltage_chan("Dev2/ai2", terminal_config = TerminalConfiguration.RSE)
self.press_taps.ai_channels.add_ai_voltage_chan("Dev2/ai3", terminal_config = TerminalConfiguration.RSE)
self.press_taps.ai_channels.add_ai_voltage_chan("Dev2/ai4", terminal_config = TerminalConfiguration.RSE)
self.press_taps.ai_channels.add_ai_voltage_chan("Dev2/ai5", terminal_config = TerminalConfiguration.RSE)
num_taps = 6
self.load_cell = nidaqmx.Task()
self.load_cell.ai_channels.add_ai_voltage_chan("Dev3/ai0")
self.load_cell.ai_channels.add_ai_voltage_chan("Dev3/ai1")
self.load_cell.ai_channels.add_ai_voltage_chan("Dev3/ai2")
self.load_cell.ai_channels.add_ai_voltage_chan("Dev3/ai3")
self.load_cell.ai_channels.add_ai_voltage_chan("Dev3/ai4")
self.load_cell.ai_channels.add_ai_voltage_chan("Dev3/ai5")
self.mfc_out1 = nidaqmx.Task()
self.mfc_out1.ao_channels.add_ao_voltage_chan("Dev2/ao0")
self.mfc_out1.write([start_volt])
self.mfc_out2 = nidaqmx.Task()
self.mfc_out2.ao_channels.add_ao_voltage_chan("Dev2/ao1")
self.mfc_out2.write([start_volt])
self.mfc_out3 = nidaqmx.Task()
self.mfc_out3.ao_channels.add_ao_voltage_chan("Dev2/ao3")
self.mfc_out3.write([start_volt])
self.new_volt1 = start_volt
# Set up serial communication for gust generator
self.ser = serial.Serial()
self.ser.baudrate = 9600
self.ser.port = 'COM5'
self.ser.open()
time.sleep(2)
speed = str('F,C,S1M6000,R')
self.ser.write(speed.encode())
self.g_done = '^'
self.g_check = 0
self.g_rotating = False
self.g_direct = True
self.max_ep_steps = steps_per_ep
self.steps_per_ep = steps_per_ep
self.num_steps = 0
self.first_ep = True
self.min_press = 0
self.max_press = 5
self.press_mean = 2.5
self.press_std = 0.5
self.lift_mean = 0
self.lift_std = 1
#Load cell Calibration Matrix
self.load_mat = np.array([[-0.91164, 0.22078, -0.71620, -35.41503, 2.10003, 34.48183],
[1.56291, 39.81764, -1.03218, -20.15276, -0.44775, -20.02389]])
#Load cell offset calibration and rotation matrix
self.offset = np.array([16.41, -8.06])
self.lift_rot_mat = np.array([-0.9962, -0.0872])
self.drag_rot_mat = np.array([-0.0872, 0.9962]) #check this
self.min_volt = 0
self.max_volt = 5
self.volt_std = 2.5 #was 2.5 post 5
self.volt_mean = 2.5
self.old_volt = 0
self.volt1 = 0
#time checking
self.delt=0.05
self.dwait = 0
self.last_dt = 0
self.prev_time = time.time()
self.current_time = time.time()
self.num_taps = num_taps
num_mfc = 1
self.num_mfc = num_mfc
self.action_space = spaces.Discrete(3)
self.actions1 = np.array([-0.25,0,0.25])
self.actions2 = np.array([-0.25,0,0.25])
self.actions3 = np.array([-0.25,0,0.25])
self.low_state = np.ones((num_taps+num_mfc,))
self.low_state[:num_taps] = self.low_state[:num_taps]*self.normalize_press(self.min_press)
self.low_state[-num_mfc:] = self.low_state[-num_mfc:]*self.normalize_volt(self.min_volt)
self.high_state = np.ones((num_taps+num_mfc,))
self.high_state[:num_taps] = self.high_state[:num_taps]*self.normalize_press(self.max_press)
self.high_state[-num_mfc:] = self.high_state[-num_mfc:]*self.normalize_volt(self.max_volt)
self.observation_space = spaces.Box(
low=self.low_state,
high=self.high_state,
shape=(num_taps+num_mfc,), #currently only 7 pressure taps, will change
dtype=np.float32
)
#set goal lift and pressure and initial obs
self.obs = np.zeros((self.num_taps+self.num_mfc,10))
self.goal_press = np.zeros((self.num_taps+self.num_mfc,))
temp_lift = []
temp_drag = []
for i in range(10):
norm_press = self.get_obs()
#norm_press=self.normalize_press(press_measurements)
time.sleep(0.04)
self.obs[:self.num_taps,i] = norm_press
self.obs[-1:,i] = self.volt1
lift, drag = self.get_lift_drag()
temp_lift.append(lift)
temp_drag.append(drag)
self.goal_lift = np.mean(temp_lift)
self.goal_press[:self.num_taps] = np.mean(self.obs[:self.num_taps],1)
self.obs = self.obs-self.goal_press.reshape((-1,1))
print(self.goal_lift)
print(self.obs[:,-1]-self.goal_press)
#self.seed()
#self.reset()
def normalize_volt(self, volt):
norm_act = (volt-self.volt_mean)/self.volt_std #/5
return norm_act
def denormalize_volt(self, norm_volt):
act = norm_volt*self.volt_std+self.volt_mean
return act
def normalize_press(self, press):
norm_press = (press-self.press_mean)/self.press_std
return norm_press
def change_gust(self):
#print('changing gust', self.g_check)
if self.g_direct:
mot_val=int(np.random.rand()*1300+150) #was 2000+750
if np.random.rand() > 0.5:
self.motor1 = str('F,C,I1M'+str(mot_val)+',R')
self.motor2 = str('F,C,I1M'+str(-mot_val)+',R')
else:
self.motor1 = str('F,C,I1M'+str(-mot_val)+',R')
self.motor2 = str('F,C,I1M'+str(mot_val)+',R')
self.ser.write(self.motor1.encode())
self.g_rotating = True
#self.g_check+=1
else:
self.ser.write(self.motor2.encode())
self.g_rotationg = True
#self.g_check+=1
def get_obs(self, N_avg=1):
press = np.array(self.press_taps.read())
norm_press=self.normalize_press(press)
return norm_press
def get_lift_drag(self):
load_read = np.array(self.load_cell.read())
force = np.dot(self.load_mat, load_read)-self.offset
lift = np.dot(force, self.lift_rot_mat)
drag = np.dot(force, self.drag_rot_mat)
return lift, drag
def get_reward(self, lift, drag):
Ker = 10
Kef = 0.02
error = self.goal_lift - lift
#dl = drag/lift
return -Ker*(error*error)#-Kef*(dl*dl)
def check_table(self):
self.g_done = self.ser.read(self.ser.inWaiting()).decode()
if self.g_done == '^':
self.g_rotating = False
def pause_for_timing(self):
self.dwait += self.delt - self.last_dt
while True:
current_time = time.time()
dt = current_time - self.prev_time
if dt >= self.dwait:
break
self.last_dt = dt
self.prev_time = current_time
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def step(self, action):
#done = False
self.old_volt1 = self.state[-1]
self.new_volt1 = self.denormalize_volt(self.old_volt1) + self.actions1[action]
if self.new_volt1<= self.min_volt:
self.new_volt1 = self.min_volt
elif self.new_volt1 >= self.max_volt:
self.new_volt1 = self.max_volt
# send voltage to mfcs
self.mfc_out1.write([self.new_volt1])
self.mfc_out2.write([self.new_volt1])
self.mfc_out3.write([self.new_volt1])
self.volt1 = self.normalize_volt(self.new_volt1)
self.state[-1] = self.volt1
#timing
self.pause_for_timing()
norm_press = self.get_obs()-self.goal_press[:self.num_taps] #difference in pressure
new_obs = np.array(norm_press).reshape((1,1,self.num_taps))
self.state[:self.num_taps] = new_obs
state = self.state.reshape(-1,1)
self.obs = np.append(self.obs, state, axis=1)
self.obs = np.delete(self.obs, 0, 1)
self._lift = self.lift_
self._drag = self.drag_
self.lift_, self.drag_ = self.get_lift_drag()
lift = (self._lift+self.lift_)/2
drag = (self._drag+self.drag_)/2
error = self.goal_lift - lift
reward = self.get_reward(lift,drag)
self.num_steps += 1
if self.num_steps >= self.steps_per_ep:
self.done = True
return self.obs, reward, self.done, lift, drag
def reset(self, start_volt = 2.5, goal=0):
self.done = False
self.g_done = '^'
self.g_check = 0
#checking turn table movement
self.check_table()
self.change_gust()
if self.g_direct:
self.g_direct = False
self.steps_per_ep = self.max_ep_steps
else:
self.g_direct = True
self.steps_per_ep = self.max_ep_steps/2
self.new_volt1 = start_volt
self.mfc_out1.write([self.new_volt1])
self.mfc_out2.write([self.new_volt1])
self.mfc_out3.write([self.new_volt1])
self.volt1 = self.normalize_volt(self.new_volt1)
press_measurements = self.get_obs()
temp_lift = []
temp_drag = []
while self.g_rotating:
time.sleep(0.04)
self.check_table()
if self.g_direct:
time.sleep(1)
else:
time.sleep(0.05)
self.num_steps = 0
self.obs = np.zeros((self.num_taps+self.num_mfc,10))
for i in range(10):
norm_press = self.get_obs()
#norm_press=self.normalize_press(press_measurements)
time.sleep(0.045)
self.obs[:self.num_taps,i] = norm_press
self.obs[-1,i] = self.volt1
if self.g_direct:
lift, drag = self.get_lift_drag()
temp_lift.append(lift)
temp_drag.append(drag)
if self.g_direct:
self.goal_lift = np.mean(temp_lift)
self.goal_press[:self.num_taps] = np.mean(self.obs[:self.num_taps],1)
self.obs = self.obs-self.goal_press.reshape((-1,1))
self.lift_, self.drag_ = self.get_lift_drag()
self.state = self.obs[:,-1]
self.prev_time = time.time()
self.first_ep = False
return self.obs
def next_goal(self, goal):
self.goal_lift = goal
def end(self):
self.new_volt1 = 2.5
self.mfc_out1.write([self.new_volt1])
self.mfc_out1.close()
self.mfc_out2.write([self.new_volt1])
self.mfc_out2.close()
self.mfc_out3.write([self.new_volt1])
self.mfc_out3.close()
def close_com(self):
self.mfc_out1.close()
self.mfc_out2.close()
self.mfc_out3.close()
self.load_cell.close()
self.press_taps.close()
self.ser.close()