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@20:c27bce3cf63b, 2019-05-20 (annotated)
- Committer:
- piter12345
- Date:
- Mon May 20 08:22:32 2019 +0000
- Revision:
- 20:c27bce3cf63b
- Parent:
- 19:428ad790bdb3
- Child:
- 21:c62412a679b0
up
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:69566eea0fba | 1 | /** |
cparata | 0:69566eea0fba | 2 | ****************************************************************************** |
cparata | 0:69566eea0fba | 3 | * @file main.cpp |
cparata | 8:8f495e604424 | 4 | * @author CLab |
cparata | 8:8f495e604424 | 5 | * @version V1.0.0 |
cparata | 8:8f495e604424 | 6 | * @date 2-December-2016 |
cparata | 0:69566eea0fba | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
cparata | 0:69566eea0fba | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
cparata | 0:69566eea0fba | 9 | ****************************************************************************** |
cparata | 0:69566eea0fba | 10 | * @attention |
cparata | 0:69566eea0fba | 11 | * |
cparata | 0:69566eea0fba | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
cparata | 0:69566eea0fba | 13 | * |
cparata | 0:69566eea0fba | 14 | * Redistribution and use in source and binary forms, with or without modification, |
cparata | 0:69566eea0fba | 15 | * are permitted provided that the following conditions are met: |
cparata | 0:69566eea0fba | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
cparata | 0:69566eea0fba | 17 | * this list of conditions and the following disclaimer. |
cparata | 0:69566eea0fba | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
cparata | 0:69566eea0fba | 19 | * this list of conditions and the following disclaimer in the documentation |
cparata | 0:69566eea0fba | 20 | * and/or other materials provided with the distribution. |
cparata | 0:69566eea0fba | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
cparata | 0:69566eea0fba | 22 | * may be used to endorse or promote products derived from this software |
cparata | 0:69566eea0fba | 23 | * without specific prior written permission. |
cparata | 0:69566eea0fba | 24 | * |
cparata | 0:69566eea0fba | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
cparata | 0:69566eea0fba | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
cparata | 0:69566eea0fba | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cparata | 0:69566eea0fba | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
cparata | 0:69566eea0fba | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
cparata | 0:69566eea0fba | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
cparata | 0:69566eea0fba | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
cparata | 0:69566eea0fba | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
cparata | 0:69566eea0fba | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
cparata | 0:69566eea0fba | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cparata | 0:69566eea0fba | 35 | * |
cparata | 0:69566eea0fba | 36 | ****************************************************************************** |
cparata | 0:69566eea0fba | 37 | */ |
cparata | 0:69566eea0fba | 38 | |
cparata | 0:69566eea0fba | 39 | /* Includes */ |
cparata | 0:69566eea0fba | 40 | #include "mbed.h" |
davide.aliprandi@st.com | 13:fc873da5b445 | 41 | #include "XNucleoIKS01A2.h" |
piter12345 | 20:c27bce3cf63b | 42 | #include "MadgwickAHRS.h" |
piter12345 | 20:c27bce3cf63b | 43 | #define sampleFreqDef 1500.0f |
cparata | 0:69566eea0fba | 44 | /* Instantiate the expansion board */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 45 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
cparata | 0:69566eea0fba | 46 | |
cparata | 0:69566eea0fba | 47 | /* Retrieve the composing elements of the expansion board */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 48 | static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; |
cparata | 0:69566eea0fba | 49 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
cparata | 0:69566eea0fba | 50 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
cparata | 0:69566eea0fba | 51 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
davide.aliprandi@st.com | 13:fc873da5b445 | 52 | static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; |
cparata | 0:69566eea0fba | 53 | |
cparata | 0:69566eea0fba | 54 | /* Helper function for printing floats & doubles */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 55 | static char *print_double(char* str, double v, int decimalDigits=2) |
cparata | 0:69566eea0fba | 56 | { |
cparata | 0:69566eea0fba | 57 | int i = 1; |
cparata | 0:69566eea0fba | 58 | int intPart, fractPart; |
cparata | 0:69566eea0fba | 59 | int len; |
cparata | 0:69566eea0fba | 60 | char *ptr; |
cparata | 0:69566eea0fba | 61 | |
cparata | 0:69566eea0fba | 62 | /* prepare decimal digits multiplicator */ |
cparata | 0:69566eea0fba | 63 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
cparata | 0:69566eea0fba | 64 | |
cparata | 0:69566eea0fba | 65 | /* calculate integer & fractinal parts */ |
cparata | 0:69566eea0fba | 66 | intPart = (int)v; |
cparata | 0:69566eea0fba | 67 | fractPart = (int)((v-(double)(int)v)*i); |
cparata | 0:69566eea0fba | 68 | |
cparata | 0:69566eea0fba | 69 | /* fill in integer part */ |
cparata | 0:69566eea0fba | 70 | sprintf(str, "%i.", intPart); |
cparata | 0:69566eea0fba | 71 | |
cparata | 0:69566eea0fba | 72 | /* prepare fill in of fractional part */ |
cparata | 0:69566eea0fba | 73 | len = strlen(str); |
cparata | 0:69566eea0fba | 74 | ptr = &str[len]; |
cparata | 0:69566eea0fba | 75 | |
cparata | 0:69566eea0fba | 76 | /* fill in leading fractional zeros */ |
cparata | 0:69566eea0fba | 77 | for (i/=10;i>1; i/=10, ptr++) { |
davide.aliprandi@st.com | 13:fc873da5b445 | 78 | if (fractPart >= i) { |
davide.aliprandi@st.com | 13:fc873da5b445 | 79 | break; |
davide.aliprandi@st.com | 13:fc873da5b445 | 80 | } |
cparata | 0:69566eea0fba | 81 | *ptr = '0'; |
cparata | 0:69566eea0fba | 82 | } |
cparata | 0:69566eea0fba | 83 | |
cparata | 0:69566eea0fba | 84 | /* fill in (rest of) fractional part */ |
cparata | 0:69566eea0fba | 85 | sprintf(ptr, "%i", fractPart); |
cparata | 0:69566eea0fba | 86 | |
cparata | 0:69566eea0fba | 87 | return str; |
cparata | 0:69566eea0fba | 88 | } |
cparata | 0:69566eea0fba | 89 | |
cparata | 0:69566eea0fba | 90 | /* Simple main function */ |
cparata | 0:69566eea0fba | 91 | int main() { |
cparata | 0:69566eea0fba | 92 | uint8_t id; |
cparata | 0:69566eea0fba | 93 | float value1, value2; |
cparata | 0:69566eea0fba | 94 | char buffer1[32], buffer2[32]; |
cparata | 0:69566eea0fba | 95 | int32_t axes[3]; |
piter12345 | 18:ba523ddf9a89 | 96 | int32_t mag[3]; |
piter12345 | 18:ba523ddf9a89 | 97 | int32_t gyr[3]; |
piter12345 | 18:ba523ddf9a89 | 98 | int32_t ac[3]; |
piter12345 | 20:c27bce3cf63b | 99 | float offset[9] = {0}; |
piter12345 | 18:ba523ddf9a89 | 100 | float pitch, roll, yaw; |
piter12345 | 20:c27bce3cf63b | 101 | int offset_cnt = 0; |
piter12345 | 18:ba523ddf9a89 | 102 | |
cparata | 0:69566eea0fba | 103 | |
cparata | 0:69566eea0fba | 104 | /* Enable all sensors */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 105 | hum_temp->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 106 | press_temp->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 107 | magnetometer->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 108 | accelerometer->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 109 | acc_gyro->enable_x(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 110 | acc_gyro->enable_g(); |
cparata | 0:69566eea0fba | 111 | |
cparata | 0:69566eea0fba | 112 | printf("\r\n--- Starting new run ---\r\n"); |
cparata | 0:69566eea0fba | 113 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 114 | magnetometer->read_id(&id); |
piter12345 | 18:ba523ddf9a89 | 115 | // printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
davide.aliprandi@st.com | 13:fc873da5b445 | 116 | accelerometer->read_id(&id); |
piter12345 | 18:ba523ddf9a89 | 117 | // printf("LSM303AGR accelerometer = 0x%X\r\n", id); |
davide.aliprandi@st.com | 13:fc873da5b445 | 118 | acc_gyro->read_id(&id); |
piter12345 | 18:ba523ddf9a89 | 119 | // printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
piter12345 | 18:ba523ddf9a89 | 120 | |
piter12345 | 18:ba523ddf9a89 | 121 | |
piter12345 | 20:c27bce3cf63b | 122 | Madgwick madgwick_filter; |
piter12345 | 20:c27bce3cf63b | 123 | madgwick_filter.begin(sampleFreqDef); |
piter12345 | 18:ba523ddf9a89 | 124 | |
cparata | 0:69566eea0fba | 125 | |
cparata | 0:69566eea0fba | 126 | while(1) { |
cparata | 0:69566eea0fba | 127 | printf("\r\n"); |
piter12345 | 18:ba523ddf9a89 | 128 | |
piter12345 | 20:c27bce3cf63b | 129 | magnetometer->get_m_axes(axes); |
piter12345 | 18:ba523ddf9a89 | 130 | mag[0] = axes[0]; |
piter12345 | 18:ba523ddf9a89 | 131 | mag[1] = axes[1]; |
piter12345 | 18:ba523ddf9a89 | 132 | mag[2] = axes[2]; |
piter12345 | 18:ba523ddf9a89 | 133 | printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", mag[0], mag[1], mag[2]); |
cparata | 0:69566eea0fba | 134 | |
piter12345 | 18:ba523ddf9a89 | 135 | // printf("mag1 is at 0x%08X\r\n", &mag[0]); it's how to get an adress for use in stm studio |
piter12345 | 18:ba523ddf9a89 | 136 | |
piter12345 | 18:ba523ddf9a89 | 137 | accelerometer->get_x_axes(axes); |
piter12345 | 18:ba523ddf9a89 | 138 | ac[0] = axes[0]; |
piter12345 | 18:ba523ddf9a89 | 139 | ac[1] = axes[1]; |
piter12345 | 18:ba523ddf9a89 | 140 | ac[2] = axes[2]; |
piter12345 | 20:c27bce3cf63b | 141 | printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", ac[0] - offset[3]*1000, ac[1] - offset[4]*1000, ac[2] - offset[5]*1000); |
piter12345 | 18:ba523ddf9a89 | 142 | |
piter12345 | 18:ba523ddf9a89 | 143 | //acc_gyro->get_x_axes(axes); |
piter12345 | 18:ba523ddf9a89 | 144 | //printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", ac[0], ac[1], ac[2]); |
piter12345 | 18:ba523ddf9a89 | 145 | |
piter12345 | 18:ba523ddf9a89 | 146 | acc_gyro->get_g_axes(axes); |
piter12345 | 18:ba523ddf9a89 | 147 | gyr[0] = axes[0]; |
piter12345 | 18:ba523ddf9a89 | 148 | gyr[1] = axes[1]; |
piter12345 | 18:ba523ddf9a89 | 149 | gyr[2] = axes[2]; |
piter12345 | 20:c27bce3cf63b | 150 | printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", gyr[0]/1000 - offset[0], gyr[1]/1000 - offset[1], gyr[2]/1000 - offset[2]); |
piter12345 | 20:c27bce3cf63b | 151 | if (offset_cnt > 10) |
piter12345 | 20:c27bce3cf63b | 152 | { |
piter12345 | 20:c27bce3cf63b | 153 | madgwick_filter.update(gyr[0]/1000 - offset[6], gyr[1]/1000 - offset[7], gyr[2]/1000 - offset[8], ac[0]/1000 - offset[3], ac[1]/1000 - offset[4], ac[2]/1000- offset[5], mag[0]/1000, mag[1]/1000, mag[2]/1000); |
piter12345 | 20:c27bce3cf63b | 154 | //madgwick_filter.update(0,0,0,0,0,0,0,0,0); |
piter12345 | 20:c27bce3cf63b | 155 | madgwick_filter.computeAngles(); |
piter12345 | 20:c27bce3cf63b | 156 | } |
piter12345 | 20:c27bce3cf63b | 157 | |
piter12345 | 20:c27bce3cf63b | 158 | pitch = madgwick_filter.getPitch(); |
piter12345 | 20:c27bce3cf63b | 159 | roll = madgwick_filter.getRoll(); |
piter12345 | 20:c27bce3cf63b | 160 | yaw = madgwick_filter.getYaw(); |
cparata | 0:69566eea0fba | 161 | |
piter12345 | 20:c27bce3cf63b | 162 | if (offset_cnt < 10) |
piter12345 | 20:c27bce3cf63b | 163 | { |
piter12345 | 20:c27bce3cf63b | 164 | offset[0] += mag[0]/1000; |
piter12345 | 20:c27bce3cf63b | 165 | offset[1] += mag[1]/1000; |
piter12345 | 20:c27bce3cf63b | 166 | offset[2] += mag[2]/1000; |
piter12345 | 20:c27bce3cf63b | 167 | offset[3] += ac[0]/1000; |
piter12345 | 20:c27bce3cf63b | 168 | offset[4] += ac[1]/1000; |
piter12345 | 20:c27bce3cf63b | 169 | offset[5] += ac[2]/1000; |
piter12345 | 20:c27bce3cf63b | 170 | offset[6] += gyr[0]/1000; |
piter12345 | 20:c27bce3cf63b | 171 | offset[7] += gyr[1]/1000; |
piter12345 | 20:c27bce3cf63b | 172 | offset[8] += gyr[2]/1000; |
piter12345 | 20:c27bce3cf63b | 173 | } |
piter12345 | 20:c27bce3cf63b | 174 | if(offset_cnt == 10) |
piter12345 | 20:c27bce3cf63b | 175 | { |
piter12345 | 20:c27bce3cf63b | 176 | for(int i = 0; i<10;i++) |
piter12345 | 20:c27bce3cf63b | 177 | { |
piter12345 | 20:c27bce3cf63b | 178 | offset[i] = offset[i]/10; |
piter12345 | 20:c27bce3cf63b | 179 | printf("Offset[%d]: %.2f/n",i,offset[i]); |
piter12345 | 20:c27bce3cf63b | 180 | } |
piter12345 | 20:c27bce3cf63b | 181 | } |
piter12345 | 20:c27bce3cf63b | 182 | |
piter12345 | 20:c27bce3cf63b | 183 | offset_cnt++; |
piter12345 | 20:c27bce3cf63b | 184 | printf(" Roll: %.2f\n Pitch: %.2f\n , Yaw: %.2f\n", roll, pitch, yaw); |
piter12345 | 19:428ad790bdb3 | 185 | printf("Hehehehe"); |
piter12345 | 18:ba523ddf9a89 | 186 | wait(0.5); |
cparata | 0:69566eea0fba | 187 | } |
cparata | 0:69566eea0fba | 188 | } |