From 544f6d3cb2f423aa353fb4ec40e12cb465a135b0 Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Sat, 10 Nov 2018 20:45:17 +0100 Subject: [PATCH] [stm32] Add example for L3GD20 gyroscope on NUCLEO-L432KC --- examples/nucleo_l432kc/gyroscope/main.cpp | 105 +++++++++++++++++++ examples/nucleo_l432kc/gyroscope/project.xml | 14 +++ 2 files changed, 119 insertions(+) create mode 100644 examples/nucleo_l432kc/gyroscope/main.cpp create mode 100644 examples/nucleo_l432kc/gyroscope/project.xml diff --git a/examples/nucleo_l432kc/gyroscope/main.cpp b/examples/nucleo_l432kc/gyroscope/main.cpp new file mode 100644 index 0000000000..846b3f8610 --- /dev/null +++ b/examples/nucleo_l432kc/gyroscope/main.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2014-2018, Niklas Hauser + * Copyright (c) 2015, Kevin Läufer + * Copyright (c) 2015, Martin Esser + * Copyright (c) 2018, Christopher Durand + * + * 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 + +// Example for L3gd20 gyroscope connected to SPI USART interface + +struct UartSpi +{ + using Ck = GpioA8; + using Tx = GpioA9; + using Rx = GpioA10; + using Cs = GpioA11; + + using Master = UartSpiMaster1; +}; + +using Transport = modm::Lis3TransportSpi; + +namespace +{ + modm::l3gd20::Data data; + modm::L3gd20 gyro{data}; +} + +class ReaderThread : public modm::pt::Protothread +{ +public: + bool + update() + { + PT_BEGIN(); + + // ping the device until it responds + while(true) + { + // we wait until the task started + if (PT_CALL(gyro.ping())) + break; + // otherwise, try again in 100ms + this->timeout.restart(100); + Board::LedD13::set(); + PT_WAIT_UNTIL(this->timeout.isExpired()); + Board::LedD13::reset(); + } + + PT_CALL(gyro.configure(gyro.Scale::Dps250, gyro.MeasurementRate::Hz380)); + + while (true) + { + PT_CALL(gyro.readRotation()); + + averageX.update(gyro.getData().getX()); + averageY.update(gyro.getData().getY()); + averageZ.update(gyro.getData().getZ()); + + MODM_LOG_INFO.printf("x: %.2f, y: %.2f, z: %.2f \n", + averageX.getValue(), + averageY.getValue(), + averageZ.getValue()); + + this->timeout.restart(50); + PT_WAIT_UNTIL(this->timeout.isExpired()); + } + + PT_END(); + } + +private: + modm::ShortTimeout timeout; + modm::filter::MovingAverage averageX; + modm::filter::MovingAverage averageY; + modm::filter::MovingAverage averageZ; +}; + +ReaderThread reader; + +int +main() +{ + Board::initialize(); + + UartSpi::Master::connect(); + UartSpi::Master::initialize(); + + while (1) { + reader.update(); + } + + return 0; +} diff --git a/examples/nucleo_l432kc/gyroscope/project.xml b/examples/nucleo_l432kc/gyroscope/project.xml new file mode 100644 index 0000000000..67de897f31 --- /dev/null +++ b/examples/nucleo_l432kc/gyroscope/project.xml @@ -0,0 +1,14 @@ + + modm:board:nucleo-l432kc + + + + + :driver:l3gd20 + :math:filter + :platform:uart.spi:1 + :processing:timer + :processing:protothread + :build:scons + +