-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_management.cpp
122 lines (91 loc) · 4.25 KB
/
sensor_management.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
This file is part of the SmartFilamentSensor distribution
Copyright (C) 2022 Slava Zanko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sensor_management.h"
/*** file scope macro definitions ****************************************************************/
#define FAST_MOVE_MULTIPLIER 5
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** global variables ****************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
bool SensorManagement::hasDelayedMovement()
{
return this->differenceMillisec < this->readingDelay;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void SensorManagement::init(MovementSensor *const sensor)
{
this->sensor = sensor;
this->readingDelay = 0;
this->totalEventsCount = 0;
this->averageIntervalBetweenEvents = 0;
this->hasSensorMovement = 0;
this->reset();
}
/* --------------------------------------------------------------------------------------------- */
void SensorManagement::reset()
{
this->lastMovementTime = get_absolute_time();
this->previousDifferenceMillisec = 0;
this->differenceMillisec = 0;
}
/* --------------------------------------------------------------------------------------------- */
void SensorManagement::heartbeat()
{
this->hasSensorMovement = this->sensor->hasMovement();
this->differenceMillisec = absolute_time_diff_us(this->lastMovementTime, get_absolute_time()) / 1000;
}
/* --------------------------------------------------------------------------------------------- */
bool SensorManagement::moved()
{
if (this->hasSensorMovement)
{
this->lastMovementTime = get_absolute_time();
this->previousDifferenceMillisec = this->differenceMillisec;
this->differenceMillisec = 0;
return true;
}
return this->hasDelayedMovement();
}
/* --------------------------------------------------------------------------------------------- */
void SensorManagement::calculateAverageInterval()
{
if (this->differenceMillisec != 0)
{
this->averageIntervalBetweenEvents = (this->averageIntervalBetweenEvents * this->totalEventsCount + this->differenceMillisec) /
(this->totalEventsCount + 1);
this->totalEventsCount++;
}
}
/* --------------------------------------------------------------------------------------------- */
bool SensorManagement::hasFastMovement()
{
uint fastMovementLimit = this->averageIntervalBetweenEvents / FAST_MOVE_MULTIPLIER;
return this->differenceMillisec < fastMovementLimit && this->previousDifferenceMillisec < fastMovementLimit;
}
/* --------------------------------------------------------------------------------------------- */
void SensorManagement::setReadingDelay(const uint32_t readingDelay)
{
this->readingDelay = readingDelay;
this->previousDifferenceMillisec = readingDelay;
}
/* --------------------------------------------------------------------------------------------- */
uint32_t SensorManagement::getReadingDelay()
{
return this->readingDelay;
}
/* --------------------------------------------------------------------------------------------- */