MotionCal/imuread.c

138 lines
2.7 KiB
C
Raw Normal View History

2016-02-20 13:46:16 -08:00
#if defined(LINUX)
2016-02-19 17:05:16 -08:00
2016-02-14 15:00:53 -08:00
#include "imuread.h"
2016-02-18 01:31:30 -08:00
#include <GL/glut.h> // sudo apt-get install xorg-dev libglu1-mesa-dev freeglut3-dev
2016-02-13 16:06:36 -08:00
2016-02-19 17:05:16 -08:00
void die(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
2016-02-18 01:31:30 -08:00
static void timer_callback(int val)
2016-02-13 16:06:36 -08:00
{
2016-02-19 17:05:16 -08:00
int r;
2016-02-13 19:58:30 -08:00
glutTimerFunc(TIMEOUT_MSEC, timer_callback, 0);
2016-02-19 17:05:16 -08:00
r = read_serial_data();
if (r < 0) die("Error reading %s\n", PORT);
2016-02-14 15:18:56 -08:00
glutPostRedisplay(); // TODO: only redisplay if data changes
}
2016-02-18 01:31:30 -08:00
static void glut_display_callback(void)
2016-02-14 15:18:56 -08:00
{
2016-02-18 01:31:30 -08:00
display_callback();
glutSwapBuffers();
2016-02-13 19:58:30 -08:00
}
extern int invert_q0;
extern int invert_q1;
extern int invert_q2;
extern int invert_q3;
extern int invert_x;
extern int invert_y;
extern int invert_z;
static void print_invert_state(void)
{
printf("Invert: %s %s %s %s %s %s %s\n",
(invert_q0 ? "Q0" : " "),
(invert_q1 ? "Q1" : " "),
(invert_q2 ? "Q2" : " "),
(invert_q3 ? "Q3" : " "),
(invert_x ? "x'" : " "),
(invert_y ? "y'" : " "),
(invert_z ? "z'" : " ")
);
}
static void glut_keystroke_callback(unsigned char ch, int x, int y)
{
if (ch == '0') {
invert_q0 ^= 1;
print_invert_state();
return;
}
if (ch == '1') {
invert_q1 ^= 1;
print_invert_state();
return;
}
if (ch == '2') {
invert_q2 ^= 1;
print_invert_state();
return;
}
if (ch == '3') {
invert_q3 ^= 1;
print_invert_state();
return;
}
if (ch == 'x') {
invert_x ^= 1;
print_invert_state();
return;
}
if (ch == 'y') {
invert_y ^= 1;
print_invert_state();
return;
}
if (ch == 'z') {
invert_z ^= 1;
print_invert_state();
return;
}
2016-03-13 07:45:41 -07:00
if (magcal.FitError > 9.0) {
printf("Poor Calibration: ");
2016-03-13 07:45:41 -07:00
printf("soft iron fit error = %.1f%%\n", magcal.FitError);
return;
}
2016-03-13 07:45:41 -07:00
printf("Magnetic Calibration: (%.1f%% fit error)\n", magcal.FitError);
printf(" %7.2f %6.3f %6.3f %6.3f\n",
2016-03-13 07:45:41 -07:00
magcal.V[0], magcal.invW[0][0], magcal.invW[0][1], magcal.invW[0][2]);
printf(" %7.2f %6.3f %6.3f %6.3f\n",
2016-03-13 07:45:41 -07:00
magcal.V[1], magcal.invW[1][0], magcal.invW[1][1], magcal.invW[1][2]);
printf(" %7.2f %6.3f %6.3f %6.3f\n",
2016-03-13 07:45:41 -07:00
magcal.V[2], magcal.invW[2][0], magcal.invW[2][1], magcal.invW[2][2]);
send_calibration();
}
2016-02-13 19:58:30 -08:00
int main(int argc, char *argv[])
{
2016-03-12 12:00:24 -08:00
raw_data_reset();
2016-02-13 19:58:30 -08:00
glutInit(&argc, argv);
2016-02-14 09:22:11 -08:00
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
2016-02-13 19:58:30 -08:00
glutInitWindowSize(600, 500);
glutCreateWindow("IMU Read");
2016-02-16 17:19:44 -08:00
visualize_init();
2016-02-14 09:22:11 -08:00
glutReshapeFunc(resize_callback);
2016-02-18 01:31:30 -08:00
glutDisplayFunc(glut_display_callback);
2016-02-13 19:58:30 -08:00
glutTimerFunc(TIMEOUT_MSEC, timer_callback, 0);
glutKeyboardFunc(glut_keystroke_callback);
2016-02-13 16:06:36 -08:00
2016-02-19 17:05:16 -08:00
if (!open_port(PORT)) die("Unable to open %s\n", PORT);
2016-02-13 19:58:30 -08:00
glutMainLoop();
2016-02-14 15:00:53 -08:00
close_port();
2016-02-13 16:06:36 -08:00
return 0;
}
2016-02-19 17:05:16 -08:00
void die(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
close_port();
exit(1);
}
2016-02-13 16:06:36 -08:00
2016-02-19 17:05:16 -08:00
#else
int main(int argc, char *argv[])
{
return 0;
}
2016-02-13 16:06:36 -08:00
2016-02-19 17:05:16 -08:00
#endif