|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Christopher Durand |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#include <modm/driver/inertial/bmi088.hpp> |
| 13 | + |
| 14 | +#include <modm/board.hpp> |
| 15 | +#include <atomic> |
| 16 | + |
| 17 | +using namespace Board; |
| 18 | + |
| 19 | +using I2c = I2cMaster1; |
| 20 | +using Scl = GpioB8; // D15 |
| 21 | +using Sda = GpioB9; // D14 |
| 22 | + |
| 23 | +using AccInt1 = GpioC6; |
| 24 | +using GyroInt3 = GpioB15; |
| 25 | + |
| 26 | +using Transport = modm::Bmi088I2cTransport<I2c>; |
| 27 | +using Imu = modm::Bmi088<Transport>; |
| 28 | + |
| 29 | +constexpr uint8_t AccAddress = 0x18; |
| 30 | +constexpr uint8_t GyroAddress = 0x68; |
| 31 | +Imu imu{AccAddress, GyroAddress}; |
| 32 | + |
| 33 | +void initializeImu() |
| 34 | +{ |
| 35 | + AccInt1::setInput(AccInt1::InputType::PullDown); |
| 36 | + GyroInt3::setInput(GyroInt3::InputType::PullDown); |
| 37 | + |
| 38 | + constexpr bool selfTest = true; |
| 39 | + while (!imu.initialize(selfTest)) { |
| 40 | + MODM_LOG_ERROR << "Initialization failed, retrying ...\n"; |
| 41 | + modm::delay(500ms); |
| 42 | + } |
| 43 | + |
| 44 | + bool ok = imu.setAccRate(Imu::AccRate::Rate12Hz_Bw5Hz); |
| 45 | + ok &= imu.setAccRange(Imu::AccRange::Range3g); |
| 46 | + |
| 47 | + const auto int1Config = (Imu::AccGpioConfig::ActiveHigh | Imu::AccGpioConfig::EnableOutput); |
| 48 | + ok &= imu.setAccInt1GpioConfig(int1Config); |
| 49 | + ok &= imu.setAccGpioMap(Imu::AccGpioMap::Int1DataReady); |
| 50 | + |
| 51 | + ok &= imu.setGyroRate(Imu::GyroRate::Rate100Hz_Bw12Hz); |
| 52 | + ok &= imu.setGyroRange(Imu::GyroRange::Range250dps); |
| 53 | + ok &= imu.setGyroGpioConfig(Imu::GyroGpioConfig::Int3ActiveHigh); |
| 54 | + ok &= imu.setGyroGpioMap(Imu::GyroGpioMap::Int3DataReady); |
| 55 | + |
| 56 | + if (!ok) { |
| 57 | + MODM_LOG_ERROR << "Configuration failed!\n"; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +int main() |
| 62 | +{ |
| 63 | + Board::initialize(); |
| 64 | + Leds::setOutput(); |
| 65 | + I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::Internal); |
| 66 | + I2c::initialize<Board::SystemClock, 100_kHz, 10_pct>(); |
| 67 | + |
| 68 | + MODM_LOG_INFO << "BMI088 I2C Test\n"; |
| 69 | + initializeImu(); |
| 70 | + |
| 71 | + std::atomic_bool accReady = false; |
| 72 | + std::atomic_bool gyroReady = false; |
| 73 | + |
| 74 | + Exti::connect<AccInt1>(Exti::Trigger::RisingEdge, [&accReady](auto){ |
| 75 | + accReady = true; |
| 76 | + }); |
| 77 | + |
| 78 | + Exti::connect<GyroInt3>(Exti::Trigger::RisingEdge, [&gyroReady](auto){ |
| 79 | + gyroReady = true; |
| 80 | + }); |
| 81 | + |
| 82 | + while (true) |
| 83 | + { |
| 84 | + while(!accReady or !gyroReady); |
| 85 | + |
| 86 | + const std::optional accResult = imu.readAccData(); |
| 87 | + accReady = false; |
| 88 | + const std::optional gyroResult = imu.readGyroData(); |
| 89 | + gyroReady = false; |
| 90 | + |
| 91 | + if (accResult) { |
| 92 | + const modm::Vector3f data = accResult->getFloat(); |
| 93 | + MODM_LOG_INFO.printf("Acc [mg]\tx:\t%5.1f\ty: %5.1f\tz: %5.1f\n", data[0], data[1], data[2]); |
| 94 | + } |
| 95 | + if (gyroResult) { |
| 96 | + const modm::Vector3f data = gyroResult->getFloat(); |
| 97 | + MODM_LOG_INFO.printf("Gyro [deg/s]\tx:\t%5.2f\ty: %5.2f\tz: %5.2f\n", data[0], data[1], data[2]); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments