-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDW1000andIMU.py
1002 lines (859 loc) · 30.5 KB
/
DW1000andIMU.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
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import DW1000
import monotonic
import DW1000Constants as C
import RPi.GPIO as GPIO
import time
import numpy as np
import math
import os
import sys, getopt
sys.path.append('.')
import RTIMU
import os.path
import operator
import socket
import threading
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import pylab
import Adafruit_CharLCD as LCD
##############test################
gyro_count=0
gyro_array=[0]*1000
LS_x_array=[0]*500
LS_y_array=[0]*500
x_array=[0]*500
y_array=[0]*500
x_accel=[0]*500
y_accel=[0]*500
Grid_count=[0]*500
now_time=[0]*500
time_count=[0]*500
x_position_error=[0]*500
y_position_error=[0]*500
start=0
#n_ekf=0
#n_ekf_start=0
# Raspberry Pi pin configuration:
lcd_rs = 6 # Note this might need to be changed to 21 for older revision Pi's.
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
# Alternatively specify a 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows)
##### distance count of each Tag #####
Grid=[0]*1
n_23=0
n_24=0
n_25=0
n_26=0
n_27=0
n_29=0
######################################
###### 2D Grid_1_Tag_position #####
X_2D_1=np.array([7.65,0.13,0,0.9,0,5.64]).reshape([3,2])
Y_2D=(4.65,6.75)
#########################
###### 2D Grid_2_Tag_position #####
X_2D_2=np.array([0,5.64,0.17,11.8,7.78,11.7]).reshape([3,2])
#Y_2D=(4.65,6.75)
#########################
###### 3D Tag position #####
X=np.array([7.65,0.13,2.7,0,0.9,2.75,0,5.64,2.64,0.17,11.8,2.72,7.78,11.7,2.5,11.09,5.48,2.73]).reshape([6,3])
Y=(0,5.64,2.73)
##### Flag #####
DistanceFinish_Flag=0
Position_Flag=0
Same_tag_flag=0
###############
###### Anchor variable #####
lastActivity = 0
expectedMsgId = C.POLL
protocolFailed = False
sentAck = False
receivedAck = False
LEN_DATA =25
data = [0] * LEN_DATA
LEN_TAG=6
tag=[0]*LEN_TAG
timePollAckSentTS = 0
timePollAckReceivedTS = 0
timePollReceivedTS = 0
timeRangeReceivedTS = 0
timePollSentTS = 0
timeRangeSentTS = 0
timeComputedRangeTS = 0
REPLY_DELAY_TIME_US = 7000
##########################
######## TAG #########
lastPoll = 0
expectedMsgID = C.POLL_ACK
POLL_RANGE_FREQ = 1000 # the distance between the tag and the anchor will be estimated every second.
TimePollSentTS = 0
TimeRangeSentTS = 0
TimePollAckReceivedTS = 0
######################
###### imu ######
IMU_IP = "127.0.0.2"
IMU_PORT = 5005
MON_IP = "127.0.0.5"
MON_PORT = 5005
SETTINGS_FILE = "RTIMULib"
s = RTIMU.Settings(SETTINGS_FILE)
imu = RTIMU.RTIMU(s)
# offsets
yawoff = 0.0
pitchoff = 0.0
rolloff = 0.0
# timers
t_print = time.time()
t_damp = time.time()
t_fail = time.time()
t_fail_timer = 0.0
t_shutdown = 0
if (not imu.IMUInit()):
hack = time.time()
imu_sentence = "$IIXDR,IMU_FAILED_TO_INITIALIZE*7C"
if (hack - t_print) > 1.0:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(imu_sentence, (IMU_IP, IMU_PORT))
t_print = hack
t_shutdown += 1
if t_shutdown > 9:
sys.exit(1)
imu.setSlerpPower(0.02)
imu.setGyroEnable(True)
imu.setAccelEnable(True)
imu.setCompassEnable(True)
poll_interval = imu.IMUGetPollInterval()
f = open('mag', 'r')
magnetic_deviation = float(f.readline())
f.close()
###################################
####### EKF #######
r2d = (180/math.pi)
d2r = (math.pi/180)
threshold_c =0.55
g =9.8
scale_factor_err = -0.0512
bx0=1*0.1*g
by0=-0.1*g
bz0=-4.97*d2r
xverr = 0.1
yverr = -0.1
xperr = 0.5
yperr = -0.5
xaerr = 0
yaerr = 0
psierr = 1
err_factor =1
radiosensor_err_factor = 1.0
sig_x_r=radiosensor_err_factor*0.1
sig_y_r=radiosensor_err_factor*0.1
sig_xr=err_factor*0.1*9.8/3600
sig_yr=err_factor*0.1*9.8/3600
sig_bx=err_factor*0.1*9.8
sig_by=err_factor*0.1*9.8
sig_arw_0 = 1*0.02
sig_rrw_0 = 0.02/3600
psi_true=0
psi=[0]*5
Imu=[0]*5
dwm=[0]*5
state = 1
select = 1
select_d = 1
u = 0
a=[0]*3
d=[0]*4
d_1=[0]*4
a_1=[0]*3
axm_h_1=0
aym_h_1=0
wzm_h_1=0
psi_h_1=0
bx_h_1=0
by_h_1=0
bz_h_1=0
xvm_Nh_1=0
yvm_Nh_1=0
xpm_Nh_1=0
ypm_Nh_1=0
xam_Nh_1=0
yam_Nh_1=0
xvm_Nh = 0
yvm_Nh = 0
xam_Nh = 0
yam_Nh = 0
psi_h = 0
bx_h = 0
by_h = 0
bz_h = 0
axm_h = 0
aym_h = 0
wzm_h = 0
R1m_h = 0
R2m_h = 0
R3m_h = 0
R4m_h = 0
xpm_Nh = 0
ypm_Nh = 0
xpm_Nh_2 = 0
ypm_Nh_2 = 0
zpm_Nh_2 = 0
end_count = 0
dt =0.005
EKF_Solution_Anc=[0]*2
def Grid_1_spread_2D():
global Solution_Anc
######Tag-------Anchor-------Distance#####
r0=((X_2D_1[0,0]-Y_2D[0])**2 + (X_2D_1[0,1]-Y_2D[1])**2)**(0.5) #26
r1=((X_2D_1[1,0]-Y_2D[0])**2 + (X_2D_1[1,1]-Y_2D[1])**2)**(0.5) #23
r2=((X_2D_1[2,0]-Y_2D[0])**2 + (X_2D_1[2,1]-Y_2D[1])**2)**(0.5) #24
A=np.array([2*(X_2D_1[0,0]-X_2D_1[1,0]),2*(X_2D_1[0,1]-X_2D_1[1,1]),2*(X_2D_1[0,0]-X_2D_1[2,0]),2*(X_2D_1[0,1]-X_2D_1[2,1])])
A=A.reshape([2,2])
b=np.array([tag[0]**2-tag[2]**2+X_2D_1[0,0]**2-X_2D_1[1,0]**2+X_2D_1[0,1]**2-X_2D_1[1,1]**2,tag[4]**2-tag[2]**2+X_2D_1[0,0]**2-X_2D_1[2,0]**2+X_2D_1[0,1]**2-X_2D_1[2,1]**2])
B=np.linalg.inv(A)
Solution_Anc=B.dot(b)
#print("Real_Position:%.2f %.2f "%(Y_2D[0],Y_2D[1]))
print("2D_Position:%.2f %.2f "%(Solution_Anc[0],Solution_Anc[1]))
def Grid_2_spread_2D():
global Solution_Anc
######Tag-------Anchor-------Distance#####
r0=((X_2D_2[0,0]-Y_2D[0])**2 + (X_2D_2[0,1]-Y_2D[1])**2)**(0.5) #24
r1=((X_2D_2[1,0]-Y_2D[0])**2 + (X_2D_2[1,1]-Y_2D[1])**2)**(0.5) #25
r2=((X_2D_2[2,0]-Y_2D[0])**2 + (X_2D_2[2,1]-Y_2D[1])**2)**(0.5) #27
A=np.array([2*(X_2D_2[0,0]-X_2D_2[1,0]),2*(X_2D_2[0,1]-X_2D_2[1,1]),2*(X_2D_2[0,0]-X_2D_2[2,0]),2*(X_2D_2[0,1]-X_2D_2[2,1])])
A=A.reshape([2,2])
b=np.array([tag[1]**2-tag[4]**2+X_2D_2[0,0]**2-X_2D_2[1,0]**2+X_2D_2[0,1]**2-X_2D_2[1,1]**2,tag[3]**2-tag[4]**2+X_2D_2[0,0]**2-X_2D_2[2,0]**2+X_2D_2[0,1]**2-X_2D_2[2,1]**2])
B=np.linalg.inv(A)
Solution_Anc=B.dot(b)
#print("Real_Position:%.2f %.2f "%(Y_2D[0],Y_2D[1]))
print("2D_Position:%.2f %.2f "%(Solution_Anc[0],Solution_Anc[1]))
def EKF_start():
EKF_message()
#t1=threading.Thread(target=EKF_message)
#t1.start()
def EKF_message():
global P00_z,tag,fusionPose,Gyro
EKF_New()
Imu[0]=fusionPose[0]
Imu[1]=-fusionPose[1]
Imu[2]=Gyro[2]
if sum([tag[2],tag[0],tag[4]]) < sum([tag[4],tag[1],tag[3]]):
dwm[0]=tag[2]
dwm[1]=tag[0]
dwm[2]=tag[4]
dwm[3]=tag[5]
else:
dwm[0]=tag[4]
dwm[1]=tag[1]
dwm[2]=tag[3]
dwm[3]=tag[5]
t = time.time()
print ("start",int(round(t * 1000)))
EKF_Update()
print ("End",int(round(t * 1000)))
time.sleep(0.01)
"""
def PSI_message():
if state ==2:
if psi_h*180/3.14 <0:
psi_true=-(psi_h*180/3.14)
psi_true=(psi_true%360)
else:
psi_true = (psi_h*(180/3.14))
psi_true =-(psi_true%360)
psi[1]=psi_true
##$$$$$$$$$$$$$$$$$##
time.sleep(0.1)
"""
def EKF_New():
global P00_z,R,F_z,Q_z,Xz_h
P00_z = np.zeros([8,8])
R=np.zeros([4,4])
H=np.zeros([4,8])
Xz_h=np.zeros([8,1])
tmp=np.zeros([8,8])
tmp_1=np.zeros([8,8])
tmpYX=np.zeros([1,8])
##### covariance matrix(P00_z=phi_z*P00_z*(phi_z')+Q_z*dt) #####
P00_z[0,0] = xperr**2
P00_z[2,2] = 1*bx0**2
P00_z[3,3] = yperr**2
P00_z[5,5] = 1*by0**2
P00_z[6,6] = (1*psierr*d2r)**2
P00_z[7,7] =100* bz0**2
##### Xz_h(xz_h=phi_z*xz_h) #####
Xz_h[7,0] = bz0
##### Q matrix -predict(P00_z=phi_z*P00_z*(phi_z')+Q_z*dt) #####
Q_z=np.zeros([8,8])
##### R matrix -measurement(H*P00_z*H'+R) #####
R=np.array([sig_x_r**2,0,0,0,0,sig_y_r**2,0,0,0,0,sig_x_r**2,0,0,0,0,sig_y_r**2])*100
R=R.reshape([4,4])
##### Kalman Filter Gain(K_z = P00_z*H'/(H*P00_z*H'+R)) #####
K_z=np.zeros([8,4])
##### Measurement covariance update #####
S=np.zeros([4,4])
##### F matrix(P = (I+F*dt)*P*(I+F*dt)' + Q) #####
F_z=np.zeros([8,8])
H_D=np.zeros([4,8])
def EKF_Update():
##### IMU_MPU9250 #####
global Grid,n_ekf,dwm,Imu,state,xpm_Nh,ypm_Nh,xvm_Nh,yvm_Nh,xam_Nh,yam_Nh,wzm_h,bx_h,by_h,bz_h,psi_h,end_count,P00_z,tmp,tmp_1,tmp_YX,u,select,count1,select_d,EKF_Solution_Anc,R,Xz_h,F_z,Q_z,H_D,bx_h_1,by_h_1,bz_h_1,psi_h_1,xpm_Nh_1,ypm_Nh_1,xvm_Nh_1,yvm_Nh_1,xam_Nh_1,yam_Nh_1
a[0]=Imu[0] #acc_x
a[1]=Imu[1] #acc_y
a[2]=Imu[2] #gyro_z
d[0]=dwm[0] #distance1
d[1]=dwm[1] #distance2
d[2]=dwm[2] #distance3
d[3]=dwm[3] #distance4
for i in range(0,4):
if d[i]>150000:
d[i]=d_1[i]
if sum([tag[2],tag[0],tag[4]]) < sum([tag[4],tag[1],tag[3]]):
Grid_1_spread_2D()
Grid[0]=1
else:
Grid_2_spread_2D()
Grid[0]=2
xpm_Nh_2=Solution_Anc[0]
ypm_Nh_2=Solution_Anc[1]
if state == 1:
bz_h = bz0
if d[0]>0 and d[1]>0 and d[2]>0 and d[3]>0:
xpm_Nh = xpm_Nh_2
ypm_Nh = ypm_Nh_2
xpm_Nh_1 = xpm_Nh
ypm_Nh_1 = ypm_Nh
state = 2
if state == 2:
for j in range(0,2):
bx_h = bx_h_1
by_h = by_h_1
bz_h = bz_h_1
axm_h = a[0]-bx_h #acc_x
aym_h = a[1]-by_h #acc_y
axm_h_1 = a_1[0]- bx_h_1
aym_h_1 = a_1[1]- by_h_1
wzm_h_1 = (1-scale_factor_err)*(a_1[2] - bz_h_1)
wzm_h = (1-scale_factor_err)*(a[2] - bz_h_1)
psi_h = psi_h_1 +(wzm_h_1+wzm_h)*dt/2.0
xam_Nh = np.cos(psi_h_1)*axm_h_1 - np.sin(psi_h_1)*aym_h_1 - wzm_h_1*yvm_Nh_1
yam_Nh = np.sin(psi_h_1)*axm_h_1 + np.cos(psi_h_1)*aym_h_1 + wzm_h_1*xvm_Nh_1
xvm_Nh = xvm_Nh_1 + (xam_Nh+xam_Nh_1)*dt/2
yvm_Nh = yvm_Nh_1 + (yam_Nh+yam_Nh_1)*dt/2
xpm_Nh = xpm_Nh_1 + (xvm_Nh+xvm_Nh_1)*dt/2
ypm_Nh = ypm_Nh_1 + (yvm_Nh+yvm_Nh_1)*dt/2
F_z[0,1] = 1
F_z[1,2] = -np.cos(psi_h_1)
F_z[1,4] = -wzm_h_1
F_z[1,5] = np.sin(psi_h_1)
F_z[1,6] = 0*(-np.sin(psi_h_1)*(axm_h_1)-np.cos(psi_h_1)*(aym_h_1))
F_z[1,7] = 0*(yvm_Nh_1)
F_z[3,4] = 1
F_z[4,1] = wzm_h_1
F_z[4,2] = -np.sin(psi_h_1)
F_z[4,5] = -np.cos(psi_h_1)
F_z[4,6] = 0*(np.cos(psi_h_1)*(axm_h_1)-np.sin(psi_h_1)*(aym_h_1))
F_z[4,7] = 0*(-xvm_Nh_1)
F_z[6,7]= -1
I=np.eye(8)
phi_z = I+(F_z*dt)
Q_z[1,1] = (np.cos(psi_h_1))*(np.cos(psi_h_1))*(sig_bx**2) + (np.sin(psi_h_1))*(np.sin(psi_h_1))*(sig_by**2) + (yvm_Nh_1*yvm_Nh_1)*(sig_arw_0**2)
Q_z[2,2] = sig_xr**2
Q_z[4,4] = (np.sin(psi_h_1))*(np.sin(psi_h_1))*(sig_bx**2) + (np.cos(psi_h_1))*(np.cos(psi_h_1))*(sig_by**2) + (xvm_Nh_1*xvm_Nh_1)*(sig_arw_0**2)
Q_z[5,5] = sig_yr**2
Q_z[6,6] = 1*sig_arw_0**2
Q_z[7,7] = 1*sig_rrw_0**2
Xz_h=phi_z.dot(Xz_h)
if select==1:
tmp_1=phi_z.dot(P00_z)
else:
tmp_1=phi_z.dot(tmp)
tmp=tmp_1.dot(phi_z.T)+Q_z*dt
if select ==1:
a[0]=Imu[0] #acc_x
a[1]=Imu[1] #acc_y
a[2]=Imu[2] #gyro_z
a_1[0]=a[0]
a_1[1]=a[1]
a_1[2]=a[2]
bx_h_1 = bx_h
by_h_1 = by_h
bz_h_1 = bz_h
xam_Nh_1 = xam_Nh
yam_Nh_1 = yam_Nh
xvm_Nh_1 = xvm_Nh
yvm_Nh_1 = yvm_Nh
xpm_Nh_1 = xpm_Nh
ypm_Nh_1 = ypm_Nh
psi_h_1 = psi_h
wzm_h_1 = wzm_h
u = u +1
select = 2
###### IMU Estimate four Distances #####
if Grid[0]==1:
print("---------Grid_1----------------")
R1m_h = ((X[0,0]-xpm_Nh)**2+(X[0,1]-ypm_Nh)**2+(X[0,2]-1.32)**2)**(0.5)
R2m_h = ((X[1,0]-xpm_Nh)**2+(X[1,1]-ypm_Nh)**2+(X[1,2]-1.32)**2)**(0.5)
R3m_h = ((X[2,0]-xpm_Nh)**2+(X[2,1]-ypm_Nh)**2+(X[2,2]-1.32)**2)**(0.5)
R4m_h = ((X[5,0]-xpm_Nh)**2+(X[5,1]-ypm_Nh)**2+(X[5,2]-1.32)**2)**(0.5)
##### H Matrix Residual Calculator #####
r1_partial_x =-(X[0,0]-xpm_Nh)/R1m_h
r1_partial_y =-(X[0,1]-ypm_Nh)/R1m_h
r2_partial_x =-(X[1,0]-xpm_Nh)/R2m_h
r2_partial_y =-(X[1,1]-ypm_Nh)/R2m_h
r3_partial_x =-(X[2,0]-xpm_Nh)/R3m_h
r3_partial_y =-(X[2,1]-ypm_Nh)/R3m_h
r4_partial_x =-(X[5,0]-xpm_Nh)/R4m_h
r4_partial_y =-(X[5,1]-ypm_Nh)/R4m_h
if Grid[0]==2:
print("---------Grid_2----------------")
R1m_h = ((X[2,0]-xpm_Nh)**2+(X[2,1]-ypm_Nh)**2+(X[2,2]-1.32)**2)**(0.5)
R2m_h = ((X[3,0]-xpm_Nh)**2+(X[3,1]-ypm_Nh)**2+(X[3,2]-1.32)**2)**(0.5)
R3m_h = ((X[4,0]-xpm_Nh)**2+(X[4,1]-ypm_Nh)**2+(X[4,2]-1.32)**2)**(0.5)
R4m_h = ((X[5,0]-xpm_Nh)**2+(X[5,1]-ypm_Nh)**2+(X[5,2]-1.32)**2)**(0.5)
##### H Matrix Residual Calculator #####
r1_partial_x =-(X[2,0]-xpm_Nh)/R1m_h
r1_partial_y =-(X[2,1]-ypm_Nh)/R1m_h
r2_partial_x =-(X[3,0]-xpm_Nh)/R2m_h
r2_partial_y =-(X[3,1]-ypm_Nh)/R2m_h
r3_partial_x =-(X[4,0]-xpm_Nh)/R3m_h
r3_partial_y =-(X[4,1]-ypm_Nh)/R3m_h
r4_partial_x =-(X[5,0]-xpm_Nh)/R4m_h
r4_partial_y =-(X[5,1]-ypm_Nh)/R4m_h
##### H Matrix Data #####
H=np.array([r1_partial_x,0,0,r1_partial_y,0,0,0,0,r2_partial_x,0,0,r2_partial_y,0,0,0,0,r3_partial_x,0,0,r3_partial_y,0,0,0,0,r4_partial_x,0,0,r4_partial_y,0,0,0,0])
#H=np.array([r1_partial_x,0,0,r1_partial_y,0,0,0,0,r2_partial_x,0,0,r2_partial_y,0,0,0,0,r3_partial_x,0,0,r3_partial_y,0,0,0,0,0,0,0,0,0,0,0,0])
H=H.reshape([4,8])
###### Kalman_Filter_update_8_4_radio ######
zxm_z=[0]*4
##### Real Distance - Esitmate Distance #####
zxm_z[0] = d[0]-R1m_h
zxm_z[1] = d[1]-R2m_h
zxm_z[2] = d[2]-R3m_h
zxm_z[3] = d[3]-R4m_h
#zxm_z[3] = 0
##### Mu Martix Data #####
Y=np.array([zxm_z[0],zxm_z[1],zxm_z[2],zxm_z[3]])
k=0
l=0
z_update=np.zeros([8,1])
H_D=np.zeros([1,8])
K_z_help=[0]*1
tmpXY=np.zeros([8,1])
Mu_z=Y.reshape([4,1])
K_z=np.zeros([8,1])
I=np.eye(8)
n_h=0
for i in range(0,4):
if end_count> 15 :
if (zxm_z[i]*zxm_z[i])**0.5< threshold_c:
for j in range(0,8):
H_D[0,j]=H[i,j]
if select_d==1:
tmpYX=H_D.dot(tmp)
else:
tmp=P00_z.dot(I)
tmpYX=H_D.dot(tmp)
K_z_help=tmpYX.dot(H_D.T)
K_z_help[0]=1/(K_z_help[0]+R[i,i])
tmpXY=tmp.dot(H_D.T)
K_z=tmpXY*(K_z_help)
P00_z=(I-K_z.dot(H_D)).dot(tmp)
else:
for j in range(0,8):
H_D[0,j]=H[i,j]
K_z=np.zeros([8,1])
P00_z=(I-K_z.dot(H_D)).dot(tmp)
count1 = count1+1
else:
for j in range(0,8):
H_D[0,j]=H[i,j]
if select_d==1:
tmpYX=H_D.dot(tmp)
else:
n_h=n_h+1
tmp=P00_z.dot(I)
tmpYX=H_D.dot(tmp)
K_z_help=tmpYX.dot(H_D.T)
K_z_help[0]=1/(K_z_help[0]+R[i,i])
tmpXY=tmp.dot(H_D.T)
K_z=tmpXY*(K_z_help)
P00_z=(I-K_z.dot(H_D)).dot(tmp)
z_update=K_z*Y[i]+z_update
select_d = 0
#print(z_update)
xpm_Nh=xpm_Nh+z_update[0,0]
xvm_Nh=xvm_Nh+z_update[1,0]
bx_h = bx_h + z_update[2,0]
ypm_Nh=ypm_Nh+z_update[3,0]
yvm_Nh=yvm_Nh+z_update[4,0]
by_h= by_h + z_update[5,0]
psi_h=psi_h+z_update[6,0]
bz_h=bz_h+z_update[7,0]
Xz_h=np.zeros([8,1])
a_1[0] = a[0]
a_1[1] = a[1]
a_1[2] = a[2]
bx_h_1 = bx_h
by_h_1 = by_h
bz_h_1 = bz_h
xam_Nh_1 = xam_Nh
yam_Nh_1 = yam_Nh
xvm_Nh_1 = xvm_Nh
yvm_Nh_1 = yvm_Nh
xpm_Nh_1 = xpm_Nh
ypm_Nh_1 = ypm_Nh
psi_h_1 = psi_h
wzm_h_1 = wzm_h
d_1[0]=d[0]#distance1
d_1[1]=d[1]#distance2
d_1[2]=d[2]#distance3
d_1[3]=d[3]#distance4
u= u+1
select = 1
select_d = 1
count1 = 0
EKF_Solution_Anc[0]=xpm_Nh
EKF_Solution_Anc[1]=ypm_Nh
print("EKF_Position:%.2f %.2f "%(EKF_Solution_Anc[0],EKF_Solution_Anc[1]))
#print("Accel_x:%.2f Accel_y:%.2F"%(fusionPose[0],-fusionPose[1]))
t = time.time()
nowTime = lambda:int(round(t * 1000))
nowTime=np.array([nowTime()])
#print(nowTime[0])
now_time[end_count]=nowTime[0]
x_array[end_count]=EKF_Solution_Anc[0]
y_array[end_count]=EKF_Solution_Anc[1]
LS_x_array[end_count]=Solution_Anc[0]
LS_y_array[end_count]=Solution_Anc[1]
x_accel[end_count]=fusionPose[0]
y_accel[end_count]=-fusionPose[1]
x_position_error[0]=0
y_position_error[0]=0
time_count[0]=0
if end_count>=1:
x_position_error[end_count]=x_array[end_count]-x_array[end_count-1]
y_position_error[end_count]=y_array[end_count]-y_array[end_count-1]
time_count[end_count]=now_time[end_count]-now_time[end_count-1]
#print(time_count[end_count])
#Grid_count[end_count]=Grid[0]
lcd_show="X:%.2f"%(float(x_array[end_count]))+" m" + '\n' + "Y:%.2f"%(float(y_array[end_count]))+" m"
lcd.clear()
lcd.message(lcd_show)
end_count = end_count+1
#print(psi_h)
#####-----plot------#####
#pylab.scatter(EKF_Solution_Anc[0],EKF_Solution_Anc[1],s=0.5)
#pylab.scatter(end_count,Gyro[2])
#pylab.xlim(4.8,5)
#pylab.ylim(6.5,6.7)
#pylab.xlabel("x-axis")
#pylab.ylabel("y-axis")
#pylab.title("EKF Position",fontsize=24)
if end_count==116:
print("--------------------------------------------------save-------------------")
np.savetxt('EKF_data_KNN.csv',(x_array,y_array,x_position_error,y_position_error,time_count,x_accel,y_accel) , delimiter=',')
#np.savetxt('EKF_data_grid.csv',(x_array,y_array,Grid_count) , delimiter=',')
#np.savetxt('LS_data_straight.csv',(LS_x_array,LS_y_array) , delimiter=',')
#np.savetxt('data_straight.csv',(x_array,y_array) , delimiter=',')
#pylab.savefig('ekf_sucess.png')
def Tag_resetInactive():
"""
This function restarts the default polling operation when the device is deemed inactive.
"""
global expectedMsgID
print("Reset inactive")
expectedMsgID = C.POLL_ACK
transmitPoll()
noteActivity()
def transmitPoll():
"""
This function sends the polling message which is the first transaction to enable ranging functionalities.
It checks if an anchor is operational.
"""
global data, lastPoll
while (millis() - lastPoll < POLL_RANGE_FREQ):
pass
DW1000.newTransmit()
data[0] = C.POLL
data[16] = 28
DW1000.setData(data, LEN_DATA)
DW1000.startTransmit()
lastPoll = millis()
def transmitRange():
"""
This function sends the range message containing the timestamps used to calculate the range between the devices.
"""
global data, timeRangeSentTS
DW1000.newTransmit()
data[0] = C.RANGE
timeRangeSentTS = DW1000.setDelay(REPLY_DELAY_TIME_US, C.MICROSECONDS)
DW1000.setTimeStamp(data, timePollSentTS, 1)
DW1000.setTimeStamp(data, timePollAckReceivedTS, 6)
DW1000.setTimeStamp(data, timeRangeSentTS, 11)
DW1000.setData(data, LEN_DATA)
DW1000.startTransmit()
################################################
def millis():
"""
This function returns the value (in milliseconds) of a clock which never goes backwards. It detects the inactivity of the chip and
is used to avoid having the chip stuck in an undesirable state.
"""
return int(round(monotonic.monotonic() * C.MILLISECONDS))
def handleSent():
"""
This is a callback called from the module's interrupt handler when a transmission was successful.
It sets the sentAck variable as True so the loop can continue.
"""
global sentAck
sentAck = True
def handleReceived():
"""
This is a callback called from the module's interrupt handler when a reception was successful.
It sets the received receivedAck as True so the loop can continue.
"""
global receivedAck
receivedAck = True
def noteActivity():
"""
This function records the time of the last activity so we can know if the device is inactive or not.
"""
global lastActivity
lastActivity = millis()
def Anchor_resetInactive():
"""
This function restarts the default polling operation when the device is deemed inactive.
"""
global expectedMsgId
expectedMsgId = C.POLL
receiver()
noteActivity()
def transmitPollAck():
"""
This function sends the polling acknowledge message which is used to confirm the reception of the polling message.
"""
global data
DW1000.newTransmit()
data[0] = C.POLL_ACK
DW1000.setDelay(REPLY_DELAY_TIME_US, C.MICROSECONDS)
DW1000.setData(data, LEN_DATA)
DW1000.startTransmit()
def transmitRangeAcknowledge():
"""
This functions sends the range acknowledge message which tells the tag that the ranging function was successful and another ranging transmission can begin.
"""
global data
DW1000.newTransmit()
data[0] = C.RANGE_REPORT
DW1000.setData(data, LEN_DATA)
DW1000.startTransmit()
def transmitRangeFailed():
"""
This functions sends the range failed message which tells the tag that the ranging function has failed and to start another ranging transmission.
"""
global data
DW1000.newTransmit()
data[0] = C.RANGE_FAILED
DW1000.setData(data, LEN_DATA)
DW1000.startTransmit()
def receiver():
"""
This function configures the chip to prepare for a message reception.
"""
global data
DW1000.newReceive()
DW1000.receivePermanently()
DW1000.startReceive()
def computeRangeAsymmetric():
"""
This is the function which calculates the timestamp used to determine the range between the devices.
"""
global timeComputedRangeTS
round1 = DW1000.wrapTimestamp(timePollAckReceivedTS - timePollSentTS)
reply1 = DW1000.wrapTimestamp(timePollAckSentTS - timePollReceivedTS)
round2 = DW1000.wrapTimestamp(timeRangeReceivedTS - timePollAckSentTS)
reply2 = DW1000.wrapTimestamp(timeRangeSentTS - timePollAckReceivedTS)
timeComputedRangeTS = (round1 * round2 - reply1 * reply2) / (round1 + round2 + reply1 + reply2)
def loop():
global gyro_array,gyro_count,sentAck,n_ekf_start,start,n_23,n_24,n_25,n_26,n_27,n_29,receivedAck, timePollAckSentTS, timePollReceivedTS, timePollSentTS, timePollAckReceivedTS, timeRangeReceivedTS, protocolFailed, data, expectedMsgId,expectedMsgID, timeRangeSentTS,Same_tag_flag,DistanceFinish_Flag,Position_Flag,EKF_start,EKF_message,EKF_New,EKF_Update
if Position_Flag==0:
if imu.IMURead():
global fusionPose,Gyro
Data = imu.getIMUData()
fusionPose = Data["accel"]
Gyro = Data["gyro"]
#gyro_array[gyro_count]=Gyro[2]
#gyro_count=gyro_count+1
#print(Gyro[2])
#plt.plot(gyro_count,Gyro[2],'r--')
#if gyro_count==800:
#print("sucess")
#np.savetxt('data.csv',gyro_array , delimiter=',')
#plt.savefig('gyro.png')
#pylab.close()
#print("fffff",fusionPose[0],fusionPose[1])
#print("[%s]"%(time.ctime(time.time())))
#t = time.time()
#nowTime = lambda:int(round(t * 10000))
#print(nowTime)
time.sleep(poll_interval*1.0/1000.0)
time.sleep(0.01)
if (sentAck == False and receivedAck == False):
if ((millis() - lastActivity) > C.RESET_PERIOD):
Anchor_resetInactive()
return
if sentAck:
sentAck = False
msgId = data[0]
if Same_tag_flag == data[16]:
if msgId == C.POLL_ACK:
timePollAckSentTS = DW1000.getTransmitTimestamp()
noteActivity()
if receivedAck:
receivedAck = False
data = DW1000.getData(LEN_DATA)
msgId = data[0]
#print(data[16])
if msgId == C.POLL:
DistanceFinish_Flag =1
Same_tag_flag = data[16]
protocolFailed = False
timePollReceivedTS = DW1000.getReceiveTimestamp()
expectedMsgId = C.RANGE
transmitPollAck()
noteActivity()
elif msgId == C.RANGE :
if (DistanceFinish_Flag == 1 and Same_tag_flag == data[16]):
DistanceFinish_Flag = 0
timeRangeReceivedTS = DW1000.getReceiveTimestamp()
expectedMsgId = C.POLL
if protocolFailed == False:
timePollSentTS = DW1000.getTimeStamp(data, 1)
timePollAckReceivedTS = DW1000.getTimeStamp(data, 6)
timeRangeSentTS = DW1000.getTimeStamp(data, 11)
computeRangeAsymmetric()
transmitRangeAcknowledge()
distance = (timeComputedRangeTS % C.TIME_OVERFLOW) * C.DISTANCE_OF_RADIO
if data[16]==23:
print("Tag: %.2d"%(data[16]))
print("Distance1: %.2f m" %(distance))
#print("[%s]"%(time.ctime(time.time())))
t = time.time()
#print (int(round(t * 1000)))
n_23=n_23+1
if distance <12:
#tag[0]=((n_23-1)*tag[0]+distance)/n_23
tag[0]=distance
#print(n_23)
if data[16]==25:
print("Tag: %.2d"%(data[16]))
print("Distance2: %.2f m" %(distance))
#print("[%s]"%(time.ctime(time.time())))
n_25=n_25+1
if distance <12:
#tag[1]=((n_25-1)*tag[1]+distance)/n_25
tag[1]=distance
if data[16]==26:
print("Tag: %.2d"%(data[16]))
print("Distance3: %.2f m" %(distance))
n_26=n_26+1
if distance <12:
#tag[2]=((n_26-1)*tag[2]+distance)/n_26
tag[2]=distance
if data[16]==27:
print("Tag: %.2d"%(data[16]))
print("Distance4: %.2f m" %(distance))
n_27=n_27+1
if distance <12:
#tag[3]=((n_27-1)*tag[3]+distance)/n_27
tag[3]=distance
if data[16]==24:
print("Tag: %.2d"%(data[16]))
print("Distance4: %.2f m" %(distance))
n_24=n_24+1
if distance <12:
#tag[4]=((n_24-1)*tag[4]+distance)/n_24
tag[4]=distance
if data[16]==29:
print("Tag: %.2d"%(data[16]))
print("Distance4: %.2f m" %(distance))
n_29=n_29+1
if distance <12:
#tag[5]=((n_29-1)*tag[5]+distance)/n_29
tag[5]=distance
if tag[0] !=0 and tag[1]!=0 and tag[2] !=0 and tag[3]!=0 and tag[4]!=0 and tag[5]!=0 and start==0:
EKF_start()
#start=1
#n_ekf_start=n_ekf_start+1
pass
if n_23 >=5 and n_25 >=5 and n_26 >=5 and n_27 >=5:
#print("transmit TAG")
#os.system("python ./DW1000RangingTAG.py")
Position_Flag=0
else:
transmitRangeFailed()
noteActivity()
if Position_Flag==1:
if (sentAck == False and receivedAck == False):
if ((millis() - lastActivity) > C.RESET_PERIOD):
Tag_resetInactive()
return
if sentAck :
sentAck = False
msgID = data[0]
if data[16]==28:
if msgID == C.POLL :
timePollSentTS = DW1000.getTransmitTimestamp()
elif msgID == C.RANGE :
timeRangeSentTS = DW1000.getTransmitTimestamp()
noteActivity()
if receivedAck:
receivedAck = False
data = DW1000.getData(LEN_DATA)
msgID = data[0]
if data[16]==28:
print(data[16])
if (msgID == C.POLL_ACK):
timePollAckReceivedTS = DW1000.getReceiveTimestamp()
expectedMsgID = C.RANGE_REPORT
transmitRange()
noteActivity()
elif msgID == C.RANGE_REPORT:
expectedMsgID = C.POLL_ACK
transmitPoll()
noteActivity()
elif msgID == C.RANGE_FAILED:
expectedMsgID = C.POLL_ACK
transmitPoll()
noteActivity()
time.sleep(0.1)
try:
PIN_RST = 17
PIN_IRQ = 19
PIN_SS = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_RST,GPIO.IN)
DW1000.begin(PIN_IRQ)
DW1000.setup(PIN_SS)
print("DW1000 initialized")
print("############### ANCHOR ##############")
DW1000.generalConfiguration("82:17:5B:D5:A9:9A:E2:9B", C.MODE_LONGDATA_RANGE_ACCURACY)
DW1000.registerCallback("handleSent", handleSent)
DW1000.registerCallback("handleReceived", handleReceived)
DW1000.setAntennaDelay(C.ANTENNA_DELAY_RASPI)
receiver()
noteActivity()
while 1:
loop()
except KeyboardInterrupt: