-
Notifications
You must be signed in to change notification settings - Fork 121
/
main.cpp
105 lines (85 loc) · 2.29 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 <modm/board.hpp>
#include <modm/driver/inertial/l3gd20.hpp>
#include <modm/processing.hpp>
#include <modm/math/filter.hpp>
// 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<UartSpi::Master, UartSpi::Cs>;
namespace
{
modm::l3gd20::Data data;
modm::L3gd20<Transport> 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<float, 10> averageX;
modm::filter::MovingAverage<float, 10> averageY;
modm::filter::MovingAverage<float, 10> averageZ;
};
ReaderThread reader;
int
main()
{
Board::initialize();
UartSpi::Master::connect<UartSpi::Ck::Ck, UartSpi::Tx::Tx, UartSpi::Rx::Rx>();
UartSpi::Master::initialize<Board::systemClock, 8'000'000, modm::Tolerance::Exact>();
while (1) {
reader.update();
}
return 0;
}