|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Kevin Läufer |
| 3 | + * Copyright (c) 2015-2018, Niklas Hauser |
| 4 | + * Copyright (c) 2024, Carl Treudler |
| 5 | + * |
| 6 | + * This file is part of the modm project. |
| 7 | + * |
| 8 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 9 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 11 | + */ |
| 12 | +// ---------------------------------------------------------------------------- |
| 13 | + |
| 14 | +#include <modm/board.hpp> |
| 15 | +#include <modm/processing.hpp> |
| 16 | +#include <modm/math/filter.hpp> |
| 17 | + |
| 18 | +using namespace Board; |
| 19 | + |
| 20 | +// create the data object |
| 21 | +Board::lsm3::Accelerometer::Data data; |
| 22 | +// and hand it to the sensor driver |
| 23 | +Board::lsm3::Accelerometer accelerometer(data); |
| 24 | + |
| 25 | + |
| 26 | +class ReaderThread : public modm::pt::Protothread |
| 27 | +{ |
| 28 | +public: |
| 29 | + bool |
| 30 | + update() |
| 31 | + { |
| 32 | + PT_BEGIN(); |
| 33 | + |
| 34 | + // initialize with limited range of ±2g |
| 35 | + PT_CALL(accelerometer.configure(accelerometer.Scale::G2)); |
| 36 | + |
| 37 | + while (true) |
| 38 | + { |
| 39 | + // read out the sensor |
| 40 | + PT_CALL(accelerometer.readAcceleration()); |
| 41 | + |
| 42 | + averageX.update(accelerometer.getData().getX()); |
| 43 | + averageY.update(accelerometer.getData().getY()); |
| 44 | + |
| 45 | + { |
| 46 | + bool xs = averageX.getValue() < -0.2f; |
| 47 | + bool xn = averageX.getValue() > 0.2f; |
| 48 | + |
| 49 | + bool xe = averageY.getValue() < -0.2f; |
| 50 | + bool xw = averageY.getValue() > 0.2f; |
| 51 | + |
| 52 | + |
| 53 | + LedBlue::set(xs); // South |
| 54 | + LedGreen::set(xw); //West |
| 55 | + LedOrange::set(xn); // North |
| 56 | + LedRed::set(xe); // East |
| 57 | + } |
| 58 | + |
| 59 | + // repeat every 5 ms |
| 60 | + timeout.restart(5ms); |
| 61 | + PT_WAIT_UNTIL(timeout.isExpired()); |
| 62 | + } |
| 63 | + |
| 64 | + PT_END(); |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + modm::ShortTimeout timeout; |
| 69 | + modm::filter::MovingAverage<float, 25> averageX; |
| 70 | + modm::filter::MovingAverage<float, 25> averageY; |
| 71 | +}; |
| 72 | + |
| 73 | +ReaderThread reader; |
| 74 | + |
| 75 | +int |
| 76 | +main() |
| 77 | +{ |
| 78 | + Board::initialize(); |
| 79 | + Board::initializeLsm3(); |
| 80 | + |
| 81 | + Leds::set(); |
| 82 | + modm::delay(42ms); |
| 83 | + |
| 84 | + modm::fiber::Scheduler::run(); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
0 commit comments