hw
Dependencies: mbed LPS22HB LSM6DSL LSM303AGR HTS221
main.cpp@2:efaeb347f9c7, 2020-04-23 (annotated)
- Committer:
- b05901043
- Date:
- Thu Apr 23 08:11:36 2020 +0000
- Revision:
- 2:efaeb347f9c7
- Parent:
- 0:9c859fc3ee1a
enable sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
b05901043 | 0:9c859fc3ee1a | 1 | |
b05901043 | 0:9c859fc3ee1a | 2 | #include "mbed.h" |
b05901043 | 0:9c859fc3ee1a | 3 | #include "XNucleoIKS01A2.h" |
b05901043 | 0:9c859fc3ee1a | 4 | |
b05901043 | 0:9c859fc3ee1a | 5 | /* Instantiate the expansion board */ |
b05901043 | 2:efaeb347f9c7 | 6 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
b05901043 | 0:9c859fc3ee1a | 7 | |
b05901043 | 2:efaeb347f9c7 | 8 | /* Retrieve the composing elements of the expansion board */ |
b05901043 | 2:efaeb347f9c7 | 9 | static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; |
b05901043 | 2:efaeb347f9c7 | 10 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
b05901043 | 2:efaeb347f9c7 | 11 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
b05901043 | 2:efaeb347f9c7 | 12 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
b05901043 | 2:efaeb347f9c7 | 13 | static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; |
b05901043 | 0:9c859fc3ee1a | 14 | |
b05901043 | 0:9c859fc3ee1a | 15 | // Create a DigitalOut objects for the LED |
b05901043 | 0:9c859fc3ee1a | 16 | DigitalOut led(LED1); |
b05901043 | 0:9c859fc3ee1a | 17 | |
b05901043 | 0:9c859fc3ee1a | 18 | // Create a Serial objects to communicate via USB |
b05901043 | 0:9c859fc3ee1a | 19 | Serial pc(USBTX, USBRX); |
b05901043 | 0:9c859fc3ee1a | 20 | |
b05901043 | 0:9c859fc3ee1a | 21 | // Create 2 Ticker objects for a recurring interrupts. One for blink a LED and the other one to update the sensor lectures periodicly. |
b05901043 | 0:9c859fc3ee1a | 22 | Ticker blinky; |
b05901043 | 0:9c859fc3ee1a | 23 | Ticker update; |
b05901043 | 0:9c859fc3ee1a | 24 | |
b05901043 | 0:9c859fc3ee1a | 25 | volatile float TEMPERATURE_C; |
b05901043 | 0:9c859fc3ee1a | 26 | volatile float TEMPERATURE_F; |
b05901043 | 0:9c859fc3ee1a | 27 | volatile float TEMPERATURE_K; |
b05901043 | 0:9c859fc3ee1a | 28 | volatile float HUMIDITY; |
b05901043 | 0:9c859fc3ee1a | 29 | volatile float PRESSURE; |
b05901043 | 0:9c859fc3ee1a | 30 | |
b05901043 | 0:9c859fc3ee1a | 31 | bool measurements_update = false; |
b05901043 | 0:9c859fc3ee1a | 32 | |
b05901043 | 0:9c859fc3ee1a | 33 | void blinky_handler(){ |
b05901043 | 0:9c859fc3ee1a | 34 | led = !led; |
b05901043 | 0:9c859fc3ee1a | 35 | } |
b05901043 | 0:9c859fc3ee1a | 36 | |
b05901043 | 0:9c859fc3ee1a | 37 | void sensors_handler(){ |
b05901043 | 0:9c859fc3ee1a | 38 | measurements_update = true; |
b05901043 | 0:9c859fc3ee1a | 39 | } |
b05901043 | 0:9c859fc3ee1a | 40 | |
b05901043 | 0:9c859fc3ee1a | 41 | int main() { |
b05901043 | 0:9c859fc3ee1a | 42 | |
b05901043 | 0:9c859fc3ee1a | 43 | /* Create a sensor shield object */ |
b05901043 | 0:9c859fc3ee1a | 44 | // static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
b05901043 | 2:efaeb347f9c7 | 45 | |
b05901043 | 2:efaeb347f9c7 | 46 | /* Enable all sensors */ |
b05901043 | 2:efaeb347f9c7 | 47 | hum_temp->enable(); |
b05901043 | 2:efaeb347f9c7 | 48 | press_temp->enable(); |
b05901043 | 2:efaeb347f9c7 | 49 | magnetometer->enable(); |
b05901043 | 2:efaeb347f9c7 | 50 | accelerometer->enable(); |
b05901043 | 2:efaeb347f9c7 | 51 | acc_gyro->enable_x(); |
b05901043 | 2:efaeb347f9c7 | 52 | acc_gyro->enable_g(); |
b05901043 | 0:9c859fc3ee1a | 53 | |
b05901043 | 0:9c859fc3ee1a | 54 | /* Attach a function to be called by the Ticker objects at a specific interval in seconds*/ |
b05901043 | 0:9c859fc3ee1a | 55 | blinky.attach(&blinky_handler, 0.5); |
b05901043 | 0:9c859fc3ee1a | 56 | update.attach(&sensors_handler, 3); |
b05901043 | 0:9c859fc3ee1a | 57 | |
b05901043 | 0:9c859fc3ee1a | 58 | while(1) { |
b05901043 | 0:9c859fc3ee1a | 59 | if(measurements_update == true){ |
b05901043 | 0:9c859fc3ee1a | 60 | /* Read the environmental sensors */ |
b05901043 | 2:efaeb347f9c7 | 61 | hum_temp->get_temperature((float *)&TEMPERATURE_C); |
b05901043 | 2:efaeb347f9c7 | 62 | hum_temp->get_humidity((float *)&HUMIDITY); |
b05901043 | 2:efaeb347f9c7 | 63 | press_temp->get_pressure((float *)&PRESSURE); |
b05901043 | 0:9c859fc3ee1a | 64 | |
b05901043 | 0:9c859fc3ee1a | 65 | TEMPERATURE_F = (TEMPERATURE_C * 1.8f) + 32.0f; //Convert the temperature from Celsius to Fahrenheit |
b05901043 | 0:9c859fc3ee1a | 66 | TEMPERATURE_K = (TEMPERATURE_C + 273.15f); //Convert the temperature from Celsius to Kelvin |
b05901043 | 0:9c859fc3ee1a | 67 | pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K); |
b05901043 | 0:9c859fc3ee1a | 68 | pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY); |
b05901043 | 0:9c859fc3ee1a | 69 | pc.printf("Pressure:\t %.2f hPa\r\n", PRESSURE); |
b05901043 | 0:9c859fc3ee1a | 70 | pc.printf("\r\n"); |
b05901043 | 0:9c859fc3ee1a | 71 | |
b05901043 | 0:9c859fc3ee1a | 72 | measurements_update = false; |
b05901043 | 0:9c859fc3ee1a | 73 | } |
b05901043 | 0:9c859fc3ee1a | 74 | __wfi(); |
b05901043 | 0:9c859fc3ee1a | 75 | } |
b05901043 | 0:9c859fc3ee1a | 76 | } |