Example from X-NUCLEO-IKS01A3 to calibrate Magnetometer and use to read compass heading degrees without declination
Dependencies: X_NUCLEO_IKS01A3
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author SRA 00005 * @version V1.0.0 00006 * @date 5-March-2019 00007 * @brief Simple Example application for using the X_NUCLEO_IKS01A3 00008 * MEMS Inertial & Environmental Sensor Nucleo expansion board. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 /* Includes */ 00040 #include "mbed.h" 00041 #include "rtos.h" 00042 #include "XNucleoIKS01A3.h" 00043 00044 #define PI 3.14159 00045 int32_t minx,miny,minz,maxx,maxy,maxz; 00046 00047 00048 /* Instantiate the expansion board */ 00049 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15);//, D4, D5, A3, D6, A4); 00050 00051 /* Retrieve the composing elements of the expansion board */ 00052 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer; 00053 00054 00055 void calibra() 00056 { 00057 int32_t axes[3]; 00058 00059 magnetometer->get_m_axes(axes); 00060 00061 //setto tutti i valori alla prima lettura 00062 minx=axes[0]; 00063 miny=axes[1]; 00064 minz=axes[2]; 00065 maxx=minx; 00066 maxy=miny; 00067 maxz=minz; 00068 //imposto un timer per leggere per 10 secondi 00069 Timer t; 00070 t.start(); 00071 while(t.read()<10){ 00072 00073 magnetometer->get_m_axes(axes); 00074 00075 if (minx>axes[0])minx=axes[0]; 00076 if (miny>axes[1])miny=axes[1]; 00077 if (minz>axes[2])minz=axes[2]; 00078 if (maxx<axes[0])maxx=axes[0]; 00079 if (maxy<axes[1])maxy=axes[1]; 00080 if (maxz<axes[2])maxz=axes[2]; 00081 } 00082 t.stop(); 00083 printf("LIS2MDL [mag/mgauss]: minx %6d, maxx %6d, miny %6d maxy %6d\r\n", minx,maxx,miny,maxy); 00084 00085 } 00086 void normalizza(int32_t axes[]){ 00087 int32_t max,min; 00088 00089 if (minx>axes[0])max=minx; 00090 else max=axes[0]; 00091 if (max<maxx)min=max; 00092 else min=maxx; 00093 axes[0]=(min-minx)*200/(maxx-minx)-100; 00094 00095 if (miny>axes[1])max=miny; 00096 else max=axes[1]; 00097 if (max<maxy)min=max; 00098 else min=maxy; 00099 axes[1]=(min-miny)*200/(maxy-miny)-100; 00100 00101 } 00102 00103 /* Simple main function */ 00104 int main() 00105 { 00106 uint8_t id; 00107 //float value1, value2; 00108 //char buffer1[32], buffer2[32]; 00109 int32_t axes[3]; 00110 int direzione=0; 00111 00112 /* Enable all sensors */ 00113 magnetometer->enable(); 00114 magnetometer->read_id(&id); 00115 calibra(); 00116 00117 // magnetometer->get_m_axes(axes); 00118 // printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); 00119 00120 while (1) { 00121 00122 magnetometer->get_m_axes(axes); 00123 normalizza(axes); 00124 direzione = atan2(axes[1], axes[0]) * 180.0 / PI; 00125 direzione+=180; 00126 printf("gradi=%d\r\n",direzione); 00127 ThisThread::sleep_for(500); 00128 } 00129 00130 00131 }
Generated on Mon Jul 18 2022 01:26:06 by
1.7.2