This program uses the MMA8451 & MAG3110 on a KL-46 freedom board to implement a tilt compensated eCompass with the heading displayed on the LCD. This program uses the mbed RTOS to manage the hard real time requirements of the eCompass algorithm

Dependencies:   MAG3110 MMA8451Q SLCD eCompass_Lib mbed-rtos mbed

Committer:
JimCarver
Date:
Fri May 16 18:23:32 2014 +0000
Revision:
3:0770c275e6e8
Parent:
2:e4ae1d748311
Child:
4:ba1dbfb683fb
Implements new virtual MotionSensor class, also makes better use of mbed-RTOS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:4e1d43dc608f 1 #include "mbed.h"
JimCarver 2:e4ae1d748311 2 #include "eCompass_Lib.h"
JimCarver 0:4e1d43dc608f 3 #include "MAG3110.h"
JimCarver 0:4e1d43dc608f 4 #include "MMA8451Q.h"
JimCarver 0:4e1d43dc608f 5 #include "rtos.h"
JimCarver 0:4e1d43dc608f 6 #include "SLCD.h"
JimCarver 0:4e1d43dc608f 7
JimCarver 0:4e1d43dc608f 8 #define MMA8451_I2C_ADDRESS (0x1d<<1)
JimCarver 0:4e1d43dc608f 9
JimCarver 0:4e1d43dc608f 10
JimCarver 2:e4ae1d748311 11 eCompass compass;
JimCarver 0:4e1d43dc608f 12 MAG3110 mag( PTE25, PTE24);
JimCarver 0:4e1d43dc608f 13 MMA8451Q acc( PTE25, PTE24, MMA8451_I2C_ADDRESS);
JimCarver 2:e4ae1d748311 14 DigitalOut red(LED_RED);
JimCarver 2:e4ae1d748311 15 DigitalOut green(LED_GREEN);
JimCarver 0:4e1d43dc608f 16 Serial pc(USBTX, USBRX);
JimCarver 0:4e1d43dc608f 17 SLCD slcd;
JimCarver 0:4e1d43dc608f 18
JimCarver 2:e4ae1d748311 19 extern axis6_t axis6;
JimCarver 2:e4ae1d748311 20 extern uint32_t seconds;
JimCarver 2:e4ae1d748311 21 extern uint32_t compass_type;
JimCarver 2:e4ae1d748311 22 extern int32_t tcount;
JimCarver 2:e4ae1d748311 23 extern uint8_t cdebug;
JimCarver 3:0770c275e6e8 24
JimCarver 3:0770c275e6e8 25
JimCarver 3:0770c275e6e8 26 MotionSensorDataCounts mag_raw;
JimCarver 3:0770c275e6e8 27 MotionSensorDataCounts acc_raw;
JimCarver 3:0770c275e6e8 28
JimCarver 3:0770c275e6e8 29
JimCarver 3:0770c275e6e8 30 Thread *(debugp);
JimCarver 3:0770c275e6e8 31 Thread *(calibrate);
JimCarver 3:0770c275e6e8 32 Thread *(lcdp);
JimCarver 3:0770c275e6e8 33
JimCarver 2:e4ae1d748311 34 //
JimCarver 2:e4ae1d748311 35 // Print data values for debug
JimCarver 2:e4ae1d748311 36 //
JimCarver 2:e4ae1d748311 37 void debug_print(void)
JimCarver 2:e4ae1d748311 38 {
JimCarver 2:e4ae1d748311 39 // Some useful printf statements for debug
JimCarver 2:e4ae1d748311 40 printf("roll=%d, pitch=%d, yaw=%d\r\n", axis6.roll, axis6.pitch, axis6.yaw);
JimCarver 2:e4ae1d748311 41 printf("Acc: X= %2.3f Y= %2.3f Z= %2.3f ", axis6.fGax, axis6.fGay, axis6.fGaz);
JimCarver 2:e4ae1d748311 42 printf("Mag: X= %4.1f Y= %4.1f Z= %4.1f\r\n", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz);
JimCarver 2:e4ae1d748311 43 printf("Quaternion: Q0= %1.4f Q1= %1.4f Q2= %1.4f Q3= %1.4f\r\n\r\n", axis6.q0, axis6.q1, axis6.q2, axis6.q3);
JimCarver 2:e4ae1d748311 44 }
JimCarver 2:e4ae1d748311 45 // HAL Map for KL46 Freedom board MMA8451Q & MAG3110 sensors
JimCarver 2:e4ae1d748311 46 //
JimCarver 0:4e1d43dc608f 47 // This routing move and negates data as needed the
JimCarver 0:4e1d43dc608f 48 // properly align the sensor axis for our desired compass (NED)
JimCarver 2:e4ae1d748311 49 // For more information see Freescale appication note AN4696
JimCarver 0:4e1d43dc608f 50 //
JimCarver 3:0770c275e6e8 51 void hal_map( MotionSensorDataCounts * acc_data, MotionSensorDataCounts * mag_data)
JimCarver 0:4e1d43dc608f 52 {
JimCarver 0:4e1d43dc608f 53 int16_t t;
JimCarver 3:0770c275e6e8 54 // swap and negate accelerometer x & y
JimCarver 3:0770c275e6e8 55 t = acc_data->y;
JimCarver 3:0770c275e6e8 56 acc_data->y = acc_data->x * -1;
JimCarver 3:0770c275e6e8 57 acc_data->x = t * -1;
JimCarver 3:0770c275e6e8 58
JimCarver 0:4e1d43dc608f 59 // negate magnetometer y
JimCarver 3:0770c275e6e8 60 mag_data->y *= -1;
JimCarver 0:4e1d43dc608f 61
JimCarver 0:4e1d43dc608f 62 }
JimCarver 0:4e1d43dc608f 63
JimCarver 0:4e1d43dc608f 64 //
JimCarver 0:4e1d43dc608f 65 // This is the 50Hz thread where the magic happens
JimCarver 0:4e1d43dc608f 66 //
JimCarver 2:e4ae1d748311 67 int l = 0;
JimCarver 2:e4ae1d748311 68
JimCarver 0:4e1d43dc608f 69 void compass_thread(void const *argument) {
JimCarver 0:4e1d43dc608f 70 // get raw data from the sensors
JimCarver 3:0770c275e6e8 71 mag.getAxis(mag_raw);
JimCarver 3:0770c275e6e8 72 acc.getAxis(acc_raw);
JimCarver 2:e4ae1d748311 73 if(tcount) compass.run( acc_raw, mag_raw); // calculate the eCompass
JimCarver 3:0770c275e6e8 74 if(!(l % 10)) lcdp->signal_set(0x04);
JimCarver 0:4e1d43dc608f 75 if(l++ >= 50) { // take car of business once a second
JimCarver 0:4e1d43dc608f 76 seconds++;
JimCarver 3:0770c275e6e8 77 calibrate->signal_set(0x2);
JimCarver 3:0770c275e6e8 78 debugp->signal_set(0x01);
JimCarver 0:4e1d43dc608f 79 l = 0;
JimCarver 2:e4ae1d748311 80 green = !green;
JimCarver 0:4e1d43dc608f 81 }
JimCarver 0:4e1d43dc608f 82 tcount++;
JimCarver 0:4e1d43dc608f 83 }
JimCarver 3:0770c275e6e8 84
JimCarver 3:0770c275e6e8 85
JimCarver 3:0770c275e6e8 86 void calibrate_thread(void const *argument) {
JimCarver 3:0770c275e6e8 87 while(true) {
JimCarver 3:0770c275e6e8 88 Thread::signal_wait(0x2);
JimCarver 3:0770c275e6e8 89 compass.calibrate();
JimCarver 3:0770c275e6e8 90 }
JimCarver 3:0770c275e6e8 91 }
JimCarver 3:0770c275e6e8 92
JimCarver 3:0770c275e6e8 93
JimCarver 3:0770c275e6e8 94 void print_thread(void const *argument) {
JimCarver 3:0770c275e6e8 95 while (true) {
JimCarver 3:0770c275e6e8 96 // Signal flags that are reported as event are automatically cleared.
JimCarver 3:0770c275e6e8 97 Thread::signal_wait(0x1);
JimCarver 3:0770c275e6e8 98 debug_print();
JimCarver 3:0770c275e6e8 99 }
JimCarver 3:0770c275e6e8 100 }
JimCarver 3:0770c275e6e8 101
JimCarver 3:0770c275e6e8 102
JimCarver 3:0770c275e6e8 103 void lcd_thread(void const *argument) {
JimCarver 3:0770c275e6e8 104 while (true) {
JimCarver 3:0770c275e6e8 105 // Signal flags that are reported as event are automatically cleared.
JimCarver 3:0770c275e6e8 106 Thread::signal_wait(0x4);
JimCarver 3:0770c275e6e8 107 slcd.printf("%04d", axis6.yaw); // print the heading (NED compass) to the LCD
JimCarver 3:0770c275e6e8 108 }
JimCarver 3:0770c275e6e8 109 }
JimCarver 3:0770c275e6e8 110
JimCarver 0:4e1d43dc608f 111 int main() {
JimCarver 3:0770c275e6e8 112
JimCarver 0:4e1d43dc608f 113 slcd.clear();
JimCarver 0:4e1d43dc608f 114 tcount = 0;
JimCarver 2:e4ae1d748311 115 red = 1;
JimCarver 2:e4ae1d748311 116 green = 1;
JimCarver 2:e4ae1d748311 117 //cdebug = 1; // Set to 1 to disable eCompass in order to observe raw mag data.
JimCarver 0:4e1d43dc608f 118 compass_type = NED_COMPASS;
JimCarver 0:4e1d43dc608f 119 seconds = 0;
JimCarver 3:0770c275e6e8 120 Thread t1(print_thread);
JimCarver 3:0770c275e6e8 121 Thread t2(calibrate_thread);
JimCarver 3:0770c275e6e8 122 Thread t3(lcd_thread);
JimCarver 3:0770c275e6e8 123 debugp = &t1;
JimCarver 3:0770c275e6e8 124 calibrate = &t2;
JimCarver 3:0770c275e6e8 125 lcdp = &t3;
JimCarver 3:0770c275e6e8 126 mag.enable();
JimCarver 3:0770c275e6e8 127 acc.enable();
JimCarver 0:4e1d43dc608f 128 // make sure our sensors are talking to us
JimCarver 2:e4ae1d748311 129 // Only available when multi-sensor shield is installed
JimCarver 2:e4ae1d748311 130 //printf("\r\nFXOS8700 ID= %X\r\n", combo0.getWhoAmI());
JimCarver 3:0770c275e6e8 131 // Native KL46-FRDM sensors
JimCarver 3:0770c275e6e8 132 printf("\r\nMMA8451Q ID= %X\r\n", acc.whoAmI());
JimCarver 3:0770c275e6e8 133 printf("MAG3110 ID= %X\r\n", mag.whoAmI());
JimCarver 3:0770c275e6e8 134 mag.getAxis(mag_raw); // flush the magnetmeter
JimCarver 2:e4ae1d748311 135 RtosTimer compass_timer(compass_thread, osTimerPeriodic);
JimCarver 3:0770c275e6e8 136
JimCarver 3:0770c275e6e8 137 /*
JimCarver 3:0770c275e6e8 138 * Thread Priorities
JimCarver 3:0770c275e6e8 139 * Compass Thread, High Priority
JimCarver 3:0770c275e6e8 140 * Compass calibration, Above Normal
JimCarver 3:0770c275e6e8 141 * LED Update, Normal
JimCarver 3:0770c275e6e8 142 * printf to console, Below Normal
JimCarver 3:0770c275e6e8 143 * main(), Normal
JimCarver 3:0770c275e6e8 144 */
JimCarver 3:0770c275e6e8 145
JimCarver 3:0770c275e6e8 146 debugp->set_priority(osPriorityBelowNormal);
JimCarver 3:0770c275e6e8 147 lcdp->set_priority(osPriorityLow);
JimCarver 3:0770c275e6e8 148 calibrate->set_priority(osPriorityAboveNormal);
JimCarver 3:0770c275e6e8 149
JimCarver 2:e4ae1d748311 150 compass_timer.start(20); // Run the Compass every 20ms
JimCarver 2:e4ae1d748311 151 Thread::wait(osWaitForever);
JimCarver 0:4e1d43dc608f 152 }