1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * This module provides the component definitions used to represent sensor
19  * data employed by the online sensor calibration algorithms.
20  */
21 
22 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_
23 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_
24 
25 #include <sys/types.h>
26 
27 #include <cstdint>
28 #include <cstring>
29 
30 #include "common/math/macros.h"
31 
32 namespace online_calibration {
33 
34 // Defines an invalid or uninitialized temperature value (referenced from
35 // common/math/macros.h).
36 constexpr float kInvalidTemperatureCelsius = INVALID_TEMPERATURE_CELSIUS;
37 
38 // Unit conversion from nanoseconds to microseconds.
NanoToMicroseconds(uint64_t x)39 constexpr uint64_t NanoToMicroseconds(uint64_t x) { return x / 1000; }
40 
41 // Identifies the various sensing devices used by the calibration algorithms.
42 enum class SensorType : int8_t {
43   kUndefined = 0,
44   kAccelerometerMps2 = 1,   // 3-axis sensor (units = meter/sec^2).
45   kGyroscopeRps = 2,        // 3-axis sensor (units = radian/sec).
46   kMagnetometerUt = 3,      // 3-axis sensor (units = micro-Tesla).
47   kTemperatureCelsius = 4,  // 1-axis sensor (units = degrees Celsius).
48   kBarometerHpa = 5,        // 1-axis sensor (units = hecto-Pascal).
49   kWifiM = 6,               // 3-axis sensor (units = meter).
50   kProximity = 7,           // 1-axis sensor (units = ?).
51 };
52 
53 // Helper function for determining if a sensor type is 3-axis, otherwise it's
54 // single-axis.
is_three_axis(SensorType type)55 inline bool is_three_axis(SensorType type) {
56   return type == SensorType::kAccelerometerMps2 ||
57          type == SensorType::kGyroscopeRps ||
58          type == SensorType::kMagnetometerUt;
59 }
60 
61 /*
62  * SensorData is a generalized data structure used to represent sensor samples
63  * produced by either a single- or three-axis device. Usage is implied through
64  * the sensor type (i.e., Gyroscope is a three-axis sensor and would therefore
65  * use all elements of 'data'; a pressure sensor is single-dimensional and would
66  * use 'data[SensorIndex::kSingleAxis]'). This arbitration is determined
67  * at the algorithm wrapper level where knowledge of a sensor's dimensionality
68  * is clearly understood.
69  *
70  * NOTE: The unified dimensional representation makes it convenient to pass
71  * either type of data into the interface functions defined in the
72  * OnlineCalibration. Additionally, this approach, although admittedly wasteful
73  * for single-axis sensors, allows one to avoid dynamic memory allocation for
74  * hardware systems where having a fixed memory footprint is either required or
75  * extremely desireable.
76  */
77 
78 // Axis index definitions for SensorData::data.
79 enum SensorIndex : int8_t {
80   kSingleAxis = 0,
81   kXAxis = kSingleAxis,
82   kYAxis = 1,
83   kZAxis = 2,
84 };
85 
86 struct SensorData {
87   // Sensor sample timestamp.
88   uint64_t timestamp_nanos;
89 
90   // Generalized sensor sample (represents either single- or three-axis data).
91   float data[3];
92 
93   // Indicates the type of sensor this data originated from.
94   SensorType type;
95 
SensorDataSensorData96   SensorData() : timestamp_nanos(0), type(SensorType::kUndefined) {
97     memset(data, 0, sizeof(data));
98   }
99 
SensorDataSensorData100   SensorData(SensorType type, uint64_t timestamp_nanos, float x_axis,
101              float y_axis, float z_axis)
102       : timestamp_nanos(timestamp_nanos), type(type) {
103     data[SensorIndex::kXAxis] = x_axis;
104     data[SensorIndex::kYAxis] = y_axis;
105     data[SensorIndex::kZAxis] = z_axis;
106   }
107 
SensorDataSensorData108   SensorData(SensorType type, uint64_t timestamp_nanos,
109              float single_axis_sample)
110       : timestamp_nanos(timestamp_nanos), type(type) {
111     data[SensorIndex::kSingleAxis] = single_axis_sample;
112   }
113 };
114 
115 }  // namespace online_calibration
116 
117 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_
118