Example program for the Freescale Multi-Sensor Shield, based on based on SPI and I2C connectivity

Dependencies:   FXAS21000 FXLS8471Q FXOS8700Q MAG3110 MMA8652 MPL3115A2 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "mbed.h"
00020 #include "MAG3110.h"
00021 #include "FXOS8700Q.h"
00022 #include "MMA8652.h"
00023 #include "FXLS8471Q.h"
00024 #include "FXAS21000.h"
00025 
00026 // Requires a Freescale-compatible platform. Comment if you're getting weird results for the alt or any other sensor. Also check the interrupt params below.
00027 #define FSL_COMPATIBLE
00028 #if defined(FSL_COMPATIBLE)
00029     #define MPL3115A2_I2C_ADDRESS (0x60<<1)
00030     #include "MPL3115A2.h" 
00031     
00032     MPL3115A2 alt(D14, D15, MPL3115A2_I2C_ADDRESS, D4, D3);
00033 #endif
00034 
00035 FXLS8471Q acc1(D11, D12, D13, D10);
00036 MMA8652   acc2(D14, D15);
00037 FXOS8700Q_acc combo_acc(D14, D15, FXOS8700CQ_SLAVE_ADDR0);
00038 FXOS8700Q_mag combo_mag(D14, D15, FXOS8700CQ_SLAVE_ADDR0);
00039 MAG3110   mag2(D14, D15);
00040 FXAS21000 gyro(D14, D15);
00041 
00042 
00043 Serial pc(USBTX, USBRX);
00044 
00045 float print_AltimiterValue( unsigned char *dt)
00046 {
00047     unsigned short altm;
00048     float faltm;
00049 
00050     /*
00051     * dt[0] = Bits 12-19 of 20-bit real-time Altitude sample. (b7-b0)
00052     * dt[1] = Bits 4-11 of 20-bit real-time Altitude sample. (b7-b0)
00053     * dt[2] = Bits 0-3 of 20-bit real-time Altitude sample (b7-b4)
00054     */
00055     altm = (dt[0]<<8) | dt[1];
00056     //
00057     if ( dt[0] > 0x7F) {
00058         altm = ~altm + 1;
00059         faltm = (float)altm * -1.0f;
00060     } else {
00061         faltm = (float)altm * 1.0f;
00062     }
00063     //
00064     faltm = faltm+((float)(dt[2]>>4) * 0.0625f);
00065     return faltm;
00066 }
00067 
00068 float print_TemperatureValue( unsigned char *dt)
00069 {
00070     unsigned short temp;
00071     float ftemp;
00072 
00073     /*
00074     * dt[0] = Bits 4-11 of 16-bit real-time temperature sample. (b7-b0)
00075     * dt[1] = Bits 0-3 of 16-bit real-time temperature sample. (b7-b4)
00076     */
00077     temp = dt[0];
00078     //
00079     if ( dt[0] > 0x7F) {
00080         temp = ~temp + 1;
00081         ftemp = (float)temp * -1.0f;
00082     } else {
00083         ftemp = (float)temp * 1.0f;
00084     }
00085     //
00086     ftemp = ftemp+((float)(dt[1]>>4) * 0.0625f);
00087     return ftemp;
00088 
00089 }
00090 
00091 
00092 int main()
00093 {
00094     float acc_data[3], gyro_data[3];
00095     MotionSensorDataUnits adata;
00096     MotionSensorDataUnits mdata;
00097     int16_t acc_raw[3];
00098 
00099     printf("\r\nStarting\r\n\r\n");
00100 
00101     combo_acc.enable();
00102     combo_mag.enable();
00103     mag2.enable();
00104     printf("FXLS8471 Acc   = %X\r\n", acc1.getWhoAmI());
00105     printf("MMA8652 Acc    = %X\r\n", acc2.getWhoAmI());
00106     printf("FXOS8700 Combo = %X\r\n", combo_acc.whoAmI());
00107     printf("MAG3110 Mag    = %X\r\n", mag2.whoAmI());
00108     printf("FXAS21000 Gyro = %X\r\n", gyro.getWhoAmI());
00109     
00110     #if defined(FSL_COMPATIBLE)
00111         unsigned char alt_data[8];
00112         alt.Altimeter_Mode();
00113         printf("MPL3115A2 Alt  = %X\r\n", alt.getDeviceID());  // May only be used on a Freescale platform, comment out for others
00114     #endif
00115     
00116     wait(3);
00117     
00118     while(1) {
00119         acc1.ReadXYZ(acc_data);
00120         acc1.ReadXYZraw(acc_raw);
00121         printf("FXLS8471 Acc:   X:%6.3f Y:%6.3f Z:%6.3f (Raw X:%4d Y:%4d Z:%4d)\r\n", acc_data[0], acc_data[1], acc_data[2], acc_raw[0], acc_raw[1], acc_raw[2]);
00122 
00123         acc2.ReadXYZ(acc_data);
00124         acc2.ReadXYZraw(acc_raw);
00125         printf("MMA8652 Acc:    X:%6.3f Y:%6.3f Z:%6.3f (Raw X:%4d Y:%4d Z:%4d)\r\n", acc_data[0], acc_data[1], acc_data[2], acc_raw[0], acc_raw[1], acc_raw[2]);
00126 
00127         combo_acc.getAxis(adata);
00128         printf("FXOS8700 Acc:   X:%6.3f Y:%6.3f Z:%6.3f\r\n", adata.x, adata.y, adata.z);
00129         
00130         combo_mag.getAxis(mdata);
00131         printf("FXOS8700 Mag:   X:%6.2f Y:%6.2f Z:%6.2f\r\n", mdata.x, mdata.y, mdata.z);
00132 
00133         mag2.getAxis(mdata);
00134         printf("MAG3110 Mag:    X:%6.2f Y:%6.2f Z:%6.2f\r\n", mdata.x, mdata.y, mdata.z);
00135 
00136         gyro.ReadXYZ(gyro_data);
00137         printf("FXAS21000 Gyro: X:%6.2f Y:%6.2f Z:%6.2f\r\n", gyro_data[0], gyro_data[1], gyro_data[2]);
00138         
00139         #if defined(FSL_COMPATIBLE)
00140             alt.getAllDataRaw(&alt_data[0]);  // May only be used on a Freescale platform, comment out for others
00141             printf("MPL3115A2 Alt:  %5.1f\r\n", print_AltimiterValue(&alt_data[0]));  // May only be used on a Freescale platform, comment out for others
00142             printf("MPL3115A2 Temp: %3.1f\r\n", print_TemperatureValue(&alt_data[3]));  // May only be used on a Freescale platform, comment out for others
00143         #endif
00144 
00145         printf("\r\n");
00146         
00147         wait(1);
00148     }
00149 }