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 };
51 
52 // Helper function for determining if a sensor type is 3-axis, otherwise it's
53 // single-axis.
is_three_axis(SensorType type)54 inline bool is_three_axis(SensorType type) {
55   return type == SensorType::kAccelerometerMps2 ||
56          type == SensorType::kGyroscopeRps ||
57          type == SensorType::kMagnetometerUt;
58 }
59 
60 /*
61  * SensorData is a generalized data structure used to represent sensor samples
62  * produced by either a single- or three-axis device. Usage is implied through
63  * the sensor type (i.e., Gyroscope is a three-axis sensor and would therefore
64  * use all elements of 'data'; a pressure sensor is single-dimensional and would
65  * use 'data[SensorIndex::kSingleAxis]'). This arbitration is determined
66  * at the algorithm wrapper level where knowledge of a sensor's dimensionality
67  * is clearly understood.
68  *
69  * NOTE: The unified dimensional representation makes it convenient to pass
70  * either type of data into the interface functions defined in the
71  * OnlineCalibration. Additionally, this approach, although admittedly wasteful
72  * for single-axis sensors, allows one to avoid dynamic memory allocation for
73  * hardware systems where having a fixed memory footprint is either required or
74  * extremely desireable.
75  */
76 
77 // Axis index definitions for SensorData::data.
78 enum SensorIndex : int8_t {
79   kSingleAxis = 0,
80   kXAxis = kSingleAxis,
81   kYAxis = 1,
82   kZAxis = 2,
83 };
84 
85 struct SensorData {
86   // Sensor sample timestamp.
87   uint64_t timestamp_nanos;
88 
89   // Generalized sensor sample (represents either single- or three-axis data).
90   float data[3];
91 
92   // Indicates the type of sensor this data originated from.
93   SensorType type;
94 
SensorDataSensorData95   SensorData() : timestamp_nanos(0), type(SensorType::kUndefined) {
96     memset(data, 0, sizeof(data));
97   }
98 
SensorDataSensorData99   SensorData(SensorType type, uint64_t timestamp_nanos, float x_axis,
100              float y_axis, float z_axis)
101       : timestamp_nanos(timestamp_nanos), type(type) {
102     data[SensorIndex::kXAxis] = x_axis;
103     data[SensorIndex::kYAxis] = y_axis;
104     data[SensorIndex::kZAxis] = z_axis;
105   }
106 
SensorDataSensorData107   SensorData(SensorType type, uint64_t timestamp_nanos,
108              float single_axis_sample)
109       : timestamp_nanos(timestamp_nanos), type(type) {
110     data[SensorIndex::kSingleAxis] = single_axis_sample;
111   }
112 };
113 
114 }  // namespace online_calibration
115 
116 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_
117