|
| 1 | +/* exampleCalib.cpp |
| 2 | + * Copyright 2018 ShellAddicted <shelladdicted@gmail.com> |
| 3 | + * GitHub: https://github.com/ShellAddicted |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
1 | 18 |
|
| 19 | +/* |
| 20 | + For more info: https://www.bosch-sensortec.com/bst/products/all_products/bno055 |
| 21 | + Information in this library refers to BST_BNO055_DS000_14 (Consulted in January 2018) |
| 22 | +*/ |
2 | 23 | #include "freertos/FreeRTOS.h" |
3 | 24 | #include "freertos/task.h" |
4 | 25 | #include "BNO055ESP32.h" |
5 | 26 | static const char *TAG = "BNO055ESP32Example"; |
6 | 27 |
|
7 | 28 | extern "C" void app_main(){ |
8 | | - BNO055 bno(UART_NUM_1, GPIO_NUM_17, GPIO_NUM_16); |
9 | | - try{ |
10 | | - bno.begin(); // at this point BNO055 is in CONFIG_MODE until it is changed |
11 | | - bno.setExtCrystalUse(true); |
12 | | - //HOW TO RESTORE OFFSETS (EXAMPLE VALUES): |
13 | | - // bno055_offsets_t ofx; |
14 | | - // ofx.accelOffsetX = 29; |
15 | | - // ofx.accelOffsetY = 24; |
16 | | - // ofx.accelOffsetZ = 16; |
17 | | - // ofx.magOffsetX = -243; |
18 | | - // ofx.magOffsetY = -420; |
19 | | - // ofx.magOffsetZ = -131; |
20 | | - // ofx.gyroOffsetX = 1; |
21 | | - // ofx.gyroOffsetY = -1; |
22 | | - // ofx.gyroOffsetZ = 0; |
23 | | - // ofx.accelRadius = 0; |
24 | | - // ofx.magRadius = 662; |
25 | | - // bno.setSensorOffsets(ofx); //NOTE: this must be executed in CONFIG_MODE |
26 | | - bno.setOpMode(BNO055_OPERATION_MODE_NDOF); // Change OPR_MODE |
27 | | - ESP_LOGI(TAG, "Setup Done."); |
28 | | - } |
29 | | - catch (std::exception& ex){ |
30 | | - ESP_LOGE(TAG, "Setup Failed, Error: %s", ex.what()); |
31 | | - return; |
32 | | - } |
| 29 | + // This values are random, see exampleCalibration.cpp for more details. |
| 30 | + // bno055_offsets_t storedOffsets; |
| 31 | + // storedOffsets.accelOffsetX = 29; |
| 32 | + // storedOffsets.accelOffsetY = 24; |
| 33 | + // storedOffsets.accelOffsetZ = 16; |
| 34 | + // storedOffsets.magOffsetX = -243; |
| 35 | + // storedOffsets.magOffsetY = -420; |
| 36 | + // storedOffsets.magOffsetZ = -131; |
| 37 | + // storedOffsets.gyroOffsetX = 1; |
| 38 | + // storedOffsets.gyroOffsetY = -1; |
| 39 | + // storedOffsets.gyroOffsetZ = 0; |
| 40 | + // storedOffsets.accelRadius = 0; |
| 41 | + // storedOffsets.magRadius = 662; |
| 42 | + |
| 43 | + BNO055 bno(UART_NUM_1, GPIO_NUM_17, GPIO_NUM_16); |
| 44 | + try{ |
| 45 | + bno.begin(); //BNO055 is in CONFIG_MODE until it is changed |
| 46 | + bno.setExtCrystalUse(true); |
| 47 | + //bno.setSensorOffsets(storedOffsets); |
| 48 | + bno.setOpMode(BNO055_OPERATION_MODE_NDOF); |
| 49 | + ESP_LOGI(TAG, "Setup Done."); |
| 50 | + } |
| 51 | + catch (BNO055BaseException& ex){ |
| 52 | + ESP_LOGE(TAG, "Setup Failed, Error: %s", ex.what()); |
| 53 | + return; |
| 54 | + } |
| 55 | + catch (std::exception& ex){ |
| 56 | + ESP_LOGE(TAG, "Setup Failed, Error: %s", ex.what()); |
| 57 | + return; |
| 58 | + } |
33 | 59 |
|
34 | 60 | while (1){ |
35 | | - try{ |
36 | | - uint8_t sys, gyro, accel, mag; |
37 | | - bno.getCalibration(&sys,&gyro,&accel,&mag); |
38 | | - bno055_vector_t v = bno.getVectorEuler(); |
39 | | - ESP_LOGI(TAG, "Euler:(%.1f;%.1f;%.1f) <--> (%d,%d,%d,%d)",(double)v.x, (double)v.y, (double)v.z, (int)sys, (int)gyro, (int)accel, (int)mag); |
40 | | - if (gyro == 3 && accel == 3 && mag == 3){ |
| 61 | + try{ |
| 62 | + //Calibration 3 = fully calibrated, 0 = uncalibrated |
| 63 | + bno055_calibration_t cal = bno.getCalibration(); |
| 64 | + bno055_vector_t v = bno.getVectorEuler(); |
| 65 | + ESP_LOGI(TAG, "Euler: X: %.1f Y: %.1f Z: %.1f || Calibration SYS: %u GYRO: %u ACC:%u MAG:%u", v.x, v.y, v.z, cal.sys, cal.gyro, cal.accel, cal.mag); |
| 66 | + if (cal.gyro == 3 && cal.accel == 3 && cal.mag == 3){ |
41 | 67 | ESP_LOGI(TAG, "Fully Calibrated."); |
42 | 68 | bno.setOpMode(BNO055_OPERATION_MODE_CONFIG); //Change OPR_MODE |
43 | 69 | bno055_offsets_t txt = bno.getSensorOffsets(); //NOTE: this must be executed in CONFIG_MODE |
44 | 70 | ESP_LOGI(TAG, "\nOffsets:\nAccel: X:%d, Y:%d, Z:%d;\nMag: X:%d, Y:%d, Z:%d;\nGyro: X:%d, Y:%d, Z:%d;\nAccelRadius: %d;\nMagRadius: %d;\n", txt.accelOffsetX, txt.accelOffsetY, txt.accelOffsetZ, txt.magOffsetX, txt.magOffsetY, txt.magOffsetZ, txt.gyroOffsetX, txt.gyroOffsetY, txt.gyroOffsetZ, txt.accelRadius, txt.magRadius); |
45 | 71 | break; |
46 | 72 | } |
47 | 73 | } |
48 | | - catch (std::exception &ex){ |
49 | | - ESP_LOGE(TAG, "exc: %s", ex.what()); |
50 | | - } |
| 74 | + catch (BNO055BaseException& ex){ |
| 75 | + ESP_LOGE(TAG, "Error: %s", ex.what()); |
| 76 | + return; |
| 77 | + } |
| 78 | + catch (std::exception &ex){ |
| 79 | + ESP_LOGE(TAG, "Error: %s", ex.what()); |
| 80 | + } |
51 | 81 | vTaskDelay(100 / portTICK_PERIOD_MS); // in fusion mode output rate is 100hz |
52 | 82 | } |
53 | 83 | } |
0 commit comments