Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Orientation_tracker X_NUCLEO_IKS01A2
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 2-December-2016 00007 * @brief Simple Example application for using the X_NUCLEO_IKS01A1 00008 * MEMS Inertial & Environmental Sensor Nucleo expansion board. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 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 "XNucleoIKS01A2.h" 00042 #include "MadgwickAHRS.h" 00043 #define sampleFreqDef 10.0f 00044 00045 Serial pc(USBTX, USBRX, 115200); 00046 uint8_t id; 00047 float value1, value2; 00048 char buffer1[32], buffer2[32]; 00049 char* nmea; 00050 int32_t axes[3]; 00051 int32_t mag[3]; 00052 int32_t gyr[3]; 00053 int32_t ac[3]; 00054 float gyrf[3]; 00055 float pitch, roll, yaw; 00056 uint32_t counter = 0; 00057 int32_t mag_max[3]; 00058 int32_t mag_min[3]; // zmienne do przechwoywania "offsetu" magnetometru 00059 bool initialized = false; 00060 00061 //enable a ticker that allows to setup a recurring interrupt to repeatedly call a function at a specified rate. 00062 Ticker flipper; 00063 00064 Madgwick madgwick_filter; 00065 00066 /* Instantiate the expansion board */ 00067 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); 00068 00069 /* Retrieve the composing elements of the expansion board */ 00070 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; 00071 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; 00072 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; 00073 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; 00074 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; 00075 00076 /* Helper function for printing floats & doubles */ 00077 static char *print_double(char* str, double v, int decimalDigits=2) 00078 { 00079 int i = 1; 00080 int intPart, fractPart; 00081 int len; 00082 char *ptr; 00083 00084 /* prepare decimal digits multiplicator */ 00085 for (;decimalDigits!=0; i*=10, decimalDigits--); 00086 00087 /* calculate integer & fractinal parts */ 00088 intPart = (int)v; 00089 fractPart = (int)((v-(double)(int)v)*i); 00090 00091 /* fill in integer part */ 00092 sprintf(str, "%i.", intPart); 00093 00094 /* prepare fill in of fractional part */ 00095 len = strlen(str); 00096 ptr = &str[len]; 00097 00098 /* fill in leading fractional zeros */ 00099 for (i/=10;i>1; i/=10, ptr++) { 00100 if (fractPart >= i) { 00101 break; 00102 } 00103 *ptr = '0'; 00104 } 00105 00106 /* fill in (rest of) fractional part */ 00107 sprintf(ptr, "%i", fractPart); 00108 00109 return str; 00110 } 00111 00112 00113 // NMEA conversion 00114 void float_to_char(float my_float, char* tab){ 00115 tab[0] = (char)( (int)(my_float/100.0) + 48); 00116 tab[1] = (char)( (int)((my_float - (tab[0]- '0')*100)/10.0) + 48); 00117 tab[2] = (char)( (int)((my_float - (tab[0]- '0')*100 - (tab[1]- '0')*10 )) + 48); 00118 tab[4] = (char)( (int)((my_float - (tab[0]- '0')*100 - (tab[1]- '0')*10 - (tab[2]- '0'))*10.0) + 48); 00119 tab[5] = (char)( (int)((my_float - (tab[0]- '0')*100 - (tab[1]- '0')*10 - (tab[2]- '0') - (tab[4]- '0')/10.0)*100.0) + 48); 00120 } 00121 00122 char * get_nmea_sentence(float roll, float pitch, float yaw){ 00123 static char sentence[] = "$PASHR,hhmmss.sss,hhh.hh,T,rrr.rr,ppp.pp,xxx.xx,a.aaa,b.bbb,c.ccc,d,e*hh\n\r\n"; 00124 //float roll_deg = roll * 360/(2*3.14); 00125 // float pitch_deg = pitch * 360/(2*3.14); 00126 // float yaw_deg = yaw * 360/(2*3.14); 00127 00128 float_to_char(roll, sentence + 27); 00129 float_to_char(pitch, sentence + 34); 00130 float_to_char(yaw, sentence + 18); 00131 00132 return sentence; 00133 } 00134 00135 00136 void update_filter(){ 00137 magnetometer->get_m_axes(axes); 00138 mag[0] = axes[0] + 15; 00139 mag[1] = axes[1] + 180; 00140 mag[2] = axes[2] + 51; 00141 00142 /* 00143 if(initialized == false){ 00144 mag_max[0] = mag[0]; 00145 mag_max[1] = mag[1]; 00146 mag_max[2] = mag[2]; 00147 mag_min[0] = mag[0]; 00148 mag_min[1] = mag[1]; 00149 mag_min[2] = mag[2]; 00150 initialized = true; 00151 } 00152 00153 00154 for(int i=0; i<3; i++){ 00155 if(mag[i] > mag_max[i]){ 00156 mag_max[i] = mag[i]; 00157 } 00158 if(mag[i] < mag_min[i]){ 00159 mag_min[i] = mag[i]; 00160 } 00161 } 00162 */ 00163 00164 00165 //pc.printf("LSM303AGR maximum axes values [mag/mgauss]: %6ld, %6ld, %6ld\r\n", mag_max[0], mag_max[1], mag_max[2]); 00166 //pc.printf("LSM303AGR minimum axes values [mag/mgauss]: %6ld, %6ld, %6ld\r\n", mag_min[0], mag_min[1], mag_min[2]); 00167 00168 //pc.printf("mag1 is at 0x%08X\r\n", &mag[0]); it's how to get an adress for use in stm studio 00169 00170 accelerometer->get_x_axes(axes); 00171 ac[0] = axes[0]; 00172 ac[1] = axes[1]; 00173 ac[2] = axes[2]; 00174 // printf("LSM303AGR [acc/g]: %6ld, %6ld, %6ld\r\n", ac[0], ac[1], ac[2]); 00175 00176 //acc_gyro->get_x_axes(axes); 00177 //printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", ac[0], ac[1], ac[2]); 00178 00179 acc_gyro->get_g_axes(axes); 00180 gyr[0] = axes[0]; 00181 gyr[1] = axes[1]; 00182 gyr[2] = axes[2]; 00183 // printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", gyr[0], gyr[1], gyr[2]); 00184 00185 gyrf[0] = 1.0*gyr[0]; 00186 gyrf[1] = 1.0*gyr[1]; 00187 gyrf[2] = 1.0*gyr[2]; 00188 // printf(" gx: %.2f gy: %.2f , gz: %.2f\n\r", gyrf[0], gyrf[1], gyrf[2]); 00189 00190 madgwick_filter.update((float)gyr[0]/1000.0, (float)gyr[1]/1000.0, (float)gyr[2]/1000.0, (float)ac[0], (float)ac[1], (float)ac[2], (float)mag[0], (float)mag[1], (float)mag[2]); 00191 //madgwick_filter.update(0,100,0,0,0,0,0,0,0); 00192 madgwick_filter.computeAngles(); 00193 00194 pitch = madgwick_filter.getPitch(); 00195 roll = madgwick_filter.getRoll(); 00196 yaw = madgwick_filter.getYaw(); 00197 } 00198 00199 00200 int main() { 00201 00202 /* Enable all sensors */ 00203 hum_temp->enable(); 00204 press_temp->enable(); 00205 magnetometer->enable(); 00206 accelerometer->enable(); 00207 acc_gyro->enable_x(); 00208 acc_gyro->enable_g(); 00209 00210 00211 magnetometer->read_id(&id); 00212 // printf("LSM303AGR magnetometer = 0x%X\r\n", id); 00213 accelerometer->read_id(&id); 00214 // printf("LSM303AGR accelerometer = 0x%X\r\n", id); 00215 acc_gyro->read_id(&id); 00216 // printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); 00217 00218 flipper.attach(&update_filter, 0.1); 00219 00220 while(1){ 00221 //nmea = get_nmea_sentence(roll,pitch,yaw); 00222 //pc.printf("%s",nmea); 00223 pc.printf("no elo"); 00224 wait_ms(500); 00225 } 00226 } 00227
Generated on Sun Sep 18 2022 06:17:28 by
