-
Notifications
You must be signed in to change notification settings - Fork 23
/
QuadrotorFlyTest.py
263 lines (239 loc) · 8.86 KB
/
QuadrotorFlyTest.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""This file is used for testing the QuadrotorFly
By xiaobo
Contact [email protected]
Created on 五月 06 17:13 2019
"""
# Copyright (C)
#
# This file is part of QuadrotorFly
#
# GWpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GWpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWpy. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
import QuadrotorFlyModel as Qfm
import QuadrotorFlyGui as Qfg
import MemoryStore
import matplotlib.pyplot as plt
from enum import Enum
import enum
import StateEstimator
import CamDown
import time
import cv2
"""
********************************************************************************************************
**-------------------------------------------------------------------------------------------------------
** Compiler : python 3.6
** Module Name: QuadrotorFlyTest
** Module Date: 2019/5/6
** Module Auth: xiaobo
** Version : V0.1
** Description: 'Replace the content between'
**-------------------------------------------------------------------------------------------------------
** Reversion :
** Modified By:
** Date :
** Content :
** Notes :
********************************************************************************************************/
"""
class TestPara(Enum):
Test_Module_Dynamic = enum.auto()
Test_Module_Dynamic_Sensor = enum.auto()
Test_Module_Dynamic_CamDown = enum.auto()
D2R = Qfm.D2R
testFlag = TestPara.Test_Module_Dynamic_Sensor
if testFlag == TestPara.Test_Module_Dynamic:
print("QuadrotorFly Dynamic Test: ")
# define the quadrotor parameters
uavPara = Qfm.QuadParas()
# define the simulation parameters
simPara = Qfm.QuadSimOpt(init_mode=Qfm.SimInitType.rand,
init_att=np.array([10., 10., 0]), init_pos=np.array([0, 3, 0]))
# define the data capture
record = MemoryStore.DataRecord()
record.clear()
# define the first uav
quad1 = Qfm.QuadModel(uavPara, simPara)
# define the second uav
quad2 = Qfm.QuadModel(uavPara, simPara)
# gui init
gui = Qfg.QuadrotorFlyGui([quad1, quad2])
# simulation begin
for i in range(1000):
# set the reference
ref = np.array([0., 0., 1., 0.])
# update the first uav
stateTemp = quad1.observe()
action2, oil = quad1.get_controller_pid(stateTemp, ref)
quad1.step(action2)
# update the second uav
action2, oil2 = quad2.get_controller_pid(quad2.observe(), ref)
quad2.step(action2)
# gui render
gui.render()
# store data
record.buffer_append((stateTemp, action2))
# Data_recorder 0.3+ store episode data with independent deque
record.episode_append()
# draw result
data = record.get_episode_buffer()
bs = data[0]
ba = data[1]
t = range(0, record.count)
fig1 = plt.figure(2)
plt.clf()
# draw position
plt.subplot(3, 1, 1)
plt.plot(t, bs[t, 6] / D2R, label='roll')
plt.plot(t, bs[t, 7] / D2R, label='pitch')
plt.plot(t, bs[t, 8] / D2R, label='yaw')
plt.ylabel('Attitude $(\circ)$', fontsize=15)
plt.legend(fontsize=15, bbox_to_anchor=(1, 1.05))
# draw position
plt.subplot(3, 1, 2)
plt.plot(t, bs[t, 0], label='x')
plt.plot(t, bs[t, 1], label='y')
plt.ylabel('Position (m)', fontsize=15)
plt.legend(fontsize=15, bbox_to_anchor=(1, 1.05))
# draw altitude
plt.subplot(3, 1, 3)
plt.plot(t, bs[t, 2], label='z')
plt.ylabel('Altitude (m)', fontsize=15)
plt.legend(fontsize=15, bbox_to_anchor=(1, 1.05))
plt.show()
elif testFlag == TestPara.Test_Module_Dynamic_Sensor:
# from QuadrotorFly import QuadrotorFlyModel as Qfm
q1 = Qfm.QuadModel(Qfm.QuadParas(), Qfm.QuadSimOpt(init_mode=Qfm.SimInitType.fixed, enable_sensor_sys=True,
init_pos=np.array([5, -4, 0]), init_att=np.array([0, 0, 5])))
# init the estimator
s1 = StateEstimator.KalmanFilterSimple()
# set the init state of estimator
s1.reset(q1.state)
# simulation period
t = np.arange(0, 30, 0.01)
ii_len = len(t)
stateRealArr = np.zeros([ii_len, 12])
stateEstArr = np.zeros([ii_len, 12])
meaArr = np.zeros([ii_len, 3])
# set the bias
s1.gyroBias = q1.imu0.gyroBias
s1.accBias = q1.imu0.accBias
s1.magRef = q1.mag0.para.refField
print(s1.gyroBias, s1.accBias)
for ii in range(ii_len):
# wait for start
if ii < 100:
sensor_data1 = q1.observe()
_, oil = q1.get_controller_pid(q1.state)
action = np.ones(4) * oil
q1.step(action)
stateEstArr[ii] = s1.update(sensor_data1, q1.ts)
stateRealArr[ii] = q1.state
else:
sensor_data1 = q1.observe()
action, oil = q1.get_controller_pid(s1.state, np.array([0, 0, 3, 0]))
q1.step(action)
stateEstArr[ii] = s1.update(sensor_data1, q1.ts)
stateRealArr[ii] = q1.state
import matplotlib.pyplot as plt
plt.figure(1)
ylabelList = ['roll', 'pitch', 'yaw', 'rate_roll', 'rate_pit', 'rate_yaw']
for ii in range(6):
plt.subplot(6, 1, ii + 1)
plt.plot(t, stateRealArr[:, 6 + ii] / D2R, '-b', label='real')
plt.plot(t, stateEstArr[:, 6 + ii] / D2R, '-g', label='est')
plt.legend()
plt.ylabel(ylabelList[ii])
# plt.show()
ylabelList = ['p_x', 'p_y', 'p_z', 'vel_x', 'vel_y', 'vel_z']
plt.figure(2)
for ii in range(6):
plt.subplot(6, 1, ii + 1)
plt.plot(t, stateRealArr[:, ii], '-b', label='real')
plt.plot(t, stateEstArr[:, ii], '-g', label='est')
plt.legend()
plt.ylabel(ylabelList[ii])
plt.show()
elif testFlag == TestPara.Test_Module_Dynamic_CamDown:
import matplotlib.pyplot as plt
from QuadrotorFlyModel import QuadModel, QuadSimOpt, QuadParas, StructureType, SimInitType
D2R = np.pi / 180
video_write_flag = True
print("PID controller test: ")
uavPara = QuadParas(structure_type=StructureType.quad_x)
simPara = QuadSimOpt(init_mode=SimInitType.fixed, enable_sensor_sys=False,
init_att=np.array([5., -5., 0]), init_pos=np.array([5, -5, 0]))
quad1 = QuadModel(uavPara, simPara)
record = MemoryStore.DataRecord()
record.clear()
step_cnt = 0
# init the camera
cam1 = CamDown.CamDown(render_mode=CamDown.CamDownPara.Render_Mode_Gpu)
cam1.load_ground_img()
print('Load img completed!')
if video_write_flag:
v_format = cv2.VideoWriter_fourcc(*'MJPG')
out1 = cv2.VideoWriter('Data/img/test.avi', v_format, 1 / quad1.uavPara.ts, (cam1.imgVertical, cam1.imgHorizon))
for i in range(1000):
if i == 0:
time_start = time.time()
ref = np.array([0., 0., 3., 0.])
stateTemp = quad1.observe()
# get image
pos_0 = quad1.position * 1000
att_0 = quad1.attitude
img1 = cam1.get_img_by_state(pos_0, att_0)
# file_name = 'Data/img/test_' + str(i) + '.jpg'
# cv2.imwrite(file_name, img1)
if video_write_flag:
out1.write(img1)
action2, oil = quad1.get_controller_pid(stateTemp, ref)
print('action: ', action2)
action2 = np.clip(action2, 0.1, 0.9)
quad1.step(action2)
record.buffer_append((stateTemp, action2))
step_cnt = step_cnt + 1
time_end = time.time()
print('time cost:', str(time_end - time_start))
record.episode_append()
if video_write_flag:
out1.release()
print('Quadrotor structure type', quad1.uavPara.structureType)
# quad1.reset_states()
print('Quadrotor get reward:', quad1.get_reward())
data = record.get_episode_buffer()
bs = data[0]
ba = data[1]
t = range(0, record.count)
# mpl.style.use('seaborn')
fig1 = plt.figure(1)
plt.clf()
plt.subplot(3, 1, 1)
plt.plot(t, bs[t, 6] / D2R, label='roll')
plt.plot(t, bs[t, 7] / D2R, label='pitch')
plt.plot(t, bs[t, 8] / D2R, label='yaw')
plt.ylabel('Attitude $(\circ)$', fontsize=15)
plt.legend(fontsize=15, bbox_to_anchor=(1, 1.05))
plt.subplot(3, 1, 2)
plt.plot(t, bs[t, 0], label='x')
plt.plot(t, bs[t, 1], label='y')
plt.ylabel('Position (m)', fontsize=15)
plt.legend(fontsize=15, bbox_to_anchor=(1, 1.05))
plt.subplot(3, 1, 3)
plt.plot(t, bs[t, 2], label='z')
plt.ylabel('Altitude (m)', fontsize=15)
plt.legend(fontsize=15, bbox_to_anchor=(1, 1.05))
plt.show()