Magnetometer, accelerometer, and altimeter example for NXP Rapid IoT prototyping kit. Read more at https://www.hackster.io/marcomerli/riotwear-mbed-2b2011 .

Dependencies:   FXOS8700 FXAS21002 MPL3115A2

Committer:
batman52
Date:
Wed Nov 20 23:27:04 2019 +0000
Revision:
79:37b253492247
Parent:
78:ddf5cff958fa
example of how to read accelerometer, magnetometer, and altimeter for NXP rapid IoT prototyping kit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
batman52 79:37b253492247 1 /* Copyright (c) 2015 NXP Semiconductors. MIT License
batman52 79:37b253492247 2 *
batman52 79:37b253492247 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
batman52 79:37b253492247 4 * and associated documentation files (the "Software"), to deal in the Software without
batman52 79:37b253492247 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
batman52 79:37b253492247 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
batman52 79:37b253492247 7 * Software is furnished to do so, subject to the following conditions:
batman52 79:37b253492247 8 *
batman52 79:37b253492247 9 * The above copyright notice and this permission notice shall be included in all copies or
batman52 79:37b253492247 10 * substantial portions of the Software.
batman52 79:37b253492247 11 *
batman52 79:37b253492247 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
batman52 79:37b253492247 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
batman52 79:37b253492247 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
batman52 79:37b253492247 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
batman52 79:37b253492247 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
batman52 79:37b253492247 17 */
batman52 79:37b253492247 18 #include "FXAS21002.h"
batman52 79:37b253492247 19 #include "FXOS8700.h"
batman52 79:37b253492247 20 #include "MPL3115.h"
Jonathan Austin 0:2757d7abb7d9 21 #include "mbed.h"
batman52 79:37b253492247 22
batman52 79:37b253492247 23 // Initialize Serial port
batman52 79:37b253492247 24 //Serial pc(USBTX, USBRX);
Jonathan Austin 0:2757d7abb7d9 25
batman52 79:37b253492247 26 // Initialize pins for I2C communication for sensors. Set jumpers J6,J7 in FRDM-STBC-AGM01 board accordingly.
batman52 79:37b253492247 27 // I2C_SCL = PTC10, I2C_SDA = PTC11,
batman52 79:37b253492247 28 FXOS8700 accel(I2C_SDA , I2C_SCL );
batman52 79:37b253492247 29 FXOS8700 mag(I2C_SDA , I2C_SCL );
batman52 79:37b253492247 30 FXAS21002 gyro(I2C_SDA , I2C_SCL );
batman52 79:37b253492247 31 MPL3115 mpl3115(I2C_SDA , I2C_SCL );
Jonathan Austin 0:2757d7abb7d9 32
batman52 79:37b253492247 33 #define SHORT_WAIT 0.1
batman52 79:37b253492247 34 #define LONG_WAIT 1.0
batman52 79:37b253492247 35
batman52 79:37b253492247 36 int main()
batman52 79:37b253492247 37 {
batman52 79:37b253492247 38
batman52 79:37b253492247 39 // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 & Gyroscope FXAS21002
batman52 79:37b253492247 40 accel.accel_config();
batman52 79:37b253492247 41 mag.mag_config();
batman52 79:37b253492247 42 gyro.gyro_config();
batman52 79:37b253492247 43 mpl3115.MPL3115_config();
batman52 79:37b253492247 44
batman52 79:37b253492247 45 //pc.baud(115200);
batman52 79:37b253492247 46
batman52 79:37b253492247 47 float accel_data[3];
batman52 79:37b253492247 48 float accel_rms=0.0;
batman52 79:37b253492247 49 float mag_data[3];
batman52 79:37b253492247 50 float mag_rms=0.0;
batman52 79:37b253492247 51 float gyro_data[3];
batman52 79:37b253492247 52 float gyro_rms=0.0;
batman52 79:37b253492247 53 float alt_data[3];
batman52 79:37b253492247 54 float alt_rms=0.0;
batman52 79:37b253492247 55
batman52 79:37b253492247 56 printf("\n\rBegin Data Acquisition....\n\r");
batman52 79:37b253492247 57 wait(5.0);
batman52 79:37b253492247 58
batman52 79:37b253492247 59 while(1)
batman52 79:37b253492247 60 {
batman52 79:37b253492247 61 accel.acquire_accel_data_g(accel_data);
batman52 79:37b253492247 62 wait(SHORT_WAIT);
batman52 79:37b253492247 63 printf("Accelleration: X:%4.2f, Y:%4.2f, Z:%4.2f\n\r",accel_data[0],accel_data[1],accel_data[2]);
batman52 79:37b253492247 64 accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
batman52 79:37b253492247 65 printf("\n\rAccelleration RMS: %4.2f\n\r",accel_rms);
batman52 79:37b253492247 66
batman52 79:37b253492247 67 mag.acquire_mag_data_uT(mag_data);
batman52 79:37b253492247 68 printf("Magnetics readings: X:%4.2f, Y:%4.2f, Z:%4.2f\n\r",mag_data[0],mag_data[1],mag_data[2]);
batman52 79:37b253492247 69 mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3);
batman52 79:37b253492247 70 printf("Magnetic RMS: %4.2f\n\r",mag_rms);
batman52 79:37b253492247 71 wait(SHORT_WAIT);
batman52 79:37b253492247 72
batman52 79:37b253492247 73 gyro.acquire_gyro_data_dps(gyro_data);
batman52 79:37b253492247 74 printf("Gyroscope readings: X:%4.2f, Y:%4.2f, Z:%4.2f\n\r",gyro_data[0],gyro_data[1],gyro_data[2]);
batman52 79:37b253492247 75 gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3);
batman52 79:37b253492247 76 printf("Gyroscopic RMS: %4.2f\n\r",gyro_rms);
batman52 79:37b253492247 77 wait(SHORT_WAIT);
batman52 79:37b253492247 78
batman52 79:37b253492247 79 mpl3115.acquire_MPL3115_data_Altitude_in_m(alt_data);
batman52 79:37b253492247 80 printf("Altitude: \t%f\n\r",alt_data[0]);
batman52 79:37b253492247 81 alt_rms = sqrt(((alt_data[0]*alt_data[0])+(alt_data[1]*alt_data[1])+(alt_data[2]*alt_data[2]))/3);
batman52 79:37b253492247 82 printf("Altitude RMS: %4.2f\n\r",alt_rms);
batman52 79:37b253492247 83 wait(SHORT_WAIT);
batman52 79:37b253492247 84
batman52 79:37b253492247 85 wait(LONG_WAIT);
batman52 79:37b253492247 86
batman52 79:37b253492247 87 }
batman52 79:37b253492247 88
Jonathan Austin 0:2757d7abb7d9 89 }