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 "zero_crossing_step_detection.h"
19 static bool detect_zero_crossing(double last_accel, double new_accel, bool up)
22 return (last_accel < 0 && new_accel >= 0);
24 return (last_accel > 0 && new_accel <= 0);
27 zero_crossing_step_detection::zero_crossing_step_detection(bool up)
29 , m_last_acceleration(0)
30 , m_last_timestamp(UNKNOWN_TIMESTAMP)
31 , m_last_zero_crossing_time(UNKNOWN_TIMESTAMP)
36 zero_crossing_step_detection::~zero_crossing_step_detection()
40 bool zero_crossing_step_detection::detect_step(timestamp_t timestamp, double acceleration)
42 bool step_detected = false;
44 if (m_last_timestamp != UNKNOWN_TIMESTAMP) {
45 // zero crossing detected
46 if (detect_zero_crossing(m_last_acceleration, acceleration, m_up)) {
47 m_time_sum += timestamp - m_last_timestamp;
48 m_last_timestamp = timestamp;
49 m_last_zero_crossing_time = timestamp;
53 m_last_timestamp = timestamp;
55 m_last_acceleration = acceleration;
59 void zero_crossing_step_detection::clr_time_sum() {
63 double zero_crossing_step_detection::get_time_sum() {
64 return m_time_sum / 1E9;
67 void zero_crossing_step_detection::reset(void)
69 m_last_acceleration = 0;
71 m_last_timestamp = UNKNOWN_TIMESTAMP;
72 m_last_zero_crossing_time = UNKNOWN_TIMESTAMP;