Code to demo/test out my IMU board

Dependencies:   mbed ITG3200_lib HMC5843_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LIS331.h"
00003 #include "ITG3200.h"
00004 #include "HMC5843.h"
00005 //#include "SDHCFileSystem.h"
00006 
00007 // Define binary expansions if needed
00008 //#define Ob(x)  ((unsigned)Ob_(0 ## x ## uL))
00009 //#define Ob_(x) (x & 1 | x >> 2 & 2 | x >> 4 & 4 | x >> 6 & 8 |        \
00010 //  x >> 8 & 16 | x >> 10 & 32 | x >> 12 & 64 | x >> 14 & 128)
00011 
00012 
00013 
00014 //SDFileSystem sd(p5, p6, p7, p8, "sd");
00015 Serial pc(USBTX, USBRX);
00016 LIS331 accel(p9, p10);
00017 ITG3200 gyro(p9, p10);
00018 HMC5843 compass(p9, p10);
00019 Timer t;
00020 DigitalOut success_led(LED4);
00021 DigitalOut progress_led(LED3);
00022 DigitalOut error_led(LED1);
00023 
00024 int readings[3];
00025 
00026 int main() {
00027     success_led = 0;
00028     error_led = 0;
00029     pc.printf("Now starting LIS331/ITG-3200 acceptance test...\n\r");
00030     
00031     // Set Highest Gyro Bandwidth
00032     gyro.setLpBandwidth(LPFBW_256HZ);
00033     
00034     // Set 8g range on accel
00035     accel.setFullScaleRange8g();
00036     
00037     //Continuous mode, , 10Hz measurement rate.
00038     // HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA
00039     compass.setDefault();
00040     
00041     //FILE *fp = fopen("/sd/data.txt", "w");
00042     //if(fp == NULL) {
00043     //    error_led = 1;  // crap something went wrong
00044     //    error("Could not open file for write\n");
00045     //}
00046     //success_led = 1;    // file is open for writing!
00047     
00048         
00049     pc.printf("Accel Address:%x\n\r",accel.getWhoAmI());
00050     pc.printf("Gyro Address:%x\n\r",gyro.getWhoAmI());
00051     pc.printf("Temp(C):%f\n\r",gyro.getTemperature());
00052     
00053     wait(0.9);
00054 
00055 
00056 
00057 
00058     t.start();  // Start our timer
00059     while (1) {
00060         progress_led = 1;
00061         //Arbitrary wait for printf clarity.
00062         wait(0.3);
00063         compass.readData(readings);
00064     
00065         
00066         pc.printf("\n\r%f,", t.read());     // get current time in seconds
00067         pc.printf("%f,%f,%f,", (float)accel.getAccelX(), (float)accel.getAccelY(), (float)accel.getAccelZ());
00068         pc.printf("%f,%f,%f", (float)gyro.getGyroX() / 14.375, (float)gyro.getGyroY() / 14.375, (float)gyro.getGyroZ() / 14.375);
00069 
00070         // uncomment next line to enable output of mag values
00071         //pc.printf(",%i,%i,%i", (int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
00072         
00073         progress_led = 0;
00074         if (t.read() > 240) {    // quit after 240 seconds, change it to however long you want to record for
00075             break;  // LED3 will remain off when done writing to card
00076         }
00077     }
00078     
00079 wait(0.5);
00080     
00081 // fclose(fp);     // Need to add Physical_Switch --> DigitalIn --> Int --> fclose()
00082 }