Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "device/generic_sensor/platform_sensor_fusion_relative_orientation.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <cmath> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "device/generic_sensor/generic_sensor_consts.h" | |
| 15 #include "device/generic_sensor/platform_sensor_provider.h" | |
| 16 #include "device/generic_sensor/public/cpp/sensor_shared_buffer_reader.h" | |
| 17 #include "device/generic_sensor/relative_orientation_fusion_algorithm_using_acce lerometer.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 constexpr double kQuaternionThreshold = 0.01; | |
|
Mikhail
2017/06/20 13:50:00
where did this value come from?
juncai
2017/06/20 19:50:55
The quaternion is a unit quaternion so I thought 0
timvolodine
2017/06/21 18:22:11
There should be some rational explanation for this
juncai
2017/07/20 00:22:16
In the new CL:
https://chromium-review.googlesourc
| |
| 22 | |
| 23 bool IsSignificantlyDifferent(const device::SensorReading& reading1, | |
| 24 const device::SensorReading& reading2) { | |
| 25 return (std::fabs(reading1.values[0] - reading2.values[0]) >= | |
| 26 kQuaternionThreshold) || | |
| 27 (std::fabs(reading1.values[1] - reading2.values[1]) >= | |
| 28 kQuaternionThreshold) || | |
| 29 (std::fabs(reading1.values[2] - reading2.values[2]) >= | |
| 30 kQuaternionThreshold); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 namespace device { | |
| 36 | |
| 37 PlatformSensorFusionRelativeOrientation:: | |
| 38 PlatformSensorFusionRelativeOrientation( | |
| 39 mojo::ScopedSharedBufferMapping mapping, | |
| 40 PlatformSensorProvider* provider, | |
| 41 const PlatformSensorProviderBase::CreateSensorCallback& callback) | |
| 42 : PlatformSensor(mojom::SensorType::RELATIVE_ORIENTATION, | |
| 43 std::move(mapping), | |
| 44 provider), | |
| 45 callback_(callback) { | |
| 46 provider->CreateSensor( | |
|
Mikhail
2017/06/20 13:50:00
think it's worth first look for existing sensors u
juncai
2017/06/20 19:50:55
Done.
| |
| 47 mojom::SensorType::ACCELEROMETER, | |
| 48 base::Bind( | |
| 49 &PlatformSensorFusionRelativeOrientation::CreateAccelerometerCallback, | |
| 50 base::Unretained(this))); | |
| 51 } | |
| 52 | |
| 53 PlatformSensorFusionRelativeOrientation:: | |
| 54 ~PlatformSensorFusionRelativeOrientation() { | |
| 55 if (accelerometer_) | |
| 56 accelerometer_->RemoveClient(this); | |
| 57 } | |
| 58 | |
| 59 mojom::ReportingMode | |
| 60 PlatformSensorFusionRelativeOrientation::GetReportingMode() { | |
| 61 return accelerometer_->GetReportingMode(); | |
| 62 } | |
| 63 | |
| 64 PlatformSensorConfiguration | |
| 65 PlatformSensorFusionRelativeOrientation::GetDefaultConfiguration() { | |
| 66 return PlatformSensorConfiguration(kDefaultAccelerometerFrequencyHz); | |
|
Mikhail
2017/06/20 13:50:00
why not use accelerometer_->GetDefaultConfiguratio
juncai
2017/06/20 19:50:55
Done.
| |
| 67 } | |
| 68 | |
| 69 bool PlatformSensorFusionRelativeOrientation::StartSensor( | |
| 70 const PlatformSensorConfiguration& configuration) { | |
| 71 if (!accelerometer_->StartSensor(configuration)) | |
| 72 return false; | |
| 73 | |
| 74 accelerometer_reader_ = base::MakeUnique<SensorSharedBufferReader>( | |
|
Mikhail
2017/06/20 13:50:00
instead of introducing SensorSharedBufferReader I'
juncai
2017/06/20 19:50:55
Done.
| |
| 75 static_cast<const SensorReadingSharedBuffer*>( | |
| 76 accelerometer_->shared_buffer_mapping().get())); | |
| 77 | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 void PlatformSensorFusionRelativeOrientation::StopSensor() { | |
| 82 accelerometer_->StopSensor(); | |
| 83 } | |
| 84 | |
| 85 void PlatformSensorFusionRelativeOrientation::OnSensorReadingChanged() { | |
| 86 SensorReading reading; | |
| 87 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
| 88 | |
| 89 if (!accelerometer_reader_->GetSensorReading(&reading)) | |
| 90 return; | |
| 91 | |
| 92 fusion_algorithm_->set_acceleration(reading.values[0], reading.values[1], | |
| 93 reading.values[2]); | |
| 94 | |
| 95 fusion_algorithm_->GetRelativeOrientationData( | |
| 96 &reading.values[0].value(), &reading.values[1].value(), | |
| 97 &reading.values[2].value(), &reading.values[3].value()); | |
| 98 | |
| 99 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE && | |
| 100 !IsSignificantlyDifferent(reading_, reading)) { | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 reading_ = reading; | |
| 105 UpdateSensorReading(reading_, | |
| 106 GetReportingMode() == mojom::ReportingMode::ON_CHANGE); | |
| 107 } | |
| 108 | |
| 109 void PlatformSensorFusionRelativeOrientation::OnSensorError() { | |
| 110 NotifySensorError(); | |
| 111 } | |
| 112 | |
| 113 bool PlatformSensorFusionRelativeOrientation::IsNotificationSuspended() { | |
|
Mikhail
2017/06/20 13:50:00
think here the suspend status of its own clients s
juncai
2017/06/20 19:50:55
Done.
| |
| 114 return false; | |
| 115 } | |
| 116 | |
| 117 bool PlatformSensorFusionRelativeOrientation::CheckSensorConfiguration( | |
| 118 const PlatformSensorConfiguration& configuration) { | |
| 119 return configuration.frequency() > 0 && | |
|
Mikhail
2017/06/20 13:50:00
why not forward this call to the contained "accele
juncai
2017/06/20 19:50:55
Done.
| |
| 120 configuration.frequency() <= | |
| 121 mojom::SensorConfiguration::kMaxAllowedFrequency; | |
| 122 } | |
| 123 | |
| 124 void PlatformSensorFusionRelativeOrientation::CreateAccelerometerCallback( | |
| 125 scoped_refptr<PlatformSensor> accelerometer) { | |
| 126 accelerometer_ = accelerometer; | |
| 127 if (accelerometer_) { | |
| 128 accelerometer_->AddClient(this); | |
| 129 fusion_algorithm_ = base::MakeUnique< | |
| 130 RelativeOrientationFusionAlgorithmUsingAccelerometer>(); | |
| 131 callback_.Run(scoped_refptr<PlatformSensor>(this)); | |
| 132 } else { | |
| 133 callback_.Run(nullptr); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 } // namespace device | |
| OLD | NEW |