4 * Copyright (c) 2014 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_log.h>
29 #include <orientation_sensor.h>
30 #include <sensor_loader.h>
31 #include <orientation_filter.h>
32 #include <virtual_sensor_config.h>
37 #define SENSOR_NAME "ORIENTATION_SENSOR"
38 #define SENSOR_TYPE_ORIENTATION "ORIENTATION"
40 #define INITIAL_VALUE -1
43 #define MIN_DELIVERY_DIFF_FACTOR 0.75f
46 #define AZIMUTH_OFFSET_DEGREES 360
47 #define AZIMUTH_OFFSET_RADIANS (2 * PI)
49 #define ELEMENT_NAME "NAME"
50 #define ELEMENT_VENDOR "VENDOR"
51 #define ELEMENT_RAW_DATA_UNIT "RAW_DATA_UNIT"
52 #define ELEMENT_DEFAULT_SAMPLING_TIME "DEFAULT_SAMPLING_TIME"
53 #define ELEMENT_PITCH_ROTATION_COMPENSATION "PITCH_ROTATION_COMPENSATION"
54 #define ELEMENT_ROLL_ROTATION_COMPENSATION "ROLL_ROTATION_COMPENSATION"
55 #define ELEMENT_AZIMUTH_ROTATION_COMPENSATION "AZIMUTH_ROTATION_COMPENSATION"
57 orientation_sensor::orientation_sensor()
58 : m_accel_sensor(NULL)
60 , m_magnetic_sensor(NULL)
61 , m_fusion_sensor(NULL)
64 virtual_sensor_config &config = virtual_sensor_config::get_instance();
66 sensor_hal *fusion_sensor_hal = sensor_loader::get_instance().get_sensor_hal(SENSOR_HAL_TYPE_FUSION);
67 if (!fusion_sensor_hal)
68 m_hardware_fusion = false;
70 m_hardware_fusion = true;
72 m_name = string(SENSOR_NAME);
73 register_supported_event(ORIENTATION_RAW_DATA_EVENT);
75 if (!config.get(SENSOR_TYPE_ORIENTATION, ELEMENT_VENDOR, m_vendor)) {
76 _E("[VENDOR] is empty\n");
80 _I("m_vendor = %s", m_vendor.c_str());
82 if (!config.get(SENSOR_TYPE_ORIENTATION, ELEMENT_RAW_DATA_UNIT, m_raw_data_unit)) {
83 _E("[RAW_DATA_UNIT] is empty\n");
87 _I("m_raw_data_unit = %s", m_raw_data_unit.c_str());
89 if (!config.get(SENSOR_TYPE_ORIENTATION, ELEMENT_DEFAULT_SAMPLING_TIME, &m_default_sampling_time)) {
90 _E("[DEFAULT_SAMPLING_TIME] is empty\n");
94 _I("m_default_sampling_time = %d", m_default_sampling_time);
96 if (!config.get(SENSOR_TYPE_ORIENTATION, ELEMENT_AZIMUTH_ROTATION_COMPENSATION, &m_azimuth_rotation_compensation)) {
97 _E("[AZIMUTH_ROTATION_COMPENSATION] is empty\n");
101 _I("m_azimuth_rotation_compensation = %d", m_azimuth_rotation_compensation);
103 if (!config.get(SENSOR_TYPE_ORIENTATION, ELEMENT_PITCH_ROTATION_COMPENSATION, &m_pitch_rotation_compensation)) {
104 _E("[PITCH_ROTATION_COMPENSATION] is empty\n");
108 _I("m_pitch_rotation_compensation = %d", m_pitch_rotation_compensation);
110 if (!config.get(SENSOR_TYPE_ORIENTATION, ELEMENT_ROLL_ROTATION_COMPENSATION, &m_roll_rotation_compensation)) {
111 _E("[ROLL_ROTATION_COMPENSATION] is empty\n");
115 _I("m_roll_rotation_compensation = %d", m_roll_rotation_compensation);
117 m_interval = m_default_sampling_time * MS_TO_US;
120 orientation_sensor::~orientation_sensor()
122 _I("orientation_sensor is destroyed!\n");
125 bool orientation_sensor::init(void)
127 m_accel_sensor = sensor_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
128 m_gyro_sensor = sensor_loader::get_instance().get_sensor(GYROSCOPE_SENSOR);
129 m_magnetic_sensor = sensor_loader::get_instance().get_sensor(GEOMAGNETIC_SENSOR);
131 m_fusion_sensor = sensor_loader::get_instance().get_sensor(FUSION_SENSOR);
133 if (!m_accel_sensor || !m_gyro_sensor || !m_magnetic_sensor || !m_fusion_sensor) {
134 _E("Failed to load sensors, accel: %#x, gyro: %#x, mag: %#x, fusion: %#x",
135 m_accel_sensor, m_gyro_sensor, m_magnetic_sensor, m_fusion_sensor);
139 _I("%s is created!\n", sensor_base::get_name());
144 void orientation_sensor::get_types(vector<sensor_type_t> &types)
146 types.push_back(ORIENTATION_SENSOR);
149 bool orientation_sensor::on_start(void)
153 if (!m_hardware_fusion) {
154 m_accel_sensor->add_client(ACCELEROMETER_RAW_DATA_EVENT);
155 m_accel_sensor->add_interval((intptr_t)this, (m_interval/MS_TO_US), false);
156 m_accel_sensor->start();
157 m_gyro_sensor->add_client(GYROSCOPE_RAW_DATA_EVENT);
158 m_gyro_sensor->add_interval((intptr_t)this, (m_interval/MS_TO_US), false);
159 m_gyro_sensor->start();
160 m_magnetic_sensor->add_client(GEOMAGNETIC_RAW_DATA_EVENT);
161 m_magnetic_sensor->add_interval((intptr_t)this, (m_interval/MS_TO_US), false);
162 m_magnetic_sensor->start();
165 m_fusion_sensor->register_supported_event(FUSION_EVENT);
166 m_fusion_sensor->register_supported_event(FUSION_ORIENTATION_ENABLED);
167 m_fusion_sensor->add_client(FUSION_EVENT);
168 m_fusion_sensor->add_interval((intptr_t)this, (m_interval/MS_TO_US), false);
169 m_fusion_sensor->start();
175 bool orientation_sensor::on_stop(void)
179 if (!m_hardware_fusion) {
180 m_accel_sensor->delete_client(ACCELEROMETER_RAW_DATA_EVENT);
181 m_accel_sensor->delete_interval((intptr_t)this, false);
182 m_accel_sensor->stop();
183 m_gyro_sensor->delete_client(GYROSCOPE_RAW_DATA_EVENT);
184 m_gyro_sensor->delete_interval((intptr_t)this, false);
185 m_gyro_sensor->stop();
186 m_magnetic_sensor->delete_client(GEOMAGNETIC_RAW_DATA_EVENT);
187 m_magnetic_sensor->delete_interval((intptr_t)this, false);
188 m_magnetic_sensor->stop();
191 m_fusion_sensor->delete_client(FUSION_EVENT);
192 m_fusion_sensor->delete_interval((intptr_t)this, false);
193 m_fusion_sensor->unregister_supported_event(FUSION_EVENT);
194 m_fusion_sensor->unregister_supported_event(FUSION_ORIENTATION_ENABLED);
195 m_fusion_sensor->stop();
201 bool orientation_sensor::add_interval(int client_id, unsigned int interval)
205 if (!m_hardware_fusion) {
206 m_accel_sensor->add_interval(client_id, interval, false);
207 m_gyro_sensor->add_interval(client_id, interval, false);
208 m_magnetic_sensor->add_interval(client_id, interval, false);
211 m_fusion_sensor->add_interval(client_id, interval, false);
213 return sensor_base::add_interval(client_id, interval, false);
216 bool orientation_sensor::delete_interval(int client_id)
220 if (!m_hardware_fusion) {
221 m_accel_sensor->delete_interval(client_id, false);
222 m_gyro_sensor->delete_interval(client_id, false);
223 m_magnetic_sensor->delete_interval(client_id, false);
226 m_fusion_sensor->delete_interval(client_id, false);
228 return sensor_base::delete_interval(client_id, false);
231 void orientation_sensor::synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs)
233 sensor_event_t orientation_event;
234 unsigned long long diff_time;
235 float azimuth_offset;
237 if (event.event_type == FUSION_EVENT) {
238 diff_time = event.data.timestamp - m_time;
240 if (m_time && (diff_time < m_interval * MIN_DELIVERY_DIFF_FACTOR))
243 quaternion<float> quat(event.data.values[0], event.data.values[1],
244 event.data.values[2], event.data.values[3]);
246 euler_angles<float> euler = quat2euler(quat);
248 if(m_raw_data_unit == "DEGREES") {
249 euler = rad2deg(euler);
250 azimuth_offset = AZIMUTH_OFFSET_DEGREES;
252 azimuth_offset = AZIMUTH_OFFSET_RADIANS;
255 euler.m_ang.m_vec[0] *= m_pitch_rotation_compensation;
256 euler.m_ang.m_vec[1] *= m_roll_rotation_compensation;
257 euler.m_ang.m_vec[2] *= m_azimuth_rotation_compensation;
259 m_time = get_timestamp();
260 orientation_event.sensor_id = get_id();
261 orientation_event.event_type = ORIENTATION_RAW_DATA_EVENT;
262 orientation_event.data.accuracy = event.data.accuracy;
263 orientation_event.data.timestamp = m_time;
264 orientation_event.data.value_count = 3;
265 orientation_event.data.values[1] = euler.m_ang.m_vec[0];
266 orientation_event.data.values[2] = euler.m_ang.m_vec[1];
267 if (euler.m_ang.m_vec[2] >= 0)
268 orientation_event.data.values[0] = euler.m_ang.m_vec[2];
270 orientation_event.data.values[0] = euler.m_ang.m_vec[2] + azimuth_offset;
272 push(orientation_event);
278 int orientation_sensor::get_sensor_data(const unsigned int event_type, sensor_data_t &data)
280 sensor_data_t fusion_data;
281 float azimuth_offset;
283 if (event_type != ORIENTATION_RAW_DATA_EVENT)
286 m_fusion_sensor->get_sensor_data(FUSION_ORIENTATION_ENABLED, fusion_data);
288 quaternion<float> quat(fusion_data.values[0], fusion_data.values[1],
289 fusion_data.values[2], fusion_data.values[3]);
291 euler_angles<float> euler = quat2euler(quat);
293 if(m_raw_data_unit == "DEGREES") {
294 euler = rad2deg(euler);
295 azimuth_offset = AZIMUTH_OFFSET_DEGREES;
297 azimuth_offset = AZIMUTH_OFFSET_RADIANS;
300 data.accuracy = fusion_data.accuracy;
301 data.timestamp = get_timestamp();
302 data.value_count = 3;
303 data.values[1] = euler.m_ang.m_vec[0];
304 data.values[2] = euler.m_ang.m_vec[1];
305 if (euler.m_ang.m_vec[2] >= 0)
306 data.values[0] = euler.m_ang.m_vec[2];
308 data.values[0] = euler.m_ang.m_vec[2] + azimuth_offset;
310 data.values[1] *= m_pitch_rotation_compensation;
311 data.values[2] *= m_roll_rotation_compensation;
312 data.values[0] *= m_azimuth_rotation_compensation;
317 bool orientation_sensor::get_properties(sensor_type_t sensor_type, sensor_properties_s &properties)
319 if(m_raw_data_unit == "DEGREES") {
320 properties.min_range = -180;
321 properties.max_range = 360;
323 properties.min_range = -PI;
324 properties.max_range = 2 * PI;
326 properties.resolution = 0.000001;
327 properties.vendor = m_vendor;
328 properties.name = SENSOR_NAME;
329 properties.min_interval = 1;
330 properties.fifo_count = 0;
331 properties.max_batch_count = 0;