Mercurial > public > ostc4
annotate Small_CPU/Src/compass.c @ 357:c3d511365552
Add Support for new end-2019 hardware:
support LSM303AGR compass (Not yet working!)
cleanup compass code a bit
| author | heinrichsweikamp |
|---|---|
| date | Sat, 23 Nov 2019 18:39:50 +0100 |
| parents | 49f5db6139d5 |
| children | c6a084d1433f |
| rev | line source |
|---|---|
| 38 | 1 /** |
| 2 ****************************************************************************** | |
| 3 * @file compass.c | |
| 4 * @author heinrichs weikamp gmbh | |
| 5 * @date 27-March-2014 | |
| 6 * @version V0.2.0 | |
| 7 * @since 21-April-2016 | |
| 8 * @brief for Honeywell Compass and ST LSM303D | |
| 9 * | |
| 10 @verbatim | |
| 11 ============================================================================== | |
| 12 ##### How to use ##### | |
| 13 ============================================================================== | |
| 14 V0.1.0 09-March-2016 | |
| 15 V0.2.0 21-April-2016 Orientation fixed for LSM303D, | |
| 16 roll and pitch added to calibration output, | |
| 17 orientation double checked with datasheets and layout | |
| 18 as well as with value output during calibration | |
| 19 V0.2.1 19-May-2016 New date rate config and full-scale selection | |
| 20 | |
| 21 @endverbatim | |
| 22 ****************************************************************************** | |
| 23 * @attention | |
| 24 * | |
| 25 * <h2><center>© COPYRIGHT(c) 2016 heinrichs weikamp</center></h2> | |
| 26 * | |
| 27 ****************************************************************************** | |
| 28 */ | |
| 29 | |
| 30 #include <math.h> | |
| 31 #include <string.h> | |
| 32 | |
| 33 #include "compass.h" | |
| 34 #include "compass_LSM303D.h" | |
| 35 | |
| 36 #include "i2c.h" | |
| 219 | 37 #include "spi.h" |
| 38 | 38 #include "RTE_FlashAccess.h" // to store compass_calib_data |
| 39 | |
| 40 #include "stm32f4xx_hal.h" | |
| 41 | |
| 42 #define TEST_IF_HMC5883L | |
| 43 //#define COMPASS_DEACTIVATE | |
| 44 | |
| 45 /// split byte to bits | |
| 46 typedef struct{ | |
| 47 uint8_t bit0:1; ///< split byte to bits | |
| 48 uint8_t bit1:1; ///< split byte to bits | |
| 49 uint8_t bit2:1; ///< split byte to bits | |
| 50 uint8_t bit3:1; ///< split byte to bits | |
| 51 uint8_t bit4:1; ///< split byte to bits | |
| 52 uint8_t bit5:1; ///< split byte to bits | |
| 53 uint8_t bit6:1; ///< split byte to bits | |
| 54 uint8_t bit7:1; ///< split byte to bits | |
| 55 } ubit8_t; | |
| 56 | |
| 57 | |
| 58 /// split byte to bits | |
| 59 typedef union{ | |
| 60 ubit8_t ub; ///< split byte to bits | |
| 61 uint8_t uw; ///< split byte to bits | |
| 62 } bit8_Type; | |
| 63 | |
| 64 | |
| 65 /// split word to 2 bytes | |
| 66 typedef struct{ | |
| 67 uint8_t low; ///< split word to 2 bytes | |
| 68 uint8_t hi; ///< split word to 2 bytes | |
| 69 } two_byte; | |
| 70 | |
| 71 | |
| 72 /// split word to 2 bytes | |
| 73 typedef union{ | |
| 74 two_byte Byte; ///< split word to 2 bytes | |
| 75 uint16_t Word; ///< split word to 2 bytes | |
| 76 } tword; | |
| 77 | |
| 78 | |
| 79 /// split signed word to 2 bytes | |
| 80 typedef union{ | |
| 81 two_byte Byte; ///< split signed word to 2 bytes | |
| 82 int16_t Word; ///< split signed word to 2 bytes | |
| 83 } signed_tword; | |
| 84 | |
| 85 | |
| 86 /// split full32 to 2 words | |
| 87 typedef struct{ | |
| 88 uint16_t low16; ///< split word to 2 bytes | |
| 89 uint16_t hi16; ///< split word to 2 bytes | |
| 90 } two_word; | |
| 91 | |
| 92 typedef union{ | |
| 93 two_word Word16; ///< split word to 2 bytes | |
| 94 uint32_t Full32; ///< split word to 2 bytes | |
| 95 } tfull32; | |
| 96 | |
| 97 | |
| 98 /// crazy compass calibration stuff | |
| 99 typedef struct | |
| 100 { | |
| 101 unsigned short int compass_N; | |
| 102 float Su, Sv, Sw; | |
| 103 float Suu, Svv, Sww, Suv, Suw, Svw; | |
| 104 float Suuu, Svvv, Swww; | |
| 105 float Suuv, Suuw, Svvu, Svvw, Swwu, Swwv; | |
| 106 } SCompassCalib; | |
| 107 | |
| 108 | |
| 109 #define Q_PI (18000) | |
| 110 #define Q_PIO2 (9000) | |
| 111 | |
| 112 | |
| 113 | |
| 114 ////////////////////////////////////////////////////////////////////////////// | |
| 115 // fifth order of polynomial approximation of atan(), giving 0.05 deg max error | |
| 116 // | |
| 117 #define K1 (5701) // Needs K1/2**16 | |
| 118 #define K2 (1645) // Needs K2/2**48 WAS NEGATIV | |
| 119 #define K3 ( 446) // Needs K3/2**80 | |
| 120 | |
| 121 const float PI = 3.14159265; ///< pi, used in compass_calc() | |
| 122 | |
| 123 typedef short int Int16; | |
| 124 typedef signed char Int8; | |
| 125 typedef Int16 Angle; | |
| 126 | |
| 127 | |
| 128 /// The (filtered) components of the magnetometer sensor | |
| 129 int16_t compass_DX_f; ///< output from sensor | |
| 130 int16_t compass_DY_f; ///< output from sensor | |
| 131 int16_t compass_DZ_f; ///< output from sensor | |
| 132 | |
| 133 | |
| 134 /// Found soft-iron calibration values, deduced from already filtered values | |
| 135 int16_t compass_CX_f; ///< calibration value | |
| 136 int16_t compass_CY_f; ///< calibration value | |
| 137 int16_t compass_CZ_f; ///< calibration value | |
| 138 | |
| 139 | |
| 140 /// The (filtered) components of the accelerometer sensor | |
| 141 int16_t accel_DX_f; ///< output from sensor | |
| 142 int16_t accel_DY_f; ///< output from sensor | |
| 143 int16_t accel_DZ_f; ///< output from sensor | |
| 144 | |
| 145 | |
| 146 /// The compass result values | |
| 147 float compass_heading; ///< the final result calculated in compass_calc() | |
| 148 float compass_roll; ///< the final result calculated in compass_calc() | |
| 149 float compass_pitch; ///< the final result calculated in compass_calc() | |
| 150 | |
| 151 | |
| 152 uint8_t compass_gain; ///< 7 on start, can be reduced during calibration | |
| 153 | |
| 357 | 154 uint8_t hardwareCompass = 0; ///< either HMC5883L (=1) or LSM303D (=2) or LSM303AGR (=3) or not defined yet (=0) |
| 38 | 155 |
| 156 /// LSM303D variables | |
| 157 uint8_t magDataBuffer[6]; ///< here raw data from LSM303D is stored, can be local | |
| 158 uint8_t accDataBuffer[6]; ///< here raw data from LSM303D is stored, can be local | |
| 159 | |
| 160 | |
| 161 // struct accel_scale _accel_scale; | |
| 162 unsigned _accel_range_m_s2; | |
| 163 float _accel_range_scale; | |
| 164 unsigned _accel_samplerate; | |
| 165 unsigned _accel_onchip_filter_bandwith; | |
| 166 | |
| 167 // struct mag_scale _mag_scale; | |
| 168 unsigned _mag_range_ga; | |
| 169 float _mag_range_scale; | |
| 170 unsigned _mag_samplerate; | |
| 171 | |
| 172 // default scale factors | |
| 173 float _accel_scale_x_offset = 0.0f; | |
| 174 float _accel_scale_x_scale = 1.0f; | |
| 175 float _accel_scale_y_offset = 0.0f; | |
| 176 float _accel_scale_y_scale = 1.0f; | |
| 177 float _accel_scale_z_offset = 0.0f; | |
| 178 float _accel_scale_z_scale = 1.0f; | |
| 179 | |
| 180 float _mag_scale_x_offset = 0.0f; | |
| 181 float _mag_scale_x_scale = 1.0f; | |
| 182 float _mag_scale_y_offset = 0.0f; | |
| 183 float _mag_scale_y_scale = 1.0f; | |
| 184 float _mag_scale_z_offset = 0.0f; | |
| 185 float _mag_scale_z_scale = 1.0f; | |
| 186 | |
| 187 | |
| 188 /* External function prototypes ----------------------------------------------*/ | |
| 189 | |
| 190 extern void copyCompassDataDuringCalibration(int16_t dx, int16_t dy, int16_t dz); | |
| 191 | |
| 192 /* Private function prototypes -----------------------------------------------*/ | |
| 193 | |
| 194 void compass_reset_calibration(SCompassCalib *g); | |
| 195 void compass_add_calibration(SCompassCalib *g); | |
| 196 void compass_solve_calibration(SCompassCalib *g); | |
| 197 | |
| 198 void compass_init_HMC5883L(uint8_t fast, uint8_t gain); | |
| 199 void compass_sleep_HMC5883L(void); | |
| 200 void compass_read_HMC5883L(void); | |
| 201 | |
| 202 void accelerator_init_MMA8452Q(void); | |
| 203 void accelerator_sleep_MMA8452Q(void); | |
| 204 void acceleration_read_MMA8452Q(void); | |
| 205 | |
| 206 void compass_init_LSM303D(uint8_t fast, uint8_t gain); | |
| 207 void compass_sleep_LSM303D(void); | |
| 208 void compass_read_LSM303D(void); | |
| 209 void acceleration_read_LSM303D(void); | |
| 210 | |
| 357 | 211 void compass_init_LSM303AGR(uint8_t fast, uint8_t gain); |
| 212 void compass_sleep_LSM303AGR(void); | |
| 213 void compass_read_LSM303AGR(void); | |
| 214 void acceleration_read_LSM303AGR(void); | |
| 215 | |
| 38 | 216 int LSM303D_accel_set_onchip_lowpass_filter_bandwidth(unsigned bandwidth); |
| 217 int compass_calib_common(void); | |
| 218 | |
| 219 void compass_calc_roll_pitch_only(void); | |
| 220 | |
| 221 void rotate_mag_3f(float *x, float *y, float *z); | |
| 222 void rotate_accel_3f(float *x, float *y, float *z); | |
| 223 | |
| 224 | |
| 225 /* Exported functions --------------------------------------------------------*/ | |
| 226 | |
| 227 | |
| 228 // =============================================================================== | |
| 229 // compass_init | |
| 230 /// @brief This might be called several times with different gain values during calibration | |
| 231 /// On first call it figures out which hardware is integrated | |
| 232 /// | |
| 233 /// @param gain: 7 is max gain, compass_calib() might reduce it | |
| 234 // =============================================================================== | |
| 235 | |
| 236 uint8_t testCompassTypeDebug = 0xFF; | |
| 237 | |
| 238 void compass_init(uint8_t fast, uint8_t gain) | |
| 239 { | |
| 240 // quick off | |
| 241 #ifdef COMPASS_DEACTIVATE | |
| 242 hardwareCompass = COMPASS_NOT_RECOGNIZED; | |
| 243 #endif | |
| 244 | |
| 245 // don't call again with fast, gain in calib mode etc. | |
| 246 // if unknown | |
| 247 if(hardwareCompass == COMPASS_NOT_RECOGNIZED) | |
| 248 { | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 // old code but without else | |
| 253 if(hardwareCompass == 0) | |
| 254 { | |
| 255 uint8_t data = WHO_AM_I; | |
| 256 I2C_Master_Transmit( DEVICE_COMPASS_303D, &data, 1); | |
| 257 I2C_Master_Receive( DEVICE_COMPASS_303D, &data, 1); | |
| 357 | 258 if(data == WHOIAM_VALUE_LSM303D) |
| 259 hardwareCompass = compass_generation2; //LSM303D; | |
| 70 | 260 else |
| 357 | 261 hardwareCompass = compass_generation1; //HMC5883L |
| 262 if(data == WHOIAM_VALUE_LSM303AGR) | |
| 263 hardwareCompass = compass_generation3; //LSM303AGR; | |
| 38 | 264 } |
| 265 | |
| 180 | 266 /* No compass identified => Retry */ |
| 38 | 267 if(hardwareCompass == 0) |
| 268 { | |
| 269 uint8_t data = WHO_AM_I; | |
| 270 I2C_Master_Transmit( DEVICE_COMPASS_303D, &data, 1); | |
| 271 I2C_Master_Receive( DEVICE_COMPASS_303D, &data, 1); | |
| 357 | 272 if(data == WHOIAM_VALUE_LSM303D) |
| 273 hardwareCompass = compass_generation2; //LSM303D; | |
| 38 | 274 else |
| 357 | 275 hardwareCompass = compass_generation1; //HMC5883L; |
| 276 if(data == WHOIAM_VALUE_LSM303AGR) | |
| 277 hardwareCompass = compass_generation3; //LSM303AGR; | |
| 38 | 278 } |
| 70 | 279 |
| 180 | 280 /* Assume that a HMC5883L is equipped by default if detection still failed */ |
| 38 | 281 if(hardwareCompass == 0) |
| 357 | 282 hardwareCompass = compass_generation1; //HMC5883L; |
| 38 | 283 |
| 284 #ifdef TEST_IF_HMC5883L | |
| 285 HAL_StatusTypeDef resultOfOperationHMC_MMA = HAL_TIMEOUT; | |
| 286 | |
| 357 | 287 if(hardwareCompass == compass_generation1) // HMC5883L) |
| 38 | 288 { |
| 289 uint8_t data = 0x2A; // CTRL_REG1 of DEVICE_ACCELARATOR_MMA8452Q | |
| 290 resultOfOperationHMC_MMA = I2C_Master_Transmit( DEVICE_ACCELARATOR_MMA8452Q, &data, 1); | |
| 291 if(resultOfOperationHMC_MMA == HAL_OK) | |
| 292 { | |
| 357 | 293 hardwareCompass = compass_generation1; //HMC5883L; // all fine, keep it |
| 38 | 294 } |
| 295 else | |
| 296 { | |
| 297 hardwareCompass = COMPASS_NOT_RECOGNIZED; | |
| 298 testCompassTypeDebug = 0xEC; | |
| 299 } | |
| 300 } | |
| 301 #endif | |
| 302 | |
| 357 | 303 if(hardwareCompass == compass_generation2) //LSM303D) |
| 38 | 304 compass_init_LSM303D(fast, gain); |
| 357 | 305 if(hardwareCompass == compass_generation3) //LSM303AGR) |
| 306 compass_init_LSM303AGR(fast, gain); | |
| 307 if(hardwareCompass == compass_generation1) //HMC5883L) | |
| 38 | 308 compass_init_HMC5883L(fast, gain); |
| 309 | |
| 357 | 310 tfull32 dataBlock[4]; |
| 311 if(BFA_readLastDataBlock((uint32_t *)dataBlock) == BFA_OK) | |
| 38 | 312 { |
| 313 compass_CX_f = dataBlock[0].Word16.low16; | |
| 314 compass_CY_f = dataBlock[0].Word16.hi16; | |
| 315 compass_CZ_f = dataBlock[1].Word16.low16; | |
| 316 } | |
| 317 | |
| 318 } | |
| 319 | |
| 320 | |
| 321 // =============================================================================== | |
| 322 // compass_calib | |
| 323 /// @brief with onchip_lowpass_filter configuration for accelerometer of LSM303D | |
| 324 // =============================================================================== | |
| 325 int compass_calib(void) | |
| 326 { | |
| 357 | 327 if(hardwareCompass == compass_generation2) //LSM303D) |
| 38 | 328 { |
| 329 LSM303D_accel_set_onchip_lowpass_filter_bandwidth(773); | |
| 330 int out = compass_calib_common(); | |
| 331 LSM303D_accel_set_onchip_lowpass_filter_bandwidth(LSM303D_ACCEL_DEFAULT_ONCHIP_FILTER_FREQ); | |
| 332 return out; | |
| 333 } | |
| 334 else | |
| 357 | 335 if(hardwareCompass == compass_generation1) //HMC5883L) |
| 336 { | |
| 337 return compass_calib_common(); | |
| 338 } | |
| 339 else | |
| 340 if(hardwareCompass == compass_generation3) //LSM303AGR) | |
| 38 | 341 { |
| 342 return compass_calib_common(); | |
| 343 } | |
| 344 else | |
| 345 { | |
| 346 return 0; // standard answer of compass_calib_common(); | |
| 347 } | |
| 348 | |
| 349 | |
| 350 } | |
| 351 | |
| 352 | |
| 353 // =============================================================================== | |
| 354 // compass_sleep | |
| 355 /// @brief low power mode | |
| 356 // =============================================================================== | |
| 357 void compass_sleep(void) | |
| 358 { | |
| 357 | 359 if(hardwareCompass == compass_generation2) //LSM303D) |
| 38 | 360 { |
| 361 compass_sleep_LSM303D(); | |
| 362 } | |
| 363 else | |
| 357 | 364 if(hardwareCompass == compass_generation1) //HMC5883L) |
| 38 | 365 { |
| 366 compass_sleep_HMC5883L(); | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 | |
| 371 // =============================================================================== | |
| 372 // compass_read | |
| 373 /// @brief reads magnetometer and accelerometer for LSM303D, | |
| 374 /// otherwise magnetometer only | |
| 375 // =============================================================================== | |
| 376 void compass_read(void) | |
| 377 { | |
| 357 | 378 if(hardwareCompass == compass_generation2) //LSM303D) |
| 38 | 379 compass_read_LSM303D(); |
| 357 | 380 if(hardwareCompass == compass_generation1) //HMC5883L) |
| 38 | 381 compass_read_HMC5883L(); |
| 357 | 382 if(hardwareCompass == compass_generation3) //LSM303AGR) |
| 383 compass_read_LSM303AGR(); | |
| 384 | |
| 38 | 385 } |
| 386 | |
| 387 | |
| 388 // =============================================================================== | |
| 389 // accelerator_init | |
| 390 /// @brief empty for for LSM303D | |
| 391 // =============================================================================== | |
| 392 void accelerator_init(void) | |
| 393 { | |
| 357 | 394 if(hardwareCompass == compass_generation1) //HMC5883L) |
| 38 | 395 accelerator_init_MMA8452Q(); |
| 396 } | |
| 397 | |
| 398 | |
| 399 // =============================================================================== | |
| 400 // accelerator_sleep | |
| 401 /// @brief empty for for LSM303D | |
| 402 // =============================================================================== | |
| 403 void accelerator_sleep(void) | |
| 404 { | |
| 357 | 405 if(hardwareCompass == compass_generation1) //HMC5883L) |
| 38 | 406 accelerator_sleep_MMA8452Q(); |
| 407 } | |
| 408 | |
| 409 | |
| 410 // =============================================================================== | |
| 411 // acceleration_read | |
| 412 /// @brief empty for for LSM303D | |
| 413 // =============================================================================== | |
| 414 void acceleration_read(void) | |
| 415 { | |
| 357 | 416 if(hardwareCompass == compass_generation2) //LSM303D) |
| 38 | 417 acceleration_read_LSM303D(); |
| 357 | 418 if(hardwareCompass == compass_generation1) //HMC5883L) |
| 38 | 419 acceleration_read_MMA8452Q(); |
| 357 | 420 if(hardwareCompass == compass_generation3) //LSM303AGR) |
| 421 acceleration_read_LSM303AGR(); | |
| 38 | 422 } |
| 423 | |
| 424 | |
| 425 /* Private functions ---------------------------------------------------------*/ | |
| 426 | |
| 427 // =============================================================================== | |
| 357 | 428 // LSM303AGR_read_reg |
| 429 // =============================================================================== | |
| 430 uint8_t LSM303AGR_read_reg(uint8_t addr) | |
| 431 { | |
| 432 uint8_t data; | |
| 433 | |
| 434 I2C_Master_Transmit( DEVICE_COMPASS_303AGR, &addr, 1); | |
| 435 I2C_Master_Receive( DEVICE_COMPASS_303AGR, &data, 1); | |
| 436 return data; | |
| 437 } | |
| 438 | |
| 439 | |
| 440 // =============================================================================== | |
| 441 // LSM303AGR_write_reg | |
| 442 // =============================================================================== | |
| 443 void LSM303AGR_write_reg(uint8_t addr, uint8_t value) | |
| 444 { | |
| 445 uint8_t data[2]; | |
| 446 | |
| 447 data[0] = addr; | |
| 448 data[1] = value; | |
| 449 I2C_Master_Transmit( DEVICE_COMPASS_303AGR, data, 2); | |
| 450 } | |
| 451 | |
| 452 // =============================================================================== | |
| 453 // LSM303AGR_acc_write_reg | |
| 454 // =============================================================================== | |
| 455 void LSM303AGR_acc_write_reg(uint8_t addr, uint8_t value) | |
| 456 { | |
| 457 uint8_t data[2]; | |
| 458 | |
| 459 data[0] = addr; | |
| 460 data[1] = value; | |
| 461 I2C_Master_Transmit( DEVICE_ACCELARATOR_303AGR, data, 2); | |
| 462 } | |
| 463 | |
| 464 | |
| 465 // =============================================================================== | |
| 466 // LSM303AGR_write_checked_reg | |
| 467 // =============================================================================== | |
| 468 void LSM303AGR_write_checked_reg(uint8_t addr, uint8_t value) | |
| 469 { | |
| 470 LSM303AGR_write_reg(addr, value); | |
| 471 } | |
| 472 | |
| 473 // =============================================================================== | |
| 474 // LSM303AGR_acc_write_checked_reg | |
| 475 // =============================================================================== | |
| 476 void LSM303AGR_acc_write_checked_reg(uint8_t addr, uint8_t value) | |
| 477 { | |
| 478 LSM303AGR_acc_write_reg(addr, value); | |
| 479 } | |
| 480 | |
| 481 // =============================================================================== | |
| 38 | 482 // LSM303D_read_reg |
| 483 // =============================================================================== | |
| 484 uint8_t LSM303D_read_reg(uint8_t addr) | |
| 485 { | |
| 486 uint8_t data; | |
| 487 | |
| 488 I2C_Master_Transmit( DEVICE_COMPASS_303D, &addr, 1); | |
| 489 I2C_Master_Receive( DEVICE_COMPASS_303D, &data, 1); | |
| 490 return data; | |
| 491 } | |
| 492 | |
| 493 | |
| 494 // =============================================================================== | |
| 495 // LSM303D_write_reg | |
| 496 // =============================================================================== | |
| 497 void LSM303D_write_reg(uint8_t addr, uint8_t value) | |
| 498 { | |
| 499 uint8_t data[2]; | |
| 500 | |
| 501 /* enable accel*/ | |
| 502 data[0] = addr; | |
| 503 data[1] = value; | |
| 504 I2C_Master_Transmit( DEVICE_COMPASS_303D, data, 2); | |
| 505 } | |
| 506 | |
| 507 | |
| 508 // =============================================================================== | |
| 509 // LSM303D_write_checked_reg | |
| 510 // =============================================================================== | |
| 511 void LSM303D_write_checked_reg(uint8_t addr, uint8_t value) | |
| 512 { | |
| 513 LSM303D_write_reg(addr, value); | |
| 514 } | |
| 515 | |
| 516 | |
| 517 // =============================================================================== | |
| 518 // LSM303D_modify_reg | |
| 519 // =============================================================================== | |
| 520 void LSM303D_modify_reg(unsigned reg, uint8_t clearbits, uint8_t setbits) | |
| 521 { | |
| 522 uint8_t val; | |
| 523 | |
| 524 val = LSM303D_read_reg(reg); | |
| 525 val &= ~clearbits; | |
| 526 val |= setbits; | |
| 527 LSM303D_write_checked_reg(reg, val); | |
| 528 } | |
| 529 | |
| 530 // =============================================================================== | |
| 531 // LSM303D_accel_set_onchip_lowpass_filter_bandwidth | |
| 532 // =============================================================================== | |
| 533 int LSM303D_accel_set_onchip_lowpass_filter_bandwidth(unsigned bandwidth) | |
| 534 { | |
| 535 uint8_t setbits = 0; | |
| 536 uint8_t clearbits = REG2_ANTIALIAS_FILTER_BW_BITS_A; | |
| 537 | |
| 538 if (bandwidth == 0) { | |
| 539 bandwidth = 773; | |
| 540 } | |
| 541 | |
| 542 if (bandwidth <= 50) { | |
| 543 setbits |= REG2_AA_FILTER_BW_50HZ_A; | |
| 544 _accel_onchip_filter_bandwith = 50; | |
| 545 | |
| 546 } else if (bandwidth <= 194) { | |
| 547 setbits |= REG2_AA_FILTER_BW_194HZ_A; | |
| 548 _accel_onchip_filter_bandwith = 194; | |
| 549 | |
| 550 } else if (bandwidth <= 362) { | |
| 551 setbits |= REG2_AA_FILTER_BW_362HZ_A; | |
| 552 _accel_onchip_filter_bandwith = 362; | |
| 553 | |
| 554 } else if (bandwidth <= 773) { | |
| 555 setbits |= REG2_AA_FILTER_BW_773HZ_A; | |
| 556 _accel_onchip_filter_bandwith = 773; | |
| 557 | |
| 558 } else { | |
| 559 return -1; | |
| 560 } | |
| 561 | |
| 562 LSM303D_modify_reg(ADDR_CTRL_REG2, clearbits, setbits); | |
| 563 | |
| 564 return 0; | |
| 565 } | |
| 566 | |
| 567 | |
| 568 // =============================================================================== | |
| 569 // LSM303D_accel_set_driver_lowpass_filter | |
| 570 // =============================================================================== | |
| 571 int LSM303D_accel_set_driver_lowpass_filter(float samplerate, float bandwidth) | |
| 572 { | |
| 573 /* | |
| 574 _accel_filter_x_set_cutoff_frequency(samplerate, bandwidth); | |
| 575 _accel_filter_y_set_cutoff_frequency(samplerate, bandwidth); | |
| 576 _accel_filter_z_set_cutoff_frequency(samplerate, bandwidth); | |
| 577 */ | |
| 578 return 0; | |
| 579 } | |
| 580 | |
| 581 | |
| 582 // rotate_mag_3f: nicht genutzt aber praktisch; rotate_accel_3f wird benutzt | |
| 583 // =============================================================================== | |
| 584 // rotate_mag_3f | |
| 585 /// @brief swap axis in convient way, by hw | |
| 586 /// @param *x raw input is set to *y input | |
| 587 /// @param *y raw input is set to -*x input | |
| 588 /// @param *z raw is not touched | |
| 589 // =============================================================================== | |
| 590 void rotate_mag_3f(float *x, float *y, float *z) | |
| 591 { | |
| 592 return; | |
| 593 /* | |
| 594 *x = *x; // HMC: *x = -*y | |
| 595 *y = *y; // HMC: *y = *x // change 20.04.2016: zuvor *y = -*y | |
| 596 *z = *z; // HMC: *z = *z | |
| 597 */ | |
| 598 } | |
| 599 | |
| 600 | |
| 601 // =============================================================================== | |
| 602 // rotate_accel_3f | |
| 603 /// @brief swap axis in convient way, by hw, same as MMA8452Q | |
| 604 /// @param *x raw input, output is with sign change | |
| 605 /// @param *y raw input, output is with sign change | |
| 606 /// @param *z raw input, output is with sign change | |
| 607 // =============================================================================== | |
| 608 void rotate_accel_3f(float *x, float *y, float *z) | |
| 609 { | |
| 610 *x = -*x; | |
| 611 *y = -*y; | |
| 612 *z = -*z; | |
| 613 /* tested: | |
| 614 x = x, y =-y, z=-z: does not work with roll | |
| 615 x = x, y =y, z=-z: does not work with pitch | |
| 616 x = x, y =y, z=z: does not work at all | |
| 617 */ | |
| 618 } | |
| 619 | |
| 620 | |
| 621 // =============================================================================== | |
| 357 | 622 // compass_init_LSM303D |
| 38 | 623 /// This might be called several times with different gain values during calibration |
| 624 /// but gain change is not supported at the moment. | |
| 625 /// | |
| 626 /// @param gain: 7 is max gain and set with here, compass_calib() might reduce it | |
| 627 // =============================================================================== | |
| 628 | |
| 629 //uint8_t testCompassLS303D[11]; | |
| 630 | |
| 631 void compass_init_LSM303D(uint8_t fast, uint8_t gain) | |
| 632 { | |
| 633 if(fast == 0) | |
| 634 { | |
| 635 LSM303D_write_checked_reg(ADDR_CTRL_REG0, 0x00); | |
| 636 LSM303D_write_checked_reg(ADDR_CTRL_REG1, 0x3F); // mod 12,5 Hz 3 instead of 6,25 Hz 2 | |
| 637 LSM303D_write_checked_reg(ADDR_CTRL_REG2, 0xC0); | |
| 638 LSM303D_write_checked_reg(ADDR_CTRL_REG3, 0x00); | |
| 639 LSM303D_write_checked_reg(ADDR_CTRL_REG4, 0x00); | |
| 640 LSM303D_write_checked_reg(ADDR_CTRL_REG5, 0x68); // mod 12,5 Hz 8 instead of 6,25 Hz 4 | |
| 641 } | |
| 642 else | |
| 643 { | |
| 644 LSM303D_write_checked_reg(ADDR_CTRL_REG0, 0x00); | |
| 645 LSM303D_write_checked_reg(ADDR_CTRL_REG1, 0x6F); // 100 Hz | |
| 646 LSM303D_write_checked_reg(ADDR_CTRL_REG2, 0xC0); | |
| 647 LSM303D_write_checked_reg(ADDR_CTRL_REG3, 0x00); | |
| 648 LSM303D_write_checked_reg(ADDR_CTRL_REG4, 0x00); | |
| 649 LSM303D_write_checked_reg(ADDR_CTRL_REG5, 0x74); // 100 Hz | |
| 650 } | |
| 651 LSM303D_write_checked_reg(ADDR_CTRL_REG6, 0x00); | |
| 652 LSM303D_write_checked_reg(ADDR_CTRL_REG7, 0x00); | |
| 653 | |
| 654 return; | |
| 655 } | |
| 656 | |
| 657 | |
| 658 // =============================================================================== | |
| 659 // compass_sleep_LSM303D | |
| 357 | 660 // @brief Gen 2 chip |
| 38 | 661 // =============================================================================== |
| 662 void compass_sleep_LSM303D(void) | |
| 663 { | |
| 664 LSM303D_write_checked_reg(ADDR_CTRL_REG1, 0x00); // CNTRL1: acceleration sensor Power-down mode | |
| 665 LSM303D_write_checked_reg(ADDR_CTRL_REG7, 0x02); // CNTRL7: magnetic sensor Power-down mode | |
| 666 } | |
| 667 | |
| 668 | |
| 669 // =============================================================================== | |
| 670 // acceleration_read_LSM303D | |
| 357 | 671 // output is accel_DX_f, accel_DY_f, accel_DZ_f |
| 38 | 672 // =============================================================================== |
| 673 void acceleration_read_LSM303D(void) | |
| 674 { | |
| 675 uint8_t data; | |
| 676 float xraw_f, yraw_f, zraw_f; | |
| 677 float accel_report_x, accel_report_y, accel_report_z; | |
| 678 | |
| 679 memset(accDataBuffer,0,6); | |
| 680 | |
| 681 accel_DX_f = 0; | |
| 682 accel_DY_f = 0; | |
| 683 accel_DZ_f = 0; | |
| 684 | |
| 685 for(int i=0;i<6;i++) | |
| 686 { | |
| 687 data = ADDR_OUT_X_L_A + i; | |
| 688 I2C_Master_Transmit( DEVICE_COMPASS_303D, &data, 1); | |
| 689 I2C_Master_Receive( DEVICE_COMPASS_303D, &accDataBuffer[i], 1); | |
| 690 } | |
| 691 | |
| 692 xraw_f = ((float)( (int16_t)((accDataBuffer[1] << 8) | (accDataBuffer[0])))); | |
| 693 yraw_f = ((float)( (int16_t)((accDataBuffer[3] << 8) | (accDataBuffer[2])))); | |
| 694 zraw_f = ((float)( (int16_t)((accDataBuffer[5] << 8) | (accDataBuffer[4])))); | |
| 695 | |
| 696 rotate_accel_3f(&xraw_f, &yraw_f, &zraw_f); | |
| 697 | |
| 698 // mh | |
| 699 accel_report_x = xraw_f; | |
| 700 accel_report_y = yraw_f; | |
| 701 accel_report_z = zraw_f; | |
| 702 | |
| 703 accel_DX_f = ((int16_t)(accel_report_x)); | |
| 704 accel_DY_f = ((int16_t)(accel_report_y)); | |
| 705 accel_DZ_f = ((int16_t)(accel_report_z)); | |
| 706 } | |
| 707 | |
| 708 | |
| 709 // =============================================================================== | |
| 710 // compass_read_LSM303D | |
| 711 /// | |
| 712 /// output is compass_DX_f, compass_DY_f, compass_DZ_f | |
| 713 // =============================================================================== | |
| 714 void compass_read_LSM303D(void) | |
| 715 { | |
| 716 uint8_t data; | |
| 717 // float xraw_f, yraw_f, zraw_f; | |
| 718 // float mag_report_x, mag_report_y, mag_report_z; | |
| 719 | |
| 720 memset(magDataBuffer,0,6); | |
| 721 | |
| 722 compass_DX_f = 0; | |
| 723 compass_DY_f = 0; | |
| 724 compass_DZ_f = 0; | |
| 725 | |
| 726 for(int i=0;i<6;i++) | |
| 727 { | |
| 728 data = ADDR_OUT_X_L_M + i; | |
| 729 I2C_Master_Transmit( DEVICE_COMPASS_303D, &data, 1); | |
| 730 I2C_Master_Receive( DEVICE_COMPASS_303D, &magDataBuffer[i], 1); | |
| 731 } | |
| 732 | |
| 733 // mh 160620 flip x and y if flip display | |
| 734 compass_DX_f = (((int16_t)((magDataBuffer[1] << 8) | (magDataBuffer[0])))); | |
| 735 compass_DY_f = (((int16_t)((magDataBuffer[3] << 8) | (magDataBuffer[2])))); | |
| 736 compass_DZ_f = (((int16_t)((magDataBuffer[5] << 8) | (magDataBuffer[4])))); | |
| 737 // no rotation | |
| 738 return; | |
| 357 | 739 } |
| 740 | |
| 741 | |
| 742 // =============================================================================== | |
| 743 // compass_init_LSM303AGR | |
| 744 /// This might be called several times with different gain values during calibration | |
| 745 /// but gain change is not supported at the moment. | |
| 746 /// | |
| 747 /// @param gain: 7 is max gain and set with here, compass_calib() might reduce it | |
| 748 // =============================================================================== | |
| 38 | 749 |
| 357 | 750 void compass_init_LSM303AGR(uint8_t fast, uint8_t gain) |
| 751 { | |
| 752 if(fast == 0) | |
| 753 { | |
| 754 LSM303AGR_write_checked_reg(0x60, 0x80); // 10Hz | |
| 755 LSM303AGR_write_checked_reg(0x61, 0x03); // CFG_REG_B_M | |
| 756 LSM303AGR_write_checked_reg(0x62, 0x10); // CFG_REG_C_M | |
| 757 } | |
| 758 else | |
| 759 { | |
| 760 LSM303AGR_write_checked_reg(0x60, 0x80); // 10Hz | |
| 761 LSM303AGR_write_checked_reg(0x61, 0x03); // CFG_REG_B_M | |
| 762 LSM303AGR_write_checked_reg(0x62, 0x10); // CFG_REG_C_M | |
| 763 } | |
| 764 // init accel (Same chip, but different address...) | |
| 765 LSM303AGR_acc_write_checked_reg(0x1F, 0x00); // TEMP_CFG_REG_A (Temp sensor off) | |
| 766 LSM303AGR_acc_write_checked_reg(0x20, 0x4F); // CTRL_REG1_A (10Hz, x,y,z = ON) | |
| 767 LSM303AGR_acc_write_checked_reg(0x21, 0x00); // CTRL_REG2_A | |
| 768 LSM303AGR_acc_write_checked_reg(0x22, 0x00); // CTRL_REG3_A | |
| 769 LSM303AGR_acc_write_checked_reg(0x23, 0x04); // CTRL_REG4_A | |
| 770 | |
| 771 return; | |
| 772 } | |
| 773 | |
| 774 | |
| 775 // =============================================================================== | |
| 776 // compass_sleep_LSM303D | |
| 777 // @brief Gen 2 chip | |
| 778 // =============================================================================== | |
| 779 void compass_sleep_LSM303AGR(void) | |
| 780 { | |
| 781 LSM303AGR_write_checked_reg(0x60, 0x03); // | |
| 782 LSM303AGR_write_checked_reg(0x61, 0x04); // | |
| 783 LSM303AGR_write_checked_reg(0x62, 0x51); // | |
| 784 LSM303AGR_write_checked_reg(0x63, 0x00); // | |
| 785 | |
| 786 | |
| 787 LSM303AGR_acc_write_checked_reg(0x1F, 0x00); // | |
| 788 LSM303AGR_acc_write_checked_reg(0x20, 0x00); // | |
| 789 } | |
| 790 | |
| 38 | 791 |
| 357 | 792 // =============================================================================== |
| 793 // acceleration_read_LSM303AGR | |
| 794 // output is accel_DX_f, accel_DY_f, accel_DZ_f | |
| 795 // =============================================================================== | |
| 796 void acceleration_read_LSM303AGR(void) | |
| 797 { | |
| 798 uint8_t data; | |
| 799 float xraw_f, yraw_f, zraw_f; | |
| 800 float accel_report_x, accel_report_y, accel_report_z; | |
| 801 | |
| 802 memset(accDataBuffer,0,6); | |
| 803 | |
| 804 accel_DX_f = 0; | |
| 805 accel_DY_f = 0; | |
| 806 accel_DZ_f = 0; | |
| 807 | |
| 808 for(int i=0;i<6;i++) | |
| 809 { | |
| 810 data = ADDR_OUT_X_L_A + i; // ADDR_OUT_X_L_A is the same as in the LSM303D (luckily) | |
| 811 I2C_Master_Transmit( DEVICE_ACCELARATOR_303AGR, &data, 1); | |
| 812 I2C_Master_Receive( DEVICE_ACCELARATOR_303AGR, &accDataBuffer[i], 1); | |
| 813 } | |
| 814 | |
| 815 xraw_f = ((float)( (int16_t)((accDataBuffer[1] << 8) | (accDataBuffer[0])))); | |
| 816 yraw_f = ((float)( (int16_t)((accDataBuffer[3] << 8) | (accDataBuffer[2])))); | |
| 817 zraw_f = ((float)( (int16_t)((accDataBuffer[5] << 8) | (accDataBuffer[4])))); | |
| 818 | |
| 819 rotate_accel_3f(&xraw_f, &yraw_f, &zraw_f); | |
| 820 | |
| 821 // mh | |
| 822 accel_report_x = xraw_f; | |
| 823 accel_report_y = yraw_f; | |
| 824 accel_report_z = zraw_f; | |
| 825 | |
| 826 accel_DX_f = ((int16_t)(accel_report_x)); | |
| 827 accel_DY_f = ((int16_t)(accel_report_y)); | |
| 828 accel_DZ_f = ((int16_t)(accel_report_z)); | |
| 829 } | |
| 830 | |
| 831 | |
| 832 // =============================================================================== | |
| 833 // compass_read_LSM303AGR | |
| 834 /// | |
| 835 /// output is compass_DX_f, compass_DY_f, compass_DZ_f | |
| 836 // =============================================================================== | |
| 837 void compass_read_LSM303AGR(void) | |
| 838 { | |
| 839 uint8_t data; | |
| 840 // float xraw_f, yraw_f, zraw_f; | |
| 841 // float mag_report_x, mag_report_y, mag_report_z; | |
| 842 | |
| 843 memset(magDataBuffer,0,6); | |
| 844 | |
| 845 compass_DX_f = 0; | |
| 846 compass_DY_f = 0; | |
| 847 compass_DZ_f = 0; | |
| 848 | |
| 849 for(int i=0;i<6;i++) | |
| 850 { | |
| 851 data = 0x68 + i; // OUTX_L_REG_M | |
| 852 I2C_Master_Transmit( DEVICE_COMPASS_303AGR, &data, 1); | |
| 853 I2C_Master_Receive( DEVICE_COMPASS_303AGR, &magDataBuffer[i], 1); | |
| 854 } | |
| 855 | |
| 856 // mh 160620 flip x and y if flip display | |
| 857 compass_DX_f = (((int16_t)((magDataBuffer[1] << 8) | (magDataBuffer[0])))); | |
| 858 compass_DY_f = (((int16_t)((magDataBuffer[3] << 8) | (magDataBuffer[2])))); | |
| 859 compass_DZ_f = (((int16_t)((magDataBuffer[5] << 8) | (magDataBuffer[4])))); | |
| 860 // no rotation | |
| 861 return; | |
| 38 | 862 } |
| 863 | |
| 864 | |
| 865 // -------------------------------------------------------------------------------- | |
| 866 // ----------EARLIER COMPONENTS --------------------------------------------------- | |
| 867 // -------------------------------------------------------------------------------- | |
| 868 | |
| 869 // =============================================================================== | |
| 870 // compass_init_HMC5883L | |
| 871 /// @brief The horrible Honeywell compass chip | |
| 872 /// This might be called several times during calibration | |
| 873 /// | |
| 874 /// @param fast: 1 is fast mode, 0 is normal mode | |
| 875 /// @param gain: 7 is max gain and set with here, compass_calib() might reduce it | |
| 876 // =============================================================================== | |
| 877 void compass_init_HMC5883L(uint8_t fast, uint8_t gain) | |
| 878 { | |
| 879 uint8_t write_buffer[4]; | |
| 880 | |
| 881 compass_gain = gain; | |
| 882 uint16_t length = 0; | |
| 883 write_buffer[0] = 0x00; // 00 = config Register A | |
| 884 | |
| 885 if( fast ) | |
| 886 write_buffer[1] = 0x38; // 0b 0011 1000; // ConfigA: 75Hz, 2 Samples averaged | |
| 887 else | |
| 888 write_buffer[1] = 0x68; // 0b 0110 1000; // ConfigA: 3Hz, 8 Samples averaged | |
| 889 | |
| 890 switch(gain) | |
| 891 { | |
| 892 case 7: | |
| 893 write_buffer[2] = 0xE0; //0b 1110 0000; // ConfigB: gain | |
| 894 break; | |
| 895 case 6: | |
| 896 write_buffer[2] = 0xC0; //0b 1100 0000; // ConfigB: gain | |
| 897 break; | |
| 898 case 5: | |
| 899 write_buffer[2] = 0xA0; //0b 1010 0000; // ConfigB: gain | |
| 900 break; | |
| 901 case 4: | |
| 902 write_buffer[2] = 0x80; //0b 1000 0000; // ConfigB: gain | |
| 903 break; | |
| 904 case 3: | |
| 905 write_buffer[2] = 0x60; //0b 0110 0000; // ConfigB: gain | |
| 906 break; | |
| 907 case 2: | |
| 908 write_buffer[2] = 0x40; //0b 01000 0000; // ConfigB: gain | |
| 909 break; | |
| 910 case 1: | |
| 911 write_buffer[2] = 0x20; //0b 00100 0000; // ConfigB: gain | |
| 912 break; | |
| 913 case 0: | |
| 914 write_buffer[2] = 0x00; //0b 00000 0000; // ConfigB: gain | |
| 915 break; | |
| 916 } | |
| 917 write_buffer[3] = 0x00; // Mode: continuous mode | |
| 918 length = 4; | |
| 919 //hmc_twi_write(0); | |
| 920 I2C_Master_Transmit( DEVICE_COMPASS_HMC5883L, write_buffer, length); | |
| 921 } | |
| 922 | |
| 923 | |
| 924 | |
| 925 // =============================================================================== | |
| 926 // compass_sleep_HMC5883L | |
| 927 /// @brief Power-down mode for Honeywell compass chip | |
| 928 // =============================================================================== | |
| 929 void compass_sleep_HMC5883L(void) | |
| 930 { | |
| 931 uint8_t write_buffer[4]; | |
| 932 uint16_t length = 0; | |
| 933 | |
| 934 write_buffer[0] = 0x00; // 00 = config Register A | |
| 935 write_buffer[1] = 0x68; // 0b 0110 1000; // ConfigA | |
| 936 write_buffer[2] = 0x20; // 0b 0010 0000; // ConfigB | |
| 937 write_buffer[3] = 0x02; // 0b 0000 0010; // Idle Mode | |
| 938 length = 4; | |
| 939 I2C_Master_Transmit( DEVICE_COMPASS_HMC5883L, write_buffer, length); | |
| 940 } | |
| 941 | |
| 942 | |
| 943 // =============================================================================== | |
| 944 // accelerator_init_MMA8452Q | |
| 945 /// @brief Power-down mode for acceleration chip used in combination with Honeywell compass | |
| 946 // =============================================================================== | |
| 947 void accelerator_init_MMA8452Q(void) | |
| 948 { | |
| 949 uint8_t write_buffer[4]; | |
| 950 uint16_t length = 0; | |
| 951 //HAL_Delay(1); | |
| 952 //return; | |
| 953 write_buffer[0] = 0x0E; // XYZ_DATA_CFG | |
| 954 write_buffer[1] = 0x00;//0b00000000; // High pass Filter=0 , +/- 2g range | |
| 955 length = 2; | |
| 956 I2C_Master_Transmit( DEVICE_ACCELARATOR_MMA8452Q, write_buffer, length); | |
| 957 //HAL_Delay(1); | |
| 958 write_buffer[0] = 0x2A; // CTRL_REG1 | |
| 959 write_buffer[1] = 0x34; //0b00110100; // CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode | |
| 960 write_buffer[2] = 0x02; //0b00000010; // CTRL_REG2: High Res in Active mode | |
| 961 length = 3; | |
| 962 I2C_Master_Transmit( DEVICE_ACCELARATOR_MMA8452Q, write_buffer, length); | |
| 963 | |
| 964 //HAL_Delay(1); | |
| 965 //hw_delay_us(100); | |
| 966 write_buffer[0] = 0x2A; // CTRL_REG1 | |
| 967 write_buffer[1] = 0x35; //0b00110101; // CTRL_REG1: ... Active Mode | |
| 968 length = 2; | |
| 969 I2C_Master_Transmit( DEVICE_ACCELARATOR_MMA8452Q, write_buffer, length); | |
| 970 /* | |
| 971 HAL_Delay(6); | |
| 972 compass_read(); | |
| 973 HAL_Delay(1); | |
| 974 acceleration_read(); | |
| 975 | |
| 976 compass_calc(); | |
| 977 */ | |
| 978 } | |
| 979 | |
| 980 | |
| 981 // =============================================================================== | |
| 982 // accelerator_sleep_MMA8452Q | |
| 983 /// @brief compass_sleep_HMC5883L | |
| 984 // =============================================================================== | |
| 985 void accelerator_sleep_MMA8452Q(void) | |
| 986 { | |
| 987 uint16_t length = 0; | |
| 988 uint8_t write_buffer[4]; | |
| 989 | |
| 990 write_buffer[0] = 0x2A; // CTRL_REG1 | |
| 991 write_buffer[1] = 0x00; //0b00000000; // CTRL_REG1: Standby Mode | |
| 992 length = 2; | |
| 993 I2C_Master_Transmit( DEVICE_ACCELARATOR_MMA8452Q, write_buffer, length); | |
| 994 } | |
| 995 | |
| 996 | |
| 997 // =============================================================================== | |
| 998 // compass_read_HMC5883L | |
| 999 /// @brief The new ST 303D - get ALL data and store in static variables | |
| 1000 /// | |
| 1001 /// output is compass_DX_f, compass_DY_f, compass_DZ_f | |
| 1002 // =============================================================================== | |
| 1003 void compass_read_HMC5883L(void) | |
| 1004 { | |
| 1005 uint8_t buffer[20]; | |
| 1006 compass_DX_f = 0; | |
| 1007 compass_DY_f = 0; | |
| 1008 compass_DZ_f = 0; | |
| 1009 uint8_t length = 0; | |
| 1010 uint8_t read_buffer[6]; | |
| 1011 signed_tword data; | |
| 1012 for(int i = 0; i<6;i++) | |
| 1013 read_buffer[i] = 0; | |
| 1014 buffer[0] = 0x03; // 03 = Data Output X MSB Register | |
| 1015 length = 1; | |
| 1016 I2C_Master_Transmit( DEVICE_COMPASS_HMC5883L, buffer, length); | |
| 1017 length = 6; | |
| 1018 I2C_Master_Receive( DEVICE_COMPASS_HMC5883L, read_buffer, length); | |
| 1019 | |
| 1020 | |
| 1021 data.Byte.hi = read_buffer[0]; | |
| 1022 data.Byte.low = read_buffer[1]; | |
| 1023 //Y = Z | |
| 1024 compass_DY_f = - data.Word; | |
| 1025 | |
| 1026 data.Byte.hi = read_buffer[2]; | |
| 1027 data.Byte.low = read_buffer[3]; | |
| 1028 compass_DZ_f = data.Word; | |
| 1029 | |
| 1030 data.Byte.hi = read_buffer[4]; | |
| 1031 data.Byte.low = read_buffer[5]; | |
| 1032 //X = -Y | |
| 1033 compass_DX_f = data.Word; | |
| 1034 } | |
| 1035 | |
| 1036 | |
| 1037 // =============================================================================== | |
| 1038 // acceleration_read_MMA8452Q | |
| 1039 /// @brief The old MMA8452Q used with the Honeywell compass | |
| 1040 /// get the acceleration data and store in static variables | |
| 1041 /// | |
| 1042 /// output is accel_DX_f, accel_DY_f, accel_DZ_f | |
| 1043 // =============================================================================== | |
| 1044 void acceleration_read_MMA8452Q(void) | |
| 1045 { | |
| 1046 uint8_t buffer[20]; | |
| 1047 accel_DX_f = 0; | |
| 1048 accel_DY_f = 0; | |
| 1049 accel_DZ_f = 0; | |
| 1050 uint8_t length = 0; | |
| 1051 // bit8_Type status ; | |
| 1052 uint8_t read_buffer[7]; | |
| 1053 signed_tword data; | |
| 1054 for(int i = 0; i<6;i++) | |
| 1055 read_buffer[i] = 0; | |
| 1056 buffer[0] = 0x00; // 03 = Data Output X MSB Register | |
| 1057 length = 1; | |
| 1058 I2C_Master_Transmit( DEVICE_ACCELARATOR_MMA8452Q, buffer, length); | |
| 1059 length = 7; | |
| 1060 I2C_Master_Receive( DEVICE_ACCELARATOR_MMA8452Q, read_buffer, length); | |
| 1061 | |
| 1062 // status.uw = read_buffer[0]; | |
| 1063 data.Byte.hi = read_buffer[1]; | |
| 1064 data.Byte.low = read_buffer[2]; | |
| 1065 accel_DX_f =data.Word/16; | |
| 1066 data.Byte.hi = read_buffer[3]; | |
| 1067 data.Byte.low = read_buffer[4]; | |
| 1068 accel_DY_f =data.Word/16; | |
| 1069 data.Byte.hi = read_buffer[5]; | |
| 1070 data.Byte.low = read_buffer[6]; | |
| 1071 accel_DZ_f =data.Word/16; | |
| 1072 | |
| 1073 accel_DX_f *= -1; | |
| 1074 accel_DY_f *= -1; | |
| 1075 accel_DZ_f *= -1; | |
| 1076 } | |
| 1077 | |
| 1078 | |
| 1079 // =============================================================================== | |
| 1080 // compass_calc_roll_pitch_only | |
| 1081 /// @brief only the roll and pitch parts of compass_calc() | |
| 1082 /// | |
| 1083 /// input is accel_DX_f, accel_DY_f, accel_DZ_f | |
| 1084 /// output is compass_pitch and compass_roll | |
| 1085 // =============================================================================== | |
| 1086 void compass_calc_roll_pitch_only(void) | |
| 1087 { | |
| 1088 float sinPhi, cosPhi; | |
| 1089 float Phi, Teta; | |
| 1090 | |
| 1091 //---- Calculate sine and cosine of roll angle Phi ----------------------- | |
| 1092 Phi= atan2f(accel_DY_f, accel_DZ_f) ; | |
| 1093 compass_roll = Phi * 180.0f /PI; | |
| 1094 sinPhi = sinf(Phi); | |
| 1095 cosPhi = cosf(Phi); | |
| 1096 | |
| 1097 //---- calculate sin and cosine of pitch angle Theta --------------------- | |
| 1098 Teta = atanf(-(float)accel_DX_f/(accel_DY_f * sinPhi + accel_DZ_f * cosPhi)); | |
| 1099 compass_pitch = Teta * 180.0f /PI; | |
| 1100 } | |
| 1101 | |
| 1102 | |
| 1103 // =============================================================================== | |
| 1104 // compass_calc | |
| 1105 /// @brief all the fancy stuff first implemented in OSTC3 | |
| 1106 /// | |
| 1107 /// input is compass_DX_f, compass_DY_f, compass_DZ_f, accel_DX_f, accel_DY_f, accel_DZ_f | |
| 1108 /// and compass_CX_f, compass_CY_f, compass_CZ_f | |
| 1109 /// output is compass_heading, compass_pitch and compass_roll | |
| 1110 // =============================================================================== | |
| 1111 void compass_calc(void) | |
| 1112 { | |
| 1113 float sinPhi, cosPhi, sinTeta, cosTeta; | |
| 1114 float Phi, Teta, Psi; | |
| 1115 int16_t iBfx, iBfy; | |
| 1116 int16_t iBpx, iBpy, iBpz; | |
| 1117 | |
| 1118 //---- Make hard iron correction ----------------------------------------- | |
| 1119 // Measured magnetometer orientation, measured ok. | |
| 1120 // From matthias drawing: (X,Y,Z) --> (X,Y,Z) : no rotation. | |
| 1121 iBpx = compass_DX_f - compass_CX_f; // X | |
| 1122 iBpy = compass_DY_f - compass_CY_f; // Y | |
| 1123 iBpz = compass_DZ_f - compass_CZ_f; // Z | |
| 1124 | |
| 1125 //---- Calculate sine and cosine of roll angle Phi ----------------------- | |
| 1126 //sincos(accel_DZ_f, accel_DY_f, &sin, &cos); | |
| 1127 Phi= atan2f(accel_DY_f, accel_DZ_f) ; | |
| 1128 compass_roll = Phi * 180.0f /PI; | |
| 1129 sinPhi = sinf(Phi); | |
| 1130 cosPhi = cosf(Phi); | |
| 1131 | |
| 1132 //---- rotate by roll angle (-Phi) --------------------------------------- | |
| 1133 iBfy = iBpy * cosPhi - iBpz * sinPhi; | |
| 1134 iBpz = iBpy * sinPhi + iBpz * cosPhi; | |
| 1135 //Gz = imul(accel_DY_f, sin) + imul(accel_DZ_f, cos); | |
| 1136 | |
| 1137 //---- calculate sin and cosine of pitch angle Theta --------------------- | |
| 1138 //sincos(Gz, -accel_DX_f, &sin, &cos); // NOTE: changed sin sign. | |
| 1139 // Teta takes into account roll of computer and sends combination of Y and Z :-) understand now hw 160421 | |
| 1140 Teta = atanf(-(float)accel_DX_f/(accel_DY_f * sinPhi + accel_DZ_f * cosPhi)); | |
| 1141 compass_pitch = Teta * 180.0f /PI; | |
| 1142 sinTeta = sinf(Teta); | |
| 1143 cosTeta = cosf(Teta); | |
| 1144 /* correct cosine if pitch not in range -90 to 90 degrees */ | |
| 1145 if( cosTeta < 0 ) cosTeta = -cosTeta; | |
| 1146 | |
| 1147 ///---- de-rotate by pitch angle Theta ----------------------------------- | |
| 1148 iBfx = iBpx * cosTeta + iBpz * sinTeta; | |
| 1149 | |
| 1150 //---- Detect uncalibrated compass --------------------------------------- | |
| 1151 if( !compass_CX_f && !compass_CY_f && !compass_CZ_f ) | |
| 1152 { | |
| 1153 compass_heading = -1; | |
| 1154 return; | |
| 1155 } | |
| 1156 | |
| 1157 //---- calculate current yaw = e-compass angle Psi ----------------------- | |
| 1158 // Result in degree (no need of 0.01 deg precision... | |
| 1159 Psi = atan2f(-iBfy,iBfx); | |
| 1160 compass_heading = Psi * 180.0f /PI; | |
| 1161 // Result in 0..360 range: | |
| 1162 if( compass_heading < 0 ) | |
| 1163 compass_heading += 360; | |
| 1164 } | |
| 1165 | |
| 1166 | |
| 1167 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 1168 // // - Calibration - /////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 1169 // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 1170 | |
| 1171 /* can be lost during sleep as those are reset with compass_reset_calibration() */ | |
| 1172 | |
| 1173 // =============================================================================== | |
| 1174 // compass_reset_calibration | |
| 1175 /// @brief all the fancy stuff first implemented in OSTC3 | |
| 1176 /// | |
| 1177 /// output is struct g and compass_CX_f, compass_CY_f, compass_CZ_f | |
| 1178 /// | |
| 1179 /// @param g: is a struct with crazy stuff like Suuu, Svvv, Svvu, etc. | |
| 1180 /// all is set to zero here | |
| 1181 // =============================================================================== | |
| 1182 void compass_reset_calibration(SCompassCalib *g) | |
| 1183 { | |
| 1184 g->compass_N = 0; | |
| 1185 g->Su = g->Sv = g->Sw = 0.0; | |
| 1186 g->Suu = g->Svv = g->Sww = g->Suv = g->Suw = g->Svw = 0.0; | |
| 1187 g->Suuu = g->Svvv = g->Swww = 0.0; | |
| 1188 g->Suuv = g->Suuw = g->Svvu = g->Svvw = g->Swwu = g->Swwv = 0.0; | |
| 1189 compass_CX_f = compass_CY_f = compass_CZ_f = 0.0; | |
| 1190 } | |
| 1191 | |
| 1192 | |
| 1193 // =============================================================================== | |
| 1194 // compass_add_calibration | |
| 1195 /// @brief all the fancy stuff first implemented in OSTC3 | |
| 1196 /// | |
| 1197 /// input is compass_DX_f, compass_DY_f, compass_DZ_f | |
| 1198 /// and compass_CX_f, compass_CY_f, compass_CZ_f | |
| 1199 /// output is struct g | |
| 1200 /// | |
| 1201 /// @param g: is a struct with crazy stuff like Suuu, Svvv, Svvu, etc. | |
| 1202 // =============================================================================== | |
| 1203 void compass_add_calibration(SCompassCalib *g) | |
| 1204 { | |
| 1205 float u, v, w; | |
| 1206 | |
| 1207 u = (compass_DX_f - compass_CX_f) / 32768.0f; | |
| 1208 v = (compass_DY_f - compass_CY_f) / 32768.0f; | |
| 1209 w = (compass_DZ_f - compass_CZ_f) / 32768.0f; | |
| 1210 | |
| 1211 g->compass_N++; | |
| 1212 g->Su += u; | |
| 1213 g->Sv += v; | |
| 1214 g->Sw += w; | |
| 1215 g->Suv += u*v; | |
| 1216 g->Suw += u*w; | |
| 1217 g->Svw += v*w; | |
| 1218 g->Suu += u*u; | |
| 1219 g->Suuu += u*u*u; | |
| 1220 g->Suuv += v*u*u; | |
| 1221 g->Suuw += w*u*u; | |
| 1222 g->Svv += v*v; | |
| 1223 g->Svvv += v*v*v; | |
| 1224 g->Svvu += u*v*v; | |
| 1225 g->Svvw += w*v*v; | |
| 1226 g->Sww += w*w; | |
| 1227 g->Swww += w*w*w; | |
| 1228 g->Swwu += u*w*w; | |
| 1229 g->Swwv += v*w*w; | |
| 1230 } | |
| 1231 | |
| 1232 ////////////////////////////////////////////////////////////////////////////// | |
| 1233 | |
| 1234 // =============================================================================== | |
| 1235 // compass_solve_calibration | |
| 1236 /// @brief all the fancy stuff first implemented in OSTC3 | |
| 1237 /// | |
| 1238 /// input is compass_CX_f, compass_CY_f, compass_CZ_f and g | |
| 1239 /// output is struct g | |
| 1240 /// | |
| 1241 /// @param g: is a struct with crazy stuff like Suuu, Svvv, Svvu, etc. | |
| 1242 // =============================================================================== | |
| 1243 void compass_solve_calibration(SCompassCalib *g) | |
| 1244 { | |
| 1245 float yu, yv, yw; | |
| 1246 float delta; | |
| 1247 float uc, vc, wc; | |
| 1248 | |
| 1249 | |
| 1250 //---- Normalize partial sums -------------------------------------------- | |
| 1251 // | |
| 1252 // u, v, w should be centered on the mean value um, vm, wm: | |
| 1253 // x = u + um, with um = Sx/N | |
| 1254 // | |
| 1255 // So: | |
| 1256 // (u + um)**2 = u**2 + 2u*um + um**2 | |
| 1257 // Su = 0, um = Sx/N | |
| 1258 // Sxx = Suu + 2 um Su + N*(Sx/N)**2 = Suu + Sx**2/N | |
| 1259 // Suu = Sxx - Sx**2/N | |
| 1260 yu = g->Su/g->compass_N; | |
| 1261 yv = g->Sv/g->compass_N; | |
| 1262 yw = g->Sw/g->compass_N; | |
| 1263 | |
| 1264 g->Suu -= g->Su*yu; | |
| 1265 g->Svv -= g->Sv*yv; | |
| 1266 g->Sww -= g->Sw*yw; | |
| 1267 | |
| 1268 // (u + um)(v + vm) = uv + u vm + v um + um vm | |
| 1269 // Sxy = Suv + N * um vm | |
| 1270 // Suv = Sxy - N * (Sx/N)(Sy/N); | |
| 1271 g->Suv -= g->Su*yv; | |
| 1272 g->Suw -= g->Su*yw; | |
| 1273 g->Svw -= g->Sv*yw; | |
| 1274 | |
| 1275 // (u + um)**3 = u**3 + 3 u**2 um + 3 u um**2 + um**3 | |
| 1276 // Sxxx = Suuu + 3 um Suu + 3 um**2 Su + N.um**3 | |
| 1277 // Su = 0, um = Sx/N: | |
| 1278 // Suuu = Sxxx - 3 Sx*Suu/N - N.(Sx/N)**3 | |
| 1279 // = Sxxx - 3 Sx*Suu/N - Sx**3/N**2 | |
| 1280 | |
| 1281 // (u + um)**2 (v + vm) = (u**2 + 2 u um + um**2)(v + vm) | |
| 1282 // Sxxy = Suuv + vm Suu + 2 um (Suv + vm Su) + um**2 (Sv + N.vm) | |
| 1283 // | |
| 1284 // Su = 0, Sv = 0, vm = Sy/N: | |
| 1285 // Sxxy = Suuv + vm Suu + 2 um Suv + N um**2 vm | |
| 1286 // | |
| 1287 // Suuv = Sxxy - (Sy/N) Suu - 2 (Sx/N) Suv - (Sx/N)**2 Sy | |
| 1288 // = Sxxy - Suu*Sy/N - 2 Suv*Sx/N - Sx*Sx*Sy/N/N | |
| 1289 // = Sxxy - (Suu + Sx*Sx/N)*Sy/N - 2 Suv*Sx/N | |
| 1290 g->Suuu -= (3*g->Suu + g->Su*yu)*yu; | |
| 1291 g->Suuv -= (g->Suu + g->Su*yu)*yv + 2*g->Suv*yu; | |
| 1292 g->Suuw -= (g->Suu + g->Su*yu)*yw + 2*g->Suw*yu; | |
| 1293 | |
| 1294 g->Svvu -= (g->Svv + g->Sv*yv)*yu + 2*g->Suv*yv; | |
| 1295 g->Svvv -= (3*g->Svv + g->Sv*yv)*yv; | |
| 1296 g->Svvw -= (g->Svv + g->Sv*yv)*yw + 2*g->Svw*yv; | |
| 1297 | |
| 1298 g->Swwu -= (g->Sww + g->Sw*yw)*yu + 2*g->Suw*yw; | |
| 1299 g->Swwv -= (g->Sww + g->Sw*yw)*yv + 2*g->Svw*yw; | |
| 1300 g->Swww -= (3*g->Sww + g->Sw*yw)*yw; | |
| 1301 | |
| 1302 //---- Solve the system -------------------------------------------------- | |
| 1303 // uc Suu + vc Suv + wc Suw = (Suuu + Svvu + Swwu) / 2 | |
| 1304 // uc Suv + vc Svv + wc Svw = (Suuv + Svvv + Swwv) / 2 | |
| 1305 // uc Suw + vc Svw + wc Sww = (Suuw + Svvw + Swww) / 2 | |
| 1306 // Note this is symetric, with a positiv diagonal, hence | |
| 1307 // it always have a uniq solution. | |
| 1308 yu = 0.5f * (g->Suuu + g->Svvu + g->Swwu); | |
| 1309 yv = 0.5f * (g->Suuv + g->Svvv + g->Swwv); | |
| 1310 yw = 0.5f * (g->Suuw + g->Svvw + g->Swww); | |
| 1311 delta = g->Suu * (g->Svv * g->Sww - g->Svw * g->Svw) | |
| 1312 - g->Suv * (g->Suv * g->Sww - g->Svw * g->Suw) | |
| 1313 + g->Suw * (g->Suv * g->Svw - g->Svv * g->Suw); | |
| 1314 | |
| 1315 uc = (yu * (g->Svv * g->Sww - g->Svw * g->Svw) | |
| 1316 - yv * (g->Suv * g->Sww - g->Svw * g->Suw) | |
| 1317 + yw * (g->Suv * g->Svw - g->Svv * g->Suw) )/delta; | |
| 1318 vc = (g->Suu * ( yv * g->Sww - yw * g->Svw) | |
| 1319 - g->Suv * ( yu * g->Sww - yw * g->Suw) | |
| 1320 + g->Suw * ( yu * g->Svw - yv * g->Suw) )/delta; | |
| 1321 wc = (g->Suu * (g->Svv * yw - g->Svw * yv ) | |
| 1322 - g->Suv * (g->Suv * yw - g->Svw * yu ) | |
| 1323 + g->Suw * (g->Suv * yv - g->Svv * yu ) )/delta; | |
| 1324 | |
| 1325 // Back to uncentered coordinates: | |
| 1326 // xc = um + uc | |
| 1327 uc = g->Su/g->compass_N + compass_CX_f/32768.0f + uc; | |
| 1328 vc = g->Sv/g->compass_N + compass_CY_f/32768.0f + vc; | |
| 1329 wc = g->Sw/g->compass_N + compass_CZ_f/32768.0f + wc; | |
| 1330 | |
| 1331 // Then save the new calibrated center: | |
| 1332 compass_CX_f = (short)(32768 * uc); | |
| 1333 compass_CY_f = (short)(32768 * vc); | |
| 1334 compass_CZ_f = (short)(32768 * wc); | |
| 1335 } | |
| 1336 | |
| 1337 | |
| 1338 // =============================================================================== | |
| 1339 // compass_calib | |
| 1340 /// @brief the main loop for calibration | |
| 1341 /// output is compass_CX_f, compass_CY_f, compass_CZ_f and g | |
| 1342 /// 160704 removed -4096 limit for LSM303D | |
| 1343 /// | |
| 1344 /// @return always 0 | |
| 1345 // =============================================================================== | |
| 1346 int compass_calib_common(void) | |
| 1347 { | |
| 1348 SCompassCalib g; | |
| 1349 | |
| 1350 // Starts with no calibration at all: | |
| 1351 compass_reset_calibration(&g); | |
| 1352 | |
| 1353 int64_t tickstart = 0; | |
| 1354 uint32_t ticks = 0; | |
| 1355 uint32_t lasttick = 0; | |
| 1356 tickstart = HAL_GetTick(); | |
| 1357 // Eine Minute kalibrieren | |
| 1358 while((ticks) < 60 * 1000) | |
| 1359 { | |
| 1360 compass_read(); | |
| 1361 acceleration_read(); | |
| 1362 compass_calc_roll_pitch_only(); | |
| 1363 | |
| 357 | 1364 if((hardwareCompass == compass_generation1 ) //HMC5883L) |
| 38 | 1365 &&((compass_DX_f == -4096) || |
| 1366 (compass_DY_f == -4096) || | |
| 1367 (compass_DZ_f == -4096) )) | |
| 1368 { | |
| 1369 if(compass_gain == 0) | |
| 1370 return -1; | |
| 1371 compass_gain--; | |
| 1372 | |
| 1373 compass_init(1, compass_gain); | |
| 1374 compass_reset_calibration(&g); | |
| 1375 //tickstart = HAL_GetTick(); | |
| 1376 continue; | |
| 1377 } | |
| 1378 | |
| 1379 copyCompassDataDuringCalibration(compass_DX_f,compass_DY_f,compass_DZ_f); | |
| 104 | 1380 compass_add_calibration(&g); |
| 38 | 1381 HAL_Delay(1); |
| 1382 lasttick = HAL_GetTick(); | |
| 1383 if(lasttick == 0) | |
| 1384 { | |
| 1385 tickstart = -ticks; | |
| 1386 } | |
| 104 | 1387 HAL_Delay(1); |
| 38 | 1388 ticks = lasttick - tickstart; |
|
147
14e4c83a7559
Forward compass data during calibration mode
Ideenmodellierer
parents:
104
diff
changeset
|
1389 SPI_Evaluate_RX_Data(); |
| 104 | 1390 } |
| 38 | 1391 |
| 1392 compass_solve_calibration(&g); | |
| 1393 | |
| 1394 tfull32 dataBlock[4]; | |
| 1395 dataBlock[0].Word16.low16 = compass_CX_f; | |
| 1396 dataBlock[0].Word16.hi16 = compass_CY_f; | |
| 1397 dataBlock[1].Word16.low16 = compass_CZ_f; | |
| 1398 dataBlock[1].Word16.hi16 = 0xFFFF; | |
| 1399 dataBlock[2].Full32 = 0x7FFFFFFF; | |
| 1400 dataBlock[3].Full32 = 0x7FFFFFFF; | |
| 1401 BFA_writeDataBlock((uint32_t *)dataBlock); | |
| 1402 | |
| 1403 return 0; | |
| 1404 } | |
| 1405 |
