/*	hostshaky.c

	This code is derived from Arduino code by Kris Winer demonstrating MPU9250 use.
	The basic idea is to offload the compute to get the highest possible sample rate.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

// ShAKY defines
// Output status tags
#define ShAKYRESP(V)	('@'+(V))	// base response code
#define ShAKYA		1		// includes Accelerometers
#define ShAKYG		2		// includes Gryroscopes
#define	ShAKYM		4		// includes Magnetometers
#define	ShAKYX		32		// Xsync happened

// Input request modes
#define	ShAKYAGM	'0'	// requests A, G, and M when ready
#define	ShAKYONE	'1'	// requests just one at a time

#define	NEVER	(1000000000.0)
#define	CBUFMAX	(64*1024)

char	ttyACM = 0;
int	acmfd = -1;
int	incremental = 0;

typedef struct {
	float	time, yaw, pitch, roll;
} typr_t;

typr_t	typr[CBUFMAX];
int	typrsp = 0;

char	*progname;
float	xsync, shutter, endexp = NEVER;
int	startexp;

#define	PI	3.141592654
#define	SQRT34	0.866025404	// sqrt(3.0f / 4.0f)

// Set initial input parameters
enum Ascale {
  AFS_2G = 0,
  AFS_4G,
  AFS_8G,
  AFS_16G
};

enum Gscale {
  GFS_250DPS = 0,
  GFS_500DPS,
  GFS_1000DPS,
  GFS_2000DPS
};

enum Mscale {
  MFS_14BITS = 0, // 0.6 mG per LSB
  MFS_16BITS      // 0.15 mG per LSB
};

// Specify sensor full scale
uint8_t Gscale = GFS_250DPS;
uint8_t Ascale = AFS_2G;
uint8_t Mscale = MFS_16BITS; // Choose either 14-bit or 16-bit magnetometer resolution
uint8_t Mmode = 0x02;        // 2 for 8 Hz, 6 for 100 Hz continuous magnetometer data read
float aRes, gRes, mRes;      // scale resolutions per LSB for the sensors
  
// Pin definitions
int intPin = 12;  // These can be changed, 2 and 3 are the Arduinos ext int pins
int myLed = 13; // Set up pin 13 led for toggling

int16_t accelCount[3];  // Stores the 16-bit signed accelerometer sensor output
int16_t gyroCount[3];   // Stores the 16-bit signed gyro sensor output
int16_t magCount[3];    // Stores the 16-bit signed magnetometer sensor output
float magCalibration[3] = {0, 0, 0}, magbias[3] = {0, 0, 0};  // Factory mag calibration and mag bias
float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0};      // Bias corrections for gyro and accelerometer
int16_t tempCount;      // temperature raw count output
float   temperature;    // Stores the real internal chip temperature in degrees Celsius
float   SelfTest[6];    // holds results of gyro and accelerometer self test

// global constants for 9 DoF fusion and AHRS (Attitude and Heading Reference System)
#define GyroMeasError (PI * (40.0f / 180.0f))   // gyroscope measurement error in rads/s (start at 40 deg/s)
#define	GyroMeasDrift (PI * (0.0f  / 180.0f))   // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
// There is a tradeoff in the beta parameter between accuracy and response speed.
// In the original Madgwick study, beta of 0.041 (corresponding to GyroMeasError of 2.7 degrees/s) was found to give optimal accuracy.
// However, with this value, the LSM9SD0 response time is about 10 seconds to a stable initial quaternion.
// Subsequent changes also require a longish lag time to a stable output, not fast enough for a quadcopter or robot car!
// By increasing beta (GyroMeasError) by about a factor of fifteen, the response time constant is reduced to ~2 sec
// I haven't noticed any reduction in solution accuracy. This is essentially the I coefficient in a PID control sense; 
// the bigger the feedback coefficient, the faster the solution converges, usually at the expense of accuracy. 
// In any case, this is the free parameter in the Madgwick filtering and fusion scheme.
float beta = SQRT34 * GyroMeasError;   // compute beta
float zeta = SQRT34 * GyroMeasDrift;   // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
#define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
#define Ki 0.0f

uint32_t delt_t = 0; // used to control display output rate
uint32_t count = 0, sumCount = 0; // used to control display output rate
float pitch, yaw, roll;
float deltat = 0.0f, sum = 0.0f;        // integration interval for both filter schemes
uint32_t lastUpdate = 0, firstUpdate = 0; // used to calculate integration interval
uint32_t Now = 0;        // used to calculate integration interval

float ax, ay, az, gx, gy, gz, mx, my, mz; // variables to hold latest sensor data values 
float q[4] = {1.0f, 0.0f, 0.0f, 0.0f};    // vector to hold quaternion
float eInt[3] = {0.0f, 0.0f, 0.0f};       // vector to hold integral error for Mahony method

float magBias[3],magScale[3];

int set_interface_attribs(int fd, int speed)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }

    cfsetospeed(&tty, (speed_t)speed);
    cfsetispeed(&tty, (speed_t)speed);

    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    /* setup for non-canonical mode */
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;

    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

void set_mincount(int fd, int mcount)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error tcgetattr: %s\n", strerror(errno));
        return;
    }

    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;        /* half second timer */

    if (tcsetattr(fd, TCSANOW, &tty) < 0)
        printf("Error tcsetattr: %s\n", strerror(errno));
}


int	nextc = ' ';

int
advance(void)
{
	unsigned char t;

	while (read(acmfd, &t, 1) < 1) ;
	nextc = t;

//printf("'%c' %u\n", nextc, nextc);
	return(nextc);
}

int
match(int c)
{
	if (c == nextc) { advance(); return(1); }
	return(0);
}

int16_t
read16(void)
{
	int16_t r = (advance() & 0xff);
	r |= (advance() << 8);
	return(r);
}

float
readf(void)
{
	float f = 0;
	float s = 1;

	if (match('-')) s = -1;
	while ((nextc >= '0') && (nextc <= '9')) {
		f = (nextc - '0') + (f * 10);
		advance();
	}
	if (match('.')) {
		float d = 1;
		while ((nextc >= '0') && (nextc <= '9')) {
			f = (nextc - '0') + (f * 10);
			d *= 10;
			advance();
		}
		f /= d;
	}
	match('\n');

	return(f * s);
}



 void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
        {
            float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3];   // short name local variable for readability
            float norm;
            float hx, hy, _2bx, _2bz;
            float s1, s2, s3, s4;
            float qDot1, qDot2, qDot3, qDot4;

            // Auxiliary variables to avoid repeated arithmetic
            float _2q1mx;
            float _2q1my;
            float _2q1mz;
            float _2q2mx;
            float _4bx;
            float _4bz;
            float _2q1 = 2.0f * q1;
            float _2q2 = 2.0f * q2;
            float _2q3 = 2.0f * q3;
            float _2q4 = 2.0f * q4;
            float _2q1q3 = 2.0f * q1 * q3;
            float _2q3q4 = 2.0f * q3 * q4;
            float q1q1 = q1 * q1;
            float q1q2 = q1 * q2;
            float q1q3 = q1 * q3;
            float q1q4 = q1 * q4;
            float q2q2 = q2 * q2;
            float q2q3 = q2 * q3;
            float q2q4 = q2 * q4;
            float q3q3 = q3 * q3;
            float q3q4 = q3 * q4;
            float q4q4 = q4 * q4;

            // Normalise accelerometer measurement
            norm = sqrt(ax * ax + ay * ay + az * az);
            if (norm == 0.0f) return; // handle NaN
            norm = 1.0f/norm;
            ax *= norm;
            ay *= norm;
            az *= norm;

            // Normalise magnetometer measurement
            norm = sqrt(mx * mx + my * my + mz * mz);
            if (norm == 0.0f) return; // handle NaN
            norm = 1.0f/norm;
            mx *= norm;
            my *= norm;
            mz *= norm;

            // Reference direction of Earth's magnetic field
            _2q1mx = 2.0f * q1 * mx;
            _2q1my = 2.0f * q1 * my;
            _2q1mz = 2.0f * q1 * mz;
            _2q2mx = 2.0f * q2 * mx;
            hx = mx * q1q1 - _2q1my * q4 + _2q1mz * q3 + mx * q2q2 + _2q2 * my * q3 + _2q2 * mz * q4 - mx * q3q3 - mx * q4q4;
            hy = _2q1mx * q4 + my * q1q1 - _2q1mz * q2 + _2q2mx * q3 - my * q2q2 + my * q3q3 + _2q3 * mz * q4 - my * q4q4;
            _2bx = sqrt(hx * hx + hy * hy);
            _2bz = -_2q1mx * q3 + _2q1my * q2 + mz * q1q1 + _2q2mx * q4 - mz * q2q2 + _2q3 * my * q4 - mz * q3q3 + mz * q4q4;
            _4bx = 2.0f * _2bx;
            _4bz = 2.0f * _2bz;

            // Gradient decent algorithm corrective step
            s1 = -_2q3 * (2.0f * q2q4 - _2q1q3 - ax) + _2q2 * (2.0f * q1q2 + _2q3q4 - ay) - _2bz * q3 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q4 + _2bz * q2) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q3 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
            s2 = _2q4 * (2.0f * q2q4 - _2q1q3 - ax) + _2q1 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q2 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + _2bz * q4 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q3 + _2bz * q1) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q4 - _4bz * q2) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
            s3 = -_2q1 * (2.0f * q2q4 - _2q1q3 - ax) + _2q4 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q3 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + (-_4bx * q3 - _2bz * q1) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q2 + _2bz * q4) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q1 - _4bz * q3) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
            s4 = _2q2 * (2.0f * q2q4 - _2q1q3 - ax) + _2q3 * (2.0f * q1q2 + _2q3q4 - ay) + (-_4bx * q4 + _2bz * q2) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q1 + _2bz * q3) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q2 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
            norm = sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4);    // normalise step magnitude
            norm = 1.0f/norm;
            s1 *= norm;
            s2 *= norm;
            s3 *= norm;
            s4 *= norm;

            // Compute rate of change of quaternion
            qDot1 = 0.5f * (-q2 * gx - q3 * gy - q4 * gz) - beta * s1;
            qDot2 = 0.5f * (q1 * gx + q3 * gz - q4 * gy) - beta * s2;
            qDot3 = 0.5f * (q1 * gy - q2 * gz + q4 * gx) - beta * s3;
            qDot4 = 0.5f * (q1 * gz + q2 * gy - q3 * gx) - beta * s4;

            // Integrate to yield quaternion
            q1 += qDot1 * deltat;
            q2 += qDot2 * deltat;
            q3 += qDot3 * deltat;
            q4 += qDot4 * deltat;
            norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);    // normalise quaternion
            norm = 1.0f/norm;
            q[0] = q1 * norm;
            q[1] = q2 * norm;
            q[2] = q3 * norm;
            q[3] = q4 * norm;

        }
  
  
  
 // Similar to Madgwick scheme but uses proportional and integral filtering on the error between estimated reference vectors and
 // measured ones. 
            void MahonyQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
        {
            float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3];   // short name local variable for readability
            float norm;
            float hx, hy, bx, bz;
            float vx, vy, vz, wx, wy, wz;
            float ex, ey, ez;
            float pa, pb, pc;

            // Auxiliary variables to avoid repeated arithmetic
            float q1q1 = q1 * q1;
            float q1q2 = q1 * q2;
            float q1q3 = q1 * q3;
            float q1q4 = q1 * q4;
            float q2q2 = q2 * q2;
            float q2q3 = q2 * q3;
            float q2q4 = q2 * q4;
            float q3q3 = q3 * q3;
            float q3q4 = q3 * q4;
            float q4q4 = q4 * q4;   

            // Normalise accelerometer measurement
            norm = sqrt(ax * ax + ay * ay + az * az);
            if (norm == 0.0f) return; // handle NaN
            norm = 1.0f / norm;        // use reciprocal for division
            ax *= norm;
            ay *= norm;
            az *= norm;

            // Normalise magnetometer measurement
            norm = sqrt(mx * mx + my * my + mz * mz);
            if (norm == 0.0f) return; // handle NaN
            norm = 1.0f / norm;        // use reciprocal for division
            mx *= norm;
            my *= norm;
            mz *= norm;

            // Reference direction of Earth's magnetic field
            hx = 2.0f * mx * (0.5f - q3q3 - q4q4) + 2.0f * my * (q2q3 - q1q4) + 2.0f * mz * (q2q4 + q1q3);
            hy = 2.0f * mx * (q2q3 + q1q4) + 2.0f * my * (0.5f - q2q2 - q4q4) + 2.0f * mz * (q3q4 - q1q2);
            bx = sqrt((hx * hx) + (hy * hy));
            bz = 2.0f * mx * (q2q4 - q1q3) + 2.0f * my * (q3q4 + q1q2) + 2.0f * mz * (0.5f - q2q2 - q3q3);

            // Estimated direction of gravity and magnetic field
            vx = 2.0f * (q2q4 - q1q3);
            vy = 2.0f * (q1q2 + q3q4);
            vz = q1q1 - q2q2 - q3q3 + q4q4;
            wx = 2.0f * bx * (0.5f - q3q3 - q4q4) + 2.0f * bz * (q2q4 - q1q3);
            wy = 2.0f * bx * (q2q3 - q1q4) + 2.0f * bz * (q1q2 + q3q4);
            wz = 2.0f * bx * (q1q3 + q2q4) + 2.0f * bz * (0.5f - q2q2 - q3q3);  

            // Error is cross product between estimated direction and measured direction of gravity
            ex = (ay * vz - az * vy) + (my * wz - mz * wy);
            ey = (az * vx - ax * vz) + (mz * wx - mx * wz);
            ez = (ax * vy - ay * vx) + (mx * wy - my * wx);
            if (Ki > 0.0f)
            {
                eInt[0] += ex;      // accumulate integral error
                eInt[1] += ey;
                eInt[2] += ez;
            }
            else
            {
                eInt[0] = 0.0f;     // prevent integral wind up
                eInt[1] = 0.0f;
                eInt[2] = 0.0f;
            }

            // Apply feedback terms
            gx = gx + Kp * ex + Ki * eInt[0];
            gy = gy + Kp * ey + Ki * eInt[1];
            gz = gz + Kp * ez + Ki * eInt[2];

            // Integrate rate of change of quaternion
            pa = q2;
            pb = q3;
            pc = q4;
            q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltat);
            q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * deltat);
            q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * deltat);
            q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * deltat);

            // Normalise quaternion
            norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);
            norm = 1.0f / norm;
            q[0] = q1 * norm;
            q[1] = q2 * norm;
            q[2] = q3 * norm;
            q[3] = q4 * norm;
 
        }

void
expose(void)
{
	// Process an exposure
	char buf[1024];
	FILE *fp;
	float whenstart = typr[startexp].time - xsync;
	int s = startexp;

	// Step backward to cover Xsync delay
	while (typr[s].time > whenstart) {
		s = (s + (CBUFMAX - 1)) & (CBUFMAX - 1);
	}

	// Open the output file
	sprintf(buf, "ShAKY%08x", time(0));
	fprintf(stderr, "Recording exposure data in %s\n", buf);
	if ((fp = fopen(buf, "w")) == NULL) {
		fprintf(stderr,
"%s: couldn't write file %s\n",
			progname, buf);
		exit(1);
	}

	fprintf(fp, "$Mydata << EOD\n");
	for (int i=s; i!=typrsp; i=((i + 1) & (CBUFMAX - 1))) {
		fprintf(fp,
"%f\t%f\t%f\t%f\n",
			(typr[i].time - typr[startexp].time),
			(typr[i].yaw - typr[startexp].yaw),
			(typr[i].pitch - typr[startexp].pitch),
			(typr[i].roll - typr[startexp].roll));
	}
	fprintf(fp,
"EOD\n"
"set key outside\n"
"set size 1, 0.5\n"
"plot	$Mydata using 1:2 with linespoints title \"Yaw\", \\\n"
"	$Mydata using 1:3 with linespoints title \"Pitch\", \\\n"
"	$Mydata using 1:4 with linespoints title \"Roll\"\n"
"pause mouse\n"
		);
	fclose(fp);

	sprintf(buf, "gnuplot <ShAKY%08x 2>/dev/null", time(0));
	system(buf);
	exit(0);
}

float
fracin(char *p)
{
	if ((p[0] == '1') && (p[1] == '/')) {
		return(1.0 / atof(p + 2));
	}
	return(atof(p));
}

int
main(int argc, char **argv)
{
	progname = argv[0];

	for (int i=1; i<argc; ++i) {
		if (argv[i][0] == '-') {
			char *p = &(argv[i][1]);

			while (*p) {
				switch (*(p++)) {
				case 'x':
					xsync = fracin(p);
					p = "";
					break;
				case 's':
					shutter = fracin(p);
					p = "";
					break;
				case 'p':
					ttyACM = atoi(p);
					p = "";
					break;
				case 'i':
					incremental = (*p ? atoi(p) : 1);
					p = "";
					if ((incremental < 1) || (incremental > 9)) goto usage;
					break;
				default:
usage:
					fprintf(stderr,
"Usage: %s {options}\n"
"-i#\tIncremental sensor updates, bias # (1-9, default 1) times Gyros\n"
"-p#\tPort number, /dev/ttyACM#\n"
"-s#\tShutter speed, as 1/#s or #.#\n"
"-x#\tXsync speed, as 1/#s or #.#\n"
"no -s will stream values to console\n",
						progname);
					exit(1);
				}
			}
		} else goto usage;
	}

	if (shutter == 0) {
		fprintf(stderr,
"%s: printing continuous trace\n",
			progname);
		xsync = 0;
	} else {
		fprintf(stderr,
"%s: recording shots with xsync=%gs, shutter=%gs\n",
			progname, xsync, shutter);
	}

	setup();
	do { loop(); } while (nextc != EOF);
}

void setup()
{
	/* Get device open */
	char buf[256];
	char incr;

	sprintf(buf, "/dev/ttyACM%d", ttyACM);
	acmfd = open(buf, O_RDWR);
	if (acmfd < 0) {
		fprintf(stderr, "Could not open %s\n", buf);
		exit(1);
	}
	set_interface_attribs(acmfd, B230400);
	fprintf(stderr, "Waking ShAKY on %s\n", buf);


	/* Ok, send ShAKY something to start it */
	incr = (incremental + '0');
	write(acmfd, &incr, 1); // really ShAKYAGM or ShAKYONE...

	advance();

	/* First thing seen should be "ShAKY########\n" */
	if (!match('S') || !match('h') || !match('A') || !match('K') || !match('Y')) {
		fprintf(stderr, "ShAKY not recognized\n");
		exit(1);
	}
	int v = 0;
	for (int i=0; i<8; ++i) {
		if ((nextc < '0') || (nextc > '9')) {
			fprintf(stderr, "ShAKY version (%d) not recognized\n", v);
			exit(1);
		}

		v = (nextc - '0') + (10 * v);
		advance();
	}
	if (nextc != '\n') {
		fprintf(stderr, "ShAKY%d couldn't connect to MPU9250\n", v);
		exit(1);
	}

	fprintf(stderr,
"ShAKY%d recognized.\n"
"Move unit in figure 8 to calibrate.\n",
		v);

	/* Advance past end of ID string, also waits for data stream */
	match('\n');

	fprintf(stderr,
"Calibration completed.\n"
		);

	/* Get callibration output */
	magBias[0] = readf();
	magBias[1] = readf();
	magBias[2] = readf();
	magScale[0] = readf();
	magScale[1] = readf();
	magScale[2] = readf();

	fprintf(stderr, "magBias[0]=%f\n", magBias[0]);
	fprintf(stderr, "magBias[1]=%f\n", magBias[1]);
	fprintf(stderr, "magBias[2]=%f\n", magBias[2]);
	fprintf(stderr, "magScale[0]=%f\n", magScale[0]);
	fprintf(stderr, "magScale[1]=%f\n", magScale[1]);
	fprintf(stderr, "magScale[2]=%f\n", magScale[2]);


	/* Get the various constants */
	float alook[4] = { 2.0/32768.0, 4.0/32768.0, 8.0/32768.0, 16.0/32768.0 };
	aRes = alook[nextc];
	advance();
	float glook[4] = { 250.0/32768.0, 500.0/32768.0, 1000.0/32768.0, 2000.0/32768.0 };
	gRes = glook[nextc];
	advance();
	mRes = 49120.0 / (nextc ? 32760.0 : 8190.0);
	// from here on, no more advance() lookahead

	fprintf(stderr, "aRes=%f\n", aRes);
	fprintf(stderr, "gRes=%f\n", gRes);
	fprintf(stderr, "mRes=%f\n", mRes);
}

void loop()
{
  int Xsync = 0;
  int status;

  // get status byte & decode Xsync
  status = advance();
  Xsync = ((status & ShAKYX) ? 1 : 0);

//printf("Status 0x%2x\n", status);

  // always a timestamp -- us since last reading
  deltat = read16() / 1000000.0;
  sum += deltat; // sum for averaging filter update rate
  sumCount++;

  // Acceleration?
  if (status & ShAKYA) {
    // Get the accleration values in G's
    ax = read16() * aRes;
    ay = read16() * aRes;
    az = read16() * aRes;
  }

  // Gyroscopes?
  if (status & ShAKYG) {
    // Get the gyro value in degrees per second
    gx = read16() * gRes;
    gy = read16() * gRes;
    gz = read16() * gRes; 
  }

  // Magenetometers?
  if (status & ShAKYM) {
    mx = read16() * mRes * magCalibration[0] - magBias[0];
    my = read16() * mRes * magCalibration[1] - magBias[1];
    mz = read16() * mRes * magCalibration[2] - magBias[2];
  }


   MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
//  MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);

    yaw   = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);   
    pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
    roll  = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
    pitch *= 180.0f / PI;
    yaw   *= 180.0f / PI; 
    yaw   += 5.49; // Declination at UK campus, 20190928, by IGRF2015 at https://www.thecompassstore.com/decvar.html
    roll  *= 180.0f / PI;


// Output --------------------------------------------------------------------------------------------------------------

typr[typrsp].time = sum;
typr[typrsp].yaw = yaw+180;
typr[typrsp].pitch = pitch;
typr[typrsp].roll = roll;


if (shutter == 0) {
	printf("%f\t%f\t%f\t%f%s", deltat, (yaw+180), pitch, roll, (Xsync ? "*\n" : "\n"));
} else {
	if (endexp <= sum) {
		// Time to end exposure
		if (!fork()) expose();
		endexp = NEVER;
	}
}

if (Xsync) {
	startexp = typrsp;
	endexp = sum + shutter;
	Xsync = 0;
}

typrsp = ((typrsp + 1) & (CBUFMAX-1));

// if (!(rand() & 0xff)) fprintf(stderr, "%f\n", 1.0/deltat);
    
// --------------------------------------------------------------------------------
}
