-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
2317 lines (1836 loc) · 73.5 KB
/
Copy pathmain.py
File metadata and controls
2317 lines (1836 loc) · 73.5 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
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 numpy as np
import csv
import math
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import fsolve
time = []
s1_distance = []
s2_distance = []
s3_distance = []
s4_distance = []
distance = []
distance_total = []
with open('file_1.csv', 'r', newline='') as file:
#reader = csv.reader(file)
reader = csv.reader(x.replace('\0', '') for x in file) # Since the file contains null characters, they are substituted using space ' '
next(reader, None) # Skips the header of the file
for row in reader:
time.append(float(row[0])/1000)
s1_distance.append(float(row[1]))
s2_distance.append(float(row[2]))
s3_distance.append(float(row[3]))
s4_distance.append(float(row[4]))
distance.append(float(row[1]))
distance.append(float(row[2]))
distance.append(float(row[3]))
distance.append(float(row[4]))
distance_total.append(distance)
distance = []
plt.plot(time, s1_distance)
plt.plot(time, s2_distance)
plt.plot(time, s3_distance)
plt.plot(time, s4_distance)
plt.title("sensors vs time - original")
plt.show()
# CREATE DATA FRAME
# ----------------
data = {'time': time, 's1_distance': s1_distance, 's2_distance': s2_distance, 's3_distance': s3_distance, 's4_distance': s4_distance}
df = pd.DataFrame(data, columns=['time', 's1_distance', 's2_distance', 's3_distance', 's4_distance'])
print(df)
# NB must use 0:1 otherwise it does not work
X = df.iloc[:, 0:1].values #take the values in the first column (time) - save them in array X
y = df.iloc[:, 1].values #take the values in the second column (s1_distance) - save them in array y
#df.plot( x= 'time', y = 's1_distance')
# TAKE MOVING AVERAGE
# here can select the number of data to use for the moving average
avg_number = 5
df.rolling(window = avg_number).mean().plot( x= 'time', y = 's1_distance')
df.rolling(window = avg_number).mean().plot( x= 'time', y = 's2_distance')
df.rolling(window = avg_number).mean().plot( x= 'time', y = 's3_distance')
df.rolling(window = avg_number).mean().plot( x= 'time', y = 's4_distance')
plt.show()
# add the new column of the moving avg
df['moving_avg_s1'] = df['s1_distance'].rolling(window=avg_number).mean()
df['moving_avg_s2'] = df['s2_distance'].rolling(window=avg_number).mean()
df['moving_avg_s3'] = df['s3_distance'].rolling(window=avg_number).mean()
df['moving_avg_s4'] = df['s4_distance'].rolling(window=avg_number).mean()
print ("\n\nNEW DATA")
print(df)
# Convert into a list
s1_avg_with_nan = df['moving_avg_s1'].tolist()
s2_avg_with_nan = df['moving_avg_s2'].tolist()
s3_avg_with_nan = df['moving_avg_s3'].tolist()
s4_avg_with_nan = df['moving_avg_s4'].tolist()
print(s1_avg_with_nan)
print("-----------------\n")
# TO REMOVE NAN VALUES FROM THE LISTS:
s1_avg = [x for x in s1_avg_with_nan if math.isnan(x) == False]
s2_avg = [x for x in s2_avg_with_nan if math.isnan(x) == False]
s3_avg = [x for x in s3_avg_with_nan if math.isnan(x) == False]
s4_avg = [x for x in s4_avg_with_nan if math.isnan(x) == False]
print(s1_avg)
# needed because nan values have been removed
for i in range(avg_number-1):
time.pop(0)
s1_distance.pop(0)
s2_distance.pop(0)
s3_distance.pop(0)
s4_distance.pop(0)
plt.plot(time, s1_avg)
plt.plot(time, s1_distance)
plt.plot(time, s2_avg)
plt.plot(time, s2_distance)
plt.plot(time, s3_avg)
plt.plot(time, s3_distance)
plt.plot(time, s4_avg)
plt.plot(time, s4_distance)
plt.show()
# ------------------------------------------
# Find moments of STILLNESS (NO MOVEMENT)
still_indexes = []
sensor_index = []
walking_indexes = []
indicator_1 = 0
indicator_2 = 0
indicator_3 = 0
indicator_4 = 0
still_indicator = 1
i = 0
min_walking_distance = 10
min_still = 5
# INDEXES PERSON NOT MOVING
# -------------------------
while i < (len(s1_avg)-1):
sum_1 = 0
sum_2 = 0
sum_3 = 0
sum_4 = 0
for q in range(min_still):
if (i+q)<len(s1_avg):
sum_1 = sum_1 + s1_avg[i+q]
sum_2 = sum_2 + s2_avg[i+q]
sum_3 = sum_3 + s3_avg[i+q]
sum_4 = sum_4 + s4_avg[i+q]
# Find avg for each sequence of distances
avg_1 = sum_1/min_still
avg_2 = sum_2/min_still
avg_3 = sum_3/min_still
avg_4 = sum_4/min_still
# PERSON NOT MOVING
for q in range(min_still):
if abs(s1_avg[i+q] - avg_1) < 8 and abs(s2_avg[i+q] - avg_2) < 8 and abs(s3_avg[i+q] - avg_3) < 8 and abs(s4_avg[i+q] - avg_4) < 8:
still_indicator = 0
else:
still_indicator=1
break
if still_indicator==0:
for y in range(min_still):
still_indexes.append(i+y)
#print("--", round((time[i] - 1622132000), 3)) # to check with the graph
i=i+(min_still-1)
i = i + 1
still_indicator = 1
# INDEXES PERSON MOVING IN STRAIGHT LINE
# --------------------------------------
i = 0
move_index = []
walking_test_1 = []
walking_test_2 = []
walking_test_3 = []
walking_test_4 = []
indicator = 0
mse_array = []
variation_array = []
# function to calculate the mse of the elements of an array
def mse(a):
b = []
mean = np.mean(a)
for i in range(len(a)):
b.append(abs(a[i]-mean))
mse = np.mean(b)
return mse
# FROM HERE, CHECK MOTION ON STRAIGHT LINE
# -------------------------
while i + min_walking_distance < (len(s1_avg) - 1):
print("\n1\n")
#if i not in still_indexes:
for q in range(min_walking_distance):
if i+q not in still_indexes:
walking_test_1.append(s1_avg[i + q + 1] - s1_avg[i + q])
walking_test_2.append(s2_avg[i + q + 1] - s2_avg[i + q])
walking_test_3.append(s3_avg[i + q + 1] - s3_avg[i + q])
walking_test_4.append(s4_avg[i + q + 1] - s4_avg[i + q])
else:
indicator = 1
# print("\n\indicator: ")
# print(indicator)
print("\n2\n")
if indicator == 0:
print("\n2.5\n")
if mse(walking_test_1)<3:
#print("yessss 1")
mse_array.append(mse(walking_test_1))
else:
mse_array.append(0)
if mse(walking_test_2)<3:
#print("yessss 2")
mse_array.append(mse(walking_test_2))
else:
mse_array.append(0)
if mse(walking_test_3)<3:
#print("yessss 3")
mse_array.append(mse(walking_test_3))
else:
mse_array.append(0)
if mse(walking_test_4)<3:
#print("yessss 4")
mse_array.append(mse(walking_test_4))
else:
mse_array.append(0)
variation_array.append(np.absolute(np.mean(walking_test_1)))
variation_array.append(np.absolute(np.mean(walking_test_2)))
variation_array.append(np.absolute(np.mean(walking_test_3)))
variation_array.append(np.absolute(np.mean(walking_test_4)))
print("\n3\n")
print(variation_array)
while len(move_index) == 0:
sensor_max_var = variation_array.index(max(variation_array))
print("sensor_max_var :")
print(sensor_max_var)
print("mse_array :")
print(mse_array)
# exit()
if mse_array[sensor_max_var] != 0:
move_index.append(sensor_max_var)
print("\n4\n")
#exit()
for t in range(min_walking_distance):
move_index.append(i + t)
i = i + min_walking_distance - 1
else:
variation_array[sensor_max_var] = 0
print("hello")
#exit()
walking_indexes.append(move_index)
move_index = []
variation_array = []
mse_array = []
print("\n5\n")
walking_test_1 = []
walking_test_2 = []
walking_test_3 = []
walking_test_4 = []
indicator = 0
indicator_1 = 0
indicator_2 = 0
indicator_3 = 0
indicator_4 = 0
i = i + 1
print("\nmse array: \n")
print(mse_array)
print("\nvariation array: \n")
print(variation_array)
print("still_indexes: ", still_indexes)
print("walking_indexes: ", walking_indexes)
# walking_index:
# sensor - index - index - index - .....
# sensor - index - index - index - .....
# sensor - index - index - index - .....
# sensor - index - index - index - .....
plt.scatter(time, s1_avg)
plt.scatter(time, s2_avg)
plt.scatter(time, s3_avg)
plt.scatter(time, s4_avg)
# STILL
for t in still_indexes:
plt.plot(time[t], s1_avg[t], marker='x', markerfacecolor='blue', markersize=8)
plt.plot(time[t], s2_avg[t], marker='x', markerfacecolor='blue', markersize=8)
plt.plot(time[t], s3_avg[t], marker='x', markerfacecolor='blue', markersize=8)
plt.plot(time[t], s4_avg[t], marker='x', markerfacecolor='blue', markersize=8)
# MOVING
# NB some points will overlap --> plotted more than once !!!! because the calculation is not shifted when a straight line sequence is found
for t in walking_indexes:
sensor = t[0]
if sensor==0:
for r in range(min_walking_distance):
plt.plot(time[t[1+r]], s1_avg[t[1+r]], marker='o', markerfacecolor='orange', markersize=8)
"""for i in range(min_walking_distance):
if t[1]+min_walking_distance <len(s1_avg):
plt.plot(time[t[1]+i], s1_avg[t[1]+i], marker='o', markerfacecolor='orange', markersize=8)"""
if sensor==1:
for r in range(min_walking_distance):
plt.plot(time[t[1 + r]], s2_avg[t[1 + r]], marker='o', markerfacecolor='orange', markersize=8)
"""for i in range(min_walking_distance):
if t[1]+min_walking_distance < len(s2_avg):
plt.plot(time[t[1]+i], s2_avg[t[1]+i], marker='o', markerfacecolor='orange', markersize=8)"""
if sensor==2:
for r in range(min_walking_distance):
plt.plot(time[t[1 + r]], s3_avg[t[1 + r]], marker='o', markerfacecolor='orange', markersize=8)
"""for i in range(min_walking_distance):
if t[1]+min_walking_distance < len(s1_avg):
plt.plot(time[t[1]+i], s3_avg[t[1]+i], marker='o', markerfacecolor='orange', markersize=8)"""
if sensor==3:
for r in range(min_walking_distance):
plt.plot(time[t[1 + r]], s4_avg[t[1 + r]], marker='o', markerfacecolor='orange', markersize=8)
"""for i in range(min_walking_distance):
if t[1]+min_walking_distance < len(s1_avg):
plt.plot(time[t[1]+i], s4_avg[t[1]+i], marker='o', markerfacecolor='orange', markersize=8)"""
plt.title("S1: blue - S2: orange - S3: green - S4: red")
plt.show()
# ----------------------------
# THREE METHODS FOR STEP ESTIMATION:
# METHOD 1: step = max variation among data --> too many errors
# Find the approximate value of the step length as the greatest variation in distance from one of the sensors
step = 0
for i in range(len(s1_avg)-1):
if not math.isnan(s1_avg[i]):
variation_1 = abs(s1_avg[i + 1] - s1_avg[i])
variation_2 = abs(s2_avg[i + 1] - s2_avg[i])
variation_3 = abs(s3_avg[i + 1] - s3_avg[i])
variation_4 = abs(s4_avg[i + 1] - s4_avg[i])
max_var = max(variation_1, variation_2, variation_3, variation_4)
if max_var > step:
step = max_var
index = i
print("step: ", step)
print("index: ", index)
# METHOD 2: Take 10 max variations for each sensor, make the average of each and take the average of each average
step = 0
n = 10
s1_20 = []
s2_20 = []
s3_20 = []
s4_20 = []
mean_1 = 0
mean_2 = 0
mean_3 = 0
mean_4 = 0
def largest_n(a, b, n):
for t in range(n):
b.append(abs(a[t+1]-a[t]))
for i in range(len(a) - (n+1)):
if (abs(a[i+n+1]-a[i+n]) > min(b)):
index = b.index(min(b))
b[index] = abs(a[i+n+1]-a[i+n])
return b
print(largest_n(s1_avg, s1_20, n))
print(largest_n(s2_avg, s2_20, n))
print(largest_n(s3_avg, s3_20, n))
print(largest_n(s4_avg, s4_20, n))
mean_1 = sum(largest_n(s1_avg, s1_20, n))/len(largest_n(s1_avg, s1_20, n))
mean_2 = sum(largest_n(s2_avg, s2_20, n))/len(largest_n(s2_avg, s2_20, n))
mean_3 = sum(largest_n(s3_avg, s3_20, n))/len(largest_n(s3_avg, s3_20, n))
mean_4 = sum(largest_n(s4_avg, s4_20, n))/len(largest_n(s4_avg, s4_20, n))
step = (mean_1+mean_2+mean_3+mean_4)/4
print ("Step with second method: ")
print(step)
# THIRD METHOD: take the average of each section where the person is moving in a straight line and then take the average of those averages
avg_vector = []
total_avg = 0
for t in walking_indexes:
total = 0
avg = 0
#sensor = "s" + str(t[0]+1) +"_avg"
sensor = t[0]
i = 1
while i<(len(t)-1):
#exec("sum = sum + sensor[i + 1] - sensor[i]")
#exec("sum = sum + s" + str(t[0]+1) +"avg[" + str(i+1)+ "] - s" + str(t[0]+1) +"avg[" + str(i)+ "]")
index = t[i]
if sensor == 0:
total = total + abs(s1_avg[index+1]-s1_avg[index])
elif sensor == 1:
total = total + abs(s2_avg[index+1]-s2_avg[index])
elif sensor == 2:
total = total + abs(s3_avg[index + 1] - s3_avg[index])
elif sensor == 3:
total = total + abs(s4_avg[index + 1] - s4_avg[index])
i = i+1
avg = total / (len(t) - 1)
avg_vector.append(avg)
total_avg = sum(avg_vector) / len(avg_vector)
print("Step third method: ")
print(total_avg)
# ---------------------------------
# STEP FOURTH EASY METHOD
# USE STEP = 12
step = 12
# DEFINE THE ROTATION FUNCTION
def rotate(origin, point, angle):
"""
Rotate a point counterclockwise by a given angle around a given origin.
The angle should be given in radians.
"""
ox, oy = origin
px, py = point
qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)
qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)
return qx, qy
# DEFINE THE ANGLE TO ROTATE BY
def getAngle(a, b, c):
ang = math.degrees(math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
return ang + 360 if ang < 0 else ang
#---------------------------------
# FIRST METHOD: overlap first sensor
# - constant step = 12
# - all values NOT in still indexes
# - overlap first sensor
# - only one rotation
X_11 = []
X_12 = []
Y_11 = []
Y_12 = []
X_21 = []
X_22 = []
Y_21 = []
Y_22 = []
X_31 = []
X_32 = []
Y_31 = []
Y_32 = []
X_41 = []
X_42 = []
Y_41 = []
Y_42 = []
step = 12
#for t in range(len(distance_total)-1):
for t in range(len(distance_total)-1):
if t not in still_indexes: # to try limit the error due to standing still
for i in range(4):
def f(x):
# APPROX: STEP LENGTH AS OBTAINED BEFORE
f0 = pow((x[0]), 2) + pow(x[1], 2) - pow(distance_total[t][i], 2)
f1 = pow((step - x[0]), 2) + pow(x[1], 2) - pow(distance_total[t + 1][i], 2)
return np.array([f0, f1])
x0_1 = np.array([1000, 1000])
x0_2 = np.array([-1000, -1000])
solution_1 = fsolve(f, x0_1)
solution_2 = fsolve(f, x0_2)
print("\n", solution_1)
print("\n", solution_2)
if i == 0:
X_11.append(solution_1[0])
X_12.append(solution_2[0])
Y_11.append(solution_1[1])
Y_12.append(solution_2[1])
if i == 1:
X_21.append(solution_1[0])
X_22.append(solution_2[0])
Y_21.append(solution_1[1])
Y_22.append(solution_2[1])
if i == 2:
X_31.append(solution_1[0])
X_32.append(solution_2[0])
Y_31.append(solution_1[1])
Y_32.append(solution_2[1])
if i == 3:
X_41.append(solution_1[0])
X_42.append(solution_2[0])
Y_41.append(solution_1[1])
Y_42.append(solution_2[1])
"""# DRAW THE CHANGING POSITION OF THE SENSORS (SOLUTION_1) (WITH RESPECT TO POINT (0,0) RECALCULATED EACH TIME)
#x = np.linspace(0, 10, 1000)
plt.plot(X_11, Y_11, marker='o', markerfacecolor='blue', markersize=4)
plt.plot(X_21, Y_21, marker='o', markerfacecolor='blue', markersize=4)
plt.plot(X_31, Y_31, marker='o', markerfacecolor='blue', markersize=4)
plt.plot(X_41, Y_41, marker='o', markerfacecolor='blue', markersize=4)
plt.plot(0, 0, marker='x', markerfacecolor='black', markersize=6)
plt.show()"""
"""if t > 40:
# ONLY DRAW THE LAST POSITION OF THE SENSORS (SOLUTION_1) WHEN THE PERSON MOVES
plt.plot(X_11[t], Y_11[t], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X_21[t], Y_21[t], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X_31[t], Y_31[t], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X_41[t], Y_41[t], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(0, 0, marker='x', markerfacecolor='black', markersize=6)
plt.title(1622132062700 + t*100)
plt.show()"""
# define the new list of coordinates shifted
X11_shift = []
X12_shift = []
Y11_shift = []
Y12_shift = []
X21_shift = []
X22_shift = []
Y21_shift = []
Y22_shift = []
X31_shift = []
X32_shift = []
Y31_shift = []
Y32_shift = []
X41_shift = []
X42_shift = []
Y41_shift = []
Y42_shift = []
x_shift = 0
y_shift = 0
# Start by moving everything to overlap the first sensor
t = 0
while t < len(X_11):
x_shift = X_11[t] - X_11[0]
y_shift = Y_11[t] - Y_11[0]
X11_shift.append(X_11[0])
Y11_shift.append(Y_11[0])
X21_shift.append(X_21[t] - x_shift)
Y21_shift.append(Y_21[t] - y_shift)
X31_shift.append(X_31[t] - x_shift)
Y31_shift.append(Y_31[t] - y_shift)
X41_shift.append(X_41[t] - x_shift)
Y41_shift.append(Y_41[t] - y_shift)
t = t + 1
# ALL SENSORS - ALL MEASUREMENTS - SHIFTED (S1 ALL OVERLAP)
plt.plot(X11_shift, Y11_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X21_shift, Y21_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_shift, Y31_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_shift, Y41_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(0, 0, marker='x', markerfacecolor='black', markersize=6)
#plt.title(1622132062700 + t*100)
plt.title("FIRST method - shift only")
plt.savefig("1_shift.png")
plt.show()
# NOW ROTATE EVERYTHING BY A CERTAIN AMOUNT
X11_rotate = []
X12_rotate = []
Y11_rotate = []
Y12_rotate = []
X21_rotate = []
X22_rotate = []
Y21_rotate = []
Y22_rotate = []
X31_rotate = []
X32_rotate = []
Y31_rotate = []
Y32_rotate = []
X41_rotate = []
X42_rotate = []
Y41_rotate = []
Y42_rotate = []
for t in range(len(X11_shift)):
origin = (X11_shift[0], Y11_shift[0])
point = (X21_shift[0], Y21_shift[0])
alpha = getAngle((X21_shift[t], Y21_shift[t]), origin, point)
X11_rotate.append(rotate(origin, (X11_shift[t], Y11_shift[t]), math.radians(alpha))[0])
Y11_rotate.append(rotate(origin, (X11_shift[t], Y11_shift[t]), math.radians(alpha))[1])
X21_rotate.append(rotate(origin, (X21_shift[t], Y21_shift[t]), math.radians(alpha))[0])
Y21_rotate.append(rotate(origin, (X21_shift[t], Y21_shift[t]), math.radians(alpha))[1])
X31_rotate.append(rotate(origin, (X31_shift[t], Y31_shift[t]), math.radians(alpha))[0])
Y31_rotate.append(rotate(origin, (X31_shift[t], Y31_shift[t]), math.radians(alpha))[1])
X41_rotate.append(rotate(origin, (X41_shift[t], Y41_shift[t]), math.radians(alpha))[0])
Y41_rotate.append(rotate(origin, (X41_shift[t], Y41_shift[t]), math.radians(alpha))[1])
plt.plot(X11_rotate, Y11_rotate, marker='o', markerfacecolor='black', markersize=6)
plt.plot(X21_rotate, Y21_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_rotate, Y31_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_rotate, Y41_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.title('FIRST method- shift and rotation')
plt.savefig("1_rotation.png")
plt.show()
print(X11_rotate)
# TRY TO PLOT THE AVERAGE POSITION
X1_avg_1 = sum(X11_rotate)/len(X11_rotate)
Y1_avg_1 = sum(Y11_rotate)/len(Y11_rotate)
X2_avg_1 = sum(X21_rotate)/len(X21_rotate)
Y2_avg_1 = sum(Y21_rotate)/len(Y21_rotate)
X3_avg_1 = sum(X31_rotate)/len(X31_rotate)
Y3_avg_1 = sum(Y31_rotate)/len(Y31_rotate)
X4_avg_1 = sum(X41_rotate)/len(X41_rotate)
Y4_avg_1 = sum(Y41_rotate)/len(Y41_rotate)
plt.plot(X1_avg_1, Y1_avg_1, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X2_avg_1, Y2_avg_1, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X3_avg_1, Y3_avg_1, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X4_avg_1, Y4_avg_1, marker='o', markerfacecolor='blue', markersize=6)
plt.annotate("S1", (X1_avg_1, Y1_avg_1))
plt.annotate("S2", (X2_avg_1, Y2_avg_1))
plt.annotate("S3", (X3_avg_1, Y3_avg_1))
plt.annotate("S4", (X4_avg_1, Y4_avg_1))
plt.title('FIRST method - average sensor location')
plt.savefig("1_avg.png")
plt.show()
#---------------------------
# NOW CALCULATE POSITION USING SECOND METHOD - CENTRE THE MIDDLE POINT ---------------------------
# - step = 12
# – all values not in still indexes
# - shift to average
# – only one rotation
X11_shift = []
X12_shift = []
Y11_shift = []
Y12_shift = []
X21_shift = []
X22_shift = []
Y21_shift = []
Y22_shift = []
X31_shift = []
X32_shift = []
Y31_shift = []
Y32_shift = []
X41_shift = []
X42_shift = []
Y41_shift = []
Y42_shift = []
X_avg = (X_11[0] + X_21[0] + X_31[0] + X_41[0])/4
Y_avg = (Y_11[0] + Y_21[0] + Y_31[0] + Y_41[0])/4
t = 0
while t < len(X_11):
x_shift = (X_11[t] + X_21[t] + X_31[t] + X_41[t])/4 - X_avg
y_shift = (Y_11[t] + Y_21[t] + Y_31[t] + Y_41[t])/4 - Y_avg
X11_shift.append(X_11[t] - x_shift)
Y11_shift.append(Y_11[t] - y_shift)
X21_shift.append(X_21[t] - x_shift)
Y21_shift.append(Y_21[t] - y_shift)
X31_shift.append(X_31[t] - x_shift)
Y31_shift.append(Y_31[t] - y_shift)
X41_shift.append(X_41[t] - x_shift)
Y41_shift.append(Y_41[t] - y_shift)
t = t + 1
plt.plot(X11_shift, Y11_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X21_shift, Y21_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_shift, Y31_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_shift, Y41_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.title("SECOND method - shift only")
plt.savefig("2_shift.png")
plt.show()
# NOW THE ROTATION PART
X11_rotate = []
X12_rotate = []
Y11_rotate = []
Y12_rotate = []
X21_rotate = []
X22_rotate = []
Y21_rotate = []
Y22_rotate = []
X31_rotate = []
X32_rotate = []
Y31_rotate = []
Y32_rotate = []
X41_rotate = []
X42_rotate = []
Y41_rotate = []
Y42_rotate = []
for t in range(len(X11_shift)):
origin = (X_avg, Y_avg)
point = (X11_shift[0], Y11_shift[0])
alpha = getAngle((X11_shift[t], Y11_shift[t]), origin, point)
X11_rotate.append(rotate(origin, (X11_shift[t], Y11_shift[t]), math.radians(alpha))[0])
Y11_rotate.append(rotate(origin, (X11_shift[t], Y11_shift[t]), math.radians(alpha))[1])
X21_rotate.append(rotate(origin, (X21_shift[t], Y21_shift[t]), math.radians(alpha))[0])
Y21_rotate.append(rotate(origin, (X21_shift[t], Y21_shift[t]), math.radians(alpha))[1])
X31_rotate.append(rotate(origin, (X31_shift[t], Y31_shift[t]), math.radians(alpha))[0])
Y31_rotate.append(rotate(origin, (X31_shift[t], Y31_shift[t]), math.radians(alpha))[1])
X41_rotate.append(rotate(origin, (X41_shift[t], Y41_shift[t]), math.radians(alpha))[0])
Y41_rotate.append(rotate(origin, (X41_shift[t], Y41_shift[t]), math.radians(alpha))[1])
plt.plot(X11_rotate, Y11_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X21_rotate, Y21_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_rotate, Y31_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_rotate, Y41_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.title("SECOND method - shift and rotation")
plt.savefig("2_rotation.png")
plt.show()
X1_avg_2 = sum(X11_rotate)/len(X11_rotate)
X2_avg_2 = sum(X21_rotate)/len(X21_rotate)
X3_avg_2 = sum(X31_rotate)/len(X31_rotate)
X4_avg_2 = sum(X41_rotate)/len(X41_rotate)
Y1_avg_2 = sum(Y11_rotate)/len(Y11_rotate)
Y2_avg_2 = sum(Y21_rotate)/len(Y21_rotate)
Y3_avg_2 = sum(Y31_rotate)/len(Y31_rotate)
Y4_avg_2 = sum(Y41_rotate)/len(Y41_rotate)
plt.plot(X1_avg_2, Y1_avg_2, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X2_avg_2, Y2_avg_2, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X3_avg_2, Y3_avg_2, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X4_avg_2, Y4_avg_2, marker='o', markerfacecolor='blue', markersize=6)
plt.annotate("S1", (X1_avg_2, Y1_avg_2))
plt.annotate("S2", (X2_avg_2, Y2_avg_2))
plt.annotate("S3", (X3_avg_2, Y3_avg_2))
plt.annotate("S4", (X4_avg_2, Y4_avg_2))
plt.title("SECOND method - Average sensor locations")
plt.savefig("2_avg.png")
plt.show()
# THIRD METHOD, SHIFT THE INTERSECTION, AND ROTATION BY AN AMOUNT TO ACHIEVE BEST POSSIBLE AVERAGE PRECISION
# TAKE THE ANGLE BETWEEN SENSOR LOCATION AND BASE
# - step = 12
# – all values not in still indexes
# - shift to intersection
# – double rotation
X11_shift = []
X12_shift = []
Y11_shift = []
Y12_shift = []
X21_shift = []
X22_shift = []
Y21_shift = []
Y22_shift = []
X31_shift = []
X32_shift = []
Y31_shift = []
Y32_shift = []
X41_shift = []
X42_shift = []
Y41_shift = []
Y42_shift = []
def get_m_q (x1, y1, x2, y2):
m = (y2-y1)/(x2-x1)
q = y1-m*x1
return [m, q]
def intersection (m1, q1, m2, q2):
X_intersection = (q2-q1)/(m1-m2)
Y_intersection = m1*X_intersection+q1
return [X_intersection, Y_intersection]
# find intersection within the area... still to do
m1 = get_m_q(X_11[0],Y_11[0], X_31[0], Y_31[0])[0]
q1 = get_m_q(X_11[0],Y_11[0], X_31[0], Y_31[0])[1]
m2 = get_m_q(X_21[0],Y_21[0], X_41[0], Y_41[0])[0]
q2 = get_m_q(X_21[0],Y_21[0], X_41[0], Y_41[0])[1]
X_avg = intersection(m1, q1, m2, q2)[0]
Y_avg = intersection(m1, q1, m2, q2)[1]
t = 0
while t < len(X_11):
m1 = get_m_q(X_11[t], Y_11[t], X_31[t], Y_31[t])[0]
q1 = get_m_q(X_11[t], Y_11[t], X_31[t], Y_31[t])[1]
m2 = get_m_q(X_21[t], Y_21[t], X_41[t], Y_41[t])[0]
q2 = get_m_q(X_21[t], Y_21[t], X_41[t], Y_41[t])[1]
X_centre = intersection(m1, q1, m2, q2)[0]
Y_centre = intersection(m1, q1, m2, q2)[1]
x_shift = X_centre - X_avg
y_shift = Y_centre - Y_avg
X11_shift.append(X_11[t] - x_shift)
Y11_shift.append(Y_11[t] - y_shift)
X21_shift.append(X_21[t] - x_shift)
Y21_shift.append(Y_21[t] - y_shift)
X31_shift.append(X_31[t] - x_shift)
Y31_shift.append(Y_31[t] - y_shift)
X41_shift.append(X_41[t] - x_shift)
Y41_shift.append(Y_41[t] - y_shift)
t = t + 1
plt.plot(X11_shift, Y11_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X21_shift, Y21_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_shift, Y31_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_shift, Y41_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X11_shift, Y11_shift, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X_avg, Y_avg, marker='x', markerfacecolor='red', markersize=8)
plt.title("THIRD method - shift only")
plt.savefig("3_shift.png")
plt.show()
# The 4 sensors at each step, after shift
"""for i in range(len(X11_shift)):
plt.plot(X11_shift[i], Y11_shift[i], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X21_shift[i], Y21_shift[i], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_shift[i], Y31_shift[i], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_shift[i], Y41_shift[i], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X11_shift[i], Y11_shift[i], marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X_avg, Y_avg, marker='x', markerfacecolor='red', markersize=8)
plt.title("third method - shift only")
plt.show()"""
# now third method rotation
X11_rotate = []
X12_rotate = []
Y11_rotate = []
Y12_rotate = []
X21_rotate = []
X22_rotate = []
Y21_rotate = []
Y22_rotate = []
X31_rotate = []
X32_rotate = []
Y31_rotate = []
Y32_rotate = []
X41_rotate = []
X42_rotate = []
Y41_rotate = []
Y42_rotate = []
for t in range(len(X11_shift)):
origin = (X_avg, Y_avg)
point = (X11_shift[0], Y11_shift[0])
alpha = getAngle((X11_shift[t], Y11_shift[t]), origin, point)
X11_rotate.append(rotate(origin, (X11_shift[t], Y11_shift[t]), math.radians(alpha))[0])
Y11_rotate.append(rotate(origin, (X11_shift[t], Y11_shift[t]), math.radians(alpha))[1])
X21_rotate.append(rotate(origin, (X21_shift[t], Y21_shift[t]), math.radians(alpha))[0])
Y21_rotate.append(rotate(origin, (X21_shift[t], Y21_shift[t]), math.radians(alpha))[1])
X31_rotate.append(rotate(origin, (X31_shift[t], Y31_shift[t]), math.radians(alpha))[0])
Y31_rotate.append(rotate(origin, (X31_shift[t], Y31_shift[t]), math.radians(alpha))[1])
X41_rotate.append(rotate(origin, (X41_shift[t], Y41_shift[t]), math.radians(alpha))[0])
Y41_rotate.append(rotate(origin, (X41_shift[t], Y41_shift[t]), math.radians(alpha))[1])
# second rotation still to be done .....
plt.plot(X11_rotate, Y11_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X21_rotate, Y21_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X31_rotate, Y31_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X41_rotate, Y41_rotate, marker='o', markerfacecolor='blue', markersize=6)
plt.plot(X_avg, Y_avg, marker='x', markerfacecolor='red', markersize=8)
plt.title("THIRD method - shift and first rotation")
plt.savefig("3_rotation1.png")
plt.show()
# SECOND rotation
X11_rotate2 = []
X12_rotate2 = []
Y11_rotate2 = []
Y12_rotate2 = []
X21_rotate2 = []
X22_rotate2 = []
Y21_rotate2 = []
Y22_rotate2 = []
X31_rotate2 = []
X32_rotate2 = []
Y31_rotate2 = []
Y32_rotate2 = []
X41_rotate2 = []
X42_rotate2 = []
Y41_rotate2 = []
Y42_rotate2 = []
for t in range(len(X11_rotate)):
origin = (X_avg, Y_avg)
point = (X21_rotate[0], Y21_rotate[0])
# divide by 2 to get average position and same angle to all sensors
# check that the measured angle is the proper one
alpha = getAngle((X21_rotate[t], Y21_rotate[t]), origin, point)
if alpha > 90:
alpha = 360-(0.5*alpha)
X11_rotate2.append(rotate(origin, (X11_rotate[t], Y11_rotate[t]), math.radians(alpha))[0])
Y11_rotate2.append(rotate(origin, (X11_rotate[t], Y11_rotate[t]), math.radians(alpha))[1])
X21_rotate2.append(rotate(origin, (X21_rotate[t], Y21_rotate[t]), math.radians(alpha))[0])
Y21_rotate2.append(rotate(origin, (X21_rotate[t], Y21_rotate[t]), math.radians(alpha))[1])
X31_rotate2.append(rotate(origin, (X31_rotate[t], Y31_rotate[t]), math.radians(alpha))[0])
Y31_rotate2.append(rotate(origin, (X31_rotate[t], Y31_rotate[t]), math.radians(alpha))[1])
X41_rotate2.append(rotate(origin, (X41_rotate[t], Y41_rotate[t]), math.radians(alpha))[0])
Y41_rotate2.append(rotate(origin, (X41_rotate[t], Y41_rotate[t]), math.radians(alpha))[1])
plt.plot(X11_rotate2, Y11_rotate2, marker='o', markerfacecolor='blue', markersize=6)