default eCompass example, but with desired serial communication

Dependencies:   MAG3110 MMA8451Q SLCD eCompass_Lib mbed-rtos mbed

Fork of KL46_eCompass by Jim Carver

Committer:
Kosinkadink
Date:
Tue Apr 05 05:47:58 2016 +0000
Revision:
5:25d98baa1ded
Parent:
4:ba1dbfb683fb
Child:
6:f68052364f92
default eCompass example with proper pc writes

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 {
Kosinkadink 5:25d98baa1ded 39 pc.printf("0,%d$",axis6.yaw);
Kosinkadink 5:25d98baa1ded 40 //int h, m, s;
Kosinkadink 5:25d98baa1ded 41 //h = seconds / 3600;
Kosinkadink 5:25d98baa1ded 42 //m = (seconds % 3600) / 60;
Kosinkadink 5:25d98baa1ded 43 //s = seconds % 60;
JimCarver 2:e4ae1d748311 44 // Some useful printf statements for debug
Kosinkadink 5:25d98baa1ded 45 //printf("Runtime= %d:%02d:%02d\r\n", h, m, s);
Kosinkadink 5:25d98baa1ded 46 //printf("roll=%d, pitch=%d, yaw=%d\r\n", axis6.roll, axis6.pitch, axis6.yaw);
Kosinkadink 5:25d98baa1ded 47 //printf("Acc: X= %2.3f Y= %2.3f Z= %2.3f ", axis6.fGax, axis6.fGay, axis6.fGaz);
Kosinkadink 5:25d98baa1ded 48 //printf("Mag: X= %4.1f Y= %4.1f Z= %4.1f\r\n", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz);
Kosinkadink 5:25d98baa1ded 49 //printf("Quaternion: Q0= %1.4f Q1= %1.4f Q2= %1.4f Q3= %1.4f\r\n\n", axis6.q0, axis6.q1, axis6.q2, axis6.q3);
JimCarver 2:e4ae1d748311 50 }
JimCarver 2:e4ae1d748311 51 // HAL Map for KL46 Freedom board MMA8451Q & MAG3110 sensors
JimCarver 2:e4ae1d748311 52 //
JimCarver 0:4e1d43dc608f 53 // This routing move and negates data as needed the
JimCarver 0:4e1d43dc608f 54 // properly align the sensor axis for our desired compass (NED)
JimCarver 2:e4ae1d748311 55 // For more information see Freescale appication note AN4696
JimCarver 0:4e1d43dc608f 56 //
JimCarver 3:0770c275e6e8 57 void hal_map( MotionSensorDataCounts * acc_data, MotionSensorDataCounts * mag_data)
JimCarver 0:4e1d43dc608f 58 {
JimCarver 0:4e1d43dc608f 59 int16_t t;
JimCarver 3:0770c275e6e8 60 // swap and negate accelerometer x & y
JimCarver 3:0770c275e6e8 61 t = acc_data->y;
JimCarver 3:0770c275e6e8 62 acc_data->y = acc_data->x * -1;
JimCarver 3:0770c275e6e8 63 acc_data->x = t * -1;
JimCarver 3:0770c275e6e8 64
JimCarver 0:4e1d43dc608f 65 // negate magnetometer y
JimCarver 3:0770c275e6e8 66 mag_data->y *= -1;
JimCarver 0:4e1d43dc608f 67
JimCarver 0:4e1d43dc608f 68 }
JimCarver 0:4e1d43dc608f 69
JimCarver 0:4e1d43dc608f 70 //
JimCarver 0:4e1d43dc608f 71 // This is the 50Hz thread where the magic happens
JimCarver 0:4e1d43dc608f 72 //
JimCarver 2:e4ae1d748311 73 int l = 0;
JimCarver 2:e4ae1d748311 74
JimCarver 0:4e1d43dc608f 75 void compass_thread(void const *argument) {
JimCarver 0:4e1d43dc608f 76 // get raw data from the sensors
JimCarver 3:0770c275e6e8 77 mag.getAxis(mag_raw);
JimCarver 3:0770c275e6e8 78 acc.getAxis(acc_raw);
JimCarver 2:e4ae1d748311 79 if(tcount) compass.run( acc_raw, mag_raw); // calculate the eCompass
JimCarver 3:0770c275e6e8 80 if(!(l % 10)) lcdp->signal_set(0x04);
JimCarver 0:4e1d43dc608f 81 if(l++ >= 50) { // take car of business once a second
JimCarver 0:4e1d43dc608f 82 seconds++;
JimCarver 3:0770c275e6e8 83 calibrate->signal_set(0x2);
JimCarver 3:0770c275e6e8 84 debugp->signal_set(0x01);
JimCarver 0:4e1d43dc608f 85 l = 0;
JimCarver 2:e4ae1d748311 86 green = !green;
JimCarver 0:4e1d43dc608f 87 }
JimCarver 0:4e1d43dc608f 88 tcount++;
JimCarver 0:4e1d43dc608f 89 }
JimCarver 3:0770c275e6e8 90
JimCarver 3:0770c275e6e8 91
JimCarver 3:0770c275e6e8 92 void calibrate_thread(void const *argument) {
JimCarver 3:0770c275e6e8 93 while(true) {
JimCarver 3:0770c275e6e8 94 Thread::signal_wait(0x2);
JimCarver 4:ba1dbfb683fb 95 red = 0;
JimCarver 3:0770c275e6e8 96 compass.calibrate();
JimCarver 4:ba1dbfb683fb 97 red = 1;
JimCarver 3:0770c275e6e8 98 }
JimCarver 3:0770c275e6e8 99 }
JimCarver 3:0770c275e6e8 100
JimCarver 3:0770c275e6e8 101
JimCarver 3:0770c275e6e8 102 void print_thread(void const *argument) {
JimCarver 3:0770c275e6e8 103 while (true) {
JimCarver 3:0770c275e6e8 104 // Signal flags that are reported as event are automatically cleared.
Kosinkadink 5:25d98baa1ded 105 //Thread::signal_wait(0x1);
Kosinkadink 5:25d98baa1ded 106 if (pc.getc())
Kosinkadink 5:25d98baa1ded 107 {
Kosinkadink 5:25d98baa1ded 108 debug_print();
Kosinkadink 5:25d98baa1ded 109 }
JimCarver 3:0770c275e6e8 110 }
JimCarver 3:0770c275e6e8 111 }
JimCarver 3:0770c275e6e8 112
JimCarver 3:0770c275e6e8 113
JimCarver 3:0770c275e6e8 114 void lcd_thread(void const *argument) {
JimCarver 3:0770c275e6e8 115 while (true) {
JimCarver 3:0770c275e6e8 116 // Signal flags that are reported as event are automatically cleared.
JimCarver 3:0770c275e6e8 117 Thread::signal_wait(0x4);
JimCarver 3:0770c275e6e8 118 slcd.printf("%04d", axis6.yaw); // print the heading (NED compass) to the LCD
JimCarver 3:0770c275e6e8 119 }
JimCarver 3:0770c275e6e8 120 }
JimCarver 3:0770c275e6e8 121
JimCarver 0:4e1d43dc608f 122 int main() {
JimCarver 3:0770c275e6e8 123
JimCarver 0:4e1d43dc608f 124 slcd.clear();
JimCarver 0:4e1d43dc608f 125 tcount = 0;
JimCarver 2:e4ae1d748311 126 red = 1;
JimCarver 2:e4ae1d748311 127 green = 1;
JimCarver 2:e4ae1d748311 128 //cdebug = 1; // Set to 1 to disable eCompass in order to observe raw mag data.
JimCarver 0:4e1d43dc608f 129 compass_type = NED_COMPASS;
JimCarver 0:4e1d43dc608f 130 seconds = 0;
JimCarver 3:0770c275e6e8 131 Thread t1(print_thread);
JimCarver 3:0770c275e6e8 132 Thread t2(calibrate_thread);
JimCarver 3:0770c275e6e8 133 Thread t3(lcd_thread);
JimCarver 3:0770c275e6e8 134 debugp = &t1;
JimCarver 3:0770c275e6e8 135 calibrate = &t2;
JimCarver 3:0770c275e6e8 136 lcdp = &t3;
JimCarver 3:0770c275e6e8 137 mag.enable();
JimCarver 3:0770c275e6e8 138 acc.enable();
JimCarver 4:ba1dbfb683fb 139 // Say hello to our sensors
JimCarver 3:0770c275e6e8 140 // Native KL46-FRDM sensors
Kosinkadink 5:25d98baa1ded 141 //printf("\r\nMMA8451Q ID= %X\r\n", acc.whoAmI());
Kosinkadink 5:25d98baa1ded 142 //printf("MAG3110 ID= %X\r\n", mag.whoAmI());
JimCarver 3:0770c275e6e8 143 mag.getAxis(mag_raw); // flush the magnetmeter
JimCarver 2:e4ae1d748311 144 RtosTimer compass_timer(compass_thread, osTimerPeriodic);
JimCarver 3:0770c275e6e8 145
JimCarver 3:0770c275e6e8 146 /*
JimCarver 3:0770c275e6e8 147 * Thread Priorities
JimCarver 3:0770c275e6e8 148 * Compass Thread, High Priority
JimCarver 3:0770c275e6e8 149 * Compass calibration, Above Normal
JimCarver 3:0770c275e6e8 150 * LED Update, Normal
JimCarver 3:0770c275e6e8 151 * printf to console, Below Normal
JimCarver 3:0770c275e6e8 152 * main(), Normal
JimCarver 3:0770c275e6e8 153 */
JimCarver 3:0770c275e6e8 154
JimCarver 3:0770c275e6e8 155 debugp->set_priority(osPriorityBelowNormal);
JimCarver 3:0770c275e6e8 156 lcdp->set_priority(osPriorityLow);
JimCarver 3:0770c275e6e8 157 calibrate->set_priority(osPriorityAboveNormal);
JimCarver 2:e4ae1d748311 158 compass_timer.start(20); // Run the Compass every 20ms
JimCarver 4:ba1dbfb683fb 159
JimCarver 2:e4ae1d748311 160 Thread::wait(osWaitForever);
JimCarver 0:4e1d43dc608f 161 }