4 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <sys/types.h>
28 #include <sensor_logs.h>
29 #include <gaming_rv_sensor.h>
30 #include <sensor_loader.h>
31 #include <orientation_filter.h>
32 #include <virtual_sensor_config.h>
37 #define SENSOR_NAME "GAMING_RV_SENSOR"
38 #define SENSOR_TYPE_GAMING_RV "GAMING_ROTATION_VECTOR"
40 #define ACCELEROMETER_ENABLED 0x01
41 #define GYROSCOPE_ENABLED 0x02
42 #define GAMING_RV_ENABLED 3
44 #define INITIAL_VALUE -1
48 #define ELEMENT_NAME "NAME"
49 #define ELEMENT_VENDOR "VENDOR"
50 #define ELEMENT_RAW_DATA_UNIT "RAW_DATA_UNIT"
51 #define ELEMENT_DEFAULT_SAMPLING_TIME "DEFAULT_SAMPLING_TIME"
52 #define ELEMENT_ACCEL_STATIC_BIAS "ACCEL_STATIC_BIAS"
53 #define ELEMENT_GYRO_STATIC_BIAS "GYRO_STATIC_BIAS"
54 #define ELEMENT_ACCEL_ROTATION_DIRECTION_COMPENSATION "ACCEL_ROTATION_DIRECTION_COMPENSATION"
55 #define ELEMENT_GYRO_ROTATION_DIRECTION_COMPENSATION "GYRO_ROTATION_DIRECTION_COMPENSATION"
56 #define ELEMENT_ACCEL_SCALE "ACCEL_SCALE"
57 #define ELEMENT_GYRO_SCALE "GYRO_SCALE"
59 gaming_rv_sensor::gaming_rv_sensor()
60 : m_accel_sensor(NULL)
65 virtual_sensor_config &config = virtual_sensor_config::get_instance();
67 sensor_hal *fusion_sensor_hal = sensor_loader::get_instance().get_sensor_hal(SENSOR_HAL_TYPE_FUSION);
68 if (!fusion_sensor_hal)
69 m_hardware_fusion = false;
71 m_hardware_fusion = true;
73 INFO("m_hardware_fusion = %d", m_hardware_fusion);
75 m_name = string(SENSOR_NAME);
76 register_supported_event(GAMING_RV_RAW_DATA_EVENT);
77 m_enable_gaming_rv = 0;
79 if (!config.get(SENSOR_TYPE_GAMING_RV, ELEMENT_VENDOR, m_vendor)) {
80 ERR("[VENDOR] is empty\n");
84 INFO("m_vendor = %s", m_vendor.c_str());
86 if (!config.get(SENSOR_TYPE_GAMING_RV, ELEMENT_DEFAULT_SAMPLING_TIME, &m_default_sampling_time)) {
87 ERR("[DEFAULT_SAMPLING_TIME] is empty\n");
91 INFO("m_default_sampling_time = %d", m_default_sampling_time);
93 if (!config.get(SENSOR_TYPE_GAMING_RV, ELEMENT_ACCEL_STATIC_BIAS, m_accel_static_bias, 3)) {
94 ERR("[ACCEL_STATIC_BIAS] is empty\n");
98 INFO("m_accel_static_bias = (%f, %f, %f)", m_accel_static_bias[0], m_accel_static_bias[1], m_accel_static_bias[2]);
100 if (!config.get(SENSOR_TYPE_GAMING_RV, ELEMENT_GYRO_STATIC_BIAS, m_gyro_static_bias,3)) {
101 ERR("[GYRO_STATIC_BIAS] is empty\n");
105 INFO("m_gyro_static_bias = (%f, %f, %f)", m_gyro_static_bias[0], m_gyro_static_bias[1], m_gyro_static_bias[2]);
107 if (!config.get(SENSOR_TYPE_GAMING_RV, ELEMENT_ACCEL_ROTATION_DIRECTION_COMPENSATION, m_accel_rotation_direction_compensation, 3)) {
108 ERR("[ACCEL_ROTATION_DIRECTION_COMPENSATION] is empty\n");
112 INFO("m_accel_rotation_direction_compensation = (%d, %d, %d)", m_accel_rotation_direction_compensation[0], m_accel_rotation_direction_compensation[1], m_accel_rotation_direction_compensation[2]);
114 if (!config.get(SENSOR_TYPE_GAMING_RV, ELEMENT_GYRO_ROTATION_DIRECTION_COMPENSATION, m_gyro_rotation_direction_compensation, 3)) {
115 ERR("[GYRO_ROTATION_DIRECTION_COMPENSATION] is empty\n");
119 INFO("m_gyro_rotation_direction_compensation = (%d, %d, %d)", m_gyro_rotation_direction_compensation[0], m_gyro_rotation_direction_compensation[1], m_gyro_rotation_direction_compensation[2]);
121 m_interval = m_default_sampling_time * MS_TO_US;
124 gaming_rv_sensor::~gaming_rv_sensor()
126 INFO("gaming_rv_sensor is destroyed!\n");
129 bool gaming_rv_sensor::init()
131 m_accel_sensor = sensor_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
132 m_gyro_sensor = sensor_loader::get_instance().get_sensor(GYROSCOPE_SENSOR);
134 if (!m_accel_sensor || !m_gyro_sensor) {
135 ERR("Failed to load sensors, accel: 0x%x, gyro: 0x%x",
136 m_accel_sensor, m_gyro_sensor);
140 INFO("%s is created!\n", sensor_base::get_name());
145 void gaming_rv_sensor::get_types(vector<sensor_type_t> &types)
147 types.push_back(GAMING_RV_SENSOR);
150 bool gaming_rv_sensor::on_start(void)
154 if (!m_hardware_fusion) {
155 m_accel_sensor->add_client(ACCELEROMETER_RAW_DATA_EVENT);
156 m_accel_sensor->add_interval((intptr_t)this, (m_interval/MS_TO_US), false);
157 m_accel_sensor->start();
158 m_gyro_sensor->add_client(GYROSCOPE_RAW_DATA_EVENT);
159 m_gyro_sensor->add_interval((intptr_t)this, (m_interval/MS_TO_US), false);
160 m_gyro_sensor->start();
167 bool gaming_rv_sensor::on_stop(void)
171 if (!m_hardware_fusion) {
172 m_accel_sensor->delete_client(ACCELEROMETER_RAW_DATA_EVENT);
173 m_accel_sensor->delete_interval((intptr_t)this, false);
174 m_accel_sensor->stop();
175 m_gyro_sensor->delete_client(GYROSCOPE_RAW_DATA_EVENT);
176 m_gyro_sensor->delete_interval((intptr_t)this, false);
177 m_gyro_sensor->stop();
184 bool gaming_rv_sensor::add_interval(int client_id, unsigned int interval)
188 if (!m_hardware_fusion) {
189 m_accel_sensor->add_interval(client_id, interval, false);
190 m_gyro_sensor->add_interval(client_id, interval, false);
193 return sensor_base::add_interval(client_id, interval, false);
196 bool gaming_rv_sensor::delete_interval(int client_id)
200 if (!m_hardware_fusion) {
201 m_accel_sensor->delete_interval(client_id, false);
202 m_gyro_sensor->delete_interval(client_id, false);
205 return sensor_base::delete_interval(client_id, false);
208 void gaming_rv_sensor::synthesize(const sensor_event_t& event, vector<sensor_event_t> &outs)
210 const float MIN_DELIVERY_DIFF_FACTOR = 0.75f;
211 unsigned long long diff_time;
213 sensor_event_t rv_event;
214 quaternion<float> quaternion_gaming_rv;
216 if (event.event_type == ACCELEROMETER_RAW_DATA_EVENT) {
217 diff_time = event.data.timestamp - m_time;
219 if (m_time && (diff_time < m_interval * MIN_DELIVERY_DIFF_FACTOR))
222 pre_process_data(m_accel, event.data.values, m_accel_static_bias, m_accel_rotation_direction_compensation, ACCEL_SCALE);
224 m_accel.m_time_stamp = event.data.timestamp;
226 m_enable_gaming_rv |= ACCELEROMETER_ENABLED;
227 } else if (event.event_type == GYROSCOPE_RAW_DATA_EVENT) {
228 diff_time = event.data.timestamp - m_time;
230 if (m_time && (diff_time < m_interval * MIN_DELIVERY_DIFF_FACTOR))
233 pre_process_data(m_gyro, event.data.values, m_gyro_static_bias, m_gyro_rotation_direction_compensation, GYRO_SCALE);
235 m_gyro.m_time_stamp = event.data.timestamp;
237 m_enable_gaming_rv |= GYROSCOPE_ENABLED;
240 if (m_enable_gaming_rv == GAMING_RV_ENABLED) {
241 m_enable_gaming_rv = 0;
243 m_orientation_filter.get_device_orientation(&m_accel, &m_gyro, NULL);
245 quaternion_gaming_rv = m_orientation_filter.m_quat_gaming_rv;
247 m_time = get_timestamp();
248 rv_event.sensor_id = get_id();
249 rv_event.event_type = GAMING_RV_RAW_DATA_EVENT;
250 rv_event.data.accuracy = SENSOR_ACCURACY_GOOD;
251 rv_event.data.timestamp = m_time;
252 rv_event.data.value_count = 4;
253 rv_event.data.values[0] = quaternion_gaming_rv.m_quat.m_vec[1];
254 rv_event.data.values[1] = quaternion_gaming_rv.m_quat.m_vec[2];
255 rv_event.data.values[2] = quaternion_gaming_rv.m_quat.m_vec[3];
256 rv_event.data.values[3] = quaternion_gaming_rv.m_quat.m_vec[0];
264 int gaming_rv_sensor::get_sensor_data(unsigned int event_type, sensor_data_t &data)
266 sensor_data<float> accel;
267 sensor_data<float> gyro;
269 sensor_data_t accel_data;
270 sensor_data_t gyro_data;
272 quaternion<float> quaternion_gaming_rv;
274 if (event_type != GAMING_RV_RAW_DATA_EVENT)
277 m_accel_sensor->get_sensor_data(ACCELEROMETER_RAW_DATA_EVENT, accel_data);
278 m_gyro_sensor->get_sensor_data(GYROSCOPE_RAW_DATA_EVENT, gyro_data);
280 pre_process_data(accel, accel_data.values, m_accel_static_bias, m_accel_rotation_direction_compensation, ACCEL_SCALE);
281 pre_process_data(gyro, gyro_data.values, m_gyro_static_bias, m_gyro_rotation_direction_compensation, GYRO_SCALE);
282 accel.m_time_stamp = accel_data.timestamp;
283 gyro.m_time_stamp = gyro_data.timestamp;
285 m_orientation_filter_poll.get_device_orientation(&m_accel, &m_gyro, NULL);
287 quaternion_gaming_rv = m_orientation_filter_poll.m_quat_gaming_rv;
289 data.accuracy = SENSOR_ACCURACY_GOOD;
290 data.timestamp = get_timestamp();
291 data.value_count = 4;
292 data.values[0] = quaternion_gaming_rv.m_quat.m_vec[1];
293 data.values[1] = quaternion_gaming_rv.m_quat.m_vec[2];
294 data.values[2] = quaternion_gaming_rv.m_quat.m_vec[3];
295 data.values[3] = quaternion_gaming_rv.m_quat.m_vec[0];
300 bool gaming_rv_sensor::get_properties(sensor_type_t sensor_type, sensor_properties_s &properties)
302 properties.vendor = m_vendor;
303 properties.name = SENSOR_NAME;
304 properties.min_range = -1;
305 properties.max_range = 1;
306 properties.resolution = 0.000001;
307 properties.fifo_count = 0;
308 properties.max_batch_count = 0;
309 properties.min_interval = 1;