ben-pololu-imu

Changeset

6:dc85b7ea0056
2013-10-14 Paul Boddie raw files shortlog changelog graph Added support for reading magnetometer calibration data from a file. Added a special signal handler for the calibration program to produce usable data for such a settings file when the program is interrupted.
calibrate.c (file) imu.h (file) measure.c (file)
     1.1 --- a/calibrate.c	Mon Oct 14 16:22:35 2013 +0000
     1.2 +++ b/calibrate.c	Mon Oct 14 16:50:39 2013 +0000
     1.3 @@ -18,14 +18,22 @@
     1.4  #include "shutdown.h"
     1.5  #include "geo.h"
     1.6  
     1.7 +vectorf vmin = {{0, 0, 0}},
     1.8 +        vmax = {{0, 0, 0}};
     1.9 +
    1.10 +void calibrate_shutdown(int signum)
    1.11 +{
    1.12 +    printf("\n%-6.1f %-6.1f %-6.1f %-6.1f %-6.1f %-6.1f\n",
    1.13 +        vmin.x, vmin.y, vmin.z, vmax.x, vmax.y, vmax.z);
    1.14 +    init_shutdown(signum);
    1.15 +}
    1.16 +
    1.17  /* Main program. */
    1.18  
    1.19  int main(int argc, char *argv[])
    1.20  {
    1.21      uint8_t result[6];
    1.22      vectorf value,
    1.23 -            vmin = {{1, 1, 1}},
    1.24 -            vmax = {{-1, -1, -1}},
    1.25              valueB[IMU_ACCEL_BUFFER_SIZE];
    1.26      int argno = 1, vindex = 0;
    1.27      bool gyroscope = false, accelerometer = false, magnetometer = false,
    1.28 @@ -64,7 +72,7 @@
    1.29          normalised = true;
    1.30      }
    1.31  
    1.32 -    signal(SIGINT, init_shutdown);
    1.33 +    signal(SIGINT, calibrate_shutdown);
    1.34  
    1.35      /* Access the 8:10 port. */
    1.36  
     2.1 --- a/imu.h	Mon Oct 14 16:22:35 2013 +0000
     2.2 +++ b/imu.h	Mon Oct 14 16:50:39 2013 +0000
     2.3 @@ -201,6 +201,7 @@
     2.4  #define IMU_UGAUSS_FACTOR           IMU_4_0G_UNIT
     2.5  #define IMU_MAGNET_SCALE            IMU_MAGNET_CRB_REG_4_0G
     2.6  #define IMU_MAGNET_FREQ             IMU_MAGNET_CRA_REG_30HZ
     2.7 +#define IMU_MAGNET_SETTINGS_FILE    "imu-magnet.cfg"
     2.8  
     2.9  #define ACCEL_G                     9.81 /* ms**-2 */
    2.10  
     3.1 --- a/measure.c	Mon Oct 14 16:22:35 2013 +0000
     3.2 +++ b/measure.c	Mon Oct 14 16:50:39 2013 +0000
     3.3 @@ -12,6 +12,8 @@
     3.4   * (at your option) any later version.
     3.5   */
     3.6  
     3.7 +#include <stdio.h>
     3.8 +#include <stdlib.h>
     3.9  #include <sys/time.h>
    3.10  #include <unistd.h>
    3.11  #include <pthread.h>
    3.12 @@ -30,6 +32,10 @@
    3.13  void ui_calibrate(bool using_filter, int (*print)(const char *, ...), void (*flush)())
    3.14  {
    3.15      vectorf tmpB[1];
    3.16 +    FILE *magnetcfg;
    3.17 +    char fieldmins[3][8], fieldmaxs[3][8], *endptr;
    3.18 +    double value;
    3.19 +    int i;
    3.20  
    3.21      print("Calibrating...\n");
    3.22      flush();
    3.23 @@ -53,6 +59,33 @@
    3.24  
    3.25      print("Calibrated using (%.4f, %.4f, %.4f).\n", acceleration0.x, acceleration0.y, acceleration0.z);
    3.26      flush();
    3.27 +
    3.28 +    /* Read magnetometer settings, if possible. */
    3.29 +
    3.30 +    magnetcfg = fopen(IMU_MAGNET_SETTINGS_FILE, "r");
    3.31 +
    3.32 +    if (magnetcfg != NULL)
    3.33 +    {
    3.34 +        fscanf(magnetcfg, "%7s %7s %7s %7s %7s %7s",
    3.35 +            fieldmins[0], fieldmins[1], fieldmins[2],
    3.36 +            fieldmaxs[0], fieldmaxs[1], fieldmaxs[2]);
    3.37 +
    3.38 +        for (i = 0; i < 3; i++)
    3.39 +        {
    3.40 +            value = strtod(fieldmins[i], &endptr);
    3.41 +            if (endptr != fieldmins[i])
    3.42 +                fieldmin.axis[i] = value;
    3.43 +            value = strtod(fieldmaxs[i], &endptr);
    3.44 +            if (endptr != fieldmaxs[i])
    3.45 +                fieldmax.axis[i] = value;
    3.46 +        }
    3.47 +
    3.48 +        print("Calibrated using (%.1f, %.1f, %.1f), (%.1f, %.1f, %.1f).\n",
    3.49 +            fieldmin.x, fieldmin.y, fieldmin.z,
    3.50 +            fieldmax.x, fieldmax.y, fieldmax.z);
    3.51 +
    3.52 +        fclose(magnetcfg);
    3.53 +    }
    3.54  }
    3.55  
    3.56  void *get_measurements(void *arg)