librobotcontrol-sys 0.4.0

Rust port of librobotcontrol
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
/**
 * @file mpu.c
 *
 * @author James Strawson
 * @date 2/1/2018
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <sys/stat.h>
#include <stdint.h>
#include <errno.h>
#include <sys/stat.h>	// for mkdir and chmod
#include <sys/types.h>	// for mkdir and chmod

#include <rc/mpu.h>
#include <rc/math/vector.h>
#include <rc/math/matrix.h>
#include <rc/math/quaternion.h>
#include <rc/math/filter.h>
#include <rc/math/algebra.h>
#include <rc/time.h>
#include <rc/gpio.h>
#include <rc/i2c.h>
#include <rc/pthread.h>

#include "mpu_defs.h"
#include "dmp_firmware.h"
#include "dmpKey.h"
#include "dmpmap.h"
#include "../common.h"

// Calibration File Locations
#define ACCEL_CAL_FILE		"accel.cal"
#define GYRO_CAL_FILE		"gyro.cal"
#define MAG_CAL_FILE		"mag.cal"

//I2C bus and address definitions for Robotics Cape & bealgebone blue
#define RC_IMU_BUS		2
#define RC_IMU_INTERRUPT_PIN_CHIP 3
#define RC_IMU_INTERRUPT_PIN_PIN  21 // gpio3.21 P9.25

// macros
#define ARRAY_SIZE(array) sizeof(array)/sizeof(array[0])
#define min(a, b)	((a < b) ? a : b)
#define unlikely(x)	__builtin_expect (!!(x), 0)
#define __unused	__attribute__ ((unused))

#define DEG_TO_RAD	0.0174532925199
#define RAD_TO_DEG	57.295779513
#define PI		M_PI
#define TWO_PI		(2.0 * M_PI)

// there should be 28 or 35 bytes in the FIFO if the magnetometer is disabled
// or enabled.
#define FIFO_LEN_QUAT_TAP 20 // 16 for quat, 4 for tap
#define FIFO_LEN_QUAT_ACCEL_GYRO_TAP 32 // 16 quat, 6 accel, 6 gyro, 4 tap
#define MAX_FIFO_BUFFER	(FIFO_LEN_QUAT_ACCEL_GYRO_TAP*5)


// error threshold checks
#define QUAT_ERROR_THRESH	(1L<<16) // very precise threshold
#define QUAT_MAG_SQ_NORMALIZED	(1L<<28)
#define QUAT_MAG_SQ_MIN		(QUAT_MAG_SQ_NORMALIZED - QUAT_ERROR_THRESH)
#define QUAT_MAG_SQ_MAX		(QUAT_MAG_SQ_NORMALIZED + QUAT_ERROR_THRESH)
#define GYRO_CAL_THRESH		50	// std dev below which to consider still
#define ACCEL_CAL_THRESH	100	// std dev below which to consider still
#define GYRO_OFFSET_THRESH	500

// Thread control
static pthread_mutex_t read_mutex	= PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  read_condition	= PTHREAD_COND_INITIALIZER;
static pthread_mutex_t tap_mutex	= PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  tap_condition	= PTHREAD_COND_INITIALIZER;

/**
*	Local variables
**/
static rc_mpu_config_t config;
static int bypass_en;
static int dmp_en=0;
static int packet_len;
static pthread_t imu_interrupt_thread;
static int thread_running_flag;
static void (*dmp_callback_func)()=NULL;
static void (*tap_callback_func)(int dir, int cnt)=NULL;
static double mag_factory_adjust[3];
static double mag_offsets[3];
static double mag_scales[3];
static double accel_lengths[3];
static int last_read_successful;
static uint64_t last_interrupt_timestamp_nanos;
static uint64_t last_tap_timestamp_nanos;
static rc_mpu_data_t* data_ptr;
static int imu_shutdown_flag = 0;
static rc_filter_t low_pass, high_pass; // for magnetometer Yaw filtering
static int was_last_steady = 0;
static double startMagYaw = 0.0;

/**
* functions for internal use only
**/
static int __reset_mpu(void);
static int __check_who_am_i(void);
static int __set_gyro_fsr(rc_mpu_gyro_fsr_t fsr, rc_mpu_data_t* data);
static int __set_accel_fsr(rc_mpu_accel_fsr_t, rc_mpu_data_t* data);
static int __set_gyro_dlpf(rc_mpu_gyro_dlpf_t dlpf);
static int __set_accel_dlpf(rc_mpu_accel_dlpf_t dlpf);
static int __init_magnetometer(int cal_mode);
static int __power_off_magnetometer(void);
static int __mpu_set_bypass(unsigned char bypass_on);
static int __mpu_write_mem(unsigned short mem_addr, unsigned short length, unsigned char *data);
static int __mpu_read_mem(unsigned short mem_addr, unsigned short length, unsigned char *data);
static int __dmp_load_motion_driver_firmware(void);
static int __dmp_set_orientation(unsigned short orient);
static int __dmp_enable_gyro_cal(unsigned char enable);
static int __dmp_enable_lp_quat(unsigned char enable);
static int __dmp_enable_6x_lp_quat(unsigned char enable);
static int __dmp_set_tap_thresh(unsigned char axis, unsigned short thresh);
static int __dmp_set_tap_axes(unsigned char axis);
static int __dmp_set_tap_count(unsigned char min_taps);
static int __dmp_set_tap_time(unsigned short time);
static int __dmp_set_tap_time_multi(unsigned short time);
static int __dmp_set_shake_reject_thresh(long sf, unsigned short thresh);
static int __dmp_set_shake_reject_time(unsigned short time);
static int __dmp_set_shake_reject_timeout(unsigned short time);
static int __collect_accel_samples(int* avg_raw);
static int __mpu_reset_fifo(void);
static int __mpu_set_sample_rate(int rate);
static int __dmp_set_fifo_rate(unsigned short rate);
static int __dmp_enable_feature(unsigned short mask);
static int __mpu_set_dmp_state(unsigned char enable);
static int __set_int_enable(unsigned char enable);
static int __dmp_set_interrupt_mode(unsigned char mode);
static int __load_gyro_calibration(void);
static int __load_mag_calibration(void);
static int __load_accel_calibration(void);
static int __write_gyro_cal_to_disk(int16_t offsets[3]);
static int __write_mag_cal_to_disk(double offsets[3], double scale[3]);
static int __write_accel_cal_to_disk(double* center, double* lengths);
static void* __dmp_interrupt_handler(void* ptr);
static int __read_dmp_fifo(rc_mpu_data_t* data);
static int __data_fusion(rc_mpu_data_t* data);
static int __mag_correct_orientation(double mag_vec[3]);


rc_mpu_config_t rc_mpu_default_config(void)
{
	rc_mpu_config_t conf;

	// connectivity
	conf.gpio_interrupt_pin_chip = RC_IMU_INTERRUPT_PIN_CHIP;
	conf.gpio_interrupt_pin = RC_IMU_INTERRUPT_PIN_PIN;
	conf.i2c_bus = RC_IMU_BUS;
	conf.i2c_addr = RC_MPU_DEFAULT_I2C_ADDR;
	conf.show_warnings = 0;

	// general stuff
	conf.accel_fsr	= ACCEL_FSR_8G;
	conf.gyro_fsr	= GYRO_FSR_2000DPS;
	conf.accel_dlpf	= ACCEL_DLPF_184;
	conf.gyro_dlpf	= GYRO_DLPF_184;
	conf.enable_magnetometer = 0;

	// DMP stuff
	conf.dmp_sample_rate = 100;
	conf.dmp_fetch_accel_gyro = 0;
	conf.dmp_auto_calibrate_gyro = 0;
	conf.orient = ORIENTATION_Z_UP;
	conf.compass_time_constant = 20.0;
	conf.dmp_interrupt_sched_policy = SCHED_OTHER;
	conf.dmp_interrupt_priority = 0;
	conf.read_mag_after_callback = 1;
	conf.mag_sample_rate_div = 4;
	conf.tap_threshold=210;

	return conf;
}


int rc_mpu_set_config_to_default(rc_mpu_config_t *conf)
{
	*conf = rc_mpu_default_config();
	return 0;
}


int rc_mpu_initialize(rc_mpu_data_t *data, rc_mpu_config_t conf)
{
	// update local copy of config struct with new values
	config=conf;

	// make sure the bus is not currently in use by another thread
	// do not proceed to prevent interfering with that process
	if(rc_i2c_get_lock(config.i2c_bus)){
		printf("i2c bus claimed by another process\n");
		printf("Continuing with rc_mpu_initialize() anyway.\n");
	}

	// if it is not claimed, start the i2c bus
	if(rc_i2c_init(config.i2c_bus, config.i2c_addr)<0){
		fprintf(stderr,"failed to initialize i2c bus\n");
		return -1;
	}
	// claiming the bus does no guarantee other code will not interfere
	// with this process, but best to claim it so other code can check
	// like we did above
	rc_i2c_lock_bus(config.i2c_bus);

	// restart the device so we start with clean registers
	if(__reset_mpu()<0){
		fprintf(stderr,"ERROR: failed to reset_mpu9250\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__check_who_am_i()){
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// load in gyro calibration offsets from disk
	if(__load_gyro_calibration()<0){
		fprintf(stderr,"ERROR: failed to load gyro calibration offsets\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__load_accel_calibration()<0){
		fprintf(stderr,"ERROR: failed to load accel calibration offsets\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// Set sample rate = 1000/(1 + SMPLRT_DIV)
	// here we use a divider of 0 for 1khz sample
	if(rc_i2c_write_byte(config.i2c_bus, SMPLRT_DIV, 0x00)){
		fprintf(stderr,"I2C bus write error\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// set full scale ranges and filter constants
	if(__set_gyro_fsr(conf.gyro_fsr, data)){
		fprintf(stderr,"failed to set gyro fsr\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__set_accel_fsr(conf.accel_fsr, data)){
		fprintf(stderr,"failed to set accel fsr\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__set_gyro_dlpf(conf.gyro_dlpf)){
		fprintf(stderr,"failed to set gyro dlpf\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__set_accel_dlpf(conf.accel_dlpf)){
		fprintf(stderr,"failed to set accel_dlpf\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// initialize the magnetometer too if requested in config
	if(conf.enable_magnetometer){
		// start magnetometer NOT in cal mode (0)
		if(__init_magnetometer(0)){
			fprintf(stderr,"failed to initialize magnetometer\n");
			rc_i2c_unlock_bus(config.i2c_bus);
			return -1;
		}
	}
	else __power_off_magnetometer();

	// all done!!
	rc_i2c_unlock_bus(config.i2c_bus);
	return 0;
}


int rc_mpu_read_accel(rc_mpu_data_t *data)
{
	// new register data stored here
	uint8_t raw[6];
	// set the device address
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// Read the six raw data registers into data array
	if(rc_i2c_read_bytes(config.i2c_bus, ACCEL_XOUT_H, 6, &raw[0])<0){
		return -1;
	}
	// Turn the MSB and LSB into a signed 16-bit value
	data->raw_accel[0] = (int16_t)(((uint16_t)raw[0]<<8)|raw[1]);
	data->raw_accel[1] = (int16_t)(((uint16_t)raw[2]<<8)|raw[3]);
	data->raw_accel[2] = (int16_t)(((uint16_t)raw[4]<<8)|raw[5]);
	// Fill in real unit values and apply calibration
	data->accel[0] = data->raw_accel[0] * data->accel_to_ms2 / accel_lengths[0];
	data->accel[1] = data->raw_accel[1] * data->accel_to_ms2 / accel_lengths[1];
	data->accel[2] = data->raw_accel[2] * data->accel_to_ms2 / accel_lengths[2];
	return 0;
}


int rc_mpu_read_gyro(rc_mpu_data_t *data)
{
	// new register data stored here
	uint8_t raw[6];
	// set the device address
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// Read the six raw data registers into data array
	if(rc_i2c_read_bytes(config.i2c_bus, GYRO_XOUT_H, 6, &raw[0])<0){
		return -1;
	}
	// Turn the MSB and LSB into a signed 16-bit value
	data->raw_gyro[0] = (int16_t)(((int16_t)raw[0]<<8)|raw[1]);
	data->raw_gyro[1] = (int16_t)(((int16_t)raw[2]<<8)|raw[3]);
	data->raw_gyro[2] = (int16_t)(((int16_t)raw[4]<<8)|raw[5]);
	// Fill in real unit values
	data->gyro[0] = data->raw_gyro[0] * data->gyro_to_degs;
	data->gyro[1] = data->raw_gyro[1] * data->gyro_to_degs;
	data->gyro[2] = data->raw_gyro[2] * data->gyro_to_degs;
	return 0;
}


int rc_mpu_read_mag(rc_mpu_data_t* data)
{
	uint8_t raw[7];
	int16_t adc[3];
	double factory_cal_data[3];
	if(!config.enable_magnetometer){
		fprintf(stderr,"ERROR: can't read magnetometer unless it is enabled in \n");
		fprintf(stderr,"rc_mpu_config_t struct before calling rc_mpu_initialize\n");
		return -1;
	}
	// magnetometer is actually a separate device with its
	// own address inside the mpu9250
	// MPU9250 was put into passthrough mode
	if(unlikely(rc_i2c_set_device_address(config.i2c_bus, AK8963_ADDR))){
		fprintf(stderr,"ERROR: in rc_mpu_read_mag, failed to set i2c address\n");
		return -1;
	}
	// don't worry about checking data ready bit, not worth thet time
	// read the data ready bit to see if there is new data
	uint8_t st1;
	if(unlikely(rc_i2c_read_byte(config.i2c_bus, AK8963_ST1, &st1)<0)){
		fprintf(stderr,"ERROR reading Magnetometer, i2c_bypass is probably not set\n");
		return -1;
	}
	#ifdef DEBUG
	printf("st1: %d", st1);
	#endif
	if(!(st1&MAG_DATA_READY)){
		if(config.show_warnings){
			printf("no new magnetometer data ready, skipping read\n");
		}
		return 0;
	}
	// Read the six raw data regs into data array
	if(unlikely(rc_i2c_read_bytes(config.i2c_bus,AK8963_XOUT_L,7,&raw[0])<0)){
		fprintf(stderr,"ERROR: rc_mpu_read_mag failed to read data register\n");
		return -1;
	}
	// check if the readings saturated such as because
	// of a local field source, discard data if so
	if(raw[6]&MAGNETOMETER_SATURATION){
		if(config.show_warnings){
			printf("WARNING: magnetometer saturated, discarding data\n");
		}
		return -1;
	}
	// Turn the MSB and LSB into a signed 16-bit value
	// Data stored as little Endian
	adc[0] = (int16_t)(((int16_t)raw[1]<<8) | raw[0]);
	adc[1] = (int16_t)(((int16_t)raw[3]<<8) | raw[2]);
	adc[2] = (int16_t)(((int16_t)raw[5]<<8) | raw[4]);
	#ifdef DEBUG
	printf("raw mag:%d %d %d\n", adc[0], adc[1], adc[2]);
	#endif

	// multiply by the sensitivity adjustment and convert to units of uT micro
	// Teslas. Also correct the coordinate system as someone in invensense
	// thought it would be bright idea to have the magnetometer coordinate
	// system aligned differently than the accelerometer and gyro.... -__-
	factory_cal_data[0] = adc[1] * mag_factory_adjust[1] * MAG_RAW_TO_uT;
	factory_cal_data[1] = adc[0] * mag_factory_adjust[0] * MAG_RAW_TO_uT;
	factory_cal_data[2] = -adc[2] * mag_factory_adjust[2] * MAG_RAW_TO_uT;

	// now apply out own calibration,
	data->mag[0] = (factory_cal_data[0]-mag_offsets[0])*mag_scales[0];
	data->mag[1] = (factory_cal_data[1]-mag_offsets[1])*mag_scales[1];
	data->mag[2] = (factory_cal_data[2]-mag_offsets[2])*mag_scales[2];

	return 0;
}


int rc_mpu_read_temp(rc_mpu_data_t* data)
{
	uint16_t adc;
	// set device address
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// Read the two raw data registers
	if(rc_i2c_read_word(config.i2c_bus, TEMP_OUT_H, &adc)<0){
		fprintf(stderr,"failed to read IMU temperature registers\n");
		return -1;
	}
	// convert to real units
	data->temp = 21.0 + adc/TEMP_SENSITIVITY;
	return 0;
}


int __reset_mpu(void)
{
	// disable the interrupt to prevent it from doing things while we reset
	imu_shutdown_flag = 1;
	// set the device address
	if(rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr)==-1){
		fprintf(stderr,"ERROR resetting MPU, failed to set i2c device adddress\n");
		return -1;
	}
	// write the reset bit
	if(rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, H_RESET)==-1){
		// wait and try again
		rc_usleep(10000);
		if(rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, H_RESET)==-1){
			fprintf(stderr,"ERROR resetting MPU, I2C write to reset bit failed\n");
			return -1;
		}
	}
	rc_usleep(10000);
	return 0;
}


int __check_who_am_i(void)
{
	uint8_t c;
	//check the who am i register to make sure the chip is alive
	if(rc_i2c_read_byte(config.i2c_bus, WHO_AM_I_MPU9250, &c)<0){
		fprintf(stderr,"i2c_read_byte failed reading who_am_i register\n");
		return -1;
	}
	// check which chip we are looking at
	// 0x71 for mpu9250, ox73 or 0x75 for mpu9255, or 0x68 for mpu9150
	// 0x70 for mpu6500,  0x68 or 0x69 for mpu6050
	if(c!=0x68 && c!=0x69 && c!=0x70 && c!=0x71 && c!=0x73 && c!=75){
		fprintf(stderr,"invalid who_am_i register: 0x%x\n", c);
		fprintf(stderr,"expected 0x68 or 0x69 for mpu6050/9150, 0x70 for mpu6500, 0x71 for mpu9250, 0x75 for mpu9255,\n");
		return -1;
	}
	return 0;
}


int __set_accel_fsr(rc_mpu_accel_fsr_t fsr, rc_mpu_data_t* data)
{
	uint8_t c;
	switch(fsr){
	case ACCEL_FSR_2G:
		c = ACCEL_FSR_CFG_2G;
		data->accel_to_ms2 = 9.80665*2.0/32768.0;
		break;
	case ACCEL_FSR_4G:
		c = ACCEL_FSR_CFG_4G;
		data->accel_to_ms2 = 9.80665*4.0/32768.0;
		break;
	case ACCEL_FSR_8G:
		c = ACCEL_FSR_CFG_8G;
		data->accel_to_ms2 = 9.80665*8.0/32768.0;
		break;
	case ACCEL_FSR_16G:
		c = ACCEL_FSR_CFG_16G;
		data->accel_to_ms2 = 9.80665*16.0/32768.0;
		break;
	default:
		fprintf(stderr,"invalid accel fsr\n");
		return -1;
	}
	return rc_i2c_write_byte(config.i2c_bus, ACCEL_CONFIG, c);
}



int __set_gyro_fsr(rc_mpu_gyro_fsr_t fsr, rc_mpu_data_t* data)
{
	uint8_t c;
	switch(fsr){
	case GYRO_FSR_250DPS:
		c = GYRO_FSR_CFG_250 | FCHOICE_B_DLPF_EN;
		data->gyro_to_degs = 250.0/32768.0;
		break;
	case GYRO_FSR_500DPS:
		c = GYRO_FSR_CFG_500 | FCHOICE_B_DLPF_EN;
		data->gyro_to_degs = 500.0/32768.0;
		break;
	case GYRO_FSR_1000DPS:
		c = GYRO_FSR_CFG_1000 | FCHOICE_B_DLPF_EN;
		data->gyro_to_degs = 1000.0/32768.0;
		break;
	case GYRO_FSR_2000DPS:
		c = GYRO_FSR_CFG_2000 | FCHOICE_B_DLPF_EN;
		data->gyro_to_degs = 2000.0/32768.0;
		break;
	default:
		fprintf(stderr,"invalid gyro fsr\n");
		return -1;
	}
	return rc_i2c_write_byte(config.i2c_bus, GYRO_CONFIG, c);
}


int __set_accel_dlpf(rc_mpu_accel_dlpf_t dlpf)
{
	uint8_t c = ACCEL_FCHOICE_1KHZ | BIT_FIFO_SIZE_1024;
	switch(dlpf){
	case ACCEL_DLPF_OFF:
		c = ACCEL_FCHOICE_4KHZ | BIT_FIFO_SIZE_1024;
		break;
	case ACCEL_DLPF_460:
		c |= 0;
		break;
	case ACCEL_DLPF_184:
		c |= 1;
		break;
	case ACCEL_DLPF_92:
		c |= 2;
		break;
	case ACCEL_DLPF_41:
		c |= 3;
		break;
	case ACCEL_DLPF_20:
		c |= 4;
		break;
	case ACCEL_DLPF_10:
		c |= 5;
		break;
	case ACCEL_DLPF_5:
		c |= 6;
		break;
	default:
		fprintf(stderr,"invalid config.accel_dlpf\n");
		return -1;
	}
	return rc_i2c_write_byte(config.i2c_bus, ACCEL_CONFIG_2, c);
}


int __set_gyro_dlpf(rc_mpu_gyro_dlpf_t dlpf)
{
	uint8_t c = FIFO_MODE_REPLACE_OLD;
	switch(dlpf){
	case GYRO_DLPF_OFF:
		c |= 7; // not really off, but 3600Hz bandwith
		break;
	case GYRO_DLPF_250:
		c |= 0;
		break;
	case GYRO_DLPF_184:
		c |= 1;
		break;
	case GYRO_DLPF_92:
		c |= 2;
		break;
	case GYRO_DLPF_41:
		c |= 3;
		break;
	case GYRO_DLPF_20:
		c |= 4;
		break;
	case GYRO_DLPF_10:
		c |= 5;
		break;
	case GYRO_DLPF_5:
		c |= 6;
		break;
	default:
		fprintf(stderr,"invalid gyro_dlpf\n");
		return -1;
	}
	return rc_i2c_write_byte(config.i2c_bus, CONFIG, c);
}


int __init_magnetometer(int cal_mode)
{
	uint8_t raw[3];	// calibration data stored here

	// Enable i2c bypass to allow talking to magnetometer
	if(__mpu_set_bypass(1)){
		fprintf(stderr,"failed to set mpu9250 into bypass i2c mode\n");
		return -1;
	}
	// magnetometer is actually a separate device with its
	// own address inside the mpu9250
	if(rc_i2c_set_device_address(config.i2c_bus, AK8963_ADDR)){
		fprintf(stderr, "ERROR: in __init_magnetometer, failed to set i2c device address\n");
		return -1;
	}
	// Power down magnetometer
	if(rc_i2c_write_byte(config.i2c_bus, AK8963_CNTL, MAG_POWER_DN)<0){
		fprintf(stderr, "ERROR: in __init_magnetometer, failed to write to AK8963_CNTL register to power down\n");
		return -1;
	}
	rc_usleep(1000);
	// Enter Fuse ROM access mode
	if(rc_i2c_write_byte(config.i2c_bus, AK8963_CNTL, MAG_FUSE_ROM)){
		fprintf(stderr, "ERROR: in __init_magnetometer, failed to write to AK8963_CNTL register\n");
		return -1;
	}
	rc_usleep(1000);
	// Read the xyz sensitivity adjustment values
	if(rc_i2c_read_bytes(config.i2c_bus, AK8963_ASAX, 3, &raw[0])<0){
		fprintf(stderr,"failed to read magnetometer adjustment register\n");
		rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
		//__mpu_set_bypass(0);
		return -1;
	}
	// Return sensitivity adjustment values
	mag_factory_adjust[0] = (raw[0]-128)/256.0 + 1.0;
	mag_factory_adjust[1] = (raw[1]-128)/256.0 + 1.0;
	mag_factory_adjust[2] = (raw[2]-128)/256.0 + 1.0;
	// Power down magnetometer again
	if(rc_i2c_write_byte(config.i2c_bus, AK8963_CNTL, MAG_POWER_DN)){
		fprintf(stderr, "ERROR: in __init_magnetometer, failed to write to AK8963_CNTL register to power on\n");
		return -1;
	}
	rc_usleep(100);
	// Configure the magnetometer for 16 bit resolution
	// and continuous sampling mode 2 (100hz)
	uint8_t c = MSCALE_16|MAG_CONT_MES_2;
	if(rc_i2c_write_byte(config.i2c_bus, AK8963_CNTL, c)){
		fprintf(stderr, "ERROR: in __init_magnetometer, failed to write to AK8963_CNTL register to set sampling mode\n");
		return -1;
	}
	rc_usleep(100);
	// go back to configuring the IMU, leave bypass on
	rc_i2c_set_device_address(config.i2c_bus,config.i2c_addr);
	// load in magnetometer calibration
	if(!cal_mode){
		__load_mag_calibration();
	}
	return 0;
}


int __power_off_magnetometer(void)
{
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// Enable i2c bypass to allow talking to magnetometer
	if(__mpu_set_bypass(1)){
		fprintf(stderr,"failed to set mpu9250 into bypass i2c mode\n");
		return -1;
	}
	// magnetometer is actually a separate device with its
	// own address inside the mpu9250
	rc_i2c_set_device_address(config.i2c_bus, AK8963_ADDR);
	// Power down magnetometer
	if(rc_i2c_write_byte(config.i2c_bus, AK8963_CNTL, MAG_POWER_DN)<0){
		fprintf(stderr,"failed to write to magnetometer\n");
		return -1;
	}
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	return 0;
}


int rc_mpu_power_off(void)
{
	imu_shutdown_flag = 1;
	// wait for the interrupt thread to exit if it hasn't already
	//allow up to 1 second for thread cleanup
	if(thread_running_flag){

		if(rc_pthread_timed_join(imu_interrupt_thread, NULL, 1.0)==1){
			fprintf(stderr,"WARNING: mpu interrupt thread exit timeout\n");
		}
		// cleanup mutexes
		pthread_cond_destroy(&read_condition);
		pthread_mutex_destroy(&read_mutex);
		pthread_cond_destroy(&tap_condition);
		pthread_mutex_destroy(&tap_mutex);
	}
	// shutdown magnetometer first if on since that requires
	// the imu to the on for bypass to work
	if(config.enable_magnetometer) __power_off_magnetometer();
	// set the device address to write the shutdown register
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// write the reset bit
	if(rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, H_RESET)){
		//wait and try again
		rc_usleep(1000);
		if(rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, H_RESET)){
			fprintf(stderr,"I2C write to MPU9250 Failed\n");
			return -1;
		}
	}
	// write the sleep bit
	if(rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, MPU_SLEEP)){
		//wait and try again
		rc_usleep(1000);
		if(rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, MPU_SLEEP)){
			fprintf(stderr,"I2C write to MPU9250 Failed\n");
			return -1;
		}
	}

	// if in dmp mode, also unexport the interrupt pin
	if(dmp_en){
		rc_gpio_cleanup(config.gpio_interrupt_pin_chip ,config.gpio_interrupt_pin);
	}

	return 0;
}


int rc_mpu_initialize_dmp(rc_mpu_data_t *data, rc_mpu_config_t conf)
{
	int i;
	uint8_t tmp;
	// range check
	if(conf.dmp_sample_rate>DMP_MAX_RATE || conf.dmp_sample_rate<DMP_MIN_RATE){
		fprintf(stderr,"ERROR:dmp_sample_rate must be between %d & %d\n", \
						DMP_MIN_RATE, DMP_MAX_RATE);
		return -1;
	}
	// make sure the sample rate is a divisor so we can find a neat rate divider
	if(DMP_MAX_RATE%conf.dmp_sample_rate != 0){
		fprintf(stderr,"DMP sample rate must be a divisor of 200\n");
		fprintf(stderr,"acceptable values: 200,100,50,40,25,20,10,8,5,4 (HZ)\n");
		return -1;
	}
	// make sure the compass filter time constant is valid
	if(conf.enable_magnetometer && conf.compass_time_constant<=0.1){
		fprintf(stderr,"ERROR: compass time constant must be greater than 0.1\n");
		return -1;
	}

	// update local copy of config and data struct with new values
	config = conf;
	data_ptr = data;

	// check dlpf
	if(conf.gyro_dlpf==GYRO_DLPF_OFF || conf.gyro_dlpf==GYRO_DLPF_250){
		fprintf(stderr,"WARNING, gyro dlpf bandwidth must be <= 184hz in DMP mode\n");
		fprintf(stderr,"setting to 184hz automatically\n");
		conf.gyro_dlpf	= GYRO_DLPF_184;
	}
	if(conf.accel_dlpf==ACCEL_DLPF_OFF || conf.accel_dlpf==ACCEL_DLPF_460){
		fprintf(stderr,"WARNING, accel dlpf bandwidth must be <= 184hz in DMP mode\n");
		fprintf(stderr,"setting to 184hz automatically\n");
		conf.accel_dlpf	= ACCEL_DLPF_184;
	}
	// check FSR
	if(conf.gyro_fsr!=GYRO_FSR_2000DPS){
		fprintf(stderr,"WARNING, gyro FSR must be GYRO_FSR_2000DPS in DMP mode\n");
		fprintf(stderr,"setting to 2000DPS automatically\n");
		config.gyro_fsr = GYRO_FSR_2000DPS;
	}
	if(conf.accel_fsr!=ACCEL_FSR_8G){
		fprintf(stderr,"WARNING, accel FSR must be ACCEL_FSR_8G in DMP mode\n");
		fprintf(stderr,"setting to ACCEL_FSR_8G automatically\n");
		config.accel_fsr = ACCEL_FSR_8G;
	}

	// start the i2c bus
	if(rc_i2c_init(config.i2c_bus, config.i2c_addr)){
		fprintf(stderr,"rc_mpu_initialize_dmp failed at rc_i2c_init\n");
		return -1;
	}
	// configure the gpio interrupt pin
	if(rc_gpio_init_event(config.gpio_interrupt_pin_chip, config.gpio_interrupt_pin, 0, GPIOEVENT_REQUEST_FALLING_EDGE)==-1){
		fprintf(stderr,"ERROR: in rc_mpu_initialize_dmp, failed to initialize GPIO\n");
		fprintf(stderr,"probably insufficient privileges\n");
		return -1;
	}

	// claiming the bus does no guarantee other code will not interfere
	// with this process, but best to claim it so other code can check
	rc_i2c_lock_bus(config.i2c_bus);
	// restart the device so we start with clean registers
	if(__reset_mpu()<0){
		fprintf(stderr,"failed to __reset_mpu()\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__check_who_am_i()){
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	// MPU6500 shares 4kB of memory between the DMP and the FIFO. Since the
	//first 3kB are needed by the DMP, we'll use the last 1kB for the FIFO.
	// this is also set in set_accel_dlpf but we set here early on
	tmp = BIT_FIFO_SIZE_1024 | 0x8;
	if(rc_i2c_write_byte(config.i2c_bus, ACCEL_CONFIG_2, tmp)){
		fprintf(stderr,"ERROR: in rc_mpu_initialize_dmp, failed to write to ACCEL_CONFIG_2 register\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	// load in calibration offsets from disk
	if(__load_gyro_calibration()<0){
		fprintf(stderr,"ERROR: failed to load gyro calibration offsets\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__load_accel_calibration()<0){
		fprintf(stderr,"ERROR: failed to load accel calibration offsets\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// set full scale ranges. It seems the DMP only scales the gyro properly
	// at 2000DPS. I'll assume the same is true for accel and use 2G like their
	// example
	if(__set_gyro_fsr(config.gyro_fsr, data_ptr)==-1){
		fprintf(stderr, "ERROR in rc_mpu_initialize_dmp, failed to set gyro_fsr register\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__set_accel_fsr(config.accel_fsr, data_ptr)==-1){
		fprintf(stderr, "ERROR in rc_mpu_initialize_dmp, failed to set accel_fsr register\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// set dlpf, these values already checked for bounds above
	if(__set_gyro_dlpf(conf.gyro_dlpf)){
		fprintf(stderr,"failed to set gyro dlpf\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__set_accel_dlpf(conf.accel_dlpf)){
		fprintf(stderr,"failed to set accel_dlpf\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// This actually sets the rate of accel/gyro sampling which should always be
	// 200 as the dmp filters at that rate
	if(__mpu_set_sample_rate(200)<0){
	//if(__mpu_set_sample_rate(config.dmp_sample_rate)<0){
		fprintf(stderr,"ERROR: setting IMU sample rate\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// enable bypass, more importantly this also configures the interrupt pin behavior
	if(__mpu_set_bypass(1)){
		fprintf(stderr, "failed to run __mpu_set_bypass\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// initialize the magnetometer too if requested in config
	if(conf.enable_magnetometer){
		if(__init_magnetometer(0)){
			fprintf(stderr,"ERROR: failed to initialize_magnetometer\n");
			rc_i2c_unlock_bus(config.i2c_bus);
			return -1;
		}
		if(rc_mpu_read_mag(data)==-1){
			fprintf(stderr,"ERROR: failed to initialize_magnetometer\n");
			rc_i2c_unlock_bus(config.i2c_bus);
			return -1;
		}
		// collect some mag data to get a starting heading
		double x_sum = 0.0;
		double y_sum = 0.0;
		double mag_vec[3];
		for(i=0;i<20;i++){
			rc_mpu_read_mag(data);
			// correct for orientation and put data into mag_vec
			if(__mag_correct_orientation(mag_vec)) return -1;
			x_sum += mag_vec[0];
			y_sum += mag_vec[1];
			rc_usleep(10000);
		}
		startMagYaw = -atan2(y_sum, x_sum);
	}
	else __power_off_magnetometer();


	// set up the DMP, order is important, from motiondrive_tutorial.pdf:
	// 1) load firmware
	// 2) set orientation matrix
	// 3) enable callbacks (we don't do this here)
	// 4) enable features
	// 5) set fifo rate
	// 6) set any feature-specific control functions
	// 7) turn dmp on
	dmp_en = 1; // log locally that the dmp will be running
	if(__dmp_load_motion_driver_firmware()<0){
		fprintf(stderr,"failed to load DMP motion driver\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// set the orientation of dmp quaternion
	if(__dmp_set_orientation((unsigned short)conf.orient)<0){
		fprintf(stderr,"ERROR: failed to set dmp orientation\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	/// enbale quaternion feature and accel/gyro if requested
	// due to a known bug in the DMP, the tap feature must be enabled to
	// get interrupts slower than 200hz
	unsigned short feature_mask = DMP_FEATURE_6X_LP_QUAT|DMP_FEATURE_TAP;

	// enable gyro calibration is requested
	if(config.dmp_auto_calibrate_gyro){
		feature_mask|=DMP_FEATURE_GYRO_CAL;
	}
	// enable reading accel/gyro is requested
	if(config.dmp_fetch_accel_gyro){
		feature_mask|=DMP_FEATURE_SEND_RAW_ACCEL|DMP_FEATURE_SEND_ANY_GYRO;
	}
	if(__dmp_enable_feature(feature_mask)<0){
		fprintf(stderr,"ERROR: failed to enable DMP features\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// this changes the rate new dmp data is put in the fifo
	// fixing at 200 causes gyro scaling issues at lower mpu sample rates
	if(__dmp_set_fifo_rate(config.dmp_sample_rate)<0){
		fprintf(stderr,"ERROR: failed to set DMP fifo rate\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// turn the dmp on
	if(__mpu_set_dmp_state(1)<0) {
		fprintf(stderr,"ERROR: __mpu_set_dmp_state(1) failed\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// set interrupt mode to continuous as opposed to GESTURE
	if(__dmp_set_interrupt_mode(DMP_INT_CONTINUOUS)<0){
		fprintf(stderr,"ERROR: failed to set DMP interrupt mode to continuous\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// done writing to bus for now
	rc_i2c_unlock_bus(config.i2c_bus);

	// get ready to start the interrupt handler thread
	data_ptr->tap_detected=0;
	imu_shutdown_flag = 0;
	dmp_callback_func=NULL;
	tap_callback_func=NULL;

	// start the thread
	if(rc_pthread_create(&imu_interrupt_thread, __dmp_interrupt_handler,NULL,
					config.dmp_interrupt_sched_policy,
					config.dmp_interrupt_priority)<0){
		fprintf(stderr,"ERROR failed to start dmp handler thread\n");
		return -1;
	}
	thread_running_flag = 1;

	// sleep for a ms so the thread can start predictably
	rc_usleep(1000);
	return 0;
}

/**
 *  @brief      Write to the DMP memory.
 *  This function prevents I2C writes past the bank boundaries. The DMP memory
 *  is only accessible when the chip is awake.
 *  @param[in]  mem_addr    Memory location (bank << 8 | start address)
 *  @param[in]  length      Number of bytes to write.
 *  @param[in]  data        Bytes to write to memory.
 *  @return     0 if successful.
**/
int __mpu_write_mem(unsigned short mem_addr, unsigned short length, unsigned char *data)
{
	unsigned char tmp[2];
	if (!data){
		fprintf(stderr,"ERROR: in mpu_write_mem, NULL pointer\n");
		return -1;
	}
	tmp[0] = (unsigned char)(mem_addr >> 8);
	tmp[1] = (unsigned char)(mem_addr & 0xFF);
	// Check bank boundaries.
	if (tmp[1] + length > MPU6500_BANK_SIZE){
		fprintf(stderr,"mpu_write_mem exceeds bank size\n");
		return -1;
	}
	if (rc_i2c_write_bytes(config.i2c_bus,MPU6500_BANK_SEL, 2, tmp))
		return -1;
	if (rc_i2c_write_bytes(config.i2c_bus,MPU6500_MEM_R_W, length, data))
		return -1;
	return 0;
}

/**
 *  @brief      Read from the DMP memory.
 *  This function prevents I2C reads past the bank boundaries. The DMP memory
 *  is only accessible when the chip is awake.
 *  @param[in]  mem_addr    Memory location (bank << 8 | start address)
 *  @param[in]  length      Number of bytes to read.
 *  @param[out] data        Bytes read from memory.
 *  @return     0 if successful.
**/
int __mpu_read_mem(unsigned short mem_addr, unsigned short length, unsigned char *data)
{
	unsigned char tmp[2];
	if (!data){
		fprintf(stderr,"ERROR: in mpu_write_mem, NULL pointer\n");
		return -1;
	}
	tmp[0] = (unsigned char)(mem_addr >> 8);
	tmp[1] = (unsigned char)(mem_addr & 0xFF);
	// Check bank boundaries.
	if (tmp[1] + length > MPU6500_BANK_SIZE){
		printf("mpu_read_mem exceeds bank size\n");
		return -1;
	}
	if (rc_i2c_write_bytes(config.i2c_bus,MPU6500_BANK_SEL, 2, tmp))
		return -1;
	if (rc_i2c_read_bytes(config.i2c_bus,MPU6500_MEM_R_W, length, data)!=length)
		return -1;
	return 0;
}

/**
* int __dmp_load_motion_driver_firmware()
*
* loads pre-compiled firmware binary from invensense onto dmp
**/
int __dmp_load_motion_driver_firmware(void)
{
	unsigned short ii;
	unsigned short this_write;
	// Must divide evenly into st.hw->bank_size to avoid bank crossings.
	unsigned char cur[DMP_LOAD_CHUNK], tmp[2];
	// make sure the address is set correctly
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// loop through 16 bytes at a time and check each write for corruption
	for (ii=0; ii<DMP_CODE_SIZE; ii+=this_write) {
		this_write = min(DMP_LOAD_CHUNK, DMP_CODE_SIZE - ii);
		if (__mpu_write_mem(ii, this_write, (uint8_t*)&dmp_firmware[ii])){
			fprintf(stderr,"dmp firmware write failed\n");
			return -1;
		}
		if (__mpu_read_mem(ii, this_write, cur)){
			fprintf(stderr,"dmp firmware read failed\n");
			return -1;
		}
		if (memcmp(dmp_firmware+ii, cur, this_write)){
			fprintf(stderr,"dmp firmware write corrupted\n");
			return -2;
		}
	}
	// Set program start address.
	tmp[0] = dmp_start_addr >> 8;
	tmp[1] = dmp_start_addr & 0xFF;
	if (rc_i2c_write_bytes(config.i2c_bus, MPU6500_PRGM_START_H, 2, tmp)){
		fprintf(stderr,"ERROR writing to MPU6500_PRGM_START register\n");
		return -1;
	}
	return 0;
}

/**
 *  @brief      Push gyro and accel orientation to the DMP.
 *  The orientation is represented here as the output of
 *  @e __inv_orientation_matrix_to_scalar.
 *  @param[in]  orient  Gyro and accel orientation in body frame.
 *  @return     0 if successful.
**/
int __dmp_set_orientation(unsigned short orient)
{
	unsigned char gyro_regs[3], accel_regs[3];
	const unsigned char gyro_axes[3] = {DINA4C, DINACD, DINA6C};
	const unsigned char accel_axes[3] = {DINA0C, DINAC9, DINA2C};
	const unsigned char gyro_sign[3] = {DINA36, DINA56, DINA76};
	const unsigned char accel_sign[3] = {DINA26, DINA46, DINA66};
	// populate fata to be written
	gyro_regs[0] = gyro_axes[orient & 3];
	gyro_regs[1] = gyro_axes[(orient >> 3) & 3];
	gyro_regs[2] = gyro_axes[(orient >> 6) & 3];
	accel_regs[0] = accel_axes[orient & 3];
	accel_regs[1] = accel_axes[(orient >> 3) & 3];
	accel_regs[2] = accel_axes[(orient >> 6) & 3];
	// Chip-to-body, axes only.
	if (__mpu_write_mem(FCFG_1, 3, gyro_regs)){
		fprintf(stderr, "ERROR: in dmp_set_orientation, failed to write dmp mem\n");
		return -1;
	}
	if (__mpu_write_mem(FCFG_2, 3, accel_regs)){
		fprintf(stderr, "ERROR: in dmp_set_orientation, failed to write dmp mem\n");
		return -1;
	}
	memcpy(gyro_regs, gyro_sign, 3);
	memcpy(accel_regs, accel_sign, 3);
	if (orient & 4) {
		gyro_regs[0] |= 1;
		accel_regs[0] |= 1;
	}
	if (orient & 0x20) {
		gyro_regs[1] |= 1;
		accel_regs[1] |= 1;
	}
	if (orient & 0x100) {
		gyro_regs[2] |= 1;
		accel_regs[2] |= 1;
	}
	// Chip-to-body, sign only.
	if(__mpu_write_mem(FCFG_3, 3, gyro_regs)){
		fprintf(stderr, "ERROR: in dmp_set_orientation, failed to write dmp mem\n");
		return -1;
	}
	if(__mpu_write_mem(FCFG_7, 3, accel_regs)){
		fprintf(stderr, "ERROR: in dmp_set_orientation, failed to write dmp mem\n");
		return -1;
	}
	return 0;
}

/**
 *  @brief      Set DMP output rate.
 *  Only used when DMP is on.
 *  @param[in]  rate    Desired fifo rate (Hz).
 *  @return     0 if successful.
**/
int __dmp_set_fifo_rate(unsigned short rate)
{
	const unsigned char regs_end[12] = {DINAFE, DINAF2, DINAAB,
		0xc4, DINAAA, DINAF1, DINADF, DINADF, 0xBB, 0xAF, DINADF, DINADF};
	unsigned short div;
	unsigned char tmp[8];
	if (rate > DMP_MAX_RATE){
		return -1;
	}
	// set the DMP scaling factors
	div = DMP_MAX_RATE / rate - 1;
	tmp[0] = (unsigned char)((div >> 8) & 0xFF);
	tmp[1] = (unsigned char)(div & 0xFF);
	if (__mpu_write_mem(D_0_22, 2, tmp)){
		fprintf(stderr,"ERROR: writing dmp sample rate reg");
		return -1;
	}
	if (__mpu_write_mem(CFG_6, 12, (unsigned char*)regs_end)){
		fprintf(stderr,"ERROR: writing dmp regs_end");
		return -1;
	}
	return 0;
}

/**
* int __mpu_set_bypass(unsigned char bypass_on)
*
* configures the USER_CTRL and INT_PIN_CFG registers to turn on and off the
* i2c bypass mode for talking to the magnetometer. In random read mode this
* is used to turn on the bypass and left as is. In DMP mode bypass is turned
* off after configuration and the MPU fetches magnetometer data automatically.
* USER_CTRL - based on global variable dsp_en
* INT_PIN_CFG based on requested bypass state
**/
int __mpu_set_bypass(uint8_t bypass_on)
{
	uint8_t tmp = 0;
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// set up USER_CTRL first
	// DONT USE FIFO_EN_BIT in DMP mode, or the MPU will generate lots of
	// unwanted interruptss
	if(dmp_en){
		tmp |= FIFO_EN_BIT; // enable fifo for dsp mode
	}
	if(!bypass_on){
		tmp |= I2C_MST_EN; // i2c master mode when not in bypass
	}
	if (rc_i2c_write_byte(config.i2c_bus, USER_CTRL, tmp)){
		fprintf(stderr,"ERROR in mpu_set_bypass, failed to write USER_CTRL register\n");
		return -1;
	}
	rc_usleep(3000);
	// INT_PIN_CFG settings
	tmp = LATCH_INT_EN | INT_ANYRD_CLEAR | ACTL_ACTIVE_LOW; // latching
	//tmp =  ACTL_ACTIVE_LOW;	// non-latching
	if(bypass_on)
		tmp |= BYPASS_EN;
	if (rc_i2c_write_byte(config.i2c_bus, INT_PIN_CFG, tmp)){
		fprintf(stderr,"ERROR in mpu_set_bypass, failed to write INT_PIN_CFG register\n");
		return -1;
	}
	if(bypass_on){
		bypass_en = 1;
	}
	else{
		bypass_en = 0;
	}
	return 0;
}

/**
* int __dmp_enable_gyro_cal(unsigned char enable)
*
* Taken straight from the Invensense DMP code. This enabled the automatic gyro
* calibration feature in the DMP. This this feature is fine for cell phones
* but annoying in control systems we do not use it here and instead ask users
* to run our own gyro_calibration routine.
**/
int __dmp_enable_gyro_cal(unsigned char enable)
{
	if(enable){
		unsigned char regs[9] = {0xb8, 0xaa, 0xb3, 0x8d, 0xb4, 0x98, 0x0d, 0x35, 0x5d};
		return __mpu_write_mem(CFG_MOTION_BIAS, 9, regs);
	}
	else{
		unsigned char regs[9] = {0xb8, 0xaa, 0xaa, 0xaa, 0xb0, 0x88, 0xc3, 0xc5, 0xc7};
		return __mpu_write_mem(CFG_MOTION_BIAS, 9, regs);
	}
}

/**
* int __dmp_enable_6x_lp_quat(unsigned char enable)
*
* Taken straight from the Invensense DMP code. This enabled quaternion filtering
* with accelerometer and gyro filtering.
**/
int __dmp_enable_6x_lp_quat(unsigned char enable)
{
	unsigned char regs[4];
	if(enable){
		regs[0] = DINA20;
		regs[1] = DINA28;
		regs[2] = DINA30;
		regs[3] = DINA38;
	}
	else{
		memset(regs, 0xA3, 4);
	}
	__mpu_write_mem(CFG_8, 4, regs);
	return 0;
}

/**
* int __dmp_enable_lp_quat(unsigned char enable)
*
* sets the DMP to do gyro-only quaternion filtering. This is not actually used
* here but remains as a vestige of the Invensense DMP code.
**/
int __dmp_enable_lp_quat(unsigned char enable)
{
	unsigned char regs[4];
	if(enable){
		regs[0] = DINBC0;
		regs[1] = DINBC2;
		regs[2] = DINBC4;
		regs[3] = DINBC6;
	}
	else{
		memset(regs, 0x8B, 4);
	}
	__mpu_write_mem(CFG_LP_QUAT, 4, regs);
	return 0;
}

/**
* int __mpu_reset_fifo()
*
* This is mostly from the Invensense open source codebase but modified to also
* allow magnetometer data to come in through the FIFO. This just turns off the
* interrupt, resets fifo and DMP, then starts them again. Used once while
* initializing (probably no necessary) then again if the fifo gets too full.
**/
int __mpu_reset_fifo(void)
{
	uint8_t data;
	// make sure the i2c address is set correctly.
	// this shouldn't take any time at all if already set
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	// turn off interrupts, fifo, and usr_ctrl which is where the dmp fifo is enabled
	data = 0;
	if (rc_i2c_write_byte(config.i2c_bus, INT_ENABLE, data)) return -1;
	if (rc_i2c_write_byte(config.i2c_bus, FIFO_EN, data)) return -1;
	if (rc_i2c_write_byte(config.i2c_bus, USER_CTRL, data)) return -1;

	// reset fifo and wait
	data = BIT_FIFO_RST | BIT_DMP_RST;
	if (rc_i2c_write_byte(config.i2c_bus, USER_CTRL, data)) return -1;
	//rc_usleep(1000); // how I had it
	rc_usleep(50000); // invensense standard

	// enable the fifo and DMP fifo flags again
	// enabling DMP but NOT BIT_FIFO_EN gives quat out of bounds
	// but also no empty interrupts
	data = BIT_DMP_EN | BIT_FIFO_EN;
	if(rc_i2c_write_byte(config.i2c_bus, USER_CTRL, data)){
		return -1;
	}

	// turn on dmp interrupt enable bit again
	data = BIT_DMP_INT_EN;
	if (rc_i2c_write_byte(config.i2c_bus, INT_ENABLE, data)) return -1;
	data = 0;
	if (rc_i2c_write_byte(config.i2c_bus, FIFO_EN, data)) return -1;

	return 0;
}

/**
 * int __dmp_set_interrupt_mode(unsigned char mode)
 *
 * This is from the Invensense open source DMP code. It configures the DMP to
 * trigger an interrupt either every sample or only on gestures. Here we only
 * ever configure for continuous sampling.
 *
 * @param[in]  mode  The mode
 *
 * @return     { description_of_the_return_value }
 */
int __dmp_set_interrupt_mode(unsigned char mode)
{
	const unsigned char regs_continuous[11] =
		{0xd8, 0xb1, 0xb9, 0xf3, 0x8b, 0xa3, 0x91, 0xb6, 0x09, 0xb4, 0xd9};
	const unsigned char regs_gesture[11] =
		{0xda, 0xb1, 0xb9, 0xf3, 0x8b, 0xa3, 0x91, 0xb6, 0xda, 0xb4, 0xda};
	switch(mode){
	case DMP_INT_CONTINUOUS:
		return __mpu_write_mem(CFG_FIFO_ON_EVENT, 11, (unsigned char*)regs_continuous);
	case DMP_INT_GESTURE:
		return __mpu_write_mem(CFG_FIFO_ON_EVENT, 11, (unsigned char*)regs_gesture);
	default:
		return -1;
	}
}

/**
 *  @brief      Set tap threshold for a specific axis.
 *  @param[in]  axis    1, 2, and 4 for XYZ accel, respectively.
 *  @param[in]  thresh  Tap threshold, in mg/ms.
 *  @return     0 if successful.
 */
int __dmp_set_tap_thresh(unsigned char axis, unsigned short thresh)
{
	unsigned char tmp[4];
	double scaled_thresh;
	unsigned short dmp_thresh, dmp_thresh_2;
	if (!(axis & TAP_XYZ) || thresh > 1600)
		return -1;

	scaled_thresh = (double)thresh / DMP_SAMPLE_RATE;

	switch (config.accel_fsr) {
	case ACCEL_FSR_2G:
		dmp_thresh = (unsigned short)(scaled_thresh * 16384);
		/* dmp_thresh * 0.75 */
		dmp_thresh_2 = (unsigned short)(scaled_thresh * 12288);
		break;
	case ACCEL_FSR_4G:
		dmp_thresh = (unsigned short)(scaled_thresh * 8192);
		/* dmp_thresh * 0.75 */
		dmp_thresh_2 = (unsigned short)(scaled_thresh * 6144);
		break;
	case ACCEL_FSR_8G:
		dmp_thresh = (unsigned short)(scaled_thresh * 4096);
		/* dmp_thresh * 0.75 */
		dmp_thresh_2 = (unsigned short)(scaled_thresh * 3072);
		break;
	case ACCEL_FSR_16G:
		dmp_thresh = (unsigned short)(scaled_thresh * 2048);
		/* dmp_thresh * 0.75 */
		dmp_thresh_2 = (unsigned short)(scaled_thresh * 1536);
		break;
	default:
		return -1;
	}
	tmp[0] = (unsigned char)(dmp_thresh >> 8);
	tmp[1] = (unsigned char)(dmp_thresh & 0xFF);
	tmp[2] = (unsigned char)(dmp_thresh_2 >> 8);
	tmp[3] = (unsigned char)(dmp_thresh_2 & 0xFF);

	if (axis & TAP_X) {
		if (__mpu_write_mem(DMP_TAP_THX, 2, tmp))
			return -1;
		if (__mpu_write_mem(D_1_36, 2, tmp+2))
			return -1;
	}
	if (axis & TAP_Y) {
		if (__mpu_write_mem(DMP_TAP_THY, 2, tmp))
			return -1;
		if (__mpu_write_mem(D_1_40, 2, tmp+2))
			return -1;
	}
	if (axis & TAP_Z) {
		if (__mpu_write_mem(DMP_TAP_THZ, 2, tmp))
			return -1;
		if (__mpu_write_mem(D_1_44, 2, tmp+2))
			return -1;
	}
	return 0;
}

/**
 *  @brief      Set which axes will register a tap.
 *  @param[in]  axis    1, 2, and 4 for XYZ, respectively.
 *  @return     0 if successful.
 */
int __dmp_set_tap_axes(unsigned char axis)
{
	unsigned char tmp = 0;

	if (axis & TAP_X)
	tmp |= 0x30;
	if (axis & TAP_Y)
	tmp |= 0x0C;
	if (axis & TAP_Z)
	tmp |= 0x03;
	return __mpu_write_mem(D_1_72, 1, &tmp);
}

/**
 *  @brief      Set minimum number of taps needed for an interrupt.
 *  @param[in]  min_taps    Minimum consecutive taps (1-4).
 *  @return     0 if successful.
 */
int __dmp_set_tap_count(unsigned char min_taps)
{
	unsigned char tmp;

	if (min_taps < 1)
	min_taps = 1;
	else if (min_taps > 4)
	min_taps = 4;

	tmp = min_taps - 1;
	return __mpu_write_mem(D_1_79, 1, &tmp);
}

/**
 *  @brief      Set length between valid taps.
 *  @param[in]  time    Milliseconds between taps.
 *  @return     0 if successful.
 */
int __dmp_set_tap_time(unsigned short time)
{
	unsigned short dmp_time;
	unsigned char tmp[2];

	dmp_time = time / (1000 / DMP_SAMPLE_RATE);
	tmp[0] = (unsigned char)(dmp_time >> 8);
	tmp[1] = (unsigned char)(dmp_time & 0xFF);
	return __mpu_write_mem(DMP_TAPW_MIN, 2, tmp);
}

/**
 *  @brief      Set max time between taps to register as a multi-tap.
 *  @param[in]  time    Max milliseconds between taps.
 *  @return     0 if successful.
 */
int __dmp_set_tap_time_multi(unsigned short time)
{
	unsigned short dmp_time;
	unsigned char tmp[2];

	dmp_time = time / (1000 / DMP_SAMPLE_RATE);
	tmp[0] = (unsigned char)(dmp_time >> 8);
	tmp[1] = (unsigned char)(dmp_time & 0xFF);
	return __mpu_write_mem(D_1_218, 2, tmp);
}

/**
 *  @brief      Set shake rejection threshold.
 *  If the DMP detects a gyro sample larger than @e thresh, taps are rejected.
 *  @param[in]  sf      Gyro scale factor.
 *  @param[in]  thresh  Gyro threshold in dps.
 *  @return     0 if successful.
 */
int __dmp_set_shake_reject_thresh(long sf, unsigned short thresh)
{
	unsigned char tmp[4];
	long thresh_scaled = sf / 1000 * thresh;
	tmp[0] = (unsigned char)(((long)thresh_scaled >> 24) & 0xFF);
	tmp[1] = (unsigned char)(((long)thresh_scaled >> 16) & 0xFF);
	tmp[2] = (unsigned char)(((long)thresh_scaled >> 8) & 0xFF);
	tmp[3] = (unsigned char)((long)thresh_scaled & 0xFF);
	return __mpu_write_mem(D_1_92, 4, tmp);
}

/**
 *  @brief      Set shake rejection time.
 *  Sets the length of time that the gyro must be outside of the threshold set
 *  by @e gyro_set_shake_reject_thresh before taps are rejected. A mandatory
 *  60 ms is added to this parameter.
 *  @param[in]  time    Time in milliseconds.
 *  @return     0 if successful.
 */
int __dmp_set_shake_reject_time(unsigned short time)
{
	unsigned char tmp[2];

	time /= (1000 / DMP_SAMPLE_RATE);
	tmp[0] = time >> 8;
	tmp[1] = time & 0xFF;
	return __mpu_write_mem(D_1_90,2,tmp);
}

/**
 *  @brief      Set shake rejection timeout.
 *  Sets the length of time after a shake rejection that the gyro must stay
 *  inside of the threshold before taps can be detected again. A mandatory
 *  60 ms is added to this parameter.
 *  @param[in]  time    Time in milliseconds.
 *  @return     0 if successful.
 */
int __dmp_set_shake_reject_timeout(unsigned short time)
{
	unsigned char tmp[2];

	time /= (1000 / DMP_SAMPLE_RATE);
	tmp[0] = time >> 8;
	tmp[1] = time & 0xFF;
	return __mpu_write_mem(D_1_88,2,tmp);
}

/**
 * int __dmp_enable_feature(unsigned short mask)
 *
 * This is mostly taken from the Invensense DMP code and serves to turn on and
 * off DMP features based on the feature mask. We modified to remove some
 * irrelevant features and set our own fifo-length variable. This probably isn't
 * necessary to remain in its current form as rc_mpu_initialize_dmp uses a fixed
 * set of features but we keep it as is since it works fine.
 *
 * @param[in]  mask  The mask
 *
 * @return     0 on success, -1 on failure
 */
int __dmp_enable_feature(unsigned short mask)
{
	unsigned char tmp[10];
	// Set integration scale factor.
	tmp[0] = (unsigned char)((GYRO_SF >> 24) & 0xFF);
	tmp[1] = (unsigned char)((GYRO_SF >> 16) & 0xFF);
	tmp[2] = (unsigned char)((GYRO_SF >> 8) & 0xFF);
	tmp[3] = (unsigned char)(GYRO_SF & 0xFF);
	if(__mpu_write_mem(D_0_104, 4, tmp)<0){
		fprintf(stderr, "ERROR: in dmp_enable_feature, failed to write mpu mem\n");
		return -1;
	}
	// Send sensor data to the FIFO.
	tmp[0] = 0xA3;
	if (mask & DMP_FEATURE_SEND_RAW_ACCEL) {
		tmp[1] = 0xC0;
		tmp[2] = 0xC8;
		tmp[3] = 0xC2;
	} else {
		tmp[1] = 0xA3;
		tmp[2] = 0xA3;
		tmp[3] = 0xA3;
	}
	if (mask & DMP_FEATURE_SEND_ANY_GYRO) {
		tmp[4] = 0xC4;
		tmp[5] = 0xCC;
		tmp[6] = 0xC6;
	} else {
		tmp[4] = 0xA3;
		tmp[5] = 0xA3;
		tmp[6] = 0xA3;
	}
	tmp[7] = 0xA3;
	tmp[8] = 0xA3;
	tmp[9] = 0xA3;
	if(__mpu_write_mem(CFG_15,10,tmp)<0){
		fprintf(stderr, "ERROR: in dmp_enable_feature, failed to write mpu mem\n");
		return -1;
	}
	// Send gesture data to the FIFO.
	if (mask & (DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT)){
		tmp[0] = DINA20;
	}
	else{
		tmp[0] = 0xD8;
	}
	if(__mpu_write_mem(CFG_27,1,tmp)){
		fprintf(stderr, "ERROR: in dmp_enable_feature, failed to write mpu mem\n");
		return -1;
	}

	if(mask & DMP_FEATURE_GYRO_CAL) __dmp_enable_gyro_cal(1);
	else __dmp_enable_gyro_cal(0);

	if (mask & DMP_FEATURE_SEND_ANY_GYRO) {
		if (mask & DMP_FEATURE_SEND_CAL_GYRO) {
			tmp[0] = 0xB2;
			tmp[1] = 0x8B;
			tmp[2] = 0xB6;
			tmp[3] = 0x9B;
		} else {
			tmp[0] = DINAC0;
			tmp[1] = DINA80;
			tmp[2] = DINAC2;
			tmp[3] = DINA90;
		}
		__mpu_write_mem(CFG_GYRO_RAW_DATA, 4, tmp);
	}

	// configure tap feature
	if (mask & DMP_FEATURE_TAP) {
		/* Enable tap. */
		tmp[0] = 0xF8;
		__mpu_write_mem(CFG_20, 1, tmp);
		__dmp_set_tap_thresh(TAP_XYZ, config.tap_threshold);
		__dmp_set_tap_axes(TAP_XYZ);
		__dmp_set_tap_count(1); //minimum number of taps needed for an interrupt (1-4)
		__dmp_set_tap_time(100); // ms between taps (factory default 100)
		__dmp_set_tap_time_multi(600); // max time between taps for multitap detection (factory default 500)

		// shake rejection ignores taps when system is moving, set threshold
		// high so this doesn't happen too often
		__dmp_set_shake_reject_thresh(GYRO_SF, 300); // default was 200
		__dmp_set_shake_reject_time(80);
		__dmp_set_shake_reject_timeout(100);
	} else {
		tmp[0] = 0xD8;
		__mpu_write_mem(CFG_20, 1, tmp);
	}


	if (mask & DMP_FEATURE_ANDROID_ORIENT) {
		tmp[0] = 0xD9;
	} else
		tmp[0] = 0xD8;
	__mpu_write_mem(CFG_ANDROID_ORIENT_INT, 1, tmp);

	if (mask & DMP_FEATURE_LP_QUAT){
		__dmp_enable_lp_quat(1);
	}
	else{
		__dmp_enable_lp_quat(0);
	}
	if (mask & DMP_FEATURE_6X_LP_QUAT){
		__dmp_enable_6x_lp_quat(1);
	}
	else{
		__dmp_enable_6x_lp_quat(0);
	}
	__mpu_reset_fifo();
	packet_len = 0;
	if(mask & DMP_FEATURE_SEND_RAW_ACCEL){
		packet_len += 6;
	}
	if(mask & DMP_FEATURE_SEND_ANY_GYRO){
		packet_len += 6;
	}
	if(mask & (DMP_FEATURE_LP_QUAT | DMP_FEATURE_6X_LP_QUAT)){
		packet_len += 16;
	}
	if(mask & (DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT)){
		packet_len += 4;
	}
	return 0;
}
/**
 * @brief      This is a vestige of the invensense mpu open source code and is
 *             probably not necessary but remains here anyway.
 *
 * @param[in]  enable  The enable
 *
 * @return     0 on success, -1 on failure
 */
int __set_int_enable(unsigned char enable)
{
	unsigned char tmp;
	if (enable){
		tmp = BIT_DMP_INT_EN;
	}
	else{
		tmp = 0x00;
	}
	if(rc_i2c_write_byte(config.i2c_bus, INT_ENABLE, tmp)){
		fprintf(stderr, "ERROR: in set_int_enable, failed to write INT_ENABLE register\n");
		return -1;
	}
	// disable all other FIFO features leaving just DMP
	if (rc_i2c_write_byte(config.i2c_bus, FIFO_EN, 0)){
		fprintf(stderr, "ERROR: in set_int_enable, failed to write FIFO_EN register\n");
		return -1;
	}
	return 0;
}

/**
 * @brief      Sets the clock rate divider for sensor sampling
 *
 * @param[in]  rate  The rate
 *
 * @return     0 on success, -1 on failure
 */
int __mpu_set_sample_rate(int rate)
{
	if(rate>1000 || rate<4){
		fprintf(stderr,"ERROR: sample rate must be between 4 & 1000\n");
		return -1;
	}
	/* Keep constant sample rate, FIFO rate controlled by DMP. */
	uint8_t div = (1000/rate) - 1;
	#ifdef DEBUG
	printf("setting divider to %d\n", div);
	#endif
	if(rc_i2c_write_byte(config.i2c_bus, SMPLRT_DIV, div)){
		fprintf(stderr,"ERROR: in mpu_set_sample_rate, failed to write SMPLRT_DIV register\n");
		return -1;
	}
	return 0;
}

/**
 * This turns on and off the DMP interrupt and resets the FIFO. This probably
 * isn't necessary as rc_mpu_initialize_dmp sets these registers but it remains
 * here as a vestige of the invensense open source dmp code.
 *
 * @param[in]  enable  The enable
 *
 * @return     0 on success, -1 on failure
 */
int __mpu_set_dmp_state(unsigned char enable)
{
	if (enable) {
		// Disable data ready interrupt.
		__set_int_enable(0);
		// make sure bypass mode is enabled
		__mpu_set_bypass(1);
		// Remove FIFO elements.
		rc_i2c_write_byte(config.i2c_bus, FIFO_EN , 0);
		// Enable DMP interrupt.
		__set_int_enable(1);
		__mpu_reset_fifo();
	}
	else {
		// Disable DMP interrupt.
		__set_int_enable(0);
		// Restore FIFO settings.
		rc_i2c_write_byte(config.i2c_bus, FIFO_EN , 0);
		__mpu_reset_fifo();
	}
	return 0;
}

/**
 * Here is where the magic happens. This function runs as its own thread and
 * monitors the gpio pin config.gpio_interrupt_pin with the blocking function
 * call poll(). If a valid interrupt is received from the IMU then mark the
 * timestamp, read in the IMU data, and call the user-defined interrupt function
 * if set.
 *
 * @return     0 on success, -1 on failure
 */
void* __dmp_interrupt_handler(__attribute__ ((unused)) void* ptr)
{
	//struct pollfd fdset[1];
	int ret;
	// start magnetometer read divider at the end of the counter
	// so it reads on the first run
	int mag_div_step = config.mag_sample_rate_div;
	//char buf[64];
	int first_run = 1;
	__mpu_reset_fifo();

	while(!imu_shutdown_flag){
		// system hangs here until IMU FIFO interrupt
		ret = rc_gpio_poll(	config.gpio_interrupt_pin_chip,
					config.gpio_interrupt_pin,
					IMU_POLL_TIMEOUT,
					&last_interrupt_timestamp_nanos);
		// check for bad things that may have happened
		if(imu_shutdown_flag) break;
		if(ret == RC_GPIOEVENT_ERROR){
			fprintf(stderr, "ERROR in IMU interrupt handler calling poll\n");
			continue;
		}
		if(ret == RC_GPIOEVENT_TIMEOUT){
			if(config.show_warnings){
				fprintf(stderr, "WARNING, gpio poll timeout\n");
			}
			continue;
		}

		// try to load fifo no matter the claim bus state
		if(rc_i2c_get_lock(config.i2c_bus)){
			fprintf(stderr,"WARNING: Something has claimed the I2C bus when an\n");
			fprintf(stderr,"IMU interrupt was received. Reading IMU anyway.\n");
		}
		// aquires bus
		rc_i2c_lock_bus(config.i2c_bus);
		// aquires mutex
		pthread_mutex_lock( &read_mutex );
		pthread_mutex_lock( &tap_mutex );
		// read data
		ret = __read_dmp_fifo(data_ptr);
		rc_i2c_unlock_bus(config.i2c_bus);
		// record if it was successful or not
		if(ret==0){
			last_read_successful=1;
			if(data_ptr->tap_detected){
				last_tap_timestamp_nanos = last_interrupt_timestamp_nanos;
			}
		}
		else{
			last_read_successful=0;
		}
		// if reading mag before callback, check divider and do it now
		if(config.enable_magnetometer && !config.read_mag_after_callback){
			if(mag_div_step>=config.mag_sample_rate_div){
				#ifdef DEBUG
				printf("reading mag before callback\n");
				#endif
				rc_mpu_read_mag(data_ptr);
				// reset address back for next read
				rc_i2c_set_device_address(config.i2c_bus,config.i2c_addr);
				mag_div_step=1;
			}
			else mag_div_step++;
		}
		// releases bus
		rc_i2c_unlock_bus(config.i2c_bus);
		// call the user function if not the first run
		if(first_run == 1){
			first_run = 0;
		}
		else if(last_read_successful){
			if(dmp_callback_func!=NULL) dmp_callback_func();
			// signals that a measurement is available to blocking function
			pthread_cond_broadcast(&read_condition);
			// additionally call tap callback if one was received
			if(data_ptr->tap_detected){
				if(tap_callback_func!=NULL) tap_callback_func(data_ptr->last_tap_direction, data_ptr->last_tap_count);
				pthread_cond_broadcast(&tap_condition);
			}
		}

		// releases mutex
		pthread_mutex_unlock(&read_mutex);
		pthread_mutex_unlock(&tap_mutex);

		// if reading mag after interrupt, check divider and do it now
		if(config.enable_magnetometer && config.read_mag_after_callback){
			if(mag_div_step>=config.mag_sample_rate_div){
				#ifdef DEBUG
				printf("reading mag after ISR\n");
				#endif
				rc_i2c_lock_bus(config.i2c_bus);
				rc_mpu_read_mag(data_ptr);
				rc_i2c_unlock_bus(config.i2c_bus);
				// reset address back for next read
				rc_i2c_set_device_address(config.i2c_bus,config.i2c_addr);
				mag_div_step=1;
			}
			else mag_div_step++;
		}
	}

	// shutting down now, do some cleanup
	// aquires mutex
	pthread_mutex_lock( &read_mutex );
	// /releases other threads
	pthread_cond_broadcast( &read_condition );
	// releases mutex
	pthread_mutex_unlock( &read_mutex );
	thread_running_flag = 0;
	return 0;
}

/**
 * sets a user function to be called when new data is read
 *
 * @param[in]  func  The function
 *
 * @return     0 on success, -1 on failure
 */
int rc_mpu_set_dmp_callback(void (*func)(void))
{
	if(func==NULL){
		fprintf(stderr,"ERROR: trying to assign NULL pointer to dmp_callback_func\n");
		return -1;
	}
	dmp_callback_func = func;
	return 0;
}

int rc_mpu_set_tap_callback(void (*func)(int dir, int cnt))
{
	if(func==NULL){
		fprintf(stderr,"ERROR: trying to assign NULL pointer to tap_callback_func\n");
		return -1;
	}
	tap_callback_func = func;
	return 0;
}


/**
 * Reads the FIFO buffer and populates the data struct. Here is where we see
 * bad/empty/double packets due to i2c bus errors and the IMU failing to have
 * data ready in time. enabling warnings in the config struct will let this
 * function print out warnings when these conditions are detected. If write
 * errors are detected then this function tries some i2c transfers a second
 * time.
 *
 * @param      data  The data pointer
 *
 * @return     0 on success, -1 on failure
 */
int __read_dmp_fifo(rc_mpu_data_t* data)
{
	unsigned char raw[MAX_FIFO_BUFFER];
	int32_t quat_q14[4], quat[4], quat_mag_sq;
	uint16_t fifo_count;
	int ret;
	int i = 0; // position of beginning of quaternion
	int j = 0; // position of beginning of accel/gyro data
	static int first_run = 1; // set to 0 after first call
	double q_tmp[4];
	double sum,qlen;

	if(!dmp_en){
		printf("only use mpu_read_fifo in dmp mode\n");
		return -1;
	}

	// if the fifo packet_len variable not set up yet, this function must
	// have been called prematurely
	if(packet_len!=FIFO_LEN_QUAT_ACCEL_GYRO_TAP && packet_len!=FIFO_LEN_QUAT_TAP){
		fprintf(stderr,"ERROR: packet_len is set incorrectly for read_dmp_fifo\n");
		return -1;
	}

	// make sure the i2c address is set correctly.
	// this shouldn't take any time at all if already set
	rc_i2c_set_device_address(config.i2c_bus, config.i2c_addr);
	int is_new_dmp_data = 0;

	// check fifo count register to make sure new data is there
	if(rc_i2c_read_word(config.i2c_bus, FIFO_COUNTH, &fifo_count)<0){
		if(config.show_warnings){
			printf("fifo_count i2c error: %s\n",strerror(errno));
		}
		return -1;
	}
	#ifdef DEBUG
	printf("fifo_count: %d\n", fifo_count);
	#endif

	/***********************************************************************
	* Check how many packets are in the fifo buffer
	***********************************************************************/

	// if empty FIFO, just return, nothing else to do
	if(fifo_count==0){
		if(config.show_warnings && first_run!=1){
			printf("WARNING: empty fifo\n");
		}
		return -1;
	}
	// one packet, perfect!
	else if(fifo_count==packet_len){
		i = 0; // set quaternion offset to 0
	}
	// if exactly 2 or 3 packets are there we just missed some (whoops)
	// read both in and set the offset i to one packet length
	// the last packet data will be read normally
	else if(fifo_count==2*packet_len){
		if(config.show_warnings&& first_run!=1){
			printf("warning: imu fifo contains two packets\n");
		}
		i=packet_len;
	}
	else if(fifo_count==3*packet_len){
		if(config.show_warnings&& first_run!=1){
			printf("warning: imu fifo contains three packets\n");
		}
		i=2*packet_len;
	}
	else if(fifo_count==4*packet_len){
		if(config.show_warnings&& first_run!=1){
			printf("warning: imu fifo contains four packets\n");
		}
		i=2*packet_len;
	}
	else if(fifo_count==5*packet_len){
		if(config.show_warnings&& first_run!=1){
			printf("warning: imu fifo contains five packets\n");
		}
		i=2*packet_len;
	}
	// finally, if we got a weird packet length, reset the fifo
	else{
		if(config.show_warnings && first_run!=1){
			printf("warning: %d bytes in FIFO, expected %d\n", fifo_count,packet_len);
		}
		__mpu_reset_fifo();
		return -1;
	}

	/***********************************************************************
	* read in the fifo
	******************\\\**************************************************/
	memset(raw,0,MAX_FIFO_BUFFER);
	// read it in!
	ret = rc_i2c_read_bytes(config.i2c_bus, FIFO_R_W, fifo_count, &raw[0]);
	if(ret<0){
		// if i2c_read returned -1 there was an error, try again
		ret = rc_i2c_read_bytes(config.i2c_bus, FIFO_R_W, fifo_count, &raw[0]);
	}
	if(ret!=fifo_count){
		if(config.show_warnings){
			fprintf(stderr,"ERROR: failed to read fifo buffer register\n");
			printf("read %d bytes, expected %d\n", ret, packet_len);
		}
		return -1;
	}


	// now we can read the quaternion which is always first
	// parse the quaternion data from the buffer
	quat[0] = ((int32_t)raw[i+0] << 24) | ((int32_t)raw[i+1] << 16) |
		((int32_t)raw[i+2] << 8) | raw[i+3];
	quat[1] = ((int32_t)raw[i+4] << 24) | ((int32_t)raw[i+5] << 16) |
		((int32_t)raw[i+6] << 8) | raw[i+7];
	quat[2] = ((int32_t)raw[i+8] << 24) | ((int32_t)raw[i+9] << 16) |
		((int32_t)raw[i+10] << 8) | raw[i+11];
	quat[3] = ((int32_t)raw[i+12] << 24) | ((int32_t)raw[i+13] << 16) |
		((int32_t)raw[i+14] << 8) | raw[i+15];

	// increment poisition in buffer after 16 bits of quaternion
	i+=16;

	// check the quaternion size, make sure it's correct
	quat_q14[0] = quat[0] >> 16;
	quat_q14[1] = quat[1] >> 16;
	quat_q14[2] = quat[2] >> 16;
	quat_q14[3] = quat[3] >> 16;
	quat_mag_sq = quat_q14[0] * quat_q14[0] + quat_q14[1] * quat_q14[1] + \
		quat_q14[2] * quat_q14[2] + quat_q14[3] * quat_q14[3];
	if ((quat_mag_sq < QUAT_MAG_SQ_MIN)||(quat_mag_sq > QUAT_MAG_SQ_MAX)){
		if(config.show_warnings){
			printf("warning: Quaternion out of bounds, fifo_count: %d\n", fifo_count);
		}
		__mpu_reset_fifo();
		return -1;
	}

	// do double-precision quaternion normalization since the numbers
	// in raw format are huge
	for(j=0;j<4;j++) q_tmp[j]=(double)quat[j];
	sum = 0.0;
	for(j=0;j<4;j++) sum+=q_tmp[j]*q_tmp[j];
	qlen=sqrt(sum);
	for(j=0;j<4;j++) q_tmp[j]/=qlen;
	// make floating point and put in output
	for(j=0;j<4;j++) data->dmp_quat[j]=(double)q_tmp[j];

	// fill in tait-bryan angles to the data struct
	rc_quaternion_to_tb_array(data->dmp_quat, data->dmp_TaitBryan);
	is_new_dmp_data=1;


	if(packet_len==FIFO_LEN_QUAT_ACCEL_GYRO_TAP){
		// Read Accel values and load into imu_data struct
		// Turn the MSB and LSB into a signed 16-bit value
		data->raw_accel[0] = (int16_t)(((uint16_t)raw[i+0]<<8)|raw[i+1]);
		data->raw_accel[1] = (int16_t)(((uint16_t)raw[i+2]<<8)|raw[i+3]);
		data->raw_accel[2] = (int16_t)(((uint16_t)raw[i+4]<<8)|raw[i+5]);
		i+=6;
		// Fill in real unit values
		data->accel[0] = data->raw_accel[0] * data->accel_to_ms2 / accel_lengths[0];
		data->accel[1] = data->raw_accel[1] * data->accel_to_ms2 / accel_lengths[1];
		data->accel[2] = data->raw_accel[2] * data->accel_to_ms2 / accel_lengths[2];

		// Read gyro values and load into imu_data struct
		// Turn the MSB and LSB into a signed 16-bit value
		data->raw_gyro[0] = (int16_t)(((int16_t)raw[0+i]<<8)|raw[1+i]);
		data->raw_gyro[1] = (int16_t)(((int16_t)raw[2+i]<<8)|raw[3+i]);
		data->raw_gyro[2] = (int16_t)(((int16_t)raw[4+i]<<8)|raw[5+i]);
		i+=6;

		// Fill in real unit values
		data->gyro[0] = data->raw_gyro[0] * data->gyro_to_degs;
		data->gyro[1] = data->raw_gyro[1] * data->gyro_to_degs;
		data->gyro[2] = data->raw_gyro[2] * data->gyro_to_degs;
	}

	// TODO read in tap data
	unsigned char tap;
	//android_orient = gesture[3] & 0xC0;
	tap = 0x3F & raw[i+3];

	if(raw[i+1] & INT_SRC_TAP){
		unsigned char direction, count;
		direction = tap >> 3;
		count = (tap % 8) + 1;
		data_ptr->last_tap_direction = direction;
		data_ptr->last_tap_count = count;
		data_ptr->tap_detected=1;
	}
	else data_ptr->tap_detected=0;

	// run data_fusion to filter yaw with compass
	if(is_new_dmp_data && config.enable_magnetometer){
		#ifdef DEBUG
		printf("running data_fusion\n");
		#endif
		__data_fusion(data);
	}

	// if we finally got dmp data, turn off the first run flag
	if(is_new_dmp_data) first_run=0;

	// finally, our return value is based on the presence of DMP data only
	// even if new magnetometer data was read, the expected timing must come
	// from the DMP samples only
	if(is_new_dmp_data) return 0;
	else return -1;
}

/**
 * This fuses the magnetometer data with the quaternion straight from the DMP to
 * correct the yaw heading to a compass heading. Much thanks to Pansenti for
 * open sourcing this routine. In addition to the Pansenti implementation I also
 * correct the magnetometer data for DMP orientation, initialize yaw with the
 * magnetometer to prevent initial rise time, and correct the yaw_mixing_factor
 * with the sample rate so the filter rise time remains constant with different
 * sample rates.
 *
 * @param      data  The data pointer
 *
 * @return     0 on success, -1 on failure
 */
int __data_fusion(rc_mpu_data_t* data)
{
	double tilt_tb[3], tilt_q[4], mag_vec[3];
	static double newMagYaw = 0;
	static double newDMPYaw = 0;
	double lastDMPYaw, lastMagYaw, newYaw;
	static int dmp_spin_counter = 0;
	static int mag_spin_counter = 0;
	static int first_run = 1; // set to 0 after first call to this function


	// start by filling in the roll/pitch components of the fused euler
	// angles from the DMP generated angles. Ignore yaw for now, we have to
	// filter that later.
	tilt_tb[0] = data->dmp_TaitBryan[TB_PITCH_X];
	tilt_tb[1] = data->dmp_TaitBryan[TB_ROLL_Y];
	tilt_tb[2] = 0.0;

	// generate a quaternion rotation of just roll/pitch
	rc_quaternion_from_tb_array(tilt_tb,tilt_q);

	// correct for orientation and put data into
	if(__mag_correct_orientation(mag_vec)) return -1;

	// tilt that vector by the roll/pitch of the IMU to align magnetic field
	// vector such that Z points vertically
	rc_quaternion_rotate_vector_array(mag_vec,tilt_q);
	// from the aligned magnetic field vector, find a yaw heading
	// check for validity and make sure the heading is positive
	lastMagYaw = newMagYaw; // save from last loop
	newMagYaw = -atan2(mag_vec[1], mag_vec[0]);

	if (isnan(newMagYaw)) {
		#ifdef WARNINGS
		printf("newMagYaw NAN\n");
		#endif
		return -1;
	}
	data->compass_heading_raw = newMagYaw;
	// save DMP last from time and record newDMPYaw for this time
	lastDMPYaw = newDMPYaw;
	newDMPYaw = data->dmp_TaitBryan[TB_YAW_Z];

	// the outputs from atan2 and dmp are between -PI and PI.
	// for our filters to run smoothly, we can't have them jump between -PI
	// to PI when doing a complete spin. Therefore we check for a skip and
	// increment or decrement the spin counter
	if(newMagYaw-lastMagYaw < -PI) mag_spin_counter++;
	else if (newMagYaw-lastMagYaw > PI) mag_spin_counter--;
	if(newDMPYaw-lastDMPYaw < -PI) dmp_spin_counter++;
	else if (newDMPYaw-lastDMPYaw > PI) dmp_spin_counter--;

	// if this is the first run, set up filters
	if(first_run){
		lastMagYaw = newMagYaw;
		lastDMPYaw = newDMPYaw;
		mag_spin_counter = 0;
		dmp_spin_counter = 0;
		// generate complementary filters
		double dt = 1.0/config.dmp_sample_rate;
		rc_filter_first_order_lowpass(&low_pass,dt,config.compass_time_constant);
		rc_filter_first_order_highpass(&high_pass,dt,config.compass_time_constant);
		rc_filter_prefill_inputs(&low_pass,startMagYaw);
		rc_filter_prefill_outputs(&low_pass,startMagYaw);
		rc_filter_prefill_inputs(&high_pass,newDMPYaw);
		rc_filter_prefill_outputs(&high_pass,0);
		first_run = 0;
	}

	// new Yaw is the sum of low and high pass complementary filters.
	double lp = rc_filter_march(&low_pass,newMagYaw+(TWO_PI*mag_spin_counter));
	double hp = rc_filter_march(&high_pass,newDMPYaw+(TWO_PI*dmp_spin_counter));
	newYaw =  lp+hp;

	newYaw = fmod(newYaw,TWO_PI); // remove the effect of the spins
	if (newYaw > PI) newYaw -= TWO_PI; // bound between +- PI
	else if (newYaw < -PI) newYaw += TWO_PI; // bound between +- PI

	// TB angles expect a yaw between -pi to pi so slide it again and
	// store in the user-accessible fused tb angle
	data->compass_heading = newYaw;
	data->fused_TaitBryan[2] = newYaw;
	data->fused_TaitBryan[0] = data->dmp_TaitBryan[0];
	data->fused_TaitBryan[1] = data->dmp_TaitBryan[1];

	// Also generate a new quaternion from the filtered tb angles
	rc_quaternion_from_tb_array(data->fused_TaitBryan, data->fused_quat);
	return 0;
}



/**
 * Loads steady state gyro offsets from the disk and puts them in the IMU's gyro
 * offset register. If no calibration file exists then make a new one.
 *
 * @return     0 on success, -1 on failure
 */
int __load_gyro_calibration(void)
{
	FILE* fd;
	uint8_t data[6];
	int x,y,z;

	fd = fopen(CALIBRATION_DIR GYRO_CAL_FILE, "r");

	if(fd==NULL){
		// calibration file doesn't exist yet
		fprintf(stderr,"WARNING: no gyro calibration data found\n");
		fprintf(stderr,"Please run rc_calibrate_gyro\n\n");
		// use zero offsets
		x = 0;
		y = 0;
		z = 0;
	}
	else{
		// read in data
		if(fscanf(fd,"%d\n%d\n%d\n", &x,&y,&z)!=3){
			fprintf(stderr,"ERROR loading gyro offsets, calibration file empty or malformed\n");
			fprintf(stderr,"please run rc_calibrate_gyro to make a new calibration file\n");
			fprintf(stderr,"using default offsets for now\n");
			// use zero offsets
			x = 0;
			y = 0;
			z = 0;
		}
		fclose(fd);
	}

	#ifdef DEBUG
	printf("offsets: %d %d %d\n", x, y, z);
	#endif

	// Divide by 4 to get 32.9 LSB per deg/s to conform to expected bias input
	// format. also make negative since we wish to subtract out the steady
	// state offset
	data[0] = (-x/4  >> 8) & 0xFF;
	data[1] = (-x/4)       & 0xFF;
	data[2] = (-y/4  >> 8) & 0xFF;
	data[3] = (-y/4)       & 0xFF;
	data[4] = (-z/4  >> 8) & 0xFF;
	data[5] = (-z/4)       & 0xFF;

	// Push gyro biases to hardware registers
	if(rc_i2c_write_bytes(config.i2c_bus, XG_OFFSET_H, 6, &data[0])){
		fprintf(stderr,"ERROR: failed to load gyro offsets into IMU register\n");
		return -1;
	}
	return 0;
}

/**
 * Loads steady state magnetometer offsets and scale from the disk into global
 * variables for correction later by read_magnetometer and FIFO read functions
 *
 * @return     0 on success, -1 on failure
 */
int __load_mag_calibration(void)
{
	FILE *fd;
	double x,y,z,sx,sy,sz;

	fd = fopen(CALIBRATION_DIR MAG_CAL_FILE, "r");
	if(fd==NULL){
		// calibration file doesn't exist yet
		fprintf(stderr,"WARNING: no magnetometer calibration data found\n");
		fprintf(stderr,"Please run rc_calibrate_mag\n\n");
		x=0.0;
		y=0.0;
		z=0.0;
		sx=1.0;
		sy=1.0;
		sz=1.0;
	}
	else{ // read in data
		if(fscanf(fd,"%lf\n%lf\n%lf\n%lf\n%lf\n%lf\n", &x,&y,&z,&sx,&sy,&sz)!=6){
			fprintf(stderr,"ERROR loading magnetometer calibration file, empty or malformed\n");
			fprintf(stderr,"please run rc_calibrate_mag to make a new calibration file\n");
			fprintf(stderr,"using default offsets for now\n");
			x=0.0;
			y=0.0;
			z=0.0;
			sx=1.0;
			sy=1.0;
			sz=1.0;
		}
		fclose(fd);
	}

	#ifdef DEBUG
	printf("magcal: %lf %lf %lf %lf %lf %lf\n", x,y,z,sx,sy,sz);
	#endif

	// write to global variables for use by rc_mpu_read_mag
	mag_offsets[0]=x;
	mag_offsets[1]=y;
	mag_offsets[2]=z;
	mag_scales[0]=sx;
	mag_scales[1]=sy;
	mag_scales[2]=sz;

	return 0;
}

/**
 * Loads steady state accel offsets from the disk and puts them in the IMU's
 * accel offset register. If no calibration file exists then make a new one.
 *
 * @return     0 on success, -1 on failure
 */
int __load_accel_calibration(void)
{
	FILE* fd;
	uint8_t raw[6] = {0,0,0,0,0,0};
	double x,y,z,sx,sy,sz; // offsets and scales in xyz
	int16_t bias[3], factory[3];

	fd = fopen(CALIBRATION_DIR ACCEL_CAL_FILE, "r");

	if(fd==NULL){
		// calibration file doesn't exist yet
		fprintf(stderr,"WARNING: no accelerometer calibration data found\n");
		fprintf(stderr,"Please run rc_calibrate_accel\n\n");
		// use zero offsets
		accel_lengths[0]=1.0;
		accel_lengths[1]=1.0;
		accel_lengths[2]=1.0;
		return 0;
	}
	// read in data
	if(fscanf(fd,"%lf\n%lf\n%lf\n%lf\n%lf\n%lf\n", &x,&y,&z,&sx,&sy,&sz)!=6){
		fprintf(stderr,"ERROR loading accel offsets, calibration file empty or malformed\n");
		fprintf(stderr,"please run rc_calibrate_accel to make a new calibration file\n");
		fprintf(stderr,"using default offsets for now\n");
		// use zero offsets
		accel_lengths[0]=1.0;
		accel_lengths[1]=1.0;
		accel_lengths[2]=1.0;
		return 0;
	}
	fclose(fd);


	#ifdef DEBUG
	printf("accel offsets: %lf %lf %lf\n", x, y, z);
	printf("accel scales:  %lf %lf %lf\n", sx, sy, sz);
	#endif

	// save scales globally
	accel_lengths[0]=sx;
	accel_lengths[1]=sy;
	accel_lengths[2]=sz;

	// read factory bias
	if(rc_i2c_read_bytes(config.i2c_bus, XA_OFFSET_H, 2, &raw[0])<0){
		return -1;
	}
	if(rc_i2c_read_bytes(config.i2c_bus, YA_OFFSET_H, 2, &raw[2])<0){
		return -1;
	}
	if(rc_i2c_read_bytes(config.i2c_bus, ZA_OFFSET_H, 2, &raw[4])<0){
		return -1;
	}
	// Turn the MSB and LSB into a signed 16-bit value
	factory[0] = (int16_t)(((uint16_t)raw[0]<<7)|(raw[1]>>1));
	factory[1] = (int16_t)(((uint16_t)raw[2]<<7)|(raw[3]>>1));
	factory[2] = (int16_t)(((uint16_t)raw[4]<<7)|(raw[5]>>1));

	// convert offset in g to bias register which is 15-bits, 16G FSR
	bias[0] = factory[0]-round(x/0.0009765615);
	bias[1] = factory[1]-round(y/0.0009765615);
	bias[2] = factory[2]-round(z/0.0009765615);

	// convert 16-bit bias to characters to write
	raw[0] = (bias[0] >> 7) & 0xFF;
	raw[1] = (bias[0] << 1) & 0xFF;
	raw[2] = (bias[1] >> 7) & 0xFF;
	raw[3] = (bias[1] << 1) & 0xFF;
	raw[4] = (bias[2] >> 7) & 0xFF;
	raw[5] = (bias[2] << 1) & 0xFF;

	// Push accel biases to hardware registers
	if(rc_i2c_write_bytes(config.i2c_bus, XA_OFFSET_H, 2, &raw[0])<0){
		fprintf(stderr,"ERROR: failed to write X accel offsets into IMU register\n");
		return -1;
	}
	if(rc_i2c_write_bytes(config.i2c_bus, YA_OFFSET_H, 2, &raw[2])<0){
		fprintf(stderr,"ERROR: failed to write Y accel offsets into IMU register\n");
		return -1;
	}
	if(rc_i2c_write_bytes(config.i2c_bus, ZA_OFFSET_H, 2, &raw[4])<0){
		fprintf(stderr,"ERROR: failed to write Z accel offsets into IMU register\n");
		return -1;
	}

	return 0;
}


/**
 * Reads steady state gyro offsets from the disk and puts them in the IMU's gyro
 * offset register. If no calibration file exists then make a new one.
 *
 * @param      offsets  The offsets
 *
 * @return     0 on success, -1 on failure
 */
int __write_gyro_cal_to_disk(int16_t offsets[3])
{
	FILE* fd;
	int ret;

	// make sure directory and calibration file exist and are writable first
	ret = mkdir(CALIBRATION_DIR, 0777);
	// error check, EEXIST is okay, we want directory to exist!
	if(ret==-1 && errno!=EEXIST){
		perror("ERROR in rc_calibrate_gyro_routine making calibration file directory");
		return -1;
	}

	// remove old file
	remove(CALIBRATION_DIR GYRO_CAL_FILE);

	fd = fopen(CALIBRATION_DIR GYRO_CAL_FILE, "w");
	if(fd==NULL){
		perror("ERROR in rc_calibrate_gyro_routine opening calibration file for writing");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/gyro.cal\n");
		return -1;
	}

	// write to the file, close, and exit
	if(fprintf(fd,"%d\n%d\n%d\n", offsets[0],offsets[1],offsets[2])<0){
		perror("ERROR in rc_calibrate_gyro_routine writing to file");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/gyro.cal\n");
		fclose(fd);
		return -1;
	}
	fclose(fd);

	// now give proper permissions
	if(chmod(CALIBRATION_DIR GYRO_CAL_FILE, S_IRWXU | S_IRWXG | S_IRWXO)==-1){
		perror("ERROR in rc_calibrate_gyro_routine setting correct permissions for file\n");
		fprintf(stderr, "writing file anyway, will probably still work\n");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/gyro.cal\n");
	}
	return 0;
}

/**
 * Writes magnetometer scale and offsets to disk. This is basically the origin
 * and dimensions of the ellipse made during calibration.
 *
 * @param      offsets  The offsets
 * @param      scale    The scale
 *
 * @return     0 on success, -1 on failure
 */
int __write_mag_cal_to_disk(double offsets[3], double scale[3])
{
	FILE* fd;
	int ret;

	// make sure directory and calibration file exist and are writable first
	ret = mkdir(CALIBRATION_DIR, 0777);
	// error check, EEXIST is okay, we want directory to exist!
	if(ret==-1 && errno!=EEXIST){
		perror("ERROR in rc_calibrate_mag_routine making calibration file directory");
		return -1;
	}
	// remove old file
	remove(CALIBRATION_DIR MAG_CAL_FILE);

	fd = fopen(CALIBRATION_DIR MAG_CAL_FILE, "w");
	if(fd==NULL){
		perror("ERROR in rc_calibrate_mag_routine opening calibration file for writing");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/mag.cal\n");
		return -1;
	}

	// write to the file, close, and exit
	ret = fprintf(fd,"%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n",
							offsets[0],
							offsets[1],
							offsets[2],
							scale[0],
							scale[1],
							scale[2]);
	if(ret<0){
		perror("ERROR in rc_calibrate_mag_routine writing to file\n");
		fclose(fd);
		return -1;
	}
	fclose(fd);

	// now give proper permissions
	if(chmod(CALIBRATION_DIR MAG_CAL_FILE, S_IRWXU | S_IRWXG | S_IRWXO)==-1){
		perror("ERROR in rc_calibrate_mag_routine setting correct permissions for file\n");
		fprintf(stderr, "writing file anyway, will probably still work\n");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/mag.cal\n");
	}

	return 0;
}

/**
 * Writes accelerometer scale and offsets to disk. This is basically the origin
 * and dimensions of the ellipse made during calibration.
 *
 * @param      center   The center of ellipsoid in g
 * @param      lengths  The lengths of ellipsoid in g
 *
 * @return     0 on success, -1 on failure
 */
int __write_accel_cal_to_disk(double* center, double* lengths)
{
	FILE* fd;
	int ret;

	// make sure directory and calibration file exist and are writable first
	ret = mkdir(CALIBRATION_DIR, 0777);
	// error check, EEXIST is okay, we want directory to exist!
	if(ret==-1 && errno!=EEXIST){
		perror("ERROR in rc_mpu_calibrate_accel_routine making calibration file directory");
		return -1;
	}
	// remove old file
	remove(CALIBRATION_DIR ACCEL_CAL_FILE);

	fd = fopen(CALIBRATION_DIR ACCEL_CAL_FILE, "w");
	if(fd==NULL){
		perror("ERROR in rc_mpu_calibrate_accel_routine opening calibration file for writing");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/accel.cal\n");
		return -1;
	}

	// write to the file, close, and exit
	ret = fprintf(fd,"%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n",
							center[0],
							center[1],
							center[2],
							lengths[0],
							lengths[1],
							lengths[2]);
	if(ret<0){
		perror("ERROR in rc_mpu_calibrate_accel_routine writing to file\n");
		fclose(fd);
		return -1;
	}
	fclose(fd);

	// now give proper permissions
	if(chmod(CALIBRATION_DIR ACCEL_CAL_FILE, S_IRWXU | S_IRWXG | S_IRWXO)==-1){
		perror("WARNING in rc_calibrate_accel_routine setting correct permissions for file\n");
		fprintf(stderr, "writing file anyway, will probably still work\n");
		fprintf(stderr, "most likely you ran this as root in the past and are now\n");
		fprintf(stderr, "running it as a normal user. try deleting the file\n");
		fprintf(stderr, "sudo rm /var/lib/robotcontrol/accel.cal\n");
	}
	return 0;
}



int rc_mpu_calibrate_gyro_routine(rc_mpu_config_t conf)
{
	uint8_t c, data[6];
	int32_t gyro_sum[3] = {0, 0, 0};
	int16_t offsets[3];
	was_last_steady = 1;

	// save bus and address globally for other functions to use
	config.i2c_bus = conf.i2c_bus;
	config.i2c_addr = conf.i2c_addr;

	// make sure the bus is not currently in use by another thread
	// do not proceed to prevent interfering with that process
	if(rc_i2c_get_lock(conf.i2c_bus)){
		fprintf(stderr,"i2c bus claimed by another process\n");
		fprintf(stderr,"aborting gyro calibration()\n");
		return -1;
	}

	// if it is not claimed, start the i2c bus
	if(rc_i2c_init(conf.i2c_bus, conf.i2c_addr)==-1){
		fprintf(stderr,"ERROR in rc_mpu_calibrate_gyro_routine, failed to init i2c bus\n");
		return -1;
	}

	// claiming the bus does no guarantee other code will not interfere
	// with this process, but best to claim it so other code can check
	// like we did above
	rc_i2c_lock_bus(conf.i2c_bus);

	// reset device, reset all registers
	if(__reset_mpu()==-1){
		fprintf(stderr,"ERROR in rc_mpu_calibrate_gyro_routine, failed to reset MPU9250\n");
		return -1;
	}

	// set up the IMU specifically for calibration.
	rc_i2c_write_byte(conf.i2c_bus, PWR_MGMT_1, 0x01);
	rc_i2c_write_byte(conf.i2c_bus, PWR_MGMT_2, 0x00);
	rc_usleep(200000);

	// // set bias registers to 0
	// // Push gyro biases to hardware registers
	// uint8_t zeros[] = {0,0,0,0,0,0};
	// if(rc_i2c_write_bytes(conf.i2c_bus, XG_OFFSET_H, 6, zeros)){
		// fprintf(stderr,"ERROR: failed to load gyro offsets into IMU register\n");
		// return -1;
	// }

	rc_i2c_write_byte(conf.i2c_bus, INT_ENABLE, 0x00);  // Disable all interrupts
	rc_i2c_write_byte(conf.i2c_bus, FIFO_EN, 0x00);     // Disable FIFO
	rc_i2c_write_byte(conf.i2c_bus, PWR_MGMT_1, 0x00);  // Turn on internal clock source
	rc_i2c_write_byte(conf.i2c_bus, I2C_MST_CTRL, 0x00);// Disable I2C master
	rc_i2c_write_byte(conf.i2c_bus, USER_CTRL, 0x00);   // Disable FIFO and I2C master
	rc_i2c_write_byte(conf.i2c_bus, USER_CTRL, 0x0C);   // Reset FIFO and DMP
	rc_usleep(15000);

	// Configure MPU9250 gyro and accelerometer for bias calculation
	rc_i2c_write_byte(conf.i2c_bus, CONFIG, 0x01);      // Set low-pass filter to 188 Hz
	rc_i2c_write_byte(conf.i2c_bus, SMPLRT_DIV, 0x04);  // Set sample rate to 200hz
	// Set gyro full-scale to 250 degrees per second, maximum sensitivity
	rc_i2c_write_byte(conf.i2c_bus, GYRO_CONFIG, 0x00);
	// Set accelerometer full-scale to 2 g, maximum sensitivity
	rc_i2c_write_byte(conf.i2c_bus, ACCEL_CONFIG, 0x00);

COLLECT_DATA:

	// Configure FIFO to capture gyro data for bias calculation
	rc_i2c_write_byte(conf.i2c_bus, USER_CTRL, 0x40);   // Enable FIFO
	// Enable gyro sensors for FIFO (max size 512 bytes in MPU-9250)
	c = FIFO_GYRO_X_EN|FIFO_GYRO_Y_EN|FIFO_GYRO_Z_EN;
	rc_i2c_write_byte(conf.i2c_bus, FIFO_EN, c);
	// 6 bytes per sample. 200hz. wait 0.4 seconds
	rc_usleep(400000);

	// At end of sample accumulation, turn off FIFO sensor read
	rc_i2c_write_byte(conf.i2c_bus, FIFO_EN, 0x00);
	// read FIFO sample count and log number of samples
	rc_i2c_read_bytes(conf.i2c_bus, FIFO_COUNTH, 2, &data[0]);
	int16_t fifo_count = ((uint16_t)data[0] << 8) | data[1];
	int samples = fifo_count/6;

	#ifdef DEBUG
	printf("calibration samples: %d\n", samples);
	#endif

	int i;
	int16_t x,y,z;
	rc_vector_t vx = rc_vector_empty();
	rc_vector_t vy = rc_vector_empty();
	rc_vector_t vz = rc_vector_empty();
	rc_vector_alloc(&vx,samples);
	rc_vector_alloc(&vy,samples);
	rc_vector_alloc(&vz,samples);
	double dev_x, dev_y, dev_z;
	gyro_sum[0] = 0;
	gyro_sum[1] = 0;
	gyro_sum[2] = 0;
	for (i=0; i<samples; i++) {
		// read data for averaging
		if(rc_i2c_read_bytes(conf.i2c_bus, FIFO_R_W, 6, data)<0){
			fprintf(stderr,"ERROR: failed to read FIFO\n");
			return -1;
		}
		x = (int16_t)(((int16_t)data[0] << 8) | data[1]) ;
		y = (int16_t)(((int16_t)data[2] << 8) | data[3]) ;
		z = (int16_t)(((int16_t)data[4] << 8) | data[5]) ;
		gyro_sum[0]  += (int32_t) x;
		gyro_sum[1]  += (int32_t) y;
		gyro_sum[2]  += (int32_t) z;
		vx.d[i] = (double)x;
		vy.d[i] = (double)y;
		vz.d[i] = (double)z;
	}
	dev_x = rc_vector_std_dev(vx);
	dev_y = rc_vector_std_dev(vy);
	dev_z = rc_vector_std_dev(vz);
	rc_vector_free(&vx);
	rc_vector_free(&vy);
	rc_vector_free(&vz);

	#ifdef DEBUG
	printf("gyro sums: %d %d %d\n", gyro_sum[0], gyro_sum[1], gyro_sum[2]);
	printf("std_deviation: %6.2f %6.2f %6.2f\n", dev_x, dev_y, dev_z);
	#endif

	// try again is standard deviation is too high
	if(dev_x>GYRO_CAL_THRESH||dev_y>GYRO_CAL_THRESH||dev_z>GYRO_CAL_THRESH){
		printf("Gyro data too noisy, put me down on a solid surface!\n");
		printf("trying again\n");
		was_last_steady = 0;
		goto COLLECT_DATA;
	}
	// this skips the first steady reading after a noisy reading
	// to make sure IMU has settled after being picked up.
	if(was_last_steady == 0){
		was_last_steady = 1;
		goto COLLECT_DATA;
	}
	// average out the samples
	offsets[0] = (int16_t) (gyro_sum[0]/(int32_t)samples);
	offsets[1] = (int16_t) (gyro_sum[1]/(int32_t)samples);
	offsets[2] = (int16_t) (gyro_sum[2]/(int32_t)samples);

	// also check for values that are way out of bounds
	if(abs(offsets[0])>GYRO_OFFSET_THRESH || abs(offsets[1])>GYRO_OFFSET_THRESH \
										|| abs(offsets[2])>GYRO_OFFSET_THRESH){
		printf("Gyro data out of bounds, put me down on a solid surface!\n");
		printf("trying again\n");
		goto COLLECT_DATA;
	}
	// done with I2C for now
	rc_i2c_unlock_bus(conf.i2c_bus);
	#ifdef DEBUG
	printf("offsets: %d %d %d\n", offsets[0], offsets[1], offsets[2]);
	#endif
	// write to disk
	if(__write_gyro_cal_to_disk(offsets)<0){
		fprintf(stderr,"ERROR in rc_calibrate_gyro_routine, failed to write to disk\n");
		return -1;
	}
	return 0;
}

int rc_mpu_calibrate_mag_routine(rc_mpu_config_t conf)
{
	int i;
	double new_scale[3];
	const int samples = 200;
	const int sample_time_us = 12000000; // 12 seconds ()
	const int loop_wait_us = sample_time_us/samples;
	const int sample_rate_hz = 1000000/loop_wait_us;

	rc_matrix_t A = rc_matrix_empty();
	rc_vector_t center = rc_vector_empty();
	rc_vector_t lengths = rc_vector_empty();
	rc_mpu_data_t imu_data; // to collect magnetometer data
	// wipe it with defaults to avoid problems
	config = rc_mpu_default_config();
	// configure with user's i2c bus info
	config.enable_magnetometer = 1;
	config.i2c_bus = conf.i2c_bus;
	config.i2c_addr = conf.i2c_addr;

	// make sure the bus is not currently in use by another thread
	// do not proceed to prevent interfering with that process
	if(rc_i2c_get_lock(config.i2c_bus)){
		fprintf(stderr,"i2c bus claimed by another process\n");
		fprintf(stderr,"aborting magnetometer calibration()\n");
		return -1;
	}

	// if it is not claimed, start the i2c bus
	if(rc_i2c_init(config.i2c_bus, config.i2c_addr)){
		fprintf(stderr,"ERROR rc_calibrate_mag_routine failed at rc_i2c_init\n");
		return -1;
	}

	// claiming the bus does no guarantee other code will not interfere
	// with this process, but best to claim it so other code can check
	// like we did above
	rc_i2c_lock_bus(config.i2c_bus);

	// reset device, reset all registers
	if(__reset_mpu()<0){
		fprintf(stderr,"ERROR: failed to reset MPU9250\n");
		return -1;
	}
	//check the who am i register to make sure the chip is alive
	if(__check_who_am_i()){
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}
	if(__init_magnetometer(1)){
		fprintf(stderr,"ERROR: failed to initialize_magnetometer\n");
		rc_i2c_unlock_bus(config.i2c_bus);
		return -1;
	}

	// set local calibration to initial values and prepare variables
	mag_offsets[0] = 0.0;
	mag_offsets[1] = 0.0;
	mag_offsets[2] = 0.0;
	mag_scales[0]  = 1.0;
	mag_scales[1]  = 1.0;
	mag_scales[2]  = 1.0;
	if(rc_matrix_alloc(&A,samples,3)){
		fprintf(stderr,"ERROR: in rc_calibrate_mag_routine, failed to alloc data matrix\n");
		return -1;
	}

	// sample data
	i = 0;
	while(i<samples){
		if(rc_mpu_read_mag(&imu_data)<0){
			fprintf(stderr,"ERROR: failed to read magnetometer\n");
			break;
		}
		// make sure the data is non-zero
		if(fabs(imu_data.mag[0]) < zero_tolerance &&
		   fabs(imu_data.mag[1]) < zero_tolerance &&
		   fabs(imu_data.mag[2]) < zero_tolerance){
			fprintf(stderr,"ERROR: retreived all zeros from magnetometer\n");
			break;
		}
		// save data to matrix for ellipse fitting
		A.d[i][0] = imu_data.mag[0];
		A.d[i][1] = imu_data.mag[1];
		A.d[i][2] = imu_data.mag[2];
		i++;

		// print "keep going" every 4 seconds
		if(i%(sample_rate_hz*4) == sample_rate_hz*2){
			printf("keep spinning\n");
		}
		// print "you're doing great" every 4 seconds
		if(i%(sample_rate_hz*4) == 0){
			printf("you're doing great\n");
		}

		rc_usleep(loop_wait_us);
	}
	// done with I2C for now
	rc_mpu_power_off();
	rc_i2c_unlock_bus(config.i2c_bus);

	printf("\n\nOkay Stop!\n");
	printf("Calculating calibration constants.....\n");
	fflush(stdout);

	// if data collection loop exited without getting enough data, warn the
	// user and return -1, otherwise keep going normally
	if(i<samples){
		printf("exiting rc_calibrate_mag_routine without saving new data\n");
		return -1;
	}
	// make empty vectors for ellipsoid fitting to populate
	if(rc_algebra_fit_ellipsoid(A,&center,&lengths)<0){
		fprintf(stderr,"failed to fit ellipsoid to magnetometer data\n");
		rc_matrix_free(&A);
		return -1;
	}
	// empty memory, we are done with A
	rc_matrix_free(&A);
	// do some sanity checks to make sure data is reasonable
	if(fabs(center.d[0])>200 || fabs(center.d[1])>200 || \
							fabs(center.d[2])>200){
		fprintf(stderr,"ERROR: center of fitted ellipsoid out of bounds\n");
		rc_vector_free(&center);
		rc_vector_free(&lengths);
		return -1;
	}
	if( lengths.d[0]>200 || lengths.d[0]<5 || \
		lengths.d[1]>200 || lengths.d[1]<5 || \
		lengths.d[2]>200 || lengths.d[2]<5){
		fprintf(stderr,"WARNING: length of fitted ellipsoid out of bounds\n");
		fprintf(stderr,"Saving suspicious calibration data anyway in case this is intentional\n");
	}
	// all seems well, calculate scaling factors to map ellipse lengths to
	// a sphere of radius 70uT, this scale will later be multiplied by the
	// factory corrected data
	new_scale[0] = 70.0/lengths.d[0];
	new_scale[1] = 70.0/lengths.d[1];
	new_scale[2] = 70.0/lengths.d[2];
	// print results
	printf("\n");
	printf("Offsets X: %7.3f Y: %7.3f Z: %7.3f\n",	center.d[0],\
							center.d[1],\
							center.d[2]);
	printf("Scales  X: %7.3f Y: %7.3f Z: %7.3f\n",	new_scale[0],\
							new_scale[1],\
							new_scale[2]);
	// write to disk
	if(__write_mag_cal_to_disk(center.d,new_scale)<0){
		rc_vector_free(&center);
		rc_vector_free(&lengths);
		return -1;
	}
	rc_vector_free(&center);
	rc_vector_free(&lengths);
	return 0;
}

/**
 * @brief      collects a second of accelerometer data
 *
 * @param      avg   pointer to array to write averages to
 *
 * @return     0 on success, -1 on error, 1 if data was just too noisy
 */
int __collect_accel_samples(int* avg_raw)
{
	uint8_t data[6];
	int32_t sum[3];
	int i, samples, fifo_count;
	int16_t x,y,z;
	double dev_x, dev_y, dev_z;
	rc_vector_t vx = rc_vector_empty();
	rc_vector_t vy = rc_vector_empty();
	rc_vector_t vz = rc_vector_empty();

	// Configure FIFO to capture gyro data for bias calculation
	rc_i2c_write_byte(config.i2c_bus, USER_CTRL, 0x40);   // Enable FIFO
	// Enable accel sensors for FIFO (max size 512 bytes in MPU-9250)
	rc_i2c_write_byte(config.i2c_bus, FIFO_EN, FIFO_ACCEL_EN);
	// 6 bytes per sample. 200hz. wait 0.4 seconds
	rc_usleep(400000);

	// At end of sample accumulation, turn off FIFO sensor read
	rc_i2c_write_byte(config.i2c_bus, FIFO_EN, 0x00);
	// read FIFO sample count and log number of samples
	rc_i2c_read_bytes(config.i2c_bus, FIFO_COUNTH, 2, &data[0]);
	fifo_count = ((uint16_t)data[0] << 8) | data[1];
	samples = fifo_count/6;

	#ifdef DEBUG
	printf("calibration samples: %d\n", samples);
	#endif

	rc_vector_alloc(&vx,samples);
	rc_vector_alloc(&vy,samples);
	rc_vector_alloc(&vz,samples);

	sum[0] = 0;
	sum[1] = 0;
	sum[2] = 0;
	for (i=0; i<samples; i++) {
		// read data for averaging
		if(rc_i2c_read_bytes(config.i2c_bus, FIFO_R_W, 6, data)<0){
			fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, failed to read FIFO\n");
			return -1;
		}
		x = (int16_t)(((int16_t)data[0] << 8) | data[1]) ;
		y = (int16_t)(((int16_t)data[2] << 8) | data[3]) ;
		z = (int16_t)(((int16_t)data[4] << 8) | data[5]) ;
		sum[0]  += (int32_t) x;
		sum[1]  += (int32_t) y;
		sum[2]  += (int32_t) z;
		vx.d[i] = (double)x;
		vy.d[i] = (double)y;
		vz.d[i] = (double)z;
	}
	dev_x = rc_vector_std_dev(vx);
	dev_y = rc_vector_std_dev(vy);
	dev_z = rc_vector_std_dev(vz);
	rc_vector_free(&vx);
	rc_vector_free(&vy);
	rc_vector_free(&vz);

	#ifdef DEBUG
	printf("sums: %d %d %d\n", sum[0], sum[1], sum[2]);
	printf("std_deviation: %6.2f %6.2f %6.2f\n", dev_x, dev_y, dev_z);
	#endif

	// try again if standard deviation is too high
	if(dev_x>ACCEL_CAL_THRESH||dev_y>ACCEL_CAL_THRESH||dev_z>ACCEL_CAL_THRESH){
		was_last_steady = 0;
		printf("data too noisy, please hold me still\n");
		return 1;
	}
	// this skips the first steady reading after a noisy reading
	// to make sure IMU has settled after being picked up.
	if(was_last_steady == 0){
		was_last_steady = 1;
		return 1;
	}
	// average out the samples
	avg_raw[0] = (sum[0]/(int32_t)samples);
	avg_raw[1] = (sum[1]/(int32_t)samples);
	avg_raw[2] = (sum[2]/(int32_t)samples);

	#ifdef DEBUG
	printf("avg: %d %d %d\n", avg_raw[0],avg_raw[1],avg_raw[2]);
	#endif
	return 0;

}

int rc_mpu_calibrate_accel_routine(rc_mpu_config_t conf)
{
	int ret, i, j;
	int avg_raw[6][3];

	// save bus and address globally for other functions to use
	config.i2c_bus = conf.i2c_bus;
	config.i2c_addr = conf.i2c_addr;

	// make sure the bus is not currently in use by another thread
	// do not proceed to prevent interfering with that process
	if(rc_i2c_get_lock(config.i2c_bus)){
		fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, i2c bus claimed by another process\n");
		return -1;
	}

	// if it is not claimed, start the i2c bus
	if(rc_i2c_init(config.i2c_bus, config.i2c_addr)){
		fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, failed at rc_i2c_init\n");
		return -1;
	}

	// claiming the bus does no guarantee other code will not interfere
	// with this process, but best to claim it so other code can check
	// like we did above
	rc_i2c_lock_bus(config.i2c_bus);

	// reset device, reset all registers
	if(__reset_mpu()<0){
		fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine failed to reset MPU9250\n");
		return -1;
	}

	// set up the IMU specifically for calibration.
	rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, 0x01);
	rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_2, 0x00);
	rc_usleep(200000);

	rc_i2c_write_byte(config.i2c_bus, INT_ENABLE, 0x00);	// Disable all interrupts
	rc_i2c_write_byte(config.i2c_bus, FIFO_EN, 0x00);	// Disable FIFO
	rc_i2c_write_byte(config.i2c_bus, PWR_MGMT_1, 0x00);	// Turn on internal clock source
	rc_i2c_write_byte(config.i2c_bus, I2C_MST_CTRL, 0x00);	// Disable I2C master
	rc_i2c_write_byte(config.i2c_bus, USER_CTRL, 0x00);	// Disable FIFO and I2C master
	rc_i2c_write_byte(config.i2c_bus, USER_CTRL, 0x0C);	// Reset FIFO and DMP
	rc_usleep(15000);

	// Configure MPU9250 gyro and accelerometer for bias calculation
	rc_i2c_write_byte(config.i2c_bus, CONFIG, 0x01);	// Set low-pass filter to 188 Hz
	rc_i2c_write_byte(config.i2c_bus, SMPLRT_DIV, 0x04);	// Set sample rate to 200hz
	rc_i2c_write_byte(config.i2c_bus, GYRO_CONFIG, 0x00);	// set G FSR to 250dps
	rc_i2c_write_byte(config.i2c_bus, ACCEL_CONFIG, 0x00);	// set A FSR to 2G


	// collect an orientation
	printf("\nOrient Z pointing up and hold as still as possible\n");
	printf("When ready, press any key to sample accelerometer\n");
	getchar();
	ret = 1;
	was_last_steady=0;
	while(ret){
		ret=__collect_accel_samples(avg_raw[0]);
		if(ret==-1) return -1;
	}
	printf("success\n");
	// collect an orientation
	printf("\nOrient Z pointing down and hold as still as possible\n");
	printf("When ready, press any key to sample accelerometer\n");
	getchar();
	ret = 1;
	was_last_steady=0;
	while(ret){
		ret=__collect_accel_samples(avg_raw[1]);
		if(ret==-1) return -1;
	}
	printf("success\n");
	// collect an orientation
	printf("\nOrient X pointing up and hold as still as possible\n");
	printf("When ready, press any key to sample accelerometer\n");
	getchar();
	ret = 1;
	was_last_steady=0;
	while(ret){
		ret=__collect_accel_samples(avg_raw[2]);
		if(ret==-1) return -1;
	}
	printf("success\n");
	// collect an orientation
	printf("\nOrient X pointing down and hold as still as possible\n");
	printf("When ready, press any key to sample accelerometer\n");
	getchar();
	ret = 1;
	was_last_steady=0;
	while(ret){
		ret=__collect_accel_samples(avg_raw[3]);
		if(ret==-1) return -1;
	}
	printf("success\n");
	// collect an orientation
	printf("\nOrient Y pointing up and hold as still as possible\n");
	printf("When ready, press any key to sample accelerometer\n");
	getchar();
	ret = 1;
	was_last_steady=0;
	while(ret){
		ret=__collect_accel_samples(avg_raw[4]);
		if(ret==-1) return -1;
	}
	printf("success\n");
	// collect an orientation
	printf("\nOrient Y pointing down and hold as still as possible\n");
	printf("When ready, press any key to sample accelerometer\n");
	getchar();
	ret = 1;
	was_last_steady=0;
	while(ret){
		ret=__collect_accel_samples(avg_raw[5]);
		if(ret==-1) return -1;
	}
	printf("success\n");

	// done with I2C for now
	rc_mpu_power_off();
	rc_i2c_unlock_bus(config.i2c_bus);

	// fit the ellipse
	rc_matrix_t A = rc_matrix_empty();
	rc_vector_t center = rc_vector_empty();
	rc_vector_t lengths = rc_vector_empty();

	if(rc_matrix_alloc(&A,6,3)){
		fprintf(stderr,"ERROR: in rc_mpu_calibrate_accel_routine, failed to alloc data matrix\n");
		return -1;
	}

	// convert to G and put in matrix
	for(i=0;i<6;i++){
		for(j=0;j<3;j++){
			A.d[i][j] = (avg_raw[i][j]/16384.0);
		}
	}

	// make empty vectors for ellipsoid fitting to populate
	if(rc_algebra_fit_ellipsoid(A,&center,&lengths)<0){
		fprintf(stderr,"failed to fit ellipsoid to magnetometer data\n");
		rc_matrix_free(&A);
		return -1;
	}
	// empty memory, we are done with A
	rc_matrix_free(&A);
	// do some sanity checks to make sure data is reasonable
	for(i=0;i<3;i++){
		if(fabs(center.d[i])>0.3){
			fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, center of fitted ellipsoid out of bounds\n");
			fprintf(stderr,"most likely the unit was held in incorrect orientation during data collection\n");
			rc_vector_free(&center);
			rc_vector_free(&lengths);
			return -1;
		}
		if(isnan(center.d[i]) || isnan(lengths.d[i])){
			fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, data fitting produced NaN\n");
			fprintf(stderr,"most likely the unit was held in incorrect orientation during data collection\n");
			rc_vector_free(&center);
			rc_vector_free(&lengths);
			return -1;
		}
		if(lengths.d[i]>1.3 || lengths.d[i]<0.7){
			fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, scale out of bounds\n");
			fprintf(stderr,"most likely the unit was held in incorrect orientation during data collection\n");
			rc_vector_free(&center);
			rc_vector_free(&lengths);
			return -1;
		}
	}

	// print results
	printf("\n");
	printf("Offsets X: %7.3f Y: %7.3f Z: %7.3f\n",	center.d[0],\
							center.d[1],\
							center.d[2]);
	printf("Scales  X: %7.3f Y: %7.3f Z: %7.3f\n",	lengths.d[0],\
							lengths.d[1],\
							lengths.d[2]);

	// write to disk
	if(__write_accel_cal_to_disk(center.d, lengths.d)==-1){
		fprintf(stderr,"ERROR in rc_mpu_calibrate_accel_routine, failed to write to disk\n");
		return -1;
	}
	rc_vector_free(&center);
	rc_vector_free(&lengths);
	return 0;
}


int rc_mpu_is_gyro_calibrated(void)
{
	if(!access(CALIBRATION_DIR GYRO_CAL_FILE, F_OK)) return 1;
	else return 0;
}

int rc_mpu_is_mag_calibrated(void)
{
	if(!access(CALIBRATION_DIR MAG_CAL_FILE, F_OK)) return 1;
	else return 0;
}

int rc_mpu_is_accel_calibrated(void)
{
	if(!access(CALIBRATION_DIR ACCEL_CAL_FILE, F_OK)) return 1;
	else return 0;
}



int64_t rc_mpu_nanos_since_last_dmp_interrupt(void)
{
	if(last_interrupt_timestamp_nanos==0) return -1;
	return rc_nanos_since_epoch() - last_interrupt_timestamp_nanos;
}

int64_t rc_mpu_nanos_since_last_tap(void)
{
	if(last_tap_timestamp_nanos==0) return -1;
	return rc_nanos_since_epoch() - last_tap_timestamp_nanos;
}

int rc_mpu_block_until_dmp_data(void)
{
	if(imu_shutdown_flag!=0){
		fprintf(stderr,"ERROR: call to rc_mpu_block_until_dmp_data after shutting down mpu\n");
		return -1;
	}
	if(!thread_running_flag){
		fprintf(stderr,"ERROR: call to rc_mpu_block_until_dmp_data when DMP handler not running\n");
		return -1;
	}
	// wait for condition signal which unlocks mutex
	pthread_mutex_lock(&read_mutex);
	pthread_cond_wait(&read_condition, &read_mutex);
	pthread_mutex_unlock(&read_mutex);
	// check if condition was broadcast due to shutdown
	if(imu_shutdown_flag) return 1;
	// otherwise return 0 on actual button press
	return 0;
}

int rc_mpu_block_until_tap(void)
{
	if(imu_shutdown_flag!=0){
		fprintf(stderr,"ERROR: call to rc_mpu_block_until_tap after shutting down mpu\n");
		return -1;
	}
	if(!thread_running_flag){
		fprintf(stderr,"ERROR: call to rc_mpu_block_until_tap when DMP handler not running\n");
		return -1;
	}
	// wait for condition signal which unlocks mutex
	pthread_mutex_lock(&tap_mutex);
	pthread_cond_wait(&tap_condition, &tap_mutex);
	pthread_mutex_unlock(&tap_mutex);
	// check if condition was broadcast due to shutdown
	if(imu_shutdown_flag) return 1;
	// otherwise return 0 on actual button press
	return 0;
}

static int __mag_correct_orientation(double mag_vec[3])
{
	// create a quaternion vector from the current magnetic field vector
	// in IMU body coordinate frame. Since the DMP quaternion is aligned with
	// a particular orientation, we must be careful to orient the magnetometer
	// data to match.
	switch(config.orient){
	case ORIENTATION_Z_UP:
		mag_vec[0] = data_ptr->mag[TB_PITCH_X];
		mag_vec[1] = data_ptr->mag[TB_ROLL_Y];
		mag_vec[2] = data_ptr->mag[TB_YAW_Z];
		break;
	case ORIENTATION_Z_DOWN:
		mag_vec[0] = -data_ptr->mag[TB_PITCH_X];
		mag_vec[1] = data_ptr->mag[TB_ROLL_Y];
		mag_vec[2] = -data_ptr->mag[TB_YAW_Z];
		break;
	case ORIENTATION_X_UP:
		mag_vec[0] = -data_ptr->mag[TB_YAW_Z];
		mag_vec[1] = data_ptr->mag[TB_ROLL_Y];
		mag_vec[2] = data_ptr->mag[TB_PITCH_X];
		break;
	case ORIENTATION_X_DOWN:
		mag_vec[0] = data_ptr->mag[TB_YAW_Z];
		mag_vec[1] = data_ptr->mag[TB_ROLL_Y];
		mag_vec[2] = -data_ptr->mag[TB_PITCH_X];
		break;
	case ORIENTATION_Y_UP:
		mag_vec[0] = data_ptr->mag[TB_PITCH_X];
		mag_vec[1] = -data_ptr->mag[TB_YAW_Z];
		mag_vec[2] = data_ptr->mag[TB_ROLL_Y];
		break;
	case ORIENTATION_Y_DOWN:
		mag_vec[0] = data_ptr->mag[TB_PITCH_X];
		mag_vec[1] = data_ptr->mag[TB_YAW_Z];
		mag_vec[2] = -data_ptr->mag[TB_ROLL_Y];
		break;
	case ORIENTATION_X_FORWARD:
		mag_vec[0] = -data_ptr->mag[TB_ROLL_Y];
		mag_vec[1] = data_ptr->mag[TB_PITCH_X];
		mag_vec[2] = data_ptr->mag[TB_YAW_Z];
		break;
	case ORIENTATION_X_BACK:
		mag_vec[0] = data_ptr->mag[TB_ROLL_Y];
		mag_vec[1] = -data_ptr->mag[TB_PITCH_X];
		mag_vec[2] = data_ptr->mag[TB_YAW_Z];
		break;
	default:
		fprintf(stderr,"ERROR: reading magnetometer, invalid orientation\n");
		return -1;
	}
	return 0;
}
// Phew, that was a lot of code....