From a8edbe841713853102ec497fd15bf6aee1102b81 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Thu, 11 Jul 2019 21:10:03 +0200 Subject: [PATCH] [examples] Add BNO055 example --- examples/README.md | 2 +- examples/nucleo_f411re/imu_bno055/main.cpp | 120 ++++++++++++++++++ examples/nucleo_f411re/imu_bno055/project.xml | 15 +++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 examples/nucleo_f411re/imu_bno055/main.cpp create mode 100644 examples/nucleo_f411re/imu_bno055/project.xml diff --git a/examples/README.md b/examples/README.md index 633d60b8ef..6dc2b7e0e8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -94,7 +94,7 @@ make gdb ## Interesting Examples -We have a lot of examples, 189 to be +We have a lot of examples, 190 to be exact, but here are some of our favorite examples for our supported development boards: diff --git a/examples/nucleo_f411re/imu_bno055/main.cpp b/examples/nucleo_f411re/imu_bno055/main.cpp new file mode 100644 index 0000000000..29651b4e2a --- /dev/null +++ b/examples/nucleo_f411re/imu_bno055/main.cpp @@ -0,0 +1,120 @@ +// coding: utf-8 +/* + * Copyright (c) 2019, Niklas Hauser + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +// ---------------------------------------------------------------------------- + +#include + +#include +#include +#include +using namespace modm::literals; + +// Set the log level +#undef MODM_LOG_LEVEL +#define MODM_LOG_LEVEL modm::log::DEBUG + +/** + * Example to demonstrate a MODM driver for imu sensor VL53L0X + * + * This example uses I2cMaster1 of STM32F401 + * + * SDA PB9 + * SCL PB8 + * + * GND and +3V are connected to the sensor. + */ + +using namespace Board; + +using MyI2cMaster = I2cMaster1; +// using MyI2cMaster = BitBangI2cMaster; + +modm::bno055::Data data; +modm::Bno055 imu(data); + +class ThreadOne : public modm::pt::Protothread +{ +public: + bool + update() + { + PT_BEGIN(); + + MODM_LOG_DEBUG << "Ping the device from ThreadOne" << modm::endl; + + // ping the device until it responds + while (true) + { + // we wait until the device started + if (PT_CALL(imu.ping())) { + break; + } + PT_WAIT_UNTIL(timer.execute()); + } + + MODM_LOG_DEBUG << "Device responded" << modm::endl; + + while (true) + { + if (PT_CALL(imu.configure())) { + break; + } + + PT_WAIT_UNTIL(timer.execute()); + } + + MODM_LOG_DEBUG << "Device configured" << modm::endl; + + while (true) + { + PT_WAIT_UNTIL(timer.execute()); + PT_CALL(imu.readData()); + MODM_LOG_INFO << (int)imu.getData().heading() << modm::endl; + } + + PT_END(); + } + +private: + modm::ShortPeriodicTimer timer{100}; +}; + +ThreadOne one; + +// ---------------------------------------------------------------------------- +int +main() +{ + Board::initialize(); + LedD13::setOutput(); + + // Board::D13::setOutput(modm::Gpio::Low); + MyI2cMaster::connect(); + MyI2cMaster::initialize(); + + MODM_LOG_INFO << "\n\nWelcome to BNO055 demo!\n\n" << modm::endl; + + modm::ShortPeriodicTimer tmr(500); + + // Board::D15::setOutput(); + + while (1) + { + one.update(); + if(tmr.execute()) { + LedD13::toggle(); + // Board::D15::toggle(); + } + + } + + return 0; +} diff --git a/examples/nucleo_f411re/imu_bno055/project.xml b/examples/nucleo_f411re/imu_bno055/project.xml new file mode 100644 index 0000000000..146a01add6 --- /dev/null +++ b/examples/nucleo_f411re/imu_bno055/project.xml @@ -0,0 +1,15 @@ + + modm:nucleo-f411re + + + + + modm:debug + modm:driver:bno055 + modm:platform:gpio + modm:platform:i2c:1 + modm:platform:i2c.bitbang + modm:processing:protothread + modm:build:scons + +