![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final, cambios pendientes
Dependencies: MAG3110 MMA8451Q SLCD eCompass_Lib mbed-rtos mbed
Fork of KL46_eCompass_TiltCompensed_Acel-Mag by
main.cpp@5:5199647e821a, 2015-09-25 (annotated)
- Committer:
- IrvingHdez
- Date:
- Fri Sep 25 19:14:30 2015 +0000
- Revision:
- 5:5199647e821a
- Parent:
- 4:ba1dbfb683fb
- Child:
- 6:93c9844af619
Ragnarok project
Who changed what in which revision?
User | Revision | Line number | New 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 | MotionSensorDataCounts mag_raw; |
JimCarver | 3:0770c275e6e8 | 26 | MotionSensorDataCounts acc_raw; |
JimCarver | 3:0770c275e6e8 | 27 | |
JimCarver | 3:0770c275e6e8 | 28 | Thread *(debugp); |
JimCarver | 3:0770c275e6e8 | 29 | Thread *(calibrate); |
JimCarver | 3:0770c275e6e8 | 30 | Thread *(lcdp); |
JimCarver | 3:0770c275e6e8 | 31 | |
JimCarver | 2:e4ae1d748311 | 32 | // |
JimCarver | 2:e4ae1d748311 | 33 | // Print data values for debug |
JimCarver | 2:e4ae1d748311 | 34 | // |
JimCarver | 2:e4ae1d748311 | 35 | void debug_print(void) |
JimCarver | 2:e4ae1d748311 | 36 | { |
JimCarver | 4:ba1dbfb683fb | 37 | int h, m, s; |
JimCarver | 4:ba1dbfb683fb | 38 | h = seconds / 3600; |
JimCarver | 4:ba1dbfb683fb | 39 | m = (seconds % 3600) / 60; |
JimCarver | 4:ba1dbfb683fb | 40 | s = seconds % 60; |
JimCarver | 2:e4ae1d748311 | 41 | // Some useful printf statements for debug |
JimCarver | 4:ba1dbfb683fb | 42 | printf("Runtime= %d:%02d:%02d\r\n", h, m, s); |
JimCarver | 2:e4ae1d748311 | 43 | printf("roll=%d, pitch=%d, yaw=%d\r\n", axis6.roll, axis6.pitch, axis6.yaw); |
JimCarver | 2:e4ae1d748311 | 44 | printf("Acc: X= %2.3f Y= %2.3f Z= %2.3f ", axis6.fGax, axis6.fGay, axis6.fGaz); |
JimCarver | 2:e4ae1d748311 | 45 | printf("Mag: X= %4.1f Y= %4.1f Z= %4.1f\r\n", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz); |
JimCarver | 4:ba1dbfb683fb | 46 | 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 | 47 | } |
JimCarver | 2:e4ae1d748311 | 48 | // HAL Map for KL46 Freedom board MMA8451Q & MAG3110 sensors |
JimCarver | 2:e4ae1d748311 | 49 | // |
JimCarver | 0:4e1d43dc608f | 50 | // This routing move and negates data as needed the |
JimCarver | 0:4e1d43dc608f | 51 | // properly align the sensor axis for our desired compass (NED) |
JimCarver | 2:e4ae1d748311 | 52 | // For more information see Freescale appication note AN4696 |
JimCarver | 0:4e1d43dc608f | 53 | // |
JimCarver | 3:0770c275e6e8 | 54 | void hal_map( MotionSensorDataCounts * acc_data, MotionSensorDataCounts * mag_data) |
JimCarver | 0:4e1d43dc608f | 55 | { |
JimCarver | 0:4e1d43dc608f | 56 | int16_t t; |
JimCarver | 3:0770c275e6e8 | 57 | // swap and negate accelerometer x & y |
JimCarver | 3:0770c275e6e8 | 58 | t = acc_data->y; |
JimCarver | 3:0770c275e6e8 | 59 | acc_data->y = acc_data->x * -1; |
JimCarver | 3:0770c275e6e8 | 60 | acc_data->x = t * -1; |
JimCarver | 3:0770c275e6e8 | 61 | |
JimCarver | 0:4e1d43dc608f | 62 | // negate magnetometer y |
JimCarver | 3:0770c275e6e8 | 63 | mag_data->y *= -1; |
JimCarver | 0:4e1d43dc608f | 64 | |
JimCarver | 0:4e1d43dc608f | 65 | } |
JimCarver | 0:4e1d43dc608f | 66 | |
JimCarver | 0:4e1d43dc608f | 67 | // |
JimCarver | 0:4e1d43dc608f | 68 | // This is the 50Hz thread where the magic happens |
JimCarver | 0:4e1d43dc608f | 69 | // |
JimCarver | 2:e4ae1d748311 | 70 | int l = 0; |
JimCarver | 2:e4ae1d748311 | 71 | |
JimCarver | 0:4e1d43dc608f | 72 | void compass_thread(void const *argument) { |
JimCarver | 0:4e1d43dc608f | 73 | // get raw data from the sensors |
JimCarver | 3:0770c275e6e8 | 74 | mag.getAxis(mag_raw); |
JimCarver | 3:0770c275e6e8 | 75 | acc.getAxis(acc_raw); |
JimCarver | 2:e4ae1d748311 | 76 | if(tcount) compass.run( acc_raw, mag_raw); // calculate the eCompass |
JimCarver | 3:0770c275e6e8 | 77 | if(!(l % 10)) lcdp->signal_set(0x04); |
JimCarver | 0:4e1d43dc608f | 78 | if(l++ >= 50) { // take car of business once a second |
JimCarver | 0:4e1d43dc608f | 79 | seconds++; |
JimCarver | 3:0770c275e6e8 | 80 | calibrate->signal_set(0x2); |
JimCarver | 3:0770c275e6e8 | 81 | debugp->signal_set(0x01); |
JimCarver | 0:4e1d43dc608f | 82 | l = 0; |
JimCarver | 2:e4ae1d748311 | 83 | green = !green; |
JimCarver | 0:4e1d43dc608f | 84 | } |
JimCarver | 0:4e1d43dc608f | 85 | tcount++; |
JimCarver | 0:4e1d43dc608f | 86 | } |
JimCarver | 3:0770c275e6e8 | 87 | |
JimCarver | 3:0770c275e6e8 | 88 | |
JimCarver | 3:0770c275e6e8 | 89 | void calibrate_thread(void const *argument) { |
JimCarver | 3:0770c275e6e8 | 90 | while(true) { |
JimCarver | 3:0770c275e6e8 | 91 | Thread::signal_wait(0x2); |
JimCarver | 4:ba1dbfb683fb | 92 | red = 0; |
JimCarver | 3:0770c275e6e8 | 93 | compass.calibrate(); |
JimCarver | 4:ba1dbfb683fb | 94 | red = 1; |
JimCarver | 3:0770c275e6e8 | 95 | } |
JimCarver | 3:0770c275e6e8 | 96 | } |
JimCarver | 3:0770c275e6e8 | 97 | |
JimCarver | 3:0770c275e6e8 | 98 | |
JimCarver | 3:0770c275e6e8 | 99 | void print_thread(void const *argument) { |
JimCarver | 3:0770c275e6e8 | 100 | while (true) { |
JimCarver | 3:0770c275e6e8 | 101 | // Signal flags that are reported as event are automatically cleared. |
JimCarver | 3:0770c275e6e8 | 102 | Thread::signal_wait(0x1); |
JimCarver | 3:0770c275e6e8 | 103 | debug_print(); |
JimCarver | 3:0770c275e6e8 | 104 | } |
JimCarver | 3:0770c275e6e8 | 105 | } |
JimCarver | 3:0770c275e6e8 | 106 | |
JimCarver | 3:0770c275e6e8 | 107 | |
JimCarver | 3:0770c275e6e8 | 108 | void lcd_thread(void const *argument) { |
JimCarver | 3:0770c275e6e8 | 109 | while (true) { |
JimCarver | 3:0770c275e6e8 | 110 | // Signal flags that are reported as event are automatically cleared. |
JimCarver | 3:0770c275e6e8 | 111 | Thread::signal_wait(0x4); |
IrvingHdez | 5:5199647e821a | 112 | slcd.printf("%04d", axis6.roll); // print the heading (NED compass) to the LCD |
JimCarver | 3:0770c275e6e8 | 113 | } |
JimCarver | 3:0770c275e6e8 | 114 | } |
JimCarver | 3:0770c275e6e8 | 115 | |
JimCarver | 0:4e1d43dc608f | 116 | int main() { |
JimCarver | 3:0770c275e6e8 | 117 | |
JimCarver | 0:4e1d43dc608f | 118 | slcd.clear(); |
JimCarver | 0:4e1d43dc608f | 119 | tcount = 0; |
JimCarver | 2:e4ae1d748311 | 120 | red = 1; |
JimCarver | 2:e4ae1d748311 | 121 | green = 1; |
JimCarver | 2:e4ae1d748311 | 122 | //cdebug = 1; // Set to 1 to disable eCompass in order to observe raw mag data. |
JimCarver | 0:4e1d43dc608f | 123 | compass_type = NED_COMPASS; |
JimCarver | 0:4e1d43dc608f | 124 | seconds = 0; |
JimCarver | 3:0770c275e6e8 | 125 | Thread t1(print_thread); |
JimCarver | 3:0770c275e6e8 | 126 | Thread t2(calibrate_thread); |
JimCarver | 3:0770c275e6e8 | 127 | Thread t3(lcd_thread); |
JimCarver | 3:0770c275e6e8 | 128 | debugp = &t1; |
JimCarver | 3:0770c275e6e8 | 129 | calibrate = &t2; |
JimCarver | 3:0770c275e6e8 | 130 | lcdp = &t3; |
JimCarver | 3:0770c275e6e8 | 131 | mag.enable(); |
JimCarver | 3:0770c275e6e8 | 132 | acc.enable(); |
JimCarver | 4:ba1dbfb683fb | 133 | // Say hello to our sensors |
JimCarver | 3:0770c275e6e8 | 134 | // Native KL46-FRDM sensors |
JimCarver | 3:0770c275e6e8 | 135 | printf("\r\nMMA8451Q ID= %X\r\n", acc.whoAmI()); |
JimCarver | 3:0770c275e6e8 | 136 | printf("MAG3110 ID= %X\r\n", mag.whoAmI()); |
JimCarver | 3:0770c275e6e8 | 137 | mag.getAxis(mag_raw); // flush the magnetmeter |
JimCarver | 2:e4ae1d748311 | 138 | RtosTimer compass_timer(compass_thread, osTimerPeriodic); |
JimCarver | 3:0770c275e6e8 | 139 | |
JimCarver | 3:0770c275e6e8 | 140 | /* |
JimCarver | 3:0770c275e6e8 | 141 | * Thread Priorities |
JimCarver | 3:0770c275e6e8 | 142 | * Compass Thread, High Priority |
JimCarver | 3:0770c275e6e8 | 143 | * Compass calibration, Above Normal |
JimCarver | 3:0770c275e6e8 | 144 | * LED Update, Normal |
JimCarver | 3:0770c275e6e8 | 145 | * printf to console, Below Normal |
JimCarver | 3:0770c275e6e8 | 146 | * main(), Normal |
JimCarver | 3:0770c275e6e8 | 147 | */ |
JimCarver | 3:0770c275e6e8 | 148 | |
JimCarver | 3:0770c275e6e8 | 149 | debugp->set_priority(osPriorityBelowNormal); |
JimCarver | 3:0770c275e6e8 | 150 | lcdp->set_priority(osPriorityLow); |
JimCarver | 3:0770c275e6e8 | 151 | calibrate->set_priority(osPriorityAboveNormal); |
JimCarver | 2:e4ae1d748311 | 152 | compass_timer.start(20); // Run the Compass every 20ms |
JimCarver | 4:ba1dbfb683fb | 153 | |
JimCarver | 2:e4ae1d748311 | 154 | Thread::wait(osWaitForever); |
JimCarver | 0:4e1d43dc608f | 155 | } |