-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrone_calc.py
More file actions
738 lines (617 loc) · 26.9 KB
/
drone_calc.py
File metadata and controls
738 lines (617 loc) · 26.9 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
import numpy as np
import matplotlib.pyplot as plt
class SupportFilesDrone:
''' 次の関数はメインファイルと関連しています。'''
def __init__(self):
''' 定数をロードします'''
# 機体の情報 (Astec Hummingbird)
Ix = 0.0034 # kg*m^2
Iy = 0.0034 # kg*m^2
Iz = 0.006 # kg*m^2
m = 0.698 # kg
g = 9.81 # m/s^2
Jtp=1.302*10**(-6) # N*m*s^2=kg*m^2
Ts=0.1 # s
# コスト関数の行列の重み (対角である必要があります)
Q=np.matrix('10 0 0;0 10 0;0 0 10') # 出力の重み (最後のサンプルを除くすべてのサンプル)
S=np.matrix('20 0 0;0 20 0;0 0 20') # 最終ホライズン期間出力の重み
R=np.matrix('10 0 0;0 10 0;0 0 10') # 入力の重み
ct = 7.6184*10**(-8)*(60/(2*np.pi))**2 # N*s^2
cq = 2.6839*10**(-9)*(60/(2*np.pi))**2 # N*m*s^2
l = 0.171 # m
controlled_states=3 # 姿勢の出力: Phi, Theta, Psi
hz = 4 # horizon period
innerDyn_length=4 # 内部制御ループの反復数
# 軸の位置
px=np.array([-1,-2])
py=np.array([-1,-2])
pz=np.array([-1,-2])
# # 複素数の極
# px=np.array([-0.1+0.3j,-0.1-0.3j])
# py=np.array([-0.1+0.3j,-0.1-0.3j])
# pz=np.array([-1+1.3j,-1-1.3j])
r=2
f=0.025
height_i=5
height_f=25
pos_x_y=0 # デフォルト: 0。視覚的な目的のために、正の x と y を長くします (1-はい、0-いいえ)。 UAV のダイナミクスには影響しません。
sub_loop=5 # アニメーション目的のため
sim_version=1 # 1 または 2 のみです。それに応じて、アニメーションで異なるグラフが表示されます。
# 空気の抗力:
drag_switch=0 # 0 または 1 のいずれかである必要があります (0 - 抗力オフ、1 - 抗力オン)
# 空気の抗力係数[-]:
C_D_u=1.5
C_D_v=1.5
C_D_w=2.0
# 抗力断面積 [m^2]
A_u=2*l*0.01+0.05**2
A_v=2*l*0.01+0.05**2
A_w=2*2*l*0.01+0.05**2
# 空気密度
rho=1.225 # [kg/m^3]
trajectory=7 # 参照軌道を選択: 1 ~ 9 のみ
no_plots=0 # 0 - プロットが表示されます。 1 - プロットはスキップします (アニメーションのみ)
self.constants={'Ix':Ix, 'Iy':Iy, 'Iz':Iz, 'm':m, 'g':g, 'Jtp':Jtp, 'Ts':Ts,\
'Q':Q, 'S':S, 'R':R, 'ct':ct, 'cq':cq, 'l':l, 'controlled_states':controlled_states,\
'hz':hz, 'innerDyn_length':innerDyn_length, 'px':px, 'py':py, 'pz':pz,\
'r':r, 'f':f, 'height_i':height_i, 'height_f':height_f, 'pos_x_y':pos_x_y,\
'sub_loop':sub_loop, 'sim_version':sim_version, 'drag_switch':drag_switch,\
'C_D_u':C_D_u, 'C_D_v':C_D_v, 'C_D_w':C_D_w, 'A_u':A_u, 'A_v':A_v, 'A_w':A_w,\
'rho':rho, 'trajectory':trajectory, 'no_plots':no_plots}
return None
def trajectory_generator(self,t):
'''ドローンがたどる参照軌道を作成します。'''
Ts=self.constants['Ts']
innerDyn_length=self.constants['innerDyn_length']
r=self.constants['r']
f=self.constants['f']
height_i=self.constants['height_i']
height_f=self.constants['height_f']
trajectory=self.constants['trajectory']
d_height=height_f-height_i
# ドローン軌道の x、y、z 次元を定義する
alpha=2*np.pi*f*t
if trajectory==1 or trajectory==2 or trajectory==3 or trajectory==4:
# Trajectory 1
x=r*np.cos(alpha)
y=r*np.sin(alpha)
z=height_i+d_height/(t[-1])*t
x_dot=-r*np.sin(alpha)*2*np.pi*f
y_dot=r*np.cos(alpha)*2*np.pi*f
z_dot=d_height/(t[-1])*np.ones(len(t))
x_dot_dot=-r*np.cos(alpha)*(2*np.pi*f)**2
y_dot_dot=-r*np.sin(alpha)*(2*np.pi*f)**2
z_dot_dot=0*np.ones(len(t))
if trajectory==2:
# Trajectory 2
# 軌道 1 との評価軌道
x[101:len(x)]=2*(t[101:len(t)]-t[100])/20+x[100]
y[101:len(y)]=2*(t[101:len(t)]-t[100])/20+y[100]
z[101:len(z)]=z[100]+d_height/t[-1]*(t[101:len(t)]-t[100])
x_dot[101:len(x_dot)]=1/10*np.ones(len(t[101:len(t)]))
y_dot[101:len(y_dot)]=1/10*np.ones(len(t[101:len(t)]))
z_dot[101:len(z_dot*(t/20))]=d_height/(t[-1])*np.ones(len(t[101:len(t)]))
x_dot_dot[101:len(x_dot_dot)]=0*np.ones(len(t[101:len(t)]))
y_dot_dot[101:len(y_dot_dot)]=0*np.ones(len(t[101:len(t)]))
z_dot_dot[101:len(z_dot_dot)]=0*np.ones(len(t[101:len(t)]))
elif trajectory==3:
# Trajectory 3
# 軌道 1 との評価軌道
x[101:len(x)]=2*(t[101:len(t)]-t[100])/20+x[100]
y[101:len(y)]=2*(t[101:len(t)]-t[100])/20+y[100]
z[101:len(z)]=z[100]+d_height/t[-1]*(t[101:len(t)]-t[100])**2
x_dot[101:len(x_dot)]=1/10*np.ones(len(t[101:len(t)]))
y_dot[101:len(y_dot)]=1/10*np.ones(len(t[101:len(t)]))
z_dot[101:len(z_dot)]=2*d_height/(t[-1])*(t[101:len(t)]-t[100])
x_dot_dot[101:len(x_dot_dot)]=0*np.ones(len(t[101:len(t)]))
y_dot_dot[101:len(y_dot_dot)]=0*np.ones(len(t[101:len(t)]))
z_dot_dot[101:len(z_dot_dot)]=2*d_height/t[-1]*np.ones(len(t[101:len(t)]))
elif trajectory==4:
# Trajectory 4
# 軌道 1 との評価軌道
x[101:len(x)]=2*(t[101:len(t)]-t[100])/20+x[100]
y[101:len(y)]=2*(t[101:len(t)]-t[100])/20+y[100]
z[101:len(z)]=z[100]+50*d_height/t[-1]*np.sin(0.1*(t[101:len(t)]-t[100]))
x_dot[101:len(x_dot)]=1/10*np.ones(len(t[101:len(t)]))
y_dot[101:len(y_dot)]=1/10*np.ones(len(t[101:len(t)]))
z_dot[101:len(z_dot)]=5*d_height/t[-1]*np.cos(0.1*(t[101:len(t)]-t[100]))
x_dot_dot[101:len(x_dot_dot)]=0*np.ones(len(t[101:len(t)]))
y_dot_dot[101:len(y_dot_dot)]=0*np.ones(len(t[101:len(t)]))
z_dot_dot[101:len(z_dot_dot)]=-0.5*d_height/t[-1]*np.sin(0.1*(t[101:len(t)]-t[100]))
elif trajectory==5 or trajectory==6:
if trajectory==5:
power=1
else:
power=2
if power == 1:
# Trajectory 5
r_2=r/15
x=(r_2*t**power+r)*np.cos(alpha)
y=(r_2*t**power+r)*np.sin(alpha)
z=height_i+d_height/t[-1]*t
x_dot=r_2*np.cos(alpha)-(r_2*t+r)*np.sin(alpha)*2*np.pi*f
y_dot=r_2*np.sin(alpha)+(r_2*t+r)*np.cos(alpha)*2*np.pi*f
z_dot=d_height/(t[-1])*np.ones(len(t))
x_dot_dot=-r_2*np.sin(alpha)*4*np.pi*f-(r_2*t+r)*np.cos(alpha)*(2*np.pi*f)**2
y_dot_dot=r_2*np.cos(alpha)*4*np.pi*f-(r_2*t+r)*np.sin(alpha)*(2*np.pi*f)**2
z_dot_dot=0*np.ones(len(t))
else:
# Trajectory 6
r_2=r/500
x=(r_2*t**power+r)*np.cos(alpha)
y=(r_2*t**power+r)*np.sin(alpha)
z=height_i+d_height/t[-1]*t
x_dot=power*r_2*t**(power-1)*np.cos(alpha)-(r_2*t**(power)+r)*np.sin(alpha)*2*np.pi*f
y_dot=power*r_2*t**(power-1)*np.sin(alpha)+(r_2*t**(power)+r)*np.cos(alpha)*2*np.pi*f
z_dot=d_height/(t[-1])*np.ones(len(t))
x_dot_dot=(power*(power-1)*r_2*t**(power-2)*np.cos(alpha)-power*r_2*t**(power-1)*np.sin(alpha)*2*np.pi*f)-(power*r_2*t**(power-1)*np.sin(alpha)*2*np.pi*f+(r_2*t**power+r_2)*np.cos(alpha)*(2*np.pi*f)**2)
y_dot_dot=(power*(power-1)*r_2*t**(power-2)*np.sin(alpha)+power*r_2*t**(power-1)*np.cos(alpha)*2*np.pi*f)+(power*r_2*t**(power-1)*np.cos(alpha)*2*np.pi*f-(r_2*t**power+r_2)*np.sin(alpha)*(2*np.pi*f)**2)
z_dot_dot=0*np.ones(len(t))
elif trajectory==7:
# Trajectory 7
x=2*t/20+1
y=2*t/20-2
z=height_i+d_height/t[-1]*t
x_dot=1/10*np.ones(len(t))
y_dot=1/10*np.ones(len(t))
z_dot=d_height/(t[-1])*np.ones(len(t))
x_dot_dot=0*np.ones(len(t))
y_dot_dot=0*np.ones(len(t))
z_dot_dot=0*np.ones(len(t))
elif trajectory==8:
# Trajectory 8
x=r/5*np.sin(alpha)+t/100
y=t/100-1
z=height_i+d_height/t[-1]*t
x_dot=r/5*np.cos(alpha)*2*np.pi*f+1/100
y_dot=1/100*np.ones(len(t))
z_dot=d_height/(t[-1])*np.ones(len(t))
x_dot_dot=-r/5*np.sin(alpha)*(2*np.pi*f)**2
y_dot_dot=0*np.ones(len(t))
z_dot_dot=0*np.ones(len(t))
elif trajectory==9:
# Trajectory 9
wave_w=1
x=r*np.cos(alpha)
y=r*np.sin(alpha)
z=height_i+7*d_height/t[-1]*np.sin(wave_w*t)
x_dot=-r*np.sin(alpha)*2*np.pi*f
y_dot=r*np.cos(alpha)*2*np.pi*f
z_dot=7*d_height/(t[-1])*np.cos(wave_w*t)*wave_w
x_dot_dot=-r*np.cos(alpha)*(2*np.pi*f)**2
y_dot_dot=-r*np.sin(alpha)*(2*np.pi*f)**2
z_dot_dot=-7*d_height/(t[-1])*np.sin(wave_w*t)*wave_w**2
else:
print(" 1 ~ 9 の参照軌道を選択してください。")
exit()
# x と y のベクトルはサンプル時間ごとに変化します
dx=x[1:len(x)]-x[0:len(x)-1]
dy=y[1:len(y)]-y[0:len(y)-1]
dz=z[1:len(z)]-z[0:len(z)-1]
dx=np.append(np.array(dx[0]),dx)
dy=np.append(np.array(dy[0]),dy)
dz=np.append(np.array(dz[0]),dz)
# 参照ヨー角を定義する
psi=np.zeros(len(x))
psiInt=psi
psi[0]=np.arctan2(y[0],x[0])+np.pi/2
psi[1:len(psi)]=np.arctan2(dy[1:len(dy)],dx[1:len(dx)])
# 回転量を追跡するためにヨー角が必要です
dpsi=psi[1:len(psi)]-psi[0:len(psi)-1]
psiInt[0]=psi[0]
for i in range(1,len(psiInt)):
if dpsi[i-1]<-np.pi:
psiInt[i]=psiInt[i-1]+(dpsi[i-1]+2*np.pi)
elif dpsi[i-1]>np.pi:
psiInt[i]=psiInt[i-1]+(dpsi[i-1]-2*np.pi)
else:
psiInt[i]=psiInt[i-1]+dpsi[i-1]
return x, x_dot, x_dot_dot, y, y_dot, y_dot_dot, z, z_dot, z_dot_dot, psiInt
def pos_controller(self,X_ref,X_dot_ref,X_dot_dot_ref,Y_ref,Y_dot_ref,Y_dot_dot_ref,Z_ref,Z_dot_ref,Z_dot_dot_ref,Psi_ref,states):
'''この関数は位置コントローラーです。開ループ システムに必要な U1、および MPC コントローラーのファイおよびシータ角度を計算します。'''
# 定数を取得
m=self.constants['m']
g=self.constants['g']
px=self.constants['px']
py=self.constants['py']
pz=self.constants['pz']
# 状態を割り振る
# States: [u,v,w,p,q,r,x,y,z,phi,theta,psi]
u = states[0]
v = states[1]
w = states[2]
x = states[6]
y = states[7]
z = states[8]
phi = states[9]
theta = states[10]
psi = states[11]
# u,v,w with x_dot,y_dot,z_dot の回転行列
R_x=np.array([[1, 0, 0],[0, np.cos(phi), -np.sin(phi)],[0, np.sin(phi), np.cos(phi)]])
R_y=np.array([[np.cos(theta),0,np.sin(theta)],[0,1,0],[-np.sin(theta),0,np.cos(theta)]])
R_z=np.array([[np.cos(psi),-np.sin(psi),0],[np.sin(psi),np.cos(psi),0],[0,0,1]])
R_matrix=np.matmul(R_z,np.matmul(R_y,R_x))
pos_vel_body=np.array([[u],[v],[w]])
pos_vel_fixed=np.matmul(R_matrix,pos_vel_body)
x_dot=pos_vel_fixed[0]
y_dot=pos_vel_fixed[1]
z_dot=pos_vel_fixed[2]
# 誤差を計算
ex=X_ref-x
ex_dot=X_dot_ref-x_dot
ey=Y_ref-y
ey_dot=Y_dot_ref-y_dot
ez=Z_ref-z
ez_dot=Z_dot_ref-z_dot
# フィードバック定数を計算する
kx1=(px[0]-(px[0]+px[1])/2)**2-(px[0]+px[1])**2/4
kx2=px[0]+px[1]
kx1=kx1.real
kx2=kx2.real
ky1=(py[0]-(py[0]+py[1])/2)**2-(py[0]+py[1])**2/4
ky2=py[0]+py[1]
ky1=ky1.real
ky2=ky2.real
kz1=(pz[0]-(pz[0]+pz[1])/2)**2-(pz[0]+pz[1])**2/4
kz2=pz[0]+pz[1]
kz1=kz1.real
kz2=kz2.real
# 位置コントローラーの値 vx、vy、vz を計算します。
ux=kx1*ex+kx2*ex_dot
uy=ky1*ey+ky2*ey_dot
uz=kz1*ez+kz2*ez_dot
vx=X_dot_dot_ref-ux[0]
vy=Y_dot_dot_ref-uy[0]
vz=Z_dot_dot_ref-uz[0]
# phi, theta, U1を計算する。
a=vx/(vz+g)
b=vy/(vz+g)
c=np.cos(Psi_ref)
d=np.sin(Psi_ref)
tan_theta=a*c+b*d
Theta_ref=np.arctan(tan_theta)
if Psi_ref>=0:
Psi_ref_singularity=Psi_ref-np.floor(abs(Psi_ref)/(2*np.pi))*2*np.pi
else:
Psi_ref_singularity=Psi_ref+np.floor(abs(Psi_ref)/(2*np.pi))*2*np.pi
if ((np.abs(Psi_ref_singularity)<np.pi/4 or np.abs(Psi_ref_singularity)>7*np.pi/4) or (np.abs(Psi_ref_singularity)>3*np.pi/4 and np.abs(Psi_ref_singularity)<5*np.pi/4)):
tan_phi=np.cos(Theta_ref)*(np.tan(Theta_ref)*d-b)/c
else:
tan_phi=np.cos(Theta_ref)*(a-np.tan(Theta_ref)*c)/d
Phi_ref=np.arctan(tan_phi)
U1=(vz+g)*m/(np.cos(Phi_ref)*np.cos(Theta_ref))
return Phi_ref, Theta_ref, U1
def LPV_cont_discrete(self,states,omega_total):
'''3つの回転軸に関するLPVモデルです。'''
# 必要な定数を取得する
Ix=self.constants['Ix'] # kg*m^2
Iy=self.constants['Iy'] # kg*m^2
Iz=self.constants['Iz'] # kg*m^2
Jtp=self.constants['Jtp'] #N*m*s^2=kg*m^2
Ts=self.constants['Ts'] #s
# 状態を割り当てる
# States: [u,v,w,p,q,r,x,y,z,phi,theta,psi]
u=states[0]
v=states[1]
w=states[2]
p=states[3]
q=states[4]
r=states[5]
phi=states[9]
theta=states[10]
psi=states[11]
# u,v,w with x_dot,y_dot,z_dot の回転行列
R_x=np.array([[1, 0, 0],[0, np.cos(phi), -np.sin(phi)],[0, np.sin(phi), np.cos(phi)]])
R_y=np.array([[np.cos(theta),0,np.sin(theta)],[0,1,0],[-np.sin(theta),0,np.cos(theta)]])
R_z=np.array([[np.cos(psi),-np.sin(psi),0],[np.sin(psi),np.cos(psi),0],[0,0,1]])
R_matrix=np.matmul(R_z,np.matmul(R_y,R_x))
pos_vel_body=np.array([[u],[v],[w]])
pos_vel_fixed=np.matmul(R_matrix,pos_vel_body)
x_dot=pos_vel_fixed[0]
y_dot=pos_vel_fixed[1]
z_dot=pos_vel_fixed[2]
x_dot=x_dot[0]
y_dot=y_dot[0]
z_dot=z_dot[0]
# phi_dot, theta_dot, psi_dot, のための転置行列(T行列)が必要です
# p、q、r と phi_dot、theta_dot、psi_dot を関連付ける変換行列
T_matrix=np.array([[1,np.sin(phi)*np.tan(theta),np.cos(phi)*np.tan(theta)],\
[0,np.cos(phi),-np.sin(phi)],\
[0,np.sin(phi)/np.cos(theta),np.cos(phi)/np.cos(theta)]])
rot_vel_body=np.array([[p],[q],[r]])
rot_vel_fixed=np.matmul(T_matrix,rot_vel_body)
phi_dot=rot_vel_fixed[0]
theta_dot=rot_vel_fixed[1]
psi_dot=rot_vel_fixed[2]
phi_dot=phi_dot[0]
theta_dot=theta_dot[0]
psi_dot=psi_dot[0]
# 連続 LPV A、B、C、D 行列を作成する
A01=1
A13=-omega_total*Jtp/Ix
A15=theta_dot*(Iy-Iz)/Ix
A23=1
A31=omega_total*Jtp/Iy
A35=phi_dot*(Iz-Ix)/Iy
A45=1
A51=(theta_dot/2)*(Ix-Iy)/Iz
A53=(phi_dot/2)*(Ix-Iy)/Iz
A=np.zeros((6,6))
B=np.zeros((6,3))
C=np.zeros((3,6))
D=0
A[0,1]=A01
A[1,3]=A13
A[1,5]=A15
A[2,3]=A23
A[3,1]=A31
A[3,5]=A35
A[4,5]=A45
A[5,1]=A51
A[5,3]=A53
B[1,0]=1/Ix
B[3,1]=1/Iy
B[5,2]=1/Iz
C[0,0]=1
C[1,2]=1
C[2,4]=1
D=np.zeros((3,3))
# システムの離散化 (フォワード オイラー法)
Ad=np.identity(np.size(A,1))+Ts*A
Bd=Ts*B
Cd=C
Dd=D
return Ad,Bd,Cd,Dd,x_dot,y_dot,z_dot,phi,phi_dot,theta,theta_dot,psi,psi_dot
def mpc_simplification(self, Ad, Bd, Cd, Dd, hz):
'''この関数は、モデル予測制御用のコンパクトな行列を作成します。'''
# db - double bar
# dbt - double bar transpose
# dc - double circumflex
A_aug=np.concatenate((Ad,Bd),axis=1)
temp1=np.zeros((np.size(Bd,1),np.size(Ad,1)))
temp2=np.identity(np.size(Bd,1))
temp=np.concatenate((temp1,temp2),axis=1)
A_aug=np.concatenate((A_aug,temp),axis=0)
B_aug=np.concatenate((Bd,np.identity(np.size(Bd,1))),axis=0)
C_aug=np.concatenate((Cd,np.zeros((np.size(Cd,0),np.size(Bd,1)))),axis=1)
D_aug=Dd
Q=self.constants['Q']
S=self.constants['S']
R=self.constants['R']
CQC=np.matmul(np.transpose(C_aug),Q)
CQC=np.matmul(CQC,C_aug)
CSC=np.matmul(np.transpose(C_aug),S)
CSC=np.matmul(CSC,C_aug)
QC=np.matmul(Q,C_aug)
SC=np.matmul(S,C_aug)
Qdb=np.zeros((np.size(CQC,0)*hz,np.size(CQC,1)*hz))
Tdb=np.zeros((np.size(QC,0)*hz,np.size(QC,1)*hz))
Rdb=np.zeros((np.size(R,0)*hz,np.size(R,1)*hz))
Cdb=np.zeros((np.size(B_aug,0)*hz,np.size(B_aug,1)*hz))
Adc=np.zeros((np.size(A_aug,0)*hz,np.size(A_aug,1)))
for i in range(0,hz):
if i == hz-1:
Qdb[np.size(CSC,0)*i:np.size(CSC,0)*i+CSC.shape[0],np.size(CSC,1)*i:np.size(CSC,1)*i+CSC.shape[1]]=CSC
Tdb[np.size(SC,0)*i:np.size(SC,0)*i+SC.shape[0],np.size(SC,1)*i:np.size(SC,1)*i+SC.shape[1]]=SC
else:
Qdb[np.size(CQC,0)*i:np.size(CQC,0)*i+CQC.shape[0],np.size(CQC,1)*i:np.size(CQC,1)*i+CQC.shape[1]]=CQC
Tdb[np.size(QC,0)*i:np.size(QC,0)*i+QC.shape[0],np.size(QC,1)*i:np.size(QC,1)*i+QC.shape[1]]=QC
Rdb[np.size(R,0)*i:np.size(R,0)*i+R.shape[0],np.size(R,1)*i:np.size(R,1)*i+R.shape[1]]=R
for j in range(0,hz):
if j<=i:
Cdb[np.size(B_aug,0)*i:np.size(B_aug,0)*i+B_aug.shape[0],np.size(B_aug,1)*j:np.size(B_aug,1)*j+B_aug.shape[1]]=np.matmul(np.linalg.matrix_power(A_aug,((i+1)-(j+1))),B_aug)
Adc[np.size(A_aug,0)*i:np.size(A_aug,0)*i+A_aug.shape[0],0:0+A_aug.shape[1]]=np.linalg.matrix_power(A_aug,i+1)
Hdb=np.matmul(np.transpose(Cdb),Qdb)
Hdb=np.matmul(Hdb,Cdb)+Rdb
temp=np.matmul(np.transpose(Adc),Qdb)
temp=np.matmul(temp,Cdb)
temp2=np.matmul(-Tdb,Cdb)
Fdbt=np.concatenate((temp,temp2),axis=0)
return Hdb,Fdbt,Cdb,Adc
def open_loop_new_states(self,states,omega_total,U1,U2,U3,U4):
'''この関数は、1 サンプル時間後の新しい状態ベクトルを計算します。'''
# 必要定数の取得
Ix=self.constants['Ix']
Iy=self.constants['Iy']
Iz=self.constants['Iz']
m=self.constants['m']
g=self.constants['g']
Jtp=self.constants['Jtp']
Ts=self.constants['Ts']
# States: [u,v,w,p,q,r,x,y,z,phi,theta,psi]
current_states=states
new_states=current_states
u = current_states[0]
v = current_states[1]
w = current_states[2]
p = current_states[3]
q = current_states[4]
r = current_states[5]
x = current_states[6]
y = current_states[7]
z = current_states[8]
phi = current_states[9]
theta = current_states[10]
psi = current_states[11]
sub_loop=self.constants['sub_loop'] # Ts/5
states_ani=np.zeros((sub_loop,6))
U_ani=np.zeros((sub_loop,4))
# 抗力:
drag_switch=self.constants['drag_switch']
C_D_u=self.constants['C_D_u']
C_D_v=self.constants['C_D_v']
C_D_w=self.constants['C_D_w']
A_u=self.constants['A_u']
A_v=self.constants['A_v']
A_w=self.constants['A_w']
rho=self.constants['rho']
# ルンゲ=クッタ法
u_or=u
v_or=v
w_or=w
p_or=p
q_or=q
r_or=r
x_or=x
y_or=y
z_or=z
phi_or=phi
theta_or=theta
psi_or=psi
Ts_pos=2
for j in range(0,4):
if drag_switch==1:
Fd_u=0.5*C_D_u*rho*u**2*A_u
Fd_v=0.5*C_D_v*rho*v**2*A_v
Fd_w=0.5*C_D_w*rho*w**2*A_w
elif drag_switch==0:
Fd_u=0
Fd_v=0
Fd_w=0
else:
print("init 関数では、drag_switch 変数は 0 または 1 でなければなりません")
exit()
# print(u)
# print(v)
# 傾き k_x
u_dot=(v*r-w*q)+g*np.sin(theta)-Fd_u/m
v_dot=(w*p-u*r)-g*np.cos(theta)*np.sin(phi)-Fd_v/m
w_dot=(u*q-v*p)-g*np.cos(theta)*np.cos(phi)+U1/m-Fd_w/m
p_dot=q*r*(Iy-Iz)/Ix-Jtp/Ix*q*omega_total+U2/Ix
q_dot=p*r*(Iz-Ix)/Iy+Jtp/Iy*p*omega_total+U3/Iy
r_dot=p*q*(Ix-Iy)/Iz+U4/Iz
# 慣性系の状態を取得する
# u,v,w with x_dot,y_dot,z_dot の回転行列
R_x=np.array([[1, 0, 0],[0, np.cos(phi), -np.sin(phi)],[0, np.sin(phi), np.cos(phi)]])
R_y=np.array([[np.cos(theta),0,np.sin(theta)],[0,1,0],[-np.sin(theta),0,np.cos(theta)]])
R_z=np.array([[np.cos(psi),-np.sin(psi),0],[np.sin(psi),np.cos(psi),0],[0,0,1]])
R_matrix=np.matmul(R_z,np.matmul(R_y,R_x))
pos_vel_body=np.array([[u],[v],[w]])
pos_vel_fixed=np.matmul(R_matrix,pos_vel_body)
x_dot=pos_vel_fixed[0]
y_dot=pos_vel_fixed[1]
z_dot=pos_vel_fixed[2]
x_dot=x_dot[0]
y_dot=y_dot[0]
z_dot=z_dot[0]
# phi_dot、theta_dot、psi_dot を取得するには、転置行列が必要です
# p、q、r と phi_dot、theta_dot、psi_dot を関連付ける変換行列
T_matrix=np.array([[1,np.sin(phi)*np.tan(theta),np.cos(phi)*np.tan(theta)],\
[0,np.cos(phi),-np.sin(phi)],\
[0,np.sin(phi)/np.cos(theta),np.cos(phi)/np.cos(theta)]])
rot_vel_body=np.array([[p],[q],[r]])
rot_vel_fixed=np.matmul(T_matrix,rot_vel_body)
phi_dot=rot_vel_fixed[0]
theta_dot=rot_vel_fixed[1]
psi_dot=rot_vel_fixed[2]
phi_dot=phi_dot[0]
theta_dot=theta_dot[0]
psi_dot=psi_dot[0]
# 傾きを保存
if j == 0:
u_dot_k1=u_dot
v_dot_k1=v_dot
w_dot_k1=w_dot
p_dot_k1=p_dot
q_dot_k1=q_dot
r_dot_k1=r_dot
x_dot_k1=x_dot
y_dot_k1=y_dot
z_dot_k1=z_dot
phi_dot_k1=phi_dot
theta_dot_k1=theta_dot
psi_dot_k1=psi_dot
elif j == 1:
u_dot_k2=u_dot
v_dot_k2=v_dot
w_dot_k2=w_dot
p_dot_k2=p_dot
q_dot_k2=q_dot
r_dot_k2=r_dot
x_dot_k2=x_dot
y_dot_k2=y_dot
z_dot_k2=z_dot
phi_dot_k2=phi_dot
theta_dot_k2=theta_dot
psi_dot_k2=psi_dot
elif j == 2:
u_dot_k3=u_dot
v_dot_k3=v_dot
w_dot_k3=w_dot
p_dot_k3=p_dot
q_dot_k3=q_dot
r_dot_k3=r_dot
x_dot_k3=x_dot
y_dot_k3=y_dot
z_dot_k3=z_dot
phi_dot_k3=phi_dot
theta_dot_k3=theta_dot
psi_dot_k3=psi_dot
Ts_pos=1
else:
u_dot_k4=u_dot
v_dot_k4=v_dot
w_dot_k4=w_dot
p_dot_k4=p_dot
q_dot_k4=q_dot
r_dot_k4=r_dot
x_dot_k4=x_dot
y_dot_k4=y_dot
z_dot_k4=z_dot
phi_dot_k4=phi_dot
theta_dot_k4=theta_dot
psi_dot_k4=psi_dot
if j<3:
# k_x を使用した新しい状態
u=u_or+u_dot*Ts/Ts_pos
v=v_or+v_dot*Ts/Ts_pos
w=w_or+w_dot*Ts/Ts_pos
p=p_or+p_dot*Ts/Ts_pos
q=q_or+q_dot*Ts/Ts_pos
r=r_or+r_dot*Ts/Ts_pos
x=x_or+x_dot*Ts/Ts_pos
y=y_or+y_dot*Ts/Ts_pos
z=z_or+z_dot*Ts/Ts_pos
phi=phi_or+phi_dot*Ts/Ts_pos
theta=theta_or+theta_dot*Ts/Ts_pos
psi=psi_or+psi_dot*Ts/Ts_pos
else:
# 平均 k_x を使用した新しい状態
u=u_or+1/6*(u_dot_k1+2*u_dot_k2+2*u_dot_k3+u_dot_k4)*Ts
v=v_or+1/6*(v_dot_k1+2*v_dot_k2+2*v_dot_k3+v_dot_k4)*Ts
w=w_or+1/6*(w_dot_k1+2*w_dot_k2+2*w_dot_k3+w_dot_k4)*Ts
p=p_or+1/6*(p_dot_k1+2*p_dot_k2+2*p_dot_k3+p_dot_k4)*Ts
q=q_or+1/6*(q_dot_k1+2*q_dot_k2+2*q_dot_k3+q_dot_k4)*Ts
r=r_or+1/6*(r_dot_k1+2*r_dot_k2+2*r_dot_k3+r_dot_k4)*Ts
x=x_or+1/6*(x_dot_k1+2*x_dot_k2+2*x_dot_k3+x_dot_k4)*Ts
y=y_or+1/6*(y_dot_k1+2*y_dot_k2+2*y_dot_k3+y_dot_k4)*Ts
z=z_or+1/6*(z_dot_k1+2*z_dot_k2+2*z_dot_k3+z_dot_k4)*Ts
phi=phi_or+1/6*(phi_dot_k1+2*phi_dot_k2+2*phi_dot_k3+phi_dot_k4)*Ts
theta=theta_or+1/6*(theta_dot_k1+2*theta_dot_k2+2*theta_dot_k3+theta_dot_k4)*Ts
psi=psi_or+1/6*(psi_dot_k1+2*psi_dot_k2+2*psi_dot_k3+psi_dot_k4)*Ts
for k in range(0,sub_loop):
states_ani[k,0]=x_or+(x-x_or)/Ts*(k/(sub_loop-1))*Ts
states_ani[k,1]=y_or+(y-y_or)/Ts*(k/(sub_loop-1))*Ts
states_ani[k,2]=z_or+(z-z_or)/Ts*(k/(sub_loop-1))*Ts
states_ani[k,3]=phi_or+(phi-phi_or)/Ts*(k/(sub_loop-1))*Ts
states_ani[k,4]=theta_or+(theta-theta_or)/Ts*(k/(sub_loop-1))*Ts
states_ani[k,5]=psi_or+(psi-psi_or)/Ts*(k/(sub_loop-1))*Ts
U_ani[:,0]=U1
U_ani[:,1]=U2
U_ani[:,2]=U3
U_ani[:,3]=U4
# ルンゲ・クッタ法ここまで
# 次の状態を取得します
new_states[0]=u
new_states[1]=v
new_states[2]=w
new_states[3]=p
new_states[4]=q
new_states[5]=r
new_states[6]=x
new_states[7]=y
new_states[8]=z
new_states[9]=phi
new_states[10]=theta
new_states[11]=psi
return new_states, states_ani, U_ani