Example from X-NUCLEO-IKS01A3 to calibrate Magnetometer and use to read compass heading degrees without declination

Dependencies:   X_NUCLEO_IKS01A3

Committer:
cparata
Date:
Thu Jun 04 16:25:19 2020 +0000
Revision:
6:94151942f287
Parent:
5:7c883cce2bc4
Child:
9:3486ba668395
Update code to be compatible with mbed OS 6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:535249dc4bf5 1 /**
cparata 0:535249dc4bf5 2 ******************************************************************************
cparata 0:535249dc4bf5 3 * @file main.cpp
cparata 0:535249dc4bf5 4 * @author SRA
cparata 0:535249dc4bf5 5 * @version V1.0.0
cparata 0:535249dc4bf5 6 * @date 5-March-2019
cparata 5:7c883cce2bc4 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A3
cparata 0:535249dc4bf5 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:535249dc4bf5 9 ******************************************************************************
cparata 0:535249dc4bf5 10 * @attention
cparata 0:535249dc4bf5 11 *
cparata 0:535249dc4bf5 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:535249dc4bf5 13 *
cparata 0:535249dc4bf5 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:535249dc4bf5 15 * are permitted provided that the following conditions are met:
cparata 0:535249dc4bf5 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:535249dc4bf5 17 * this list of conditions and the following disclaimer.
cparata 0:535249dc4bf5 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:535249dc4bf5 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:535249dc4bf5 20 * and/or other materials provided with the distribution.
cparata 0:535249dc4bf5 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:535249dc4bf5 22 * may be used to endorse or promote products derived from this software
cparata 0:535249dc4bf5 23 * without specific prior written permission.
cparata 0:535249dc4bf5 24 *
cparata 0:535249dc4bf5 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:535249dc4bf5 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:535249dc4bf5 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:535249dc4bf5 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:535249dc4bf5 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:535249dc4bf5 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:535249dc4bf5 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:535249dc4bf5 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:535249dc4bf5 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:535249dc4bf5 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:535249dc4bf5 35 *
cparata 0:535249dc4bf5 36 ******************************************************************************
cparata 5:7c883cce2bc4 37 */
cparata 0:535249dc4bf5 38
cparata 0:535249dc4bf5 39 /* Includes */
cparata 0:535249dc4bf5 40 #include "mbed.h"
cparata 6:94151942f287 41 #include "rtos.h"
cparata 0:535249dc4bf5 42 #include "XNucleoIKS01A3.h"
cparata 0:535249dc4bf5 43
cparata 0:535249dc4bf5 44 /* Instantiate the expansion board */
cparata 0:535249dc4bf5 45 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 0:535249dc4bf5 46
cparata 0:535249dc4bf5 47 /* Retrieve the composing elements of the expansion board */
cparata 0:535249dc4bf5 48 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
cparata 0:535249dc4bf5 49 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
cparata 0:535249dc4bf5 50 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
cparata 0:535249dc4bf5 51 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 0:535249dc4bf5 52 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
cparata 0:535249dc4bf5 53 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
cparata 0:535249dc4bf5 54
cparata 0:535249dc4bf5 55 /* Helper function for printing floats & doubles */
cparata 5:7c883cce2bc4 56 static char *print_double(char *str, double v, int decimalDigits = 2)
cparata 0:535249dc4bf5 57 {
cparata 5:7c883cce2bc4 58 int i = 1;
cparata 5:7c883cce2bc4 59 int intPart, fractPart;
cparata 5:7c883cce2bc4 60 int len;
cparata 5:7c883cce2bc4 61 char *ptr;
cparata 0:535249dc4bf5 62
cparata 5:7c883cce2bc4 63 /* prepare decimal digits multiplicator */
cparata 5:7c883cce2bc4 64 for (; decimalDigits != 0; i *= 10, decimalDigits--);
cparata 0:535249dc4bf5 65
cparata 5:7c883cce2bc4 66 /* calculate integer & fractinal parts */
cparata 5:7c883cce2bc4 67 intPart = (int)v;
cparata 5:7c883cce2bc4 68 fractPart = (int)((v - (double)(int)v) * i);
cparata 0:535249dc4bf5 69
cparata 5:7c883cce2bc4 70 /* fill in integer part */
cparata 5:7c883cce2bc4 71 sprintf(str, "%i.", intPart);
cparata 0:535249dc4bf5 72
cparata 5:7c883cce2bc4 73 /* prepare fill in of fractional part */
cparata 5:7c883cce2bc4 74 len = strlen(str);
cparata 5:7c883cce2bc4 75 ptr = &str[len];
cparata 0:535249dc4bf5 76
cparata 5:7c883cce2bc4 77 /* fill in leading fractional zeros */
cparata 5:7c883cce2bc4 78 for (i /= 10; i > 1; i /= 10, ptr++) {
cparata 5:7c883cce2bc4 79 if (fractPart >= i) {
cparata 5:7c883cce2bc4 80 break;
cparata 5:7c883cce2bc4 81 }
cparata 5:7c883cce2bc4 82 *ptr = '0';
cparata 0:535249dc4bf5 83 }
cparata 0:535249dc4bf5 84
cparata 5:7c883cce2bc4 85 /* fill in (rest of) fractional part */
cparata 5:7c883cce2bc4 86 sprintf(ptr, "%i", fractPart);
cparata 0:535249dc4bf5 87
cparata 5:7c883cce2bc4 88 return str;
cparata 0:535249dc4bf5 89 }
cparata 0:535249dc4bf5 90
cparata 0:535249dc4bf5 91 /* Simple main function */
cparata 5:7c883cce2bc4 92 int main()
cparata 5:7c883cce2bc4 93 {
cparata 5:7c883cce2bc4 94 uint8_t id;
cparata 5:7c883cce2bc4 95 float value1, value2;
cparata 5:7c883cce2bc4 96 char buffer1[32], buffer2[32];
cparata 5:7c883cce2bc4 97 int32_t axes[3];
cparata 5:7c883cce2bc4 98
cparata 5:7c883cce2bc4 99 /* Enable all sensors */
cparata 5:7c883cce2bc4 100 hum_temp->enable();
cparata 5:7c883cce2bc4 101 press_temp->enable();
cparata 5:7c883cce2bc4 102 temp->enable();
cparata 5:7c883cce2bc4 103 magnetometer->enable();
cparata 5:7c883cce2bc4 104 accelerometer->enable_x();
cparata 5:7c883cce2bc4 105 acc_gyro->enable_x();
cparata 5:7c883cce2bc4 106 acc_gyro->enable_g();
cparata 5:7c883cce2bc4 107
cparata 5:7c883cce2bc4 108 printf("\r\n--- Starting new run ---\r\n");
cparata 5:7c883cce2bc4 109
cparata 5:7c883cce2bc4 110 hum_temp->read_id(&id);
cparata 5:7c883cce2bc4 111 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 112 press_temp->read_id(&id);
cparata 5:7c883cce2bc4 113 printf("LPS22HH pressure & temperature = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 114 temp->read_id(&id);
cparata 5:7c883cce2bc4 115 printf("STTS751 temperature = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 116 magnetometer->read_id(&id);
cparata 5:7c883cce2bc4 117 printf("LIS2MDL magnetometer = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 118 accelerometer->read_id(&id);
cparata 5:7c883cce2bc4 119 printf("LIS2DW12 accelerometer = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 120 acc_gyro->read_id(&id);
cparata 5:7c883cce2bc4 121 printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
cparata 0:535249dc4bf5 122
cparata 5:7c883cce2bc4 123 while (1) {
cparata 5:7c883cce2bc4 124 printf("\r\n");
cparata 5:7c883cce2bc4 125
cparata 5:7c883cce2bc4 126 hum_temp->get_temperature(&value1);
cparata 5:7c883cce2bc4 127 hum_temp->get_humidity(&value2);
cparata 5:7c883cce2bc4 128 printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
cparata 5:7c883cce2bc4 129
cparata 5:7c883cce2bc4 130 press_temp->get_temperature(&value1);
cparata 5:7c883cce2bc4 131 press_temp->get_pressure(&value2);
cparata 5:7c883cce2bc4 132 printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
cparata 5:7c883cce2bc4 133
cparata 5:7c883cce2bc4 134 temp->get_temperature(&value1);
cparata 5:7c883cce2bc4 135 printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1));
cparata 5:7c883cce2bc4 136
cparata 5:7c883cce2bc4 137 printf("---\r\n");
cparata 0:535249dc4bf5 138
cparata 5:7c883cce2bc4 139 magnetometer->get_m_axes(axes);
cparata 5:7c883cce2bc4 140 printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 141
cparata 5:7c883cce2bc4 142 accelerometer->get_x_axes(axes);
cparata 5:7c883cce2bc4 143 printf("LIS2DW12 [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 144
cparata 5:7c883cce2bc4 145 acc_gyro->get_x_axes(axes);
cparata 5:7c883cce2bc4 146 printf("LSM6DSO [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 147
cparata 5:7c883cce2bc4 148 acc_gyro->get_g_axes(axes);
cparata 5:7c883cce2bc4 149 printf("LSM6DSO [gyro/mdps]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 150
cparata 6:94151942f287 151 ThisThread::sleep_for(1500);
cparata 5:7c883cce2bc4 152 }
cparata 0:535249dc4bf5 153 }