Prueba para alumnos de la UNSL.

Dependencies:   mbed mbed-rtos eCompass_Lib MMA8451Q SLCD MAG3110

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers <title>main.cpp Source File</title>

main.cpp

00001 #include "mbed.h"
00002 #include "eCompass_Lib.h"
00003 #include "MAG3110.h"
00004 #include "MMA8451Q.h"
00005 #include "rtos.h"
00006 #include "SLCD.h"
00007 
00008 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00009 
00010 
00011 eCompass compass;
00012 MAG3110  mag( PTE25, PTE24);
00013 MMA8451Q acc( PTE25, PTE24, MMA8451_I2C_ADDRESS);
00014 DigitalOut red(LED_RED);
00015 DigitalOut green(LED_GREEN);
00016 Serial pc(USBTX, USBRX);
00017 SLCD slcd;
00018 
00019 extern axis6_t axis6;
00020 extern uint32_t seconds;
00021 extern uint32_t compass_type;
00022 extern int32_t tcount;
00023 extern uint8_t cdebug; 
00024 
00025 
00026 MotionSensorDataCounts mag_raw;
00027 MotionSensorDataCounts acc_raw;
00028 
00029 
00030 Thread *(debugp);
00031 Thread *(calibrate);
00032 Thread *(lcdp);
00033 
00034 //
00035 // Print data values for debug
00036 //
00037 void debug_print(void)
00038 {
00039     int h, m, s;
00040     h = seconds / 3600;
00041     m = (seconds % 3600) / 60;
00042     s = seconds % 60;
00043     // Some useful printf statements for debug
00044     printf("Runtime= %d:%02d:%02d\r\n", h, m, s);
00045     printf("roll=%d, pitch=%d, yaw=%d\r\n", axis6.roll, axis6.pitch, axis6.yaw);
00046     printf("Acc: X= %2.3f Y= %2.3f Z= %2.3f    ", axis6.fGax, axis6.fGay, axis6.fGaz);
00047     printf("Mag: X= %4.1f Y= %4.1f Z= %4.1f\r\n", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz);
00048     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); 
00049 }
00050 // HAL Map for KL46 Freedom board MMA8451Q & MAG3110 sensors
00051 //
00052 // This routing move and negates data as needed the
00053 // properly align the sensor axis for our desired compass (NED)
00054 // For more information see Freescale appication note AN4696
00055 //
00056 void hal_map( MotionSensorDataCounts * acc_data, MotionSensorDataCounts * mag_data)
00057 {
00058     int16_t t;
00059     // swap and negate accelerometer x & y
00060     t = acc_data->y;
00061     acc_data->y = acc_data->x * -1;
00062     acc_data->x = t * -1;
00063 
00064     // negate magnetometer y
00065     mag_data->y *= -1;
00066     
00067 }
00068 
00069 //
00070 // This is the 50Hz thread where the magic happens
00071 //
00072 int  l = 0;
00073 
00074 void compass_thread(void const *argument) {
00075         // get raw data from the sensors
00076         mag.getAxis(mag_raw);
00077         acc.getAxis(acc_raw);
00078         if(tcount) compass.run( acc_raw, mag_raw); // calculate the eCompass
00079         if(!(l % 10)) lcdp->signal_set(0x04);
00080         if(l++ >= 50) { // take car of business once a second
00081             seconds++;
00082             calibrate->signal_set(0x2);
00083             debugp->signal_set(0x01);
00084             l = 0;
00085             green = !green;
00086             }
00087         tcount++;
00088 }
00089 
00090 
00091 void calibrate_thread(void const *argument) {
00092     while(true) {
00093         Thread::signal_wait(0x2); 
00094         red = 0;
00095         compass.calibrate();
00096         red = 1;
00097         }
00098 }
00099 
00100  
00101 void print_thread(void const *argument) {
00102     while (true) {
00103         // Signal flags that are reported as event are automatically cleared.
00104         Thread::signal_wait(0x1);    
00105         debug_print();
00106     }
00107 }
00108 
00109 
00110 void lcd_thread(void const *argument) {
00111     while (true) {
00112         // Signal flags that are reported as event are automatically cleared.
00113         Thread::signal_wait(0x4);  
00114         slcd.printf("Hola"); // print the heading (NED compass) to the LCD
00115     }
00116 }
00117                                  
00118 int main() {
00119 
00120     slcd.clear();
00121     tcount = 0;
00122     red = 1;
00123     green = 1;
00124     //cdebug = 1; // Set to 1 to disable eCompass in order to observe raw mag data.
00125     compass_type = NED_COMPASS;
00126     seconds = 0;
00127     Thread t1(print_thread);  
00128     Thread t2(calibrate_thread);
00129     Thread t3(lcd_thread);
00130     debugp = &t1;
00131     calibrate = &t2;
00132     lcdp = &t3;  
00133     mag.enable();
00134     acc.enable();
00135     // Say hello to our sensors
00136     // Native KL46-FRDM sensors
00137     printf("\r\nMMA8451Q ID= %X\r\n", acc.whoAmI());
00138     printf("MAG3110 ID= %X\r\n", mag.whoAmI());
00139     mag.getAxis(mag_raw); // flush the magnetmeter
00140     RtosTimer compass_timer(compass_thread, osTimerPeriodic);
00141     
00142     /*
00143      * Thread Priorities
00144      * Compass Thread,      High Priority
00145      * Compass calibration, Above Normal
00146      * LED Update,          Normal
00147      * printf to console,   Below Normal
00148      * main(),              Normal
00149      */
00150 
00151     debugp->set_priority(osPriorityBelowNormal);
00152     lcdp->set_priority(osPriorityLow);
00153     calibrate->set_priority(osPriorityAboveNormal);
00154     compass_timer.start(20); // Run the Compass every 20ms
00155     
00156     Thread::wait(osWaitForever);   
00157 }