-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gundog.Compass.R
1451 lines (1283 loc) · 55.8 KB
/
Gundog.Compass.R
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
#########################################################Gundog.Compass#######################################################
#######Tilt compensated compass method with optional initial soft and hard iron distortion correction and optional tag rotation correction#########
#Outputs angles (pitch, roll, yaw (heading)) computed either by using the standard euler angle approach, or via SAAM (Super-fast Attitude of Accelerometer and Magnetometer - using quaternions) - cf. Wu et al., (2018) [https://github.com/zarathustr/SAAM]
##########################################################################################################################################
#+eval=FALSE
#Required packages installed: rgl
########################
#Required inputs:
#Raw tri-axial magnetometer data (mag.x,y,z)
#Tri-axial static acceleration data (acc.x,y,z) (for computation of pitch and roll) --> Note, these are not raw values - it is the static ('gravity') component of acceleration. One method to derive these values from their raw equivalent is to implement a running mean (e.g., 1-2 s in window length) per channel and use the resultant values
#Marked events data (ME) specifying the period of the magnetic calibration period (as denoted by 'M' - any other input signifies data acquisition not part of calibration procedure). The mangetic calibration period should involve rotating the device slowly ideally in an open space, away from potential sources of magnetic disturbance, relative to the Earth’s magnetic field. Each orientation of roll, pitch and yaw should be incorporated in the device rotations (simply put, imagine a pen is attached to the end of the device being rotated and the aim is to ‘colour in’ all parts of a sphere). This section of data can then be used as a reference for the vectorial sum of magnetometry data across all three spatial dimensions, from which ‘hard’ and ‘soft iron’ errors which can occur in magnetometry data can be corrected.
#Up to 8 method variants of correction to choose from. For rotated ellipsoids use method = 2 (more influenced by noise or more spherical) or method = 3 (default)
#For simple orthogonal re-scaling use method = 1. For non-rotated ellipsoids use method = 4, 5 (x & y axis equal), 6 (x & z axis equal), 7 (y & z axis are equal) or 8 (spherical data)
#As a starting point, I recommend method = 3 if a good calibration period is present. If the calibration period is not great (only 'some' of a sphere is covered when calibrating), then use method = 1.
#The magnetic and gravity vectors are converted from the NED-carried device frame to the NED-carried animal's body frame via de-rotation according to the supplied Euler angles (pitch, roll and yaw --> default values = 'pitch.offset' = 0, 'roll.offset' = 0, 'yaw.offset' = 0 (default assumes no offset). For example, a positive supplied pitch value of 45°, reflects that the device is pitched up from level by 45° and so the function de-rotates by this value. A positive supplied roll value reflects a bank angle device tilt to the right about this axis and a positive supplied yaw value reflects a clock-wise rotated offset.
#algorithm = "standard" (standard euler angle approach) or "SAAM" (using quaternions)
#####################acc.ref.frame | positive.g | mag.ref.frame######################
#Computation of pitch, roll and yaw (heading) uses the right-handed North-East-Down (NED) coordinate system (measures + 1 g when pointing directly down) - This assumes that the x, y and z input fields for both magnetometry and accelerometery data represent the surge (forward-backward), sway (side-to-side) and heave (up-down) dimensions of movement.
#In line with the above point, #The NED system measures +1 g when facing directly downwards, -1 g when facing directly upwards and 0 g when orientated parallel to the earth's surface. Readings of the magnetometer x- and y-axis are at a maximum and minimum when they are pointed at magnetic North and South, respectively. Assuming that the orientation of the encapsulated device (positioned flat on a table) starts at North, readings of the y-axis will be at a maximum and minimum when the device is rotated 90 degrees East and 90 degrees West, respectively, with the z-axis remaining constant
#Some devices may have a different local coordinate frame, e.g., the END (East-North-Down) coordinate system assume the surge dimension of movement is represented by the z-channel, and the sway and heave dimension, by the x- and y-channels, respectively.
#################Changing coordinate frame of the data to the NED frame used here#################
#This function comes with three inputs to allow the user to transform their device's coordinate frame into the NED that the specific Direction Cosine Matrix (DCM) and quaternion augmentation uses to compute angles:'acc.ref.frame', 'positive.g', and 'mag.ref.frame'
#With the NED coordinate frame, the user should imagine that the x-, y, and z- axes of the device are the surge (which represents North/South), sway (which represents East/West) and heave (which represents Up/Down) dimensions of movement. If this is not the case, then acc.ref.frame and mag.ref.frame should be changed so that the function allocates the appropriate channel and/or required negation to readings
#All 48 combinations of coordinate frame configurations are possible to correct for to the NED system:
#acc.ref.frame (or mag.ref.frame) = "NED" | "NEU" | "NWD" | "NWU" | "SED" | "SEU" | "SWD" | "SWU"
#"DEN" | "DES" | "DWN" | "DWS" | "UEN" | "UES" | "UWN" | "UWS"
#"END" | "ENU" | "ESD" | "ESU" | "WND" | "WNU" | "WSD" | "WSU"
#"NDE" | "NDW" | "NUE" | "NUW" | "SDE" | "SDW" | "SUE" | "SUW"
#"DNE" | "DNW" | "DSE" | "DSW" | "UNE" | "UNW" | "USE" | "USW"
#"EDN" | "EDS" | "EUN" | "EUS" | "WDN" | "WDS" | "WUN" | "WUS"
#Essentially, always assume that the x-axis of your device is your index finger, and your middle finger is always the y-axis of the device and your thumb is always the z-axis.
#Then hold up your two fingers and thumb perpendicular (at right-angles to one another) with either your left or right hand to make the shape of your device coordinate system (use which ever hand that can make the shape)
#Starting with the index ('x'-axis) finger, where is it pointing? North, East, South, West, Up, Down?
#Then, where is your middle ('y'-axis) finger pointing? North, East, South, West, Up, Down?
#Lastly, where is your thumb ('z'-axis) pointing? North, East, South, West, Up, Down?
#Take the first letter of the given direction (N, E, S, W, U, D) that each of these fingers/thumb are pointing in the order stated above, and that is the local coordinate system of your device
#If the magnetometer channels are not aligned to that of the accelerometer channels, then repeat the above process
#A good tip if the device configuration is unknown, is to position the device as it would be on the animal, and starting at North, slowly pitch the device down and up three times, then roll the device left and right three times. Do the same at the other cardinal directions (E, W, S), and finish by circling three times, starting and finishing at North.
#Then see which channel has most sinusoidal wave output when pitching and when rolling. That will reflect the surge and sway dimensions, respectively. The sinusoidal waves of two of the magnetometer channels during circling will reveal the surge and sway dimensions of the magentometry configuration (with the channel that records either highest or lowest values when pointing directly North (depending on if it is pointing directly towards or against Magnetic North) being the surge dimension.
#For example, a DSE coordinate frame requires you with your left hand to point your index finger down, your middle finger towards your body, and your thumb pointing East. This therefore assumes that the surge axis is represented by the y-axis pointing towards South (away from the head of the body (or, in other words, towards your body)). The sway axis is represented by the z-channel, pointing East (towards the right of the body), and the heave axis is represented by the x-channel, pointing down (towards the ground).
#Some devices measure +1 g when a channel points directly towards the gravity vector (upwards), while others measure -1 g. So that any required negations of values are correct, the user needs to specify positive.g = "up" or positive.g = "down". In the above calibration tip, when pitching down first, are values of the channel reflecting the surge dimension increasing in g or decreasing in g. If increasing, then positive.g = "down"
#plot = TRUE - Inspection plots of corrected tri-axial magnetometery data provided
##############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
#Outputs: Depending on input, output can include:
#Normalized tri-axial static acceleration data expressed in the animal's body frame (NGbx,y,z).
#Calibrated tri-axial magnetometer data (Mx,y,z)
#Calibrated, normalized tri-axial magnetometer data expressed in the animal's body frame (NMbx,y,z)
#Calibrated, normalized tri-axial magnetometer data expressed in the animal's body frame, after tilt-correction (NMbfx,y,z) - If method = "standard"
#q.w, q.x, q.y, q.z (Unitary (normalised) Quaternions) - (otherwise known as q0, q1, q2, q3) - If method = "SAAM"
#Marked events (ME)
#Pitch
#Roll
#Yaw (heading - 0 to 360 degs)
#Summary plots of correction if plot = TRUE (and method is not = 0)
#Method = 1 based on mathematical protocols outlined by here ; https://github.com/kriswiner/MPU6050/wiki/Simple-and-Effective-Magnetometer-Calibration ; Winer (2017)
#Method = 2 to 8 based on mathematical protocols outlined here; https://www.st.com/resource/en/design_tip/dm00286302-ellipsoid-or-sphere-fitting-for-sensor-calibration-stmicroelectronics.pdf ; Vitali (2016)
#If method = 0, then it is assumed the raw mag data is already corrected, and only pitch, roll and heading is computed. Only pitch, roll and heading (and quats if method = "SAAM") are returned
#If algorithm = "standard", then typical tilt-compensated compass approach using Euler angles. Otherwise if "SAAM", then the Super-fast attitude from accelerometer and magnetometer algorithm is used (simplified version of Davenport's solution for solving Wahba's problem with the magnetic and gravitational reference vectors)
##############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
#########Start of function######
Gundog.Compass = function(mag.x, mag.y, mag.z, acc.x, acc.y, acc.z,
ME, acc.ref.frame = "NED", positive.g = "down", mag.ref.frame = "NED",
pitch.offset = 0, roll.offset = 0, yaw.offset = 0,
method = 3, algorithm = "standard", plot = TRUE){
if(any(is.na(mag.x) == TRUE) | any(is.na(mag.y) == TRUE) | any(is.na(mag.z) == TRUE) | any(is.na(acc.x) == TRUE) | any(is.na(acc.y) == TRUE) | any(is.na(acc.z) == TRUE)) {
stop("The function stops - User has input data containing NA(s)")
}
#Coordinate frame transformation to the NED if required for both acceleration and magnetic vectors#
ax = acc.x ; ay = acc.y ; az = acc.z ; mx = mag.x ; my = mag.y ; mz = mag.z
#North-East-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "NED" & positive.g == "up"){
acc.x = -ax
acc.y = -ay
acc.z = -az
}
#North-East-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "NED" & positive.g == "down"){
acc.x = ax
acc.y = ay
acc.z = az
}
#North-East-Down ; magnetism
if(mag.ref.frame == "NED"){
mag.x = mx
mag.y = my
mag.z = mz
}
########################################################
#North-East-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "NEU" & positive.g == "up"){
acc.x = -ax
acc.y = -ay
acc.z = az
}
#North-East-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "NEU" & positive.g == "down"){
acc.x = ax
acc.y = ay
acc.z = -az
}
#North-East-Up ; magnetism
if(mag.ref.frame == "NEU"){
mag.x = mx
mag.y = my
mag.z = -mz
}
########################################################
#North-West-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "NWD" & positive.g == "up"){
acc.x = -ax
acc.y = ay
acc.z = -az
}
#North-West-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "NWD" & positive.g == "down"){
acc.x = ax
acc.y = -ay
acc.z = az
}
#North-West-Down ; magnetism
if(mag.ref.frame == "NWD"){
mag.x = mx
mag.y = -my
mag.z = mz
}
########################################################
#North-West-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "NWU" & positive.g == "up"){
acc.x = -ax
acc.y = ay
acc.z = az
}
#North-West-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "NWU" & positive.g == "down"){
acc.x = ax
acc.y = -ay
acc.z = -az
}
#North-West-Up ; magnetism
if(mag.ref.frame == "NWU"){
mag.x = mx
mag.y = -my
mag.z = -mz
}
########################################################
#South-East-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "SED" & positive.g == "up"){
acc.x = ax
acc.y = -ay
acc.z = -az
}
#South-East-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "SED" & positive.g == "down"){
acc.x = -ax
acc.y = ay
acc.z = az
}
#South-East-Down ; magnetism
if(mag.ref.frame == "SED"){
mag.x = -mx
mag.y = my
mag.z = mz
}
########################################################
#South-East-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "SEU" & positive.g == "up"){
acc.x = ax
acc.y = -ay
acc.z = az
}
#South-East-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "SEU" & positive.g == "down"){
acc.x = -ax
acc.y = ay
acc.z = -az
}
#South-East-Up ; magnetism
if(mag.ref.frame == "SEU"){
mag.x = -mx
mag.y = my
mag.z = -mz
}
########################################################
#South-West-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "SWD" & positive.g == "up"){
acc.x = ax
acc.y = ay
acc.z = -az
}
#South-West-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "SWD" & positive.g == "down"){
acc.x = -ax
acc.y = -ay
acc.z = az
}
#South-West-Down ; magnetism
if(mag.ref.frame == "SWD"){
mag.x = -mx
mag.y = -my
mag.z = mz
}
########################################################
#South-West-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "SWU" & positive.g == "up"){
acc.x = ax
acc.y = ay
acc.z = az
}
#South-West-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "SWU" & positive.g == "down"){
acc.x = -ax
acc.y = -ay
acc.z = -az
}
#South-West-Up ; magnetism
if(mag.ref.frame == "SWU"){
mag.x = -mx
mag.y = -my
mag.z = -mz
}
########################################################
#Down-East-North ; +g against the gravity vector (up)
if(acc.ref.frame == "DEN" & positive.g == "up"){
acc.x = -az
acc.y = -ay
acc.z = -ax
}
#Down-East-North ; +g with the gravity vector (down)
if(acc.ref.frame == "DEN" & positive.g == "down"){
acc.x = az
acc.y = ay
acc.z = ax
}
#Down-East-North ; magnetism
if(mag.ref.frame == "DEN"){
mag.x = mz
mag.y = my
mag.z = mx
}
########################################################
#Down-East-South ; +g against the gravity vector (up)
if(acc.ref.frame == "DES" & positive.g == "up"){
acc.x = az
acc.y = -ay
acc.z = -ax
}
#Down-East-South ; +g with the gravity vector (down)
if(acc.ref.frame == "DES" & positive.g == "down"){
acc.x = -az
acc.y = ay
acc.z = ax
}
#Down-East-South ; magnetism
if(mag.ref.frame == "DES"){
mag.x = -mz
mag.y = my
mag.z = mx
}
########################################################
#Down-West-North ; +g against the gravity vector (up)
if(acc.ref.frame == "DWN" & positive.g == "up"){
acc.x = -az
acc.y = ay
acc.z = -ax
}
#Down-West-North ; +g with the gravity vector (down)
if(acc.ref.frame == "DWN" & positive.g == "down"){
acc.x = az
acc.y = -ay
acc.z = ax
}
#Down-West-North ; magnetism
if(mag.ref.frame == "DWN"){
mag.x = mz
mag.y = -my
mag.z = mx
}
########################################################
#Down-West-South ; +g against the gravity vector (up)
if(acc.ref.frame == "DWS" & positive.g == "up"){
acc.x = az
acc.y = ay
acc.z = -ax
}
#Down-west-South ; +g with the gravity vector (down)
if(acc.ref.frame == "DWS" & positive.g == "down"){
acc.x = -az
acc.y = -ay
acc.z = ax
}
#Down-West-South ; magnetism
if(mag.ref.frame == "DWS"){
mag.x = -mz
mag.y = -my
mag.z = mx
}
########################################################
#Up-East-North ; +g against the gravity vector (up)
if(acc.ref.frame == "UEN" & positive.g == "up"){
acc.x = -az
acc.y = -ay
acc.z = ax
}
#Up-East-North ; +g with the gravity vector (down)
if(acc.ref.frame == "UEN" & positive.g == "down"){
acc.x = az
acc.y = ay
acc.z = -ax
}
#Up-East-North ; magnetism
if(mag.ref.frame == "UEN"){
mag.x = mz
mag.y = my
mag.z = -mx
}
########################################################
#Up-East-South ; +g against the gravity vector (up)
if(acc.ref.frame == "UES" & positive.g == "up"){
acc.x = az
acc.y = -ay
acc.z = ax
}
#Up-East-South ; +g with the gravity vector (down)
if(acc.ref.frame == "UES" & positive.g == "down"){
acc.x = -az
acc.y = ay
acc.z = -ax
}
#Up-East-South ; magnetism
if(mag.ref.frame == "UES"){
mag.x = -mz
mag.y = my
mag.z = -mx
}
########################################################
#Up-West-North ; +g against the gravity vector (up)
if(acc.ref.frame == "UWN" & positive.g == "up"){
acc.x = -az
acc.y = ay
acc.z = ax
}
#Up-West-North ; +g with the gravity vector (down)
if(acc.ref.frame == "UWN" & positive.g == "down"){
acc.x = az
acc.y = -ay
acc.z = -ax
}
#Up-West-North ; magnetism
if(mag.ref.frame == "UWN"){
mag.x = mz
mag.y = -my
mag.z = -mx
}
########################################################
#Up-West-South ; +g against the gravity vector (up)
if(acc.ref.frame == "UWS" & positive.g == "up"){
acc.x = az
acc.y = ay
acc.z = ax
}
#Up-West-South ; +g with the gravity vector (down)
if(acc.ref.frame == "UWS" & positive.g == "down"){
acc.x = -az
acc.y = -ay
acc.z = -ax
}
#Up-West-South ; magnetism
if(mag.ref.frame == "UWS"){
mag.x = -mz
mag.y = -my
mag.z = -mx
}
########################################################
#East-North-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "END" & positive.g == "up"){
acc.x = -ay
acc.y = -ax
acc.z = -az
}
#East-North-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "END" & positive.g == "down"){
acc.x = ay
acc.y = ax
acc.z = az
}
#East-North-Down ; magnetism
if(mag.ref.frame == "END"){
mag.x = my
mag.y = mx
mag.z = mz
}
########################################################
#East-North-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "ENU" & positive.g == "up"){
acc.x = -ay
acc.y = -ax
acc.z = az
}
#East-North-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "ENU" & positive.g == "down"){
acc.x = ay
acc.y = ax
acc.z = -az
}
#East-North-Up ; magnetism
if(mag.ref.frame == "ENU"){
mag.x = my
mag.y = mx
mag.z = -mz
}
########################################################
#East-South-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "ESD" & positive.g == "up"){
acc.x = ay
acc.y = -ax
acc.z = -az
}
#East-South-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "ESD" & positive.g == "down"){
acc.x = -ay
acc.y = ax
acc.z = az
}
#East-South-Down ; magnetism
if(mag.ref.frame == "ESD"){
mag.x = -my
mag.y = mx
mag.z = mz
}
########################################################
#East-South-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "ESU" & positive.g == "up"){
acc.x = ay
acc.y = -ax
acc.z = az
}
#East-South-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "ESU" & positive.g == "down"){
acc.x = -ay
acc.y = ax
acc.z = -az
}
#East-South-Up ; magnetism
if(mag.ref.frame == "ESU"){
mag.x = -my
mag.y = mx
mag.z = -mz
}
########################################################
#West-North-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "WND" & positive.g == "up"){
acc.x = -ay
acc.y = ax
acc.z = -az
}
#West-North-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "WND" & positive.g == "down"){
acc.x = ay
acc.y = -ax
acc.z = az
}
#West-North-Down ; magnetism
if(mag.ref.frame == "WND"){
mag.x = my
mag.y = -mx
mag.z = mz
}
########################################################
#West-North-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "WNU" & positive.g == "up"){
acc.x = -ay
acc.y = ax
acc.z = az
}
#West-North-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "WNU" & positive.g == "down"){
acc.x = ay
acc.y = -ax
acc.z = -az
}
#West-North-Up ; magnetism
if(mag.ref.frame == "WNU"){
mag.x = my
mag.y = -mx
mag.z = -mz
}
########################################################
#West-South-Down ; +g against the gravity vector (up)
if(acc.ref.frame == "WSD" & positive.g == "up"){
acc.x = ay
acc.y = ax
acc.z = -az
}
#West-South-Down ; +g with the gravity vector (down)
if(acc.ref.frame == "WSD" & positive.g == "down"){
acc.x = -ay
acc.y = -ax
acc.z = az
}
#West-South-Down ; magnetism
if(mag.ref.frame == "WSD"){
mag.x = -my
mag.y = -mx
mag.z = mz
}
########################################################
#West-South-Up ; +g against the gravity vector (up)
if(acc.ref.frame == "WSU" & positive.g == "up"){
acc.x = ay
acc.y = ax
acc.z = az
}
#West-South-Up ; +g with the gravity vector (down)
if(acc.ref.frame == "WSU" & positive.g == "down"){
acc.x = -ay
acc.y = -ax
acc.z = -az
}
#West-South-Up ; magnetism
if(mag.ref.frame == "WSU"){
mag.x = -my
mag.y = -mx
mag.z = -mz
}
########################################################
#North-Down-East ; +g against the gravity vector (up)
if(acc.ref.frame == "NDE" & positive.g == "up"){
acc.x = -ax
acc.y = -az
acc.z = -ay
}
#North-Down-East ; +g with the gravity vector (down)
if(acc.ref.frame == "NDE" & positive.g == "down"){
acc.x = ax
acc.y = az
acc.z = ay
}
#North-Down-East ; magnetism
if(mag.ref.frame == "NDE"){
mag.x = mx
mag.y = mz
mag.z = my
}
########################################################
#North-Down-West ; +g against the gravity vector (up)
if(acc.ref.frame == "NDW" & positive.g == "up"){
acc.x = -ax
acc.y = az
acc.z = -ay
}
#North-Down-West ; +g with the gravity vector (down)
if(acc.ref.frame == "NDW" & positive.g == "down"){
acc.x = ax
acc.y = -az
acc.z = ay
}
#North-Down-West ; magnetism
if(mag.ref.frame == "NDW"){
mag.x = mx
mag.y = -mz
mag.z = my
}
########################################################
#North-Up-East ; +g against the gravity vector (up)
if(acc.ref.frame == "NUE" & positive.g == "up"){
acc.x = -ax
acc.y = -az
acc.z = ay
}
#North-Up-East ; +g with the gravity vector (down)
if(acc.ref.frame == "NUE" & positive.g == "down"){
acc.x = ax
acc.y = az
acc.z = -ay
}
#North-Up-East ; magnetism
if(mag.ref.frame == "NUE"){
mag.x = mx
mag.y = mz
mag.z = -my
}
########################################################
#North-Up-West ; +g against the gravity vector (up)
if(acc.ref.frame == "NUW" & positive.g == "up"){
acc.x = -ax
acc.y = az
acc.z = ay
}
#North-Up-West ; +g with the gravity vector (down)
if(acc.ref.frame == "NUW" & positive.g == "down"){
acc.x = ax
acc.y = -az
acc.z = -ay
}
#North-Up-West ; magnetism
if(mag.ref.frame == "NUW"){
mag.x = mx
mag.y = -mz
mag.z = -my
}
########################################################
#South-Down-East ; +g against the gravity vector (up)
if(acc.ref.frame == "SDE" & positive.g == "up"){
acc.x = ax
acc.y = -az
acc.z = -ay
}
#South-Down-East ; +g with the gravity vector (down)
if(acc.ref.frame == "SDE" & positive.g == "down"){
acc.x = -ax
acc.y = az
acc.z = ay
}
#South-Down-East ; magnetism
if(mag.ref.frame == "SDE"){
mag.x = -mx
mag.y = mz
mag.z = my
}
########################################################
#South-Down-West ; +g against the gravity vector (up)
if(acc.ref.frame == "SDW" & positive.g == "up"){
acc.x = ax
acc.y = az
acc.z = -ay
}
#South-Down-West ; +g with the gravity vector (down)
if(acc.ref.frame == "SDW" & positive.g == "down"){
acc.x = -ax
acc.y = -az
acc.z = ay
}
#South-Down-West ; magnetism
if(mag.ref.frame == "SDW"){
mag.x = -mx
mag.y = -mz
mag.z = my
}
########################################################
#South-Up-East ; +g against the gravity vector (up)
if(acc.ref.frame == "SUE" & positive.g == "up"){
acc.x = ax
acc.y = -az
acc.z = ay
}
#South-Up-East ; +g with the gravity vector (down)
if(acc.ref.frame == "SUE" & positive.g == "down"){
acc.x = -ax
acc.y = az
acc.z = -ay
}
#South-Up-East ; magnetism
if(mag.ref.frame == "SUE"){
mag.x = -mx
mag.y = mz
mag.z = -my
}
########################################################
#South-Up-West ; +g against the gravity vector (up)
if(acc.ref.frame == "SUW" & positive.g == "up"){
acc.x = ax
acc.y = az
acc.z = ay
}
#South-Up-West ; +g with the gravity vector (down)
if(acc.ref.frame == "SUW" & positive.g == "down"){
acc.x = -ax
acc.y = -az
acc.z = -ay
}
#South-Up-West ; magnetism
if(mag.ref.frame == "SUW"){
mag.x = -mx
mag.y = -mz
mag.z = -my
}
########################################################
#Down-North-East ; +g against the gravity vector (up)
if(acc.ref.frame == "DNE" & positive.g == "up"){
acc.x = -ay
acc.y = -az
acc.z = -ax
}
#Down-North-East ; +g with the gravity vector (down)
if(acc.ref.frame == "DNE" & positive.g == "down"){
acc.x = ay
acc.y = az
acc.z = ax
}
#Down-North-East ; magnetism
if(mag.ref.frame == "DNE"){
mag.x = my
mag.y = mz
mag.z = mx
}
########################################################
#Down-North-West ; +g against the gravity vector (up)
if(acc.ref.frame == "DNW" & positive.g == "up"){
acc.x = -ay
acc.y = az
acc.z = -ax
}
#Down-North-West ; +g with the gravity vector (down)
if(acc.ref.frame == "DNW" & positive.g == "down"){
acc.x = ay
acc.y = -az
acc.z = ax
}
#Down-North-West ; magnetism
if(mag.ref.frame == "DNW"){
mag.x = my
mag.y = -mz
mag.z = mx
}
########################################################
#Down-South-East ; +g against the gravity vector (up)
if(acc.ref.frame == "DSE" & positive.g == "up"){
acc.x = ay
acc.y = -az
acc.z = -ax
}
#Down-South-East ; +g with the gravity vector (down)
if(acc.ref.frame == "DSE" & positive.g == "down"){
acc.x = -ay
acc.y = az
acc.z = ax
}
#Down-South-East ; magnetism
if(mag.ref.frame == "DSE"){
mag.x = -my
mag.y = mz
mag.z = mx
}
########################################################
#Down-South-West ; +g against the gravity vector (up)
if(acc.ref.frame == "DSW" & positive.g == "up"){
acc.x = ay
acc.y = az
acc.z = -ax
}
#Down-South-West ; +g with the gravity vector (down)
if(acc.ref.frame == "DSW" & positive.g == "down"){
acc.x = -ay
acc.y = -az
acc.z = ax
}
#Down-South-West ; magnetism
if(mag.ref.frame == "DSW"){
mag.x = -my
mag.y = -mz
mag.z = mx
}
########################################################
#Up-North-East ; +g against the gravity vector (up)
if(acc.ref.frame == "UNE" & positive.g == "up"){
acc.x = -ay
acc.y = -az
acc.z = ax
}
#Up-North-East ; +g with the gravity vector (down)
if(acc.ref.frame == "UNE" & positive.g == "down"){
acc.x = ay
acc.y = az
acc.z = -ax
}
#Up-North-East ; magnetism
if(mag.ref.frame == "UNE"){
mag.x = my
mag.y = mz
mag.z = -mx
}
########################################################
#Up-North-West ; +g against the gravity vector (up)
if(acc.ref.frame == "UNW" & positive.g == "up"){
acc.x = -ay
acc.y = az
acc.z = ax
}
#Up-North-West ; +g with the gravity vector (down)
if(acc.ref.frame == "UNW" & positive.g == "down"){
acc.x = ay
acc.y = -az
acc.z = -ax
}
#Up-North-West ; magnetism
if(mag.ref.frame == "UNW"){
mag.x = my
mag.y = -mz
mag.z = -mx
}
########################################################
#Up-South-East ; +g against the gravity vector (up)
if(acc.ref.frame == "USE" & positive.g == "up"){
acc.x = ay
acc.y = -az
acc.z = ax
}
#Up-South-East ; +g with the gravity vector (down)
if(acc.ref.frame == "USE" & positive.g == "down"){
acc.x = -ay
acc.y = az
acc.z = -ax
}
#Up-South-East ; magnetism
if(mag.ref.frame == "USE"){
mag.x = -my
mag.y = mz
mag.z = -mx
}
########################################################
#Up-South-West ; +g against the gravity vector (up)
if(acc.ref.frame == "USW" & positive.g == "up"){
acc.x = ay
acc.y = az
acc.z = ax
}
#Up-South-West ; +g with the gravity vector (down)
if(acc.ref.frame == "USW" & positive.g == "down"){
acc.x = -ay
acc.y = -az
acc.z = -ax
}
#Up-South-West ; magnetism
if(mag.ref.frame == "USW"){
mag.x = -my
mag.y = -mz
mag.z = -mx
}
########################################################
#East-Down-North ; +g against the gravity vector (up)
if(acc.ref.frame == "EDN" & positive.g == "up"){
acc.x = -az
acc.y = -ax
acc.z = -ay
}
#East-Down-North ; +g with the gravity vector (down)
if(acc.ref.frame == "EDN" & positive.g == "down"){
acc.x = az
acc.y = ax
acc.z = ay
}
#East-Down-North ; magnetism
if(mag.ref.frame == "EDN"){
mag.x = mz
mag.y = mx
mag.z = my
}
########################################################
#East-Down-South ; +g against the gravity vector (up)
if(acc.ref.frame == "EDS" & positive.g == "up"){
acc.x = az
acc.y = -ax
acc.z = -ay
}
#East-Down-South ; +g with the gravity vector (down)
if(acc.ref.frame == "EDS" & positive.g == "down"){
acc.x = -az
acc.y = ax
acc.z = ay
}
#East-Down-South ; magnetism
if(mag.ref.frame == "EDS"){
mag.x = -mz
mag.y = mx
mag.z = my
}
########################################################
#East-Up-North ; +g against the gravity vector (up)
if(acc.ref.frame == "EUN" & positive.g == "up"){
acc.x = -az
acc.y = -ax
acc.z = ay
}
#East-Up-North ; +g with the gravity vector (down)
if(acc.ref.frame == "EUN" & positive.g == "down"){
acc.x = az
acc.y = ax
acc.z = -ay
}
#East-Up-North ; magnetism
if(mag.ref.frame == "EUN"){
mag.x = mz
mag.y = mx
mag.z = -my
}
########################################################
#East-Up-South ; +g against the gravity vector (up)
if(acc.ref.frame == "EUS" & positive.g == "up"){
acc.x = az
acc.y = -ax
acc.z = ay
}
#East-Up-South ; +g with the gravity vector (down)