Home | History | Annotate | Download | only in common_data
      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 <stdint.h>
     26 #include <string.h>
     27 #include <sys/types.h>
     28 
     29 #include "common/math/macros.h"
     30 
     31 namespace online_calibration {
     32 
     33 // Defines an invalid or uninitialized temperature value (referenced from
     34 // common/math/macros.h).
     35 constexpr float kInvalidTemperatureCelsius = INVALID_TEMPERATURE_CELSIUS;
     36 
     37 // Unit conversion from nanoseconds to microseconds.
     38 constexpr uint64_t NanoToMicroseconds(uint64_t x) { return x / 1000; }
     39 
     40 // Identifies the various sensing devices used by the calibration algorithms.
     41 enum class SensorType : int8_t {
     42   kUndefined = 0,
     43   kAccelerometerMps2 = 1,   // 3-axis sensor (units = meter/sec^2).
     44   kGyroscopeRps = 2,        // 3-axis sensor (units = radian/sec).
     45   kMagnetometerUt = 3,      // 3-axis sensor (units = micro-Tesla).
     46   kTemperatureCelsius = 4,  // 1-axis sensor (units = degrees Celsius).
     47   kBarometerHpa = 5,        // 1-axis sensor (units = hecto-Pascal).
     48   kWifiM = 6                // 3-axis sensor (units = meter).
     49 };
     50 
     51 /*
     52  * SensorData is a generalized data structure used to represent sensor samples
     53  * produced by either a single- or three-axis device. Usage is implied through
     54  * the sensor type (i.e., Gyroscope is a three-axis sensor and would therefore
     55  * use all elements of 'data'; a pressure sensor is single-dimensional and would
     56  * use 'data[SensorIndex::kSingleAxis]'). This arbitration is determined
     57  * at the algorithm wrapper level where knowledge of a sensor's dimensionality
     58  * is clearly understood.
     59  *
     60  * NOTE: The unified dimensional representation makes it convenient to pass
     61  * either type of data into the interface functions defined in the
     62  * OnlineCalibration.
     63  */
     64 
     65 // Axis index definitions for SensorData::data.
     66 enum SensorIndex : int8_t {
     67   kSingleAxis = 0,
     68   kXAxis = kSingleAxis,
     69   kYAxis = 1,
     70   kZAxis = 2,
     71 };
     72 
     73 struct SensorData {
     74   // Indicates the type of sensor this data originated from.
     75   SensorType type;
     76 
     77   // Sensor sample timestamp.
     78   uint64_t timestamp_nanos;
     79 
     80   // Generalized sensor sample (represents either single- or three-axis data).
     81   float data[3];
     82 
     83   SensorData() : type(SensorType::kUndefined), timestamp_nanos(0) {
     84     memset(data, 0, sizeof(data));
     85   }
     86 
     87   SensorData(SensorType type, uint64_t ts, float x, float y, float z)
     88       : type(type), timestamp_nanos(ts) {
     89     data[SensorIndex::kXAxis] = x;
     90     data[SensorIndex::kYAxis] = y;
     91     data[SensorIndex::kZAxis] = z;
     92   }
     93 
     94   SensorData(SensorType type, uint64_t ts, float single_axis_sample)
     95       : type(type), timestamp_nanos(ts) {
     96     data[SensorIndex::kSingleAxis] = single_axis_sample;
     97   }
     98 };
     99 
    100 }  // namespace online_calibration
    101 
    102 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_ONLINE_CALIBRATION_COMMON_DATA_SENSOR_DATA_H_
    103