Arduino board run on Mbed-os6.8.1 (only test purpose not official). Need Mbed Studio(1.3.1) not online compiler. If you compile on the online compiler, you can get a hex file but it does NOT work!!!

Dependencies:   APDS_9960 LPS22HB LSM9DS1 HTS221

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main2.cpp Source File

main2.cpp

00001 /*
00002  * Mbed Application program
00003  *  Nano 33 BLE Sense board runs on mbed-OS6
00004  *  LSM9DS1 -> iNEMO inertial module:
00005  *    3D accelerometer, 3D gyroscope, 3D magnetometer
00006  *      by STMicroelectronics
00007  *
00008  * Copyright (c) 2020,'21 Kenji Arai / JH1PJL
00009  *      http://www7b.biglobe.ne.jp/~kenjia/
00010  *      https://os.mbed.com/users/kenjiArai/
00011  *      Started:    January   22nd, 2020
00012  *      Revised:    February  28th, 2021
00013  */
00014 
00015 //  Pre-selection --------------------------------------------------------------
00016 #include    "select_example.h"
00017 //#define EXAMPLE_2_CHECK_LSM9DS1
00018 #ifdef EXAMPLE_2_CHECK_LSM9DS1
00019 
00020 //  Include --------------------------------------------------------------------
00021 #include    "mbed.h"
00022 #include    "nano33blesense_iodef.h"
00023 #include    "LSM9DS1.h"
00024 
00025 //  Definition -----------------------------------------------------------------
00026 
00027 //  Constructor ----------------------------------------------------------------
00028 DigitalOut      sen_pwr(PIN_VDD_ENV, 1);
00029 DigitalOut      i2c_pullup(PIN_I2C_PULLUP, 1);
00030 I2C             i2c(PIN_SDA1, PIN_SCL1);
00031 LSM9DS1         *imu = NULL;
00032 Timer           t;
00033 
00034 //  RAM ------------------------------------------------------------------------
00035 
00036 //  ROM / Constant data --------------------------------------------------------
00037 
00038 //  Function prototypes --------------------------------------------------------
00039 
00040 //  subroutin (must be at this below line and do NOT change order) -------------
00041 #include    "usb_serial_as_stdio.h"
00042 #include    "check_revision.h"
00043 #include    "common.h"
00044 
00045 //------------------------------------------------------------------------------
00046 //  Control Program
00047 //------------------------------------------------------------------------------
00048 int main()
00049 {
00050     usb_serial_initialize();
00051     print_revision();
00052     i2c_pullup = 1;
00053     sen_pwr = 1;
00054     print_usb("Check LSM9DS1\r\n");
00055     ThisThread::sleep_for(200ms);
00056     imu = new LSM9DS1(PIN_SDA1, PIN_SCL1);
00057     uint16_t reg = imu->begin();
00058     print_usb("IMU(LSM9DS1) 0x%x\r\n", reg);
00059     imu->calibration();
00060     //  check I2C line
00061     check_i2c_connected_devices();
00062     if (reg == 0x683d){
00063         print_usb("ACC+GYR+MAG are ready!\r\n");
00064     } else {
00065         print_usb("ACC+GYR+MAG are NOT ready!\r\n");
00066     }
00067     uint32_t n = 0;
00068     while(true) {
00069         t.reset();
00070         t.start();
00071         imu->readAccel();
00072         imu->readGyro();
00073         imu->readMag();
00074         imu->readTemp();
00075         print_usb("acc=,%+5.3f,%+5.3f,%+5.3f,", imu->ax, imu->ay, imu->az);
00076         print_usb("gyr=,%+8.3f,%+8.3f,%+8.3f,", imu->gx, imu->gy, imu->gz);
00077         print_usb("mag=,%+5.3f,%+5.3f,%+5.3f,", imu->mx, imu->my, imu->mz);
00078         print_usb("temperature=,%+5.1f,", imu->temperature_c);
00079         ++n;
00080         uint32_t passed_time = chrono::duration_cast<chrono::milliseconds>(
00081                                t.elapsed_time()).count();
00082         print_usb("processing time:,%2d,[ms],count:,%4d\r\n", passed_time, n);
00083         if (passed_time < 199){
00084             ThisThread::sleep_for(chrono::milliseconds(200 - passed_time));
00085         }
00086     }
00087 }
00088 
00089 #endif // EXAMPLE_2_CHECK_LSM9DS1