ECE 4781 Project

Dependencies:   LSM9DS0 SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // skeleton code for ECE 2036 thermostat lab
00002 // code must be added by students
00003 #include "mbed.h"
00004 //#include "TMP36.h"
00005 #include "SDFileSystem.h"
00006 //#include "TextLCD.h"
00007 //#include "PinDetect.h"
00008 //#include "Speaker.h"
00009 // must add your new class code to the project file Shiftbrite.h
00010 //#include "Shiftbrite.h"
00011 #include "LSM9DS0.h"
00012 #include <cmath>
00013 
00014 // use class to setup temperature sensor pins
00015 //TMP36 myTMP36(p15);  //Analog in
00016 
00017 // use class to setup microSD card filesystem
00018 SDFileSystem sd(p5, p6, p7, p8, "sd");
00019 
00020 // use class to setup the LCD
00021 //TextLCD myLCD(p22, p23, p24, p25, p26, p27); // rs, e, d4-d7
00022 
00023 // use class to setup Mbed's four on-board LEDs
00024 DigitalOut myLED1(LED1);
00025 DigitalOut myLED2(LED2);
00026 DigitalOut myLED3(LED3);
00027 DigitalOut myLED4(LED4);
00028 
00029 //also setting any unused analog input pins to digital outputs reduces A/D noise a bit
00030 //see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
00031 DigitalOut P16(p16);
00032 DigitalOut P17(p17);
00033 DigitalOut P18(p18);
00034 DigitalOut P19(p19);
00035 DigitalOut P20(p20);
00036 
00037 //Copied straight from the SDFileSystem_HelloWorld Code
00038 int main() {
00039     printf("Hello World!\n");   
00040  
00041     mkdir("/sd/mydir", 0777);
00042     
00043     FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
00044     if(fp == NULL) {
00045         error("Could not open file for write\n");
00046     }
00047     fprintf(fp, "Hello fun SD Card TEST1");
00048     fclose(fp); 
00049  
00050     printf("Goodbye World!\n");
00051 }
00052 
00053 // This code is for the IMU and is taken straight from https://developer.mbed.org/users/randrews33/notebook/lsm9ds0-inertial-measurement-unit/
00054 // For testing purposes
00055 // SDO_XM and SDO_G are both grounded, so our addresses are:
00056 #define LSM9DS0_XM  0x1E // Would be 0x1E if SDO_XM is LOW
00057 #define LSM9DS0_G   0x6A // Would be 0x6A if SDO_G is LOW
00058  
00059 // Create an instance of the LSM9DS0 library called `dof` the
00060 // parameters for this constructor are:
00061 // pins,[gyro I2C address],[xm I2C add.]
00062 LSM9DS0 dof(p28, p27, LSM9DS0_G, LSM9DS0_XM);
00063 DigitalIn DReady(p23);
00064  
00065 Serial pc(USBTX, USBRX); // tx, rx
00066  
00067 bool printMag = true;
00068 bool printAccel = true;
00069 bool printGyro = true;
00070  
00071 void setup()
00072 {
00073     pc.baud(115200); // Start serial at 115200 bps
00074     // Use the begin() function to initialize the LSM9DS0 library.
00075     // You can either call it with no parameters (the easy way):
00076     uint16_t status = dof.begin();
00077     
00078     // Or call it with declarations for sensor scales and data rates:
00079     //uint16_t status = dof.begin(dof.G_SCALE_2000DPS,
00080     //                            dof.A_SCALE_6G, dof.M_SCALE_2GS);
00081     
00082     // begin() returns a 16-bit value which includes both the gyro
00083     // and accelerometers WHO_AM_I response. You can check this to
00084     // make sure communication was successful.
00085     pc.printf("LSM9DS0 WHO_AM_I's returned: 0x");
00086     pc.printf("%x\n",status);
00087     pc.printf("Should be 0x49D4\n");
00088     pc.printf("\n");
00089 }
00090  
00091 void printGyro() {
00092     dof.readGyro();
00093     
00094     pc.printf("G: ");
00095     
00096     pc.printf("%2f",dof.calcGyro(dof.gx));
00097     pc.printf(", ");
00098     pc.printf("%2f",dof.calcGyro(dof.gy));
00099     pc.printf(", ");
00100     pc.printf("%2f\n",dof.calcGyro(dof.gz));
00101 }
00102  
00103 void printAccel() {
00104     dof.readAccel();
00105     
00106     pc.printf("A: ");
00107     
00108     pc.printf("%2f",dof.calcAccel(dof.ax));
00109     pc.printf(", ");
00110     pc.printf("%2f",dof.calcAccel(dof.ay));
00111     pc.printf(", ");
00112     pc.printf("%2f\n",dof.calcAccel(dof.az));
00113     
00114 }
00115  
00116 void printMag() {
00117     dof.readMag();
00118     
00119     pc.printf("M: ");
00120     
00121     pc.printf("%2f",dof.calcMag(dof.mx));
00122     pc.printf(", ");
00123     pc.printf("%2f",dof.calcMag(dof.my));
00124     pc.printf(", ");
00125     pc.printf("%2f\n",dof.calcMag(dof.mz));
00126 }
00127  
00128 // Here's a fun function to calculate your heading, using Earth's
00129 // magnetic field.
00130 // It only works if the sensor is flat (z-axis normal to Earth).
00131 // Additionally, you may need to add or subtract a declination
00132 // angle to get the heading normalized to your location.
00133 // See: http://www.ngdc.noaa.gov/geomag/declination.shtml
00134 void printHeading(float hx, float hy)
00135 {
00136     float heading;
00137     
00138     if (hy > 0) heading = 90 - (atan(hx / hy) * (180 / 3.14));
00139     else if (hy < 0) heading = - (atan(hx / hy) * (180 / 3.14));
00140     else // hy = 0
00141     {
00142         if (hx < 0) heading = 180;
00143         else heading = 0;
00144     }
00145     
00146     pc.printf("Heading: ");
00147     pc.printf("%2f\n",heading);
00148 }
00149  
00150 // Another fun function that does calculations based on the
00151 // acclerometer data. This function will print your LSM9DS0's
00152 // orientation -- it's roll and pitch angles.
00153 void printOrientation(float x, float y, float z)
00154 {
00155     float pitch, roll;
00156     
00157     pitch = atan2(x, sqrt(y * y) + (z * z));
00158     roll = atan2(y, sqrt(x * x) + (z * z));
00159     pitch *= 180.0 / 3.14;
00160     roll *= 180.0 / 3.14;
00161     
00162     pc.printf("Pitch, Roll: ");
00163     pc.printf("%2f",pitch);
00164     pc.printf(", ");
00165     pc.printf("%2f\n",roll);
00166 }
00167  
00168 void readData() {
00169     // To read from the device, you must first call the
00170     // readMag(), readAccel(), and readGyro() functions.
00171     // When this exits, it'll update the appropriate
00172     // variables ([mx, my, mz], [ax, ay, az], [gx, gy, gz])
00173     // with the most current data.
00174  
00175     dof.readMag();
00176     dof.readAccel();
00177     dof.readGyro();
00178 }
00179  
00180 void loop() {
00181     // Loop until the Data Ready signal goes high
00182     while (!Dready) {}
00183     
00184     readData();
00185     
00186     if (printGyro) printGyro();  // Print "G: gx, gy, gz"
00187     if (printAccel) printAccel(); // Print "A: ax, ay, az"
00188     if (printMag) printMag();   // Print "M: mx, my, mz"
00189     
00190     // Print the heading and orientation for fun!
00191     printHeading((float) dof.mx, (float) dof.my);
00192     printOrientation(dof.calcAccel(dof.ax), dof.calcAccel(dof.ay),
00193                      dof.calcAccel(dof.az));
00194     pc.printf("\n");
00195     
00196     wait(2);
00197 }
00198  
00199  
00200 int main()
00201 {
00202     setup();
00203     while (true)
00204     {
00205       loop();
00206     }
00207 }