platform_sensor_fusion_algorithm.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_FUSION_ALGORITHM_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_FUSION_ALGORITHM_H_
  6. #include <vector>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
  10. namespace device {
  11. class PlatformSensorFusion;
  12. // Base class for platform sensor fusion algorithm.
  13. class PlatformSensorFusionAlgorithm {
  14. public:
  15. PlatformSensorFusionAlgorithm(const PlatformSensorFusionAlgorithm&) = delete;
  16. PlatformSensorFusionAlgorithm& operator=(
  17. const PlatformSensorFusionAlgorithm&) = delete;
  18. virtual ~PlatformSensorFusionAlgorithm();
  19. void set_threshold(double threshold) { threshold_ = threshold; }
  20. double threshold() const { return threshold_; }
  21. void set_fusion_sensor(PlatformSensorFusion* fusion_sensor) {
  22. fusion_sensor_ = fusion_sensor;
  23. }
  24. const std::vector<mojom::SensorType>& source_types() const {
  25. return source_types_;
  26. }
  27. mojom::SensorType fused_type() const { return fused_type_; }
  28. bool GetFusedData(mojom::SensorType which_sensor_changed,
  29. SensorReading* fused_reading);
  30. // Sets frequency at which data is expected to be obtained from the platform.
  31. // This might be needed e.g., to calculate cut-off frequency of low/high-pass
  32. // filters.
  33. virtual void SetFrequency(double frequency);
  34. // Algorithms that use statistical data (moving average, Kalman filter, etc),
  35. // might need to be reset when sensor is stopped.
  36. virtual void Reset();
  37. protected:
  38. PlatformSensorFusionAlgorithm(
  39. mojom::SensorType fused_type,
  40. const std::vector<mojom::SensorType>& source_types);
  41. virtual bool GetFusedDataInternal(mojom::SensorType which_sensor_changed,
  42. SensorReading* fused_reading) = 0;
  43. // This raw pointer is safe because |fusion_sensor_| owns this object.
  44. raw_ptr<PlatformSensorFusion> fusion_sensor_ = nullptr;
  45. private:
  46. // Default threshold for comparing SensorReading values. If a
  47. // different threshold is better for a certain sensor type, set_threshold()
  48. // should be used to change it.
  49. double threshold_ = 0.1;
  50. mojom::SensorType fused_type_;
  51. std::vector<mojom::SensorType> source_types_;
  52. };
  53. } // namespace device
  54. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_FUSION_ALGORITHM_H_