2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "sensor_frequency_compensator.h"
23 /************************************************************************
25 sensor_frequency_compensator::sensor_frequency_compensator(double desired_rate)
26 : m_desired_frequency(desired_rate)
35 /************************************************************************
37 sensor_frequency_compensator::~sensor_frequency_compensator()
41 /************************************************************************
43 void sensor_frequency_compensator::reset() {
44 m_t1 = UNKNOWN_TIMESTAMP;
45 m_t2 = UNKNOWN_TIMESTAMP;
46 m_timestamp = UNKNOWN_TIMESTAMP;
49 /************************************************************************
51 void sensor_frequency_compensator::add(timestamp_t t, double *v) {
52 if (m_timestamp == UNKNOWN_TIMESTAMP) {
56 memcpy(m_v1, m_v2, sizeof(m_v1));
58 memcpy(m_v2, v, sizeof(m_v2));
61 /************************************************************************
63 bool sensor_frequency_compensator::has_next() {
64 if (m_t1 == UNKNOWN_TIMESTAMP || m_t2 == UNKNOWN_TIMESTAMP) {
67 return m_timestamp + m_desired_frequency < m_t2;
70 /************************************************************************
72 void sensor_frequency_compensator::get_next(double *v) {
73 timestamp_t t3 = m_timestamp;
77 m_timestamp += m_desired_frequency;
78 double t = (t3 - m_t1) / ((double) (m_t2 - m_t1));
80 for (i = 0; i < 3; i++) {
81 v[i] = (1.0 - t) * m_v1[i] + t * m_v2[i];