From 754b1ad3492a0df9282e39365555daa6ffcd40cd Mon Sep 17 00:00:00 2001 From: Hai Shang Date: Thu, 11 Aug 2022 13:05:34 -0700 Subject: [PATCH] Check in sensor fusion interface PiperOrigin-RevId: 467029545 --- presence/device_motion.h | 1 + presence/implementation/BUILD | 12 ++ presence/implementation/sensor_fusion.h | 145 ++++++++++++++++++++++++ presence/presence_zone.h | 6 +- presence/presence_zone_test.cc | 2 +- 5 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 presence/implementation/sensor_fusion.h diff --git a/presence/device_motion.h b/presence/device_motion.h index 6a41dbf1..439b5349 100644 --- a/presence/device_motion.h +++ b/presence/device_motion.h @@ -21,6 +21,7 @@ class DeviceMotion { public: enum class MotionType { kPointAndHold = 0, + kStationaryAndHold = 1, }; DeviceMotion(MotionType motion_type = MotionType::kPointAndHold, float confidence = 0) noexcept; diff --git a/presence/implementation/BUILD b/presence/implementation/BUILD index 8d56f2f6..47c96c69 100644 --- a/presence/implementation/BUILD +++ b/presence/implementation/BUILD @@ -63,6 +63,18 @@ cc_library( ], ) +cc_library( + name = "sensor_fusion", + hdrs = ["sensor_fusion.h"], + visibility = [ + "//presence:__subpackages__", + ], + deps = [ + "//presence:types", + "@com_google_absl//absl/types:optional", + ], +) + cc_test( name = "advertisement_decoder_test", size = "small", diff --git a/presence/implementation/sensor_fusion.h b/presence/implementation/sensor_fusion.h new file mode 100644 index 00000000..587100e8 --- /dev/null +++ b/presence/implementation/sensor_fusion.h @@ -0,0 +1,145 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SENSOR_FUSION_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SENSOR_FUSION_H_ + +#include +#include +#include + +#include "absl/types/optional.h" +#include "presence/device_motion.h" +#include "presence/presence_zone.h" + +namespace nearby { +namespace presence { + +enum class DataSource { + kUnknown = 0, + kBle = 1, + kUwb = 2, + kNanRtt = 4, +}; + +struct RangingMeasurement { + // [0.0, 1.0], 1.0 is the max confidence. + float confidence_level; + float value; +}; + +struct RangingPosition { + RangingMeasurement distance; + absl::optional azimuth; + absl::optional elevation; + uint64_t elapsed_realtime_millis; +}; + +struct ZoneTransition { + PresenceZone::DistanceBoundary::RangeType distance_range_type; + float confidence_level; +}; + +struct RangingData { + std::vector data_sources; + RangingPosition position; + absl::optional zone_transition; + std::vector device_motions; +}; + +class SensorFusion { + public: + virtual ~SensorFusion() = default; + + // Called when the proximity zone to a nearby peer device has changed. + typedef std::function + ZoneTransitionCallback; + + // Called when a device motion gesture is detected. + typedef std::function + DeviceMotionCallback; + + /** + * Returns a list of data sources would be used by the sensor fusion if they + * are available. + * This is to control what kinds of sources the NP scan engine should use for + * ranging. For instance, if both NAN RTT and UWB are supported, FPP may + * decide NAN RTT isn't useful at a certain moment, so NP scan engine won't + * try to request NAN RTT. + * + * @param elapsed_realtime_millis Elapsed timestamp since boot of the data + * source query. + * @param available_sources A bit mask of data sources that are available. + */ + virtual std::vector getDataSources( + uint64_t elapsed_realtime_millis, + std::vector available_sources); + + /** + * Updates BLE scanned results to Sensor Fusion. + * + * @param device_id A unique device id of the peer device. + * @param txPower Calibrated TX power of the scan result, {@code + * absl::nullopt} if the calibrated TX power is not available. + * @param rssi Received signal strength indicator for the scan result. + * @param elapsed_realtime_millis Elapsed timestamp since boot when the + * scan result is discovered. + */ + virtual void updateBleScanResult(std::string device_id, + absl::optional txPower, int rssi, + uint64_t elapsed_realtime_millis); + + /** + * Updates UWB ranging results to Sensor Fusion. + * + * @param device_id A unique device id of the peer device. + * @param position UWB ranging result (distance and optionally angle) + */ + virtual void updateUwbRangingResult(std::string device_id, + RangingPosition position); + + /** + * Adds callback for updates of proximity zone transitions. + */ + virtual void requestZoneTransitionUpdates(ZoneTransitionCallback callback); + + /** + * Removes callback for updates of proximity zone transitions. + */ + virtual void removeZoneTransitionUpdates(ZoneTransitionCallback callback); + + /** + * Adds callback for updates of device motion events. + */ + virtual void requestDeviceMotionUpdates(DeviceMotionCallback callback); + + /** + * Remove callback for updates of device motion events. + */ + virtual void removeDeviceMotionUpdates(DeviceMotionCallback callback); + + /** + * Returns the best ranging estimate to a given device. Returns {@code + * absl::nullopt} if the sensor fusion cannot produce a ranging estimate. + * + * @param device_id Id of the peer device. + */ + virtual absl::optional getRangingData(std::string device_id); +}; + +} // namespace presence +} // namespace nearby +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SENSOR_FUSION_H_ diff --git a/presence/presence_zone.h b/presence/presence_zone.h index dd65e939..c2883100 100644 --- a/presence/presence_zone.h +++ b/presence/presence_zone.h @@ -26,11 +26,7 @@ class PresenceZone { public: enum class RangeType { kRangeUnknown = 0, - kFar, // Distance is very far away from the peer device. - kLong, // Distance is relatively long from the peer device, typically a - // few meters. - kClose, // Distance is close to the peer device, typically with one or - // two meter. + kFar, // Distance is very far away from the peer device. kWithinReach, // Distance is very close to the peer device, typically // within one meter or less. kWithinTap, // Distance is within tap range to the peer device, typically diff --git a/presence/presence_zone_test.cc b/presence/presence_zone_test.cc index 588f0a86..e813bf48 100644 --- a/presence/presence_zone_test.cc +++ b/presence/presence_zone_test.cc @@ -39,7 +39,7 @@ static const float kTestMaxAngleDegrees = 20; static const float kTestConfidence = 0.1; static const RangeType kDefaultRangeType = RangeType::kRangeUnknown; -static const RangeType kTestRangeType = RangeType::kClose; +static const RangeType kTestRangeType = RangeType::kFar; static const DistanceBoundary kDefaultDistanceBoundary; static const DistanceBoundary kTestDistanceBoundary = {