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

Dependencies:   X_NUCLEO_IKS01A3

Committer:
teachloris
Date:
Tue Jun 14 20:20:06 2022 +0000
Revision:
9:3486ba668395
Parent:
6:94151942f287
Example calibration and read magnetometer

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
teachloris 9:3486ba668395 44 #define PI 3.14159
teachloris 9:3486ba668395 45 int32_t minx,miny,minz,maxx,maxy,maxz;
teachloris 9:3486ba668395 46
teachloris 9:3486ba668395 47
cparata 0:535249dc4bf5 48 /* Instantiate the expansion board */
teachloris 9:3486ba668395 49 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15);//, D4, D5, A3, D6, A4);
cparata 0:535249dc4bf5 50
cparata 0:535249dc4bf5 51 /* Retrieve the composing elements of the expansion board */
cparata 0:535249dc4bf5 52 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
teachloris 9:3486ba668395 53
cparata 0:535249dc4bf5 54
teachloris 9:3486ba668395 55 void calibra()
cparata 0:535249dc4bf5 56 {
teachloris 9:3486ba668395 57 int32_t axes[3];
cparata 0:535249dc4bf5 58
teachloris 9:3486ba668395 59 magnetometer->get_m_axes(axes);
teachloris 9:3486ba668395 60
teachloris 9:3486ba668395 61 //setto tutti i valori alla prima lettura
teachloris 9:3486ba668395 62 minx=axes[0];
teachloris 9:3486ba668395 63 miny=axes[1];
teachloris 9:3486ba668395 64 minz=axes[2];
teachloris 9:3486ba668395 65 maxx=minx;
teachloris 9:3486ba668395 66 maxy=miny;
teachloris 9:3486ba668395 67 maxz=minz;
teachloris 9:3486ba668395 68 //imposto un timer per leggere per 10 secondi
teachloris 9:3486ba668395 69 Timer t;
teachloris 9:3486ba668395 70 t.start();
teachloris 9:3486ba668395 71 while(t.read()<10){
teachloris 9:3486ba668395 72
teachloris 9:3486ba668395 73 magnetometer->get_m_axes(axes);
teachloris 9:3486ba668395 74
teachloris 9:3486ba668395 75 if (minx>axes[0])minx=axes[0];
teachloris 9:3486ba668395 76 if (miny>axes[1])miny=axes[1];
teachloris 9:3486ba668395 77 if (minz>axes[2])minz=axes[2];
teachloris 9:3486ba668395 78 if (maxx<axes[0])maxx=axes[0];
teachloris 9:3486ba668395 79 if (maxy<axes[1])maxy=axes[1];
teachloris 9:3486ba668395 80 if (maxz<axes[2])maxz=axes[2];
cparata 0:535249dc4bf5 81 }
teachloris 9:3486ba668395 82 t.stop();
teachloris 9:3486ba668395 83 printf("LIS2MDL [mag/mgauss]: minx %6d, maxx %6d, miny %6d maxy %6d\r\n", minx,maxx,miny,maxy);
teachloris 9:3486ba668395 84
cparata 0:535249dc4bf5 85 }
teachloris 9:3486ba668395 86 void normalizza(int32_t axes[]){
teachloris 9:3486ba668395 87 int32_t max,min;
teachloris 9:3486ba668395 88
teachloris 9:3486ba668395 89 if (minx>axes[0])max=minx;
teachloris 9:3486ba668395 90 else max=axes[0];
teachloris 9:3486ba668395 91 if (max<maxx)min=max;
teachloris 9:3486ba668395 92 else min=maxx;
teachloris 9:3486ba668395 93 axes[0]=(min-minx)*200/(maxx-minx)-100;
teachloris 9:3486ba668395 94
teachloris 9:3486ba668395 95 if (miny>axes[1])max=miny;
teachloris 9:3486ba668395 96 else max=axes[1];
teachloris 9:3486ba668395 97 if (max<maxy)min=max;
teachloris 9:3486ba668395 98 else min=maxy;
teachloris 9:3486ba668395 99 axes[1]=(min-miny)*200/(maxy-miny)-100;
teachloris 9:3486ba668395 100
teachloris 9:3486ba668395 101 }
teachloris 9:3486ba668395 102
cparata 0:535249dc4bf5 103 /* Simple main function */
cparata 5:7c883cce2bc4 104 int main()
cparata 5:7c883cce2bc4 105 {
cparata 5:7c883cce2bc4 106 uint8_t id;
teachloris 9:3486ba668395 107 //float value1, value2;
teachloris 9:3486ba668395 108 //char buffer1[32], buffer2[32];
cparata 5:7c883cce2bc4 109 int32_t axes[3];
teachloris 9:3486ba668395 110 int direzione=0;
teachloris 9:3486ba668395 111
cparata 5:7c883cce2bc4 112 /* Enable all sensors */
cparata 5:7c883cce2bc4 113 magnetometer->enable();
cparata 5:7c883cce2bc4 114 magnetometer->read_id(&id);
teachloris 9:3486ba668395 115 calibra();
teachloris 9:3486ba668395 116
teachloris 9:3486ba668395 117 // magnetometer->get_m_axes(axes);
teachloris 9:3486ba668395 118 // printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
teachloris 9:3486ba668395 119
cparata 5:7c883cce2bc4 120 while (1) {
teachloris 9:3486ba668395 121
cparata 5:7c883cce2bc4 122 magnetometer->get_m_axes(axes);
teachloris 9:3486ba668395 123 normalizza(axes);
teachloris 9:3486ba668395 124 direzione = atan2(axes[1], axes[0]) * 180.0 / PI;
teachloris 9:3486ba668395 125 direzione+=180;
teachloris 9:3486ba668395 126 printf("gradi=%d\r\n",direzione);
teachloris 9:3486ba668395 127 ThisThread::sleep_for(500);
cparata 5:7c883cce2bc4 128 }
teachloris 9:3486ba668395 129
teachloris 9:3486ba668395 130
cparata 0:535249dc4bf5 131 }