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