• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef GYRO_STILLNESS_DETECT_H_
2 #define GYRO_STILLNESS_DETECT_H_
3 
4 /*
5  * Copyright (C) 2016 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 ///////////////////////////////////////////////////////////////
21 /*
22  * This module implements a sensor stillness detector with a
23  * look-ahead feature that allows the sensor's mean to be
24  * computed for an extended stillness period (one-pass, online
25  * method) sample by sample without a memory buffer. Stillness
26  * is computed using non-overlapping windows of signal variance
27  * and thresholding logic. The look-ahead feature ensures that
28  * the mean computation is not corrupted by the onset of sensor
29  * activity.
30  *
31  * NOTE - Time units are agnostic (i.e., determined by the
32  * user's application and usage). However, typical time units
33  * are nanoseconds.
34  */
35 ///////////////////////////////////////////////////////////////
36 
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <sys/types.h>
40 #include <stdio.h>
41 #include <math.h>
42 
43 struct gyroStillDet_t{
44 
45   // Variance threshold for the stillness confidence score.
46   float var_threshold; //[sensor units]^2
47 
48   // Delta about the variance threshold for calculation of the
49   // stillness confidence score [0,1].
50   float confidence_delta; //[sensor units]^2
51 
52   // Flag to indicate when enough samples have been collected for
53   // a complete stillness calculation.
54   bool stillness_window_ready;
55 
56   // Flag to signal the beginning of a new stillness detection window. This
57   // is used to keep track of the window start time.
58   bool start_new_window;
59 
60   // Starting time stamp for the the current window.
61   uint64_t window_start_time;
62 
63   // Accumulator variables for tracking the sample mean during
64   // the stillness period.
65   uint32_t num_acc_samples;
66   float mean_x, mean_y, mean_z;
67 
68   // Accumulator variables for computing the window sample mean and
69   // variance for the current window (used for stillness detection).
70   uint32_t num_acc_win_samples;
71   float win_mean_x, win_mean_y, win_mean_z;
72   float assumed_mean_x, assumed_mean_y, assumed_mean_z;
73   float acc_var_x, acc_var_y, acc_var_z;
74 
75   // Stillness period mean (used for look-ahead).
76   float prev_mean_x, prev_mean_y, prev_mean_z;
77 
78   // Latest computed variance.
79   float win_var_x, win_var_y, win_var_z;
80 
81   // Stillness confidence score for current and previous sample
82   // windows [0,1] (used for look-ahead).
83   float stillness_confidence;
84   float prev_stillness_confidence;
85 
86   // Timestamp of last sample recorded.
87   uint64_t last_sample_time;
88 
89 };
90 
91 /////// FUNCTION PROTOTYPES //////////////////////////////////////////
92 
93 // Initialize the gyro_still_det_t structure.
94 void gyroStillDetInit(struct gyroStillDet_t* gyro_still_det,
95                       float var_threshold,
96                       float confidence_delta);
97 
98 // Update the stillness detector with a new sample.
99 void gyroStillDetUpdate(struct gyroStillDet_t* gyro_still_det,
100                         uint64_t stillness_win_endtime,
101                         uint64_t sample_time,
102                         float x, float y, float z);
103 
104 // Calculates and returns the stillness confidence score [0,1].
105 float gyroStillDetCompute(struct gyroStillDet_t* gyro_still_det);
106 
107 // Resets the stillness detector and initiates a new detection window.
108 // 'reset_stats' determines whether the stillness statistics are reset.
109 void gyroStillDetReset(struct gyroStillDet_t* gyro_still_det,
110                        bool reset_stats);
111 
112 #endif // GYRO_STILLNESS_DETECT_H_
113