psiphi75 / esp-mpu9250

MPU9250 driver for the ESP8266 and the ESP32
Apache License 2.0
34 stars 18 forks source link

Drift and Calibration Issues #4

Closed thomasnugent closed 4 years ago

thomasnugent commented 4 years ago

Hey psiphi75, I was wondering if you experienced any issues with drift and/or some really bad performance due to calibration?

I've tried to calibrate my MPU many times and found that one of my two MPU's works quite well, while the other is quite bad (i.e. sometimes pitch is interpreted as roll basically). In both sensors, I've found that there is a small amount of drift too, whereas I thought the MadgwickAHRS algorithm is thought to be really good at reducing this?

I know it's been ages since you've worked on this project but I thought I'd just double check whether it worked perfectly for you or not!

psiphi75 commented 4 years ago

Can you attach the raw, unfiltered, uncalibrated, readings from the mpu. Then also the calibration values.

thomasnugent commented 4 years ago

Thanks for your quick reply! I'm working in this forked repo if it helps.

Calibration Values

I've tried a number of these after calibrating as per instructions: .mag_offset = {.x = -9.301373, .y = -18.240196, .z = 20.286949}, .mag_scale = {.x = 1.578519, .y = 1.329861, .z = 1.627243}, .accel_offset = {.x = -0.328293, .y = 0.268094, .z = -0.645369}, .accel_scale_lo = {.x = -1.220864, .y = -0.839191, .z = -1.422819}, .accel_scale_hi = {.x = 0.818928, .y = 1.185486, .z = 0.685583} .gyro_bias_offset = {.x = -0.456019, .y = 0.010889, .z = -0.372210},

Try again #2: .mag_offset = {.x = 11.593460, .y = -42.728920, .z = 81.969849}, .mag_scale = {.x = 1.468301, .y = 1.603489, .z = 1.438226}, .accel_offset = {.x = -0.153241, .y = 0.139184, .z = 0.186212}, .accel_scale_lo = {.x = -1.100374, .y = -0.919392, .z = -0.868380}, .accel_scale_hi = {.x = 0.915838, .y = 1.085994, .z = 1.161044}, .gyro_bias_offset = {.x = -3.053099, .y = 0.869566, .z = 0.341163}

Try again with double magnetometer readings (so that min/max values are more reliable): .mag_offset = {.x = 8.787285, .y = 108.107750, .z = -165.139145}, .mag_scale = {.x = 1.664211, .y = 1.407706, .z = 1.451929}, .accel_offset = {.x = 0.273095, .y = -0.196444, .z = -0.230595}, .accel_scale_lo = {.x = 1.097297, .y = 0.925665, .z = 0.907561}, .accel_scale_hi = {.x = -0.876847, .y = -1.062055, .z = -1.084525}, .gyro_bias_offset = {.x = 2.616736, .y = -1.975317, .z = -1.188345}

Logs

The temp where I am is certainly not 30 deg C. Here I rotate about the X, then Y, then Z axis. The Z axis is aligned with gravity.

gx:    691.00 gy:    -50.00 gz:     14.00 ax:     14.00 ay:     38.00 az:  -7770.00 mx:     45.70 my:     62.32 mz:     31.72 temp:     30.01C
gx:    503.00 gy:     49.00 gz:    175.00 ax:    -22.00 ay:     22.00 az:  -7974.00 mx:     49.22 my:     61.14 mz:     31.72 temp:     30.34C
gx:    357.00 gy:    559.00 gz:    112.00 ax:     -6.00 ay:     -2.00 az:  -7692.00 mx:     46.88 my:     61.14 mz:     32.85 temp:     30.06C
gx:    172.00 gy:    231.00 gz:    265.00 ax:    -44.00 ay:    -14.00 az:  -7910.00 mx:     44.53 my:     61.14 mz:     33.98 temp:     30.20C
gx:   1199.00 gy:   -209.00 gz:    -22.00 ax:    -54.00 ay:    -20.00 az:  -8140.00 mx:     48.05 my:     62.32 mz:     31.72 temp:     30.01C
gx:   3551.00 gy:   -725.00 gz:    634.00 ax:     88.00 ay:   -270.00 az:  -8302.00 mx:     44.53 my:     62.32 mz:     33.98 temp:     30.34C
gx:   8967.00 gy:   -378.00 gz:   2288.00 ax:     46.00 ay:   -692.00 az:  -8866.00 mx:     43.36 my:     59.96 mz:     33.98 temp:     30.01C
gx:  14605.00 gy:    849.00 gz:   2412.00 ax:   -296.00 ay:  -1002.00 az:  -9334.00 mx:     37.50 my:     62.32 mz:     37.38 temp:     30.15C
gx:  21650.00 gy:   1475.00 gz:   2519.00 ax:   -234.00 ay:  -1880.00 az:  -8764.00 mx:     28.12 my:     62.32 mz:     37.38 temp:     30.20C
gx:  26675.00 gy:   2374.00 gz:   3025.00 ax:    -96.00 ay:  -3014.00 az:  -7858.00 mx:     15.23 my:     63.49 mz:     40.78 temp:     30.39C
gx:  28857.00 gy:   2015.00 gz:   3093.00 ax:     -4.00 ay:  -4192.00 az:  -6984.00 mx:     14.06 my:     63.49 mz:     38.52 temp:     30.39C
gx:  29830.00 gy:  -1523.00 gz:    732.00 ax:   -364.00 ay:  -4690.00 az:  -5942.00 mx:     -8.20 my:     63.49 mz:     33.98 temp:     30.01C
gx:  30932.00 gy:  -4943.00 gz:   1012.00 ax:    888.00 ay:  -5752.00 az:  -4184.00 mx:    -22.27 my:     61.14 mz:     27.19 temp:     30.20C
gx:  32767.00 gy:  -7889.00 gz:    231.00 ax:  -1174.00 ay:  -7216.00 az:  -2822.00 mx:    -33.98 my:     58.79 mz:     16.99 temp:     30.06C
gx:  32767.00 gy:  -3767.00 gz:  -2083.00 ax:   -390.00 ay:  -6090.00 az:   -368.00 mx:    -43.36 my:     56.44 mz:      3.40 temp:     30.15C
gx:  32767.00 gy:  -1950.00 gz:   -805.00 ax:    158.00 ay:  -7548.00 az:   2382.00 mx:    -49.22 my:     58.79 mz:    -11.33 temp:     30.15C
gx:  29456.00 gy:  -3564.00 gz:   1695.00 ax:     14.00 ay:  -6418.00 az:   4346.00 mx:    -49.22 my:     58.79 mz:    -11.33 temp:     30.34C
gx:  14363.00 gy:   1741.00 gz:  -2288.00 ax:    222.00 ay:  -6216.00 az:   7102.00 mx:    -52.73 my:     56.44 mz:    -38.52 temp:     30.25C
gx:  -4580.00 gy:   2354.00 gz:    -79.00 ax:    -68.00 ay:  -6752.00 az:   7344.00 mx:    -52.73 my:     58.79 mz:    -41.91 temp:     30.49C
gx: -17549.00 gy:   3347.00 gz:   2860.00 ax:    114.00 ay:  -5608.00 az:   6032.00 mx:    -53.91 my:     54.09 mz:    -39.65 temp:     30.39C
gx: -23368.00 gy:   4777.00 gz:   2821.00 ax:   -524.00 ay:  -7706.00 az:   4268.00 mx:    -53.91 my:     56.44 mz:    -30.59 temp:     30.06C
gx: -30735.00 gy:   -190.00 gz:   1864.00 ax:   -804.00 ay:  -6438.00 az:   2680.00 mx:    -53.91 my:     56.44 mz:    -21.52 temp:     29.87C
gx: -32768.00 gy:  -2570.00 gz:    518.00 ax:   -416.00 ay:  -8234.00 az:    378.00 mx:    -53.91 my:     56.44 mz:    -21.52 temp:     30.06C
gx: -32768.00 gy:  -3950.00 gz:     99.00 ax:  -1502.00 ay:  -6328.00 az:  -2376.00 mx:    -38.67 my:     52.91 mz:     12.46 temp:     30.15C
gx: -32768.00 gy:  -1475.00 gz:  -4935.00 ax:   -842.00 ay:  -4538.00 az:  -4602.00 mx:    -22.27 my:     49.38 mz:     29.45 temp:     30.06C
gx: -32768.00 gy:   2063.00 gz:  -5329.00 ax:   -868.00 ay:  -4260.00 az:  -6216.00 mx:     -3.52 my:     52.91 mz:     38.52 temp:     30.06C
gx: -32768.00 gy:  -5455.00 gz:  -2290.00 ax:    -24.00 ay:  -2354.00 az:  -6326.00 mx:     15.23 my:     54.09 mz:     39.65 temp:     30.39C
gx: -26554.00 gy:  -3239.00 gz:    862.00 ax:   -702.00 ay:  -1604.00 az:  -8682.00 mx:     23.44 my:     51.73 mz:     41.91 temp:     30.01C
gx: -17196.00 gy:  -2074.00 gz:    139.00 ax:   -746.00 ay:   -624.00 az:  -8448.00 mx:     43.36 my:     48.21 mz:     40.78 temp:     30.25C
gx: -27516.00 gy:   2127.00 gz:   5999.00 ax:   -666.00 ay:   1126.00 az:  -7772.00 mx:     50.39 my:     49.38 mz:     35.12 temp:     30.25C
gx: -32768.00 gy:  -8501.00 gz:    884.00 ax:  -2098.00 ay:   3408.00 az:  -7574.00 mx:     65.62 my:     50.56 mz:     22.66 temp:     30.15C
gx: -32768.00 gy: -10569.00 gz:  -1042.00 ax:  -1644.00 ay:   5786.00 az:  -5328.00 mx:     82.03 my:     48.21 mz:      9.06 temp:     30.39C
gx: -32768.00 gy:  -7800.00 gz:  -2324.00 ax:  -1886.00 ay:   7766.00 az:  -4538.00 mx:     91.41 my:     47.03 mz:    -13.59 temp:     30.15C
gx: -32768.00 gy:  -6966.00 gz:   1978.00 ax:  -1878.00 ay:   7720.00 az:   -848.00 mx:     91.41 my:     47.03 mz:    -13.59 temp:     30.15C
gx: -32768.00 gy:   -591.00 gz:   3282.00 ax:  -2056.00 ay:   9966.00 az:    502.00 mx:     90.23 my:     47.03 mz:    -60.04 temp:     30.15C
gx: -32768.00 gy:  -2923.00 gz:   2092.00 ax:  -1600.00 ay:   8728.00 az:   2916.00 mx:     82.03 my:     49.38 mz:    -79.30 temp:     30.15C
gx: -27987.00 gy:   1104.00 gz:   3955.00 ax:  -1900.00 ay:   8018.00 az:   4918.00 mx:     69.14 my:     50.56 mz:    -92.89 temp:     30.34C
gx: -22817.00 gy:  -5429.00 gz:   1970.00 ax:  -1576.00 ay:   6770.00 az:   5864.00 mx:     53.91 my:     51.73 mz:   -100.82 temp:     30.25C
gx:  -8462.00 gy:   -428.00 gz:   2803.00 ax:   -792.00 ay:   5224.00 az:   4896.00 mx:     48.05 my:     51.73 mz:   -103.09 temp:     30.25C
gx:   8098.00 gy:   2633.00 gz:    886.00 ax:  -1028.00 ay:   6132.00 az:   5536.00 mx:     48.05 my:     51.73 mz:   -103.09 temp:     30.25C
gx:  22890.00 gy:   3161.00 gz:    247.00 ax:  -1054.00 ay:   6752.00 az:   5594.00 mx:     52.73 my:     51.73 mz:    -98.55 temp:     30.39C
gx:  27855.00 gy:   7233.00 gz:   2441.00 ax:  -1152.00 ay:   7694.00 az:   4418.00 mx:     63.28 my:     54.09 mz:    -94.02 temp:     30.25C
gx:  28447.00 gy:  10020.00 gz:   5121.00 ax:  -2410.00 ay:   8602.00 az:    504.00 mx:     72.66 my:     49.38 mz:    -88.36 temp:     30.15C
gx:  30737.00 gy:   4474.00 gz:   9392.00 ax:  -1206.00 ay:   8534.00 az:   1684.00 mx:     79.69 my:     45.86 mz:    -75.90 temp:     30.39C
gx:  32767.00 gy:   5207.00 gz:   7577.00 ax:  -1002.00 ay:   9586.00 az:    336.00 mx:     83.20 my:     49.38 mz:    -70.23 temp:     30.20C
gx:  32767.00 gy:  -1261.00 gz:   4155.00 ax:  -1372.00 ay:   8050.00 az:  -5092.00 mx:     91.41 my:     50.56 mz:    -49.84 temp:     30.34C
gx:  32767.00 gy:   4141.00 gz:   8525.00 ax:    496.00 ay:   7876.00 az:  -4392.00 mx:     89.06 my:     56.44 mz:    -32.85 temp:     30.15C
gx:  32767.00 gy:  -1554.00 gz:   6464.00 ax:    204.00 ay:   5970.00 az:  -5118.00 mx:     86.72 my:     61.14 mz:    -14.73 temp:     30.01C
gx:  31258.00 gy:  -2196.00 gz:   3550.00 ax:    210.00 ay:   4650.00 az:  -5684.00 mx:     75.00 my:     61.14 mz:      3.40 temp:     30.01C
gx:  22574.00 gy:   2164.00 gz:   5905.00 ax:    888.00 ay:   2700.00 az:  -6308.00 mx:     69.14 my:     64.67 mz:     13.59 temp:     30.15C
gx:  17852.00 gy:   6891.00 gz:   2901.00 ax:    918.00 ay:   1244.00 az:  -7316.00 mx:     65.62 my:     62.32 mz:     15.86 temp:     30.34C
gx:  14458.00 gy:   8358.00 gz:   2906.00 ax:   1388.00 ay:    -68.00 az:  -6136.00 mx:     52.73 my:     69.37 mz:     20.39 temp:     30.34C
gx:   6785.00 gy:   5099.00 gz:   3654.00 ax:   1004.00 ay:    322.00 az:  -7038.00 mx:     48.05 my:     71.72 mz:     19.26 temp:     30.25C
gx:   4600.00 gy:  -4163.00 gz:    633.00 ax:   1686.00 ay:   -374.00 az:  -8034.00 mx:     43.36 my:     71.72 mz:     20.39 temp:     30.15C
gx:   2874.00 gy: -11633.00 gz:   2490.00 ax:   2796.00 ay:   -696.00 az:  -9568.00 mx:     44.53 my:     69.37 mz:     22.66 temp:     30.01C
gx:    437.00 gy:  -2171.00 gz:    192.00 ax:   1354.00 ay:  -1054.00 az:  -6988.00 mx:     42.19 my:     65.84 mz:     28.32 temp:     30.15C
gx:  -6082.00 gy:  -1665.00 gz:   -355.00 ax:   -412.00 ay:   -746.00 az:  -7198.00 mx:     43.36 my:     69.37 mz:     29.45 temp:     30.25C
gx:  -2864.00 gy:  -2569.00 gz:   2211.00 ax:   3248.00 ay:   -500.00 az:  -9460.00 mx:     45.70 my:     67.02 mz:     26.05 temp:     30.25C
gx:  -1027.00 gy:  -1230.00 gz:    830.00 ax:     94.00 ay:    -70.00 az:  -8352.00 mx:     43.36 my:     65.84 mz:     29.45 temp:     30.34C
gx:   -416.00 gy:  -5247.00 gz:    860.00 ax:    572.00 ay:   -376.00 az:  -8474.00 mx:     46.88 my:     64.67 mz:     28.32 temp:     30.06C
gx:    966.00 gy:  -6030.00 gz:   -502.00 ax:    204.00 ay:   -160.00 az:  -8244.00 mx:     48.05 my:     63.49 mz:     30.59 temp:     30.20C
gx:   -836.00 gy: -21728.00 gz:     57.00 ax:   -326.00 ay:   -492.00 az:  -9704.00 mx:     43.36 my:     61.14 mz:     33.98 temp:     30.06C
gx:   1202.00 gy: -21915.00 gz:    273.00 ax:  -1524.00 ay:    -80.00 az:  -8290.00 mx:     43.36 my:     61.14 mz:     33.98 temp:     30.25C
gx:    750.00 gy: -28863.00 gz:   -661.00 ax:  -1946.00 ay:   -348.00 az:  -7338.00 mx:     43.36 my:     36.45 mz:     45.31 temp:     30.25C
gx:  -1259.00 gy: -28616.00 gz:  -2236.00 ax:  -4158.00 ay:   -218.00 az:  -6678.00 mx:     44.53 my:     24.69 mz:     48.71 temp:     30.01C
gx:   1653.00 gy: -32768.00 gz:  -1731.00 ax:  -4124.00 ay:   -514.00 az:  -5450.00 mx:     46.88 my:      4.70 mz:     46.45 temp:     30.01C
gx:   1320.00 gy: -32768.00 gz:  -2060.00 ax:  -5942.00 ay:   -852.00 az:  -4422.00 mx:     43.36 my:    -10.58 mz:     44.18 temp:     30.39C
gx:  -1368.00 gy: -32768.00 gz:   1106.00 ax:  -7028.00 ay:    -76.00 az:  -1676.00 mx:     42.19 my:    -19.99 mz:     38.52 temp:     30.39C
gx:  -4925.00 gy: -32768.00 gz:   2862.00 ax:  -7288.00 ay:   -226.00 az:    836.00 mx:     42.19 my:    -19.99 mz:     38.52 temp:     30.01C
gx:  -3562.00 gy: -30977.00 gz:  -1690.00 ax:  -7944.00 ay:  -1238.00 az:   2006.00 mx:     48.05 my:    -51.73 mz:      1.13 temp:     30.34C
gx:  -3385.00 gy: -32768.00 gz:  -1419.00 ax:  -7056.00 ay:   -268.00 az:   4488.00 mx:     49.22 my:    -56.44 mz:    -16.99 temp:     30.20C
gx:   -653.00 gy: -29882.00 gz:  -2127.00 ax:  -6020.00 ay:  -1164.00 az:   5156.00 mx:     46.88 my:    -57.61 mz:    -30.59 temp:     30.15C
gx:   2376.00 gy: -20147.00 gz:    238.00 ax:  -4796.00 ay:   -790.00 az:   6422.00 mx:     44.53 my:    -56.44 mz:    -43.05 temp:     30.39C
gx:   1928.00 gy:  -7479.00 gz:  -3255.00 ax:  -4998.00 ay:   -742.00 az:   7910.00 mx:     46.88 my:    -56.44 mz:    -52.11 temp:     30.25C
gx:   3056.00 gy:  22052.00 gz:  -1965.00 ax:  -5232.00 ay:   -510.00 az:   8208.00 mx:     44.53 my:    -58.79 mz:    -50.98 temp:     30.15C
gx:   6402.00 gy:  32767.00 gz:  -2636.00 ax:  -5764.00 ay:   -772.00 az:   6042.00 mx:     42.19 my:    -59.96 mz:    -33.98 temp:     30.15C
gx:   4870.00 gy:  32767.00 gz:  -1410.00 ax:  -7294.00 ay:   -392.00 az:   4282.00 mx:     43.36 my:    -59.96 mz:    -14.73 temp:     30.34C
gx:   4965.00 gy:  32767.00 gz:  -1454.00 ax:  -6768.00 ay:   -494.00 az:   1940.00 mx:     41.02 my:    -52.91 mz:      3.40 temp:     29.87C
gx:   2210.00 gy:  32767.00 gz:  -2941.00 ax:  -8274.00 ay:   -372.00 az:    776.00 mx:     38.67 my:    -43.50 mz:     18.12 temp:     30.34C
gx:   4153.00 gy:  32767.00 gz:   -543.00 ax:  -7400.00 ay:   -546.00 az:  -1674.00 mx:     42.19 my:    -41.15 mz:     23.79 temp:     29.96C
gx:   5028.00 gy:  32767.00 gz:  -5902.00 ax:  -6314.00 ay:   -780.00 az:  -3638.00 mx:     33.98 my:    -10.58 mz:     40.78 temp:     30.25C
gx:    227.00 gy:  32767.00 gz:   2694.00 ax:   2902.00 ay:  -2558.00 az:  -2644.00 mx:     32.81 my:     22.34 mz:     47.58 temp:     30.25C
gx:   5202.00 gy:  32767.00 gz:  -5312.00 ax:    820.00 ay:  -2262.00 az:  -9020.00 mx:     32.81 my:     65.84 mz:     31.72 temp:     30.25C
gx:   7040.00 gy:  32767.00 gz:  -4481.00 ax:   4680.00 ay:  -1714.00 az:  -8476.00 mx:     36.33 my:     89.36 mz:      4.53 temp:     30.01C
gx:   5236.00 gy:  32767.00 gz:  -2028.00 ax:   5178.00 ay:  -1366.00 az:  -3924.00 mx:     32.81 my:     96.41 mz:    -19.26 temp:     30.15C
gx:   4566.00 gy:  32767.00 gz:  -1970.00 ax:   7434.00 ay:  -1750.00 az:  -3386.00 mx:     32.81 my:     96.41 mz:    -19.26 temp:     30.34C
gx:   3420.00 gy:  32767.00 gz:  -4471.00 ax:   6846.00 ay:  -1194.00 az:    124.00 mx:     35.16 my:     92.89 mz:    -61.17 temp:     30.25C
gx:   6466.00 gy:  32767.00 gz:  -1865.00 ax:   9038.00 ay:  -1682.00 az:    932.00 mx:     38.67 my:     82.30 mz:    -81.56 temp:     30.06C
gx:   9127.00 gy:  32767.00 gz:  -7291.00 ax:   7286.00 ay:   -832.00 az:   4130.00 mx:     43.36 my:     61.14 mz:    -95.16 temp:     30.15C
gx:   4288.00 gy:  32767.00 gz:   4252.00 ax:   5642.00 ay:   -472.00 az:   5748.00 mx:     50.39 my:     32.92 mz:   -106.48 temp:     30.15C
gx:   3306.00 gy:  28786.00 gz:   2038.00 ax:   3086.00 ay:   -442.00 az:   7548.00 mx:     48.05 my:     19.99 mz:   -104.22 temp:     30.25C
gx:   1086.00 gy:  17874.00 gz:  -6472.00 ax:   5220.00 ay:   1106.00 az:   3932.00 mx:     50.39 my:      3.53 mz:   -103.09 temp:     30.15C
gx:  -3910.00 gy:  12411.00 gz:  -3016.00 ax:    716.00 ay:   -376.00 az:   8482.00 mx:     50.39 my:     -7.05 mz:   -103.09 temp:     30.06C
gx:  -3248.00 gy:  -2640.00 gz:  -3576.00 ax:   1506.00 ay:   -412.00 az:   8474.00 mx:     48.05 my:     -9.41 mz:   -103.09 temp:     30.15C
gx:   1214.00 gy:  -9582.00 gz:  -2780.00 ax:   1260.00 ay:   -890.00 az:   8120.00 mx:     46.88 my:    -11.76 mz:   -105.35 temp:     30.39C
gx:   6010.00 gy: -18440.00 gz:  -4116.00 ax:   1766.00 ay:   -182.00 az:   7402.00 mx:     46.88 my:     -3.53 mz:   -106.48 temp:     30.34C
gx:   1944.00 gy: -32768.00 gz:  -2469.00 ax:   3166.00 ay:   -184.00 az:   8126.00 mx:     49.22 my:      0.00 mz:   -108.75 temp:     30.20C
gx:  -4759.00 gy: -32768.00 gz:  -4336.00 ax:   6560.00 ay:   -192.00 az:   6324.00 mx:     48.05 my:     29.39 mz:   -111.02 temp:     30.25C
gx:  -8726.00 gy: -32768.00 gz:   -438.00 ax:   7482.00 ay:   -612.00 az:   4332.00 mx:     46.88 my:     52.91 mz:   -104.22 temp:     30.25C
gx:  -6697.00 gy: -32768.00 gz:  -6789.00 ax:   9464.00 ay:    222.00 az:   -726.00 mx:     41.02 my:     76.43 mz:    -86.09 temp:     30.39C
gx:    824.00 gy: -32768.00 gz:  -8364.00 ax:   8136.00 ay:    134.00 az:  -2476.00 mx:     44.53 my:     88.18 mz:    -61.17 temp:     30.15C
gx:    800.00 gy: -32768.00 gz: -11454.00 ax:   6820.00 ay:    774.00 az:  -5844.00 mx:     49.22 my:     91.71 mz:    -37.38 temp:     30.06C
gx:   9226.00 gy: -32768.00 gz:  -3102.00 ax:   5952.00 ay:    488.00 az:  -7604.00 mx:     52.73 my:     92.89 mz:    -32.85 temp:     30.25C
gx:   8367.00 gy: -32768.00 gz:    492.00 ax:   3508.00 ay:   -622.00 az:  -7212.00 mx:     55.08 my:     74.07 mz:     16.99 temp:     30.20C
gx:   5874.00 gy: -32768.00 gz:    140.00 ax:   -140.00 ay:   -168.00 az:  -7446.00 mx:     51.56 my:     57.61 mz:     31.72 temp:     30.25C
gx:  -2198.00 gy:   4291.00 gz:   1238.00 ax:  -3636.00 ay:   -130.00 az:  -8148.00 mx:     49.22 my:     48.21 mz:     39.65 temp:     30.15C
gx:  -1165.00 gy:   -875.00 gz:  -1402.00 ax:  -1146.00 ay:   -474.00 az:  -8078.00 mx:     48.05 my:     47.03 mz:     38.52 temp:     30.39C
gx:   -280.00 gy:   3697.00 gz:  -1041.00 ax:  -1346.00 ay:   -314.00 az:  -7020.00 mx:     46.88 my:     49.38 mz:     39.65 temp:     30.25C
gx:   -468.00 gy:   5843.00 gz:    287.00 ax:   -544.00 ay:   -218.00 az:  -7340.00 mx:     49.22 my:     48.21 mz:     36.25 temp:     30.34C
gx:   -218.00 gy:   3482.00 gz:     27.00 ax:  -1044.00 ay:   -182.00 az:  -8646.00 mx:     51.56 my:     55.26 mz:     37.38 temp:     30.06C
gx:    908.00 gy:   1607.00 gz:   2794.00 ax:   -316.00 ay:     22.00 az:  -7592.00 mx:     49.22 my:     55.26 mz:     35.12 temp:     30.15C
gx:   2417.00 gy:   2269.00 gz:   5012.00 ax:   -348.00 ay:   -186.00 az:  -7894.00 mx:     46.88 my:     56.44 mz:     33.98 temp:     30.15C
gx:   2866.00 gy:   5356.00 gz:   8822.00 ax:     68.00 ay:   -496.00 az:  -8118.00 mx:     45.70 my:     59.96 mz:     35.12 temp:     30.25C
gx:   2642.00 gy:   4676.00 gz:  14152.00 ax:    404.00 ay:   -706.00 az:  -8088.00 mx:     42.19 my:     64.67 mz:     31.72 temp:     30.25C
gx:   2174.00 gy:  -1501.00 gz:  14508.00 ax:   -132.00 ay:    -60.00 az:  -8552.00 mx:     42.19 my:     64.67 mz:     31.72 temp:     30.06C
gx:   6593.00 gy:   1075.00 gz:  15027.00 ax:    438.00 ay:  -1148.00 az:  -8322.00 mx:     29.30 my:     72.90 mz:     30.59 temp:     30.01C
gx:  10994.00 gy:   6977.00 gz:  20975.00 ax:   1180.00 ay:  -1400.00 az:  -7694.00 mx:     18.75 my:     74.07 mz:     30.59 temp:     30.25C
gx:   7528.00 gy:   1832.00 gz:  28141.00 ax:    664.00 ay:  -1106.00 az:  -7310.00 mx:      8.20 my:     76.43 mz:     28.32 temp:     30.01C
gx:   5619.00 gy:  10445.00 gz:  32310.00 ax:   1572.00 ay:  -1388.00 az:  -6122.00 mx:     -7.03 my:     72.90 mz:     26.05 temp:     30.15C
gx:    175.00 gy:  12624.00 gz:  32767.00 ax:   1600.00 ay:  -1386.00 az:  -6938.00 mx:    -17.58 my:     74.07 mz:     19.26 temp:     30.15C
gx:  -5771.00 gy:   3472.00 gz:  32767.00 ax:    696.00 ay:  -1390.00 az:  -7934.00 mx:    -32.81 my:     61.14 mz:     13.59 temp:     30.15C
gx:  -3905.00 gy:  -2880.00 gz:  27754.00 ax:   -646.00 ay:  -1406.00 az:  -7584.00 mx:    -41.02 my:     51.73 mz:     15.86 temp:     30.06C
gx:    -87.00 gy:   3839.00 gz:  16154.00 ax:  -1426.00 ay:  -1266.00 az:  -7198.00 mx:    -43.36 my:     41.15 mz:     14.73 temp:     30.20C
gx:  -4439.00 gy:    844.00 gz:  11600.00 ax:  -1712.00 ay:   -832.00 az:  -8046.00 mx:    -44.53 my:     37.62 mz:     12.46 temp:     30.01C
gx:  -6619.00 gy:   4913.00 gz:   3424.00 ax:  -1146.00 ay:   -758.00 az:  -7566.00 mx:    -46.88 my:     35.27 mz:     14.73 temp:     30.15C
gx:  -4285.00 gy:   2320.00 gz:  -1390.00 ax:  -1300.00 ay:   -204.00 az:  -8444.00 mx:    -44.53 my:     32.92 mz:     20.39 temp:     30.25C
gx:   -738.00 gy:  -3208.00 gz:  -7062.00 ax:  -1468.00 ay:   -152.00 az:  -8942.00 mx:    -42.19 my:     34.10 mz:     21.52 temp:     30.25C
gx:   2534.00 gy:  -5571.00 gz: -15032.00 ax:  -1422.00 ay:   -200.00 az:  -8250.00 mx:    -39.84 my:     35.27 mz:     22.66 temp:     30.25C
gx:   1715.00 gy:  -6578.00 gz: -24057.00 ax:  -1752.00 ay:   -620.00 az:  -8028.00 mx:    -37.50 my:     41.15 mz:     22.66 temp:     30.20C
gx:  -1568.00 gy: -11318.00 gz: -32768.00 ax:  -1010.00 ay:    378.00 az:  -7786.00 mx:    -33.98 my:     48.21 mz:     23.79 temp:     29.96C
gx:  -3395.00 gy:  -7879.00 gz: -32768.00 ax:  -1420.00 ay:   -242.00 az:  -7732.00 mx:    -23.44 my:     57.61 mz:     27.19 temp:     30.15C
gx:  -2945.00 gy: -13238.00 gz: -32768.00 ax:  -1022.00 ay:    628.00 az:  -7680.00 mx:    -19.92 my:     58.79 mz:     29.45 temp:     30.06C
gx:   -744.00 gy:  -6289.00 gz: -32768.00 ax:   -894.00 ay:   -544.00 az:  -8062.00 mx:      4.69 my:     63.49 mz:     38.52 temp:     30.15C
gx:  -5798.00 gy:   -229.00 gz: -32768.00 ax:   -770.00 ay:   -378.00 az:  -7858.00 mx:     18.75 my:     59.96 mz:     36.25 temp:     30.06C
gx:  -2497.00 gy:  -3174.00 gz: -32768.00 ax:    522.00 ay:  -1248.00 az:  -8418.00 mx:     32.81 my:     56.44 mz:     37.38 temp:     30.15C
gx:  -1035.00 gy:   7683.00 gz: -32768.00 ax:    346.00 ay:   -880.00 az:  -7848.00 mx:     45.70 my:     50.56 mz:     37.38 temp:     30.25C
gx:    610.00 gy:  -9330.00 gz: -32768.00 ax:    252.00 ay:   -872.00 az:  -7582.00 mx:     52.73 my:     44.68 mz:     35.12 temp:     30.06C
gx:  -7609.00 gy:  -4566.00 gz: -32768.00 ax:   1860.00 ay:   -208.00 az:  -7116.00 mx:     52.73 my:     44.68 mz:     35.12 temp:     30.20C
gx:  -9199.00 gy:  -6944.00 gz: -32768.00 ax:   1352.00 ay:   1776.00 az:  -6576.00 mx:     59.77 my:     16.46 mz:     39.65 temp:     30.39C
gx:  -3485.00 gy:  -6361.00 gz: -30054.00 ax:    794.00 ay:   -824.00 az:  -8164.00 mx:     62.11 my:      5.88 mz:     36.25 temp:     30.01C
gx:  -1075.00 gy:  -4149.00 gz: -30032.00 ax:   1030.00 ay:    336.00 az:  -8194.00 mx:     59.77 my:     -2.35 mz:     35.12 temp:     30.06C
gx:   1026.00 gy:   2876.00 gz: -30905.00 ax:   1516.00 ay:   1014.00 az:  -8010.00 mx:     53.91 my:    -11.76 mz:     35.12 temp:     30.01C
gx:  -1439.00 gy:  -6046.00 gz: -26582.00 ax:   2850.00 ay:    682.00 az:  -7188.00 mx:     48.05 my:    -15.29 mz:     35.12 temp:     30.15C
gx:  -1792.00 gy:  -3369.00 gz: -14599.00 ax:   1548.00 ay:    348.00 az:  -7524.00 mx:     44.53 my:    -27.04 mz:     33.98 temp:     29.96C
gx:  -1796.00 gy:  -2264.00 gz:  -6610.00 ax:    560.00 ay:    176.00 az:  -8072.00 mx:     41.02 my:    -30.57 mz:     31.72 temp:     30.06C
gx:   -690.00 gy:   1252.00 gz:  -1265.00 ax:   1436.00 ay:    -30.00 az:  -8704.00 mx:     38.67 my:    -30.57 mz:     32.85 temp:     30.01C
gx:    428.00 gy:   1639.00 gz:   4261.00 ax:    488.00 ay:    580.00 az:  -7458.00 mx:     38.67 my:    -28.22 mz:     29.45 temp:     30.25C
gx:   -264.00 gy:   2548.00 gz:  10349.00 ax:   1052.00 ay:    462.00 az:  -8800.00 mx:     42.19 my:    -25.87 mz:     31.72 temp:     30.01C
gx:   4923.00 gy:  -3791.00 gz:  21994.00 ax:   1282.00 ay:    728.00 az:  -8270.00 mx:     42.19 my:    -25.87 mz:     31.72 temp:     30.25C
gx:   2769.00 gy:   -954.00 gz:  27359.00 ax:    914.00 ay:    780.00 az:  -8332.00 mx:     48.05 my:    -22.34 mz:     31.72 temp:     30.06C
gx:   2296.00 gy:   1240.00 gz:  30434.00 ax:    580.00 ay:   1414.00 az:  -7988.00 mx:     59.77 my:    -15.29 mz:     32.85 temp:     30.25C
gx:   4186.00 gy:   3201.00 gz:  32767.00 ax:   1146.00 ay:    850.00 az:  -7954.00 mx:     62.11 my:     -8.23 mz:     31.72 temp:     30.01C
gx:   1028.00 gy:   8398.00 gz:  32767.00 ax:   -274.00 ay:    370.00 az:  -6612.00 mx:     62.11 my:     -1.18 mz:     32.85 temp:     30.25C
gx:   2379.00 gy:   -542.00 gz:  29396.00 ax:   -240.00 ay:   1018.00 az:  -8572.00 mx:     65.62 my:     14.11 mz:     35.12 temp:     30.01C
gx:  -2746.00 gy:  17111.00 gz:  28506.00 ax:   -776.00 ay:   1584.00 az:  -4176.00 mx:     67.97 my:     17.64 mz:     35.12 temp:     30.01C
gx:  -6716.00 gy:  14484.00 gz:  20336.00 ax:   1864.00 ay:   -702.00 az:  -6358.00 mx:     66.80 my:     34.10 mz:     31.72 temp:     30.15C
gx:   5127.00 gy: -25508.00 gz:  17200.00 ax:   -462.00 ay:   1530.00 az:  -9916.00 mx:     64.45 my:     44.68 mz:     28.32 temp:     30.25C
gx:  -4456.00 gy:   9189.00 gz:  16131.00 ax:   -454.00 ay:    -78.00 az:  -7622.00 mx:     60.94 my:     44.68 mz:     30.59 temp:     30.25C
gx:  -4932.00 gy:   4532.00 gz:  12965.00 ax:    586.00 ay:    890.00 az: -10390.00 mx:     62.11 my:     48.21 mz:     24.92 temp:     30.06C
gx:  11167.00 gy:  -2341.00 gz:  12793.00 ax:   2426.00 ay:     56.00 az:  -8022.00 mx:     60.94 my:     50.56 mz:     28.32 temp:     30.20C
gx:   6881.00 gy:  -4895.00 gz:   7123.00 ax:   -294.00 ay:   -132.00 az:  -9280.00 mx:     57.42 my:     52.91 mz:     27.19 temp:     30.25C
gx:    -22.00 gy:   2302.00 gz:  -4652.00 ax:  -1648.00 ay:     68.00 az:  -4156.00 mx:     50.39 my:     51.73 mz:     32.85 temp:     29.96C
gx:   1903.00 gy:   2760.00 gz:   8974.00 ax:   1018.00 ay:  -1388.00 az:  -8112.00 mx:     49.22 my:     54.09 mz:     33.98 temp:     30.06C
gx:  -4717.00 gy:   3390.00 gz:   7731.00 ax:  -1506.00 ay:   -336.00 az:  -8274.00 mx:     49.22 my:     58.79 mz:     32.85 temp:     30.01C
gx:  -1875.00 gy:   1384.00 gz:   1796.00 ax:   -666.00 ay:     46.00 az:  -8246.00 mx:     48.05 my:     58.79 mz:     32.85 temp:     30.25C
gx:  -2193.00 gy:   2723.00 gz:   1419.00 ax:   -320.00 ay:    122.00 az:  -8186.00 mx:     49.22 my:     62.32 mz:     27.19 temp:     30.15C
gx:    -80.00 gy:   1079.00 gz:   1536.00 ax:    -14.00 ay:      4.00 az:  -8300.00 mx:     49.22 my:     62.32 mz:     27.19 temp:     30.06C
gx:   -902.00 gy:    382.00 gz:    815.00 ax:    -60.00 ay:    -60.00 az:  -7834.00 mx:     48.05 my:     65.84 mz:     27.19 temp:     30.25C
gx:      4.00 gy:     50.00 gz:    985.00 ax:    -72.00 ay:    -22.00 az:  -7892.00 mx:     51.56 my:     64.67 mz:     27.19 temp:     30.25C
gx:    304.00 gy:  -1065.00 gz:    847.00 ax:   -254.00 ay:    -84.00 az:  -7730.00 mx:     49.22 my:     64.67 mz:     28.32 temp:     30.25C
gx:    647.00 gy:  -1090.00 gz:    607.00 ax:    -52.00 ay:    -32.00 az:  -8004.00 mx:     49.22 my:     63.49 mz:     28.32 temp:     30.39C
gx:    432.00 gy:   -516.00 gz:   -584.00 ax:    306.00 ay:   -312.00 az:  -8016.00 mx:     48.05 my:     63.49 mz:     28.32 temp:     30.15C
gx:   -857.00 gy:    415.00 gz:   -406.00 ax:    468.00 ay:   -474.00 az:  -8220.00 mx:     48.05 my:     63.49 mz:     28.32 temp:     29.96C
gx:  -1774.00 gy:    620.00 gz:    314.00 ax:    624.00 ay:   -326.00 az:  -7258.00 mx:     46.88 my:     61.14 mz:     28.32 temp:     30.25C
psiphi75 commented 4 years ago

Are those really the numbers you get from get_accel_gyro_mag()? They look many orders of magnitude off. Can you do the same with the default calibration?

By the way, the temperature is the temperature of the chip, it's not to be used as the ambient room temperature. You should find that when you first turn it on that it will quickly reach 30 degrees C, or so. The purpose of this is because the gyroscope is sensitive to temperature changes. Hence, in theory you should calibrate the gyro for a few temperatures.

psiphi75 commented 4 years ago

It appears there may be some differences in the MPU9150 (which I believe you are using) and MPU9250 which this library is written for, I have to say looking at the Invensense documentation I couldn't spot the difference, someone has documented this https://sureshjoshi.com/embedded/invensense-imus-what-to-know/, although this is from many years ago, I don't know how applicable it is.

thomasnugent commented 4 years ago

I'll do the same with the default cal and let you know soon, and I'm using the MPU9250 (SparkFun breakout board here).

I edited the align_accel and same for gyro and mag slightly. e.g.:

void align_accel(uint8_t bytes[6], vector_t *v)
{
  int16_t xi = BYTE_2_INT_BE(bytes, 0);
  int16_t yi = BYTE_2_INT_BE(bytes, 2);
  int16_t zi = BYTE_2_INT_BE(bytes, 4);

  v->x = (float)xi;
  v->y = (float)yi;
  v->z = (float)zi;
}

I basically just removed any use of the calibration values, but not sure if that's how you meant sorry!

Good point regarding the chip temperature, I hadn't considered that.

thomasnugent commented 4 years ago

I must admit I'm not sure if I need to use the transform_accel_gyro calls (or if I need to do a different transform).

I added back in your original implementation of scale_accel etc. and here's a couple of outputs using the default calibration values (i.e. no offsets), with and without transforming. with_transform-default_calibration.txt no_transform-default_calibration.txt

thomasnugent commented 4 years ago

I must admit, having no calibration at all (i.e default) gives better results than just about any of the calibration values that are being reported after running a calibration. The calibrations I do often report very different values (as per my 2nd comment haha)

But I do have 2 MPU's (the exact same) and one seems to be working quite nicely -- but still not as well as videos like this shows!

psiphi75 commented 4 years ago

Getting the transforms correct is the key, more important than calibration. Do you have a photo of the PCB, it should have two orientations on it, one for the accel/gyro and one for the magnetometer. See here for an example: https://hackster.imgix.net/uploads/image/file/138911/xyz.JPG. You need to get the magnetometer into the same reference frame as the accel/gyro.

thomasnugent commented 4 years ago

Good point, here's a pic of the axes I have to use: image

Is it right to assume I'd need to do the following transformation to have the magnetometer in the same reference? Rotate magnetometer about Y 180deg, then Z 90 deg

Is there any need to transform the accel/gyro too?

psiphi75 commented 4 years ago

No need to transform the accel/gyro, unless you want them in a frame of reference that makes sense to the rest of your code. But for debugging purposes, it's best not to.

I find making the transformation in terms of switching axes simpler than rotation. Such that the following transform should work:

new_mag.x = mag.y;
new_mag.y = mag.x;
new_mag.z = -mag.z;
thomasnugent commented 4 years ago

I think the transformation you mentioned above is the same as what your original code had, after applying the same transformation as the accel/gyro. So I believe your original code is compatible with my device.

i.e. given the transformation of accel/gyro:

  v->x = -x;
  v->y = -z;
  v->z = -y;

Then

new_mag.x = mag.y;
new_mag.y = mag.x;
new_mag.z = -mag.z;

becomes

new_mag.x = -mag.y;
new_mag.y = mag.z;
new_mag.z = -mag.x;

I do think the calibration values I'm receiving seem a bit too inconsistent, especially for the magnetometer:

.mag_offset = {.x = 16.580055, .y = -125.701469, .z = 191.249786} // on one calibration
.mag_offset = {.x = 8.787285, .y = 108.107750, .z = -165.139145},  // on the next cal

Do the offsets in mag_offset wrap around from -180 to +180?

And I should point out again that of the 2 identical MPU's I'm working with, after flashing the exact same code (even with/without the same calibration values) onto them, they can perform super differently.

The best working one used the following calibration:

    .mag_offset = {.x = 12.367111, .y = 41.837597, .z = -87.932884},
    .mag_scale = {.x = 1.534589, .y = 1.448836, .z = 1.519409},
    .accel_offset = {.x = -0.046729, .y = 0.076270, .z = 0.120380},
    .accel_scale_lo = {.x = 0.975473, .y = 1.053684, .z = 1.051681},
    .accel_scale_hi = {.x = -1.016280, .y = -0.944078, .z = -0.974523},
    .gyro_bias_offset = {.x = 2.240171, .y = -2.027138, .z = -0.885103}

Are there any macros I should be changing due to my location on Earth (I saw this in the SparkFun's MPU9250 Hookup Guide), or anything else I could be getting wrong?

Thanks for all your help so far by the way, I really appreciate it!

thomasnugent commented 4 years ago

If it's worth anything, the MPU that has worked best so far spits out the following data when I use default calibration values (I've left in the transformation functions as they were): with_transform-default_calibration_esp1.txt

And here's the same code flashed on my less-cooperative MPU: with_transform-default_calibration_esp2.txt

Both were held in the same orientation with a small rotation in Z. Their values are quite different.

Also the ay is giving -1, whereas my device should be showing az = -1 (due to gravity). I believe this is just because of those transformations that I left in.

psiphi75 commented 4 years ago

It makes sense that they are the same orientation.

The magnetometer values do look very off, but only because of the large variation from one calibration to the next. How are you calibrating the magnetometer. You need to rotate it in all directions (not just around axes). Here are some calibration instructions for a JavaScript version of same AHRS algorithm: https://github.com/psiphi75/ahrs/

No the magnetometer is not +/-180, the value is proportional to units of Tesla (I believe).

Your location on Earth will only change your magnetometer calibration values. If you want to find true north you would need to take into account the magnetic declination (https://www.ncei.noaa.gov/news/world-magnetic-model-2020-released). This changes based on your position and slowly over time.

Your accelerometer calibration looks good. The gyro bias offset looks a little high but could still be okay, to calibrate the gyro you need to keep the device completely still. I put it on a solid table and don't move.

thomasnugent commented 4 years ago

Hi Psiphi, I found some silly errors on my end which ended up fixing my problems. It was the magnetometer's calibration that I screwed up (I was running consecutive calibrations without zeroing the calibration values before running the next one). Thanks again for helping me last week and for giving me a better understanding of it all!

Before closing this issue, is there any benefit in doing the following, to improve my IMU's performance?

psiphi75 commented 4 years ago

It was the magnetometer's calibration that I screwed up (I was running consecutive calibrations without zeroing the calibration values before running the next one). Thanks again for helping me last week and for giving me a better understanding of it all!

That's great.

* Adjusting the scale/gyro's `inv_scale` (both set to 1.0 currently)

Actually, this value is used, 1.0 is just the initialisation value.

However, you could introduce a second inv_scale_cal value that would scale the rate of turn. You would need an advanced rate-of-turn calibration setup that can measure or simulate the rate of turn. Essentially this would be a per-axis calibration value. It would correct the rate of turn of an axis, where as the bias compensates the offset. Let me know if you come up with a simple way to measure the rate of change of an axis.

* Adjusting the scale/gyro's full scale range (FS_250 for e.g.)

I believe the MPU9250 documentation explains this. Both the gyro and accel analogous values. You set the value to limit of what you will be experiencing. I can't remember what the limits are or which way around the are. You can get more precision in your measurement, but then you lose range, or vice-versa.

* Adjusting the ASA of the mag

I believe these are set at the factory. I wouldn't play with them unless you have advanced knowledge of how the magnetometer works.

* Increasing the magnetometer's measurement mode from "0x06 Continuous measurement mode 2: 100Hz" to 200Hz perhaps?

From what I can remember the maximum data rate of the magnetometer is 100 Hz. I believe this is the internal polling rate of the magnetometer. But you can query the device more frequently than that, but you may get the same result for 2 or 3 queries. I would recommend keeping the magnetometer at 100 Hz, but you can run the accel/gyro at the same time at 250 Hz.

* Adjusting beta or sample freq for AHRS

The sample frequency needs to be what you are querying the MPU9250 at. Yes, adjust the beta value will give you different results, a smaller value will give you a smoother value, but the latency of your changes will increase. You will need to experiment.

thomasnugent commented 4 years ago

Thanks so much again Simon, I'll let you know if I figure out anything new! You've been a superb help :) 🎉