summaryrefslogtreecommitdiff
path: root/src/model/model_sensors.c
blob: 1f335e653a95c4f8d760ffd8dc2d7fe303d00188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Flora License, Version 1.1 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <sensor.h>
#include "gear-racing-controller.h"
#include "model/model_sensors.h"
#include "log.h"

#define VALUE_LIST_SIZE 10

typedef struct _s_data_history {
	int current_index;
	float sum;
	float array[VALUE_LIST_SIZE];
} s_data_history;


typedef struct _s_model_sensors {
	t_model_sensors_update_cb sensors_update_cb;

	sensor_h sensor;
	sensor_listener_h listener;

	float initial_sensor_data[3];
	s_data_history velocity;
	s_data_history direction;
} s_model_sensors;

static s_model_sensors s_info = {
		.initial_sensor_data = { 0, },
		.direction = { 0, },
};

static inline float _vector_length(float *vector, int count)
{
	float len = 0;
	int i = 0;

	for (i = 0; i < count; ++i) {
		len += vector[i] * vector[i];
	}

	return sqrtf(len);
}

static inline void _vector_diff(float *vector_1, float *vector_2, float *vector_out, int count)
{
	int i;
	for (i = 0; i < count; ++i) {
		vector_out[i] = vector_1[i] - vector_2[i];
	}
}

static inline float _range_map(float value, float input_min, float input_max, float output_min, float output_max)
{
	float result =  ((value - input_min) / (input_max - input_min)) * (output_max - output_min) + output_min;

	if (result < output_min) {
		result = output_min;
	} else if (result > output_max) {
		result = output_max;
	}

	return result;
}

static void _update_data_history(float value, s_data_history *parameter)
{
	parameter->sum = parameter->sum - parameter->array[parameter->current_index] + value;
	parameter->array[parameter->current_index] = value;
	++parameter->current_index;
	parameter->current_index %= VALUE_LIST_SIZE;
}

static inline float _get_average_parameter_from_history(float value, s_data_history *parameter)
{
	_update_data_history(value, parameter);
	return parameter->sum / VALUE_LIST_SIZE;
}

static void _sensor_event_cb(sensor_h sensor, sensor_event_s *event, void *data)
{
	static s_model_sensors_cb_data model_data = {
			.type = MODEL_TYPE_UPDATE,
	};

	float vector[3] = { 0,};
	float len = 0;

	_vector_diff(event->values, s_info.initial_sensor_data, &vector[0], 3);
	len = _vector_length(&vector[0], 3);

	float direction = vector[0];
	float velocity  = vector[1];

	direction = _get_average_parameter_from_history(direction, &s_info.direction);

	model_data.direction = _range_map(direction, -8.00f, 8.00f, -1.0f, 1.0f);
	model_data.velocity  = velocity;

	_D("MODEL VALUES{%f}: dir:% .4f ranged:% .4f", len, direction, model_data.direction);

	if (s_info.sensors_update_cb) {
		s_info.sensors_update_cb(&model_data);
	}
}

void model_sensors_set_initial_values(void)
{
	sensor_event_s event = { 0, };

	int ret = sensor_listener_read_data(s_info.listener, &event);
	ASSERT_FUNCTION(ret != SENSOR_ERROR_NONE);

	memcpy(s_info.initial_sensor_data, event.values, sizeof(s_info.initial_sensor_data));

	_D("Initial sensor data: {%f, %f, %f}", s_info.initial_sensor_data[0], s_info.initial_sensor_data[1],
			s_info.initial_sensor_data[2]);
}

void model_sensors_init(void)
{
	bool supported = false;

	int ret = sensor_is_supported(SENSOR_ACCELEROMETER, &supported);
	ASSERT_FUNCTION(ret != SENSOR_ERROR_NONE);
	ASSERT(!supported, "Sensor %d is not supported", SENSOR_ACCELEROMETER)

	ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &s_info.sensor);
	ASSERT_FUNCTION(ret != SENSOR_ERROR_NONE);

	ret = sensor_create_listener(s_info.sensor, &s_info.listener);
	ASSERT_FUNCTION(ret != SENSOR_ERROR_NONE);

	ret = sensor_listener_set_event_cb(s_info.listener, 1000, _sensor_event_cb, NULL);
	ASSERT_FUNCTION(ret != SENSOR_ERROR_NONE);

	sensor_listener_start(s_info.listener);

	model_sensors_set_initial_values();
}

void model_sensors_subscribe_event(t_model_sensors_update_cb model_update_cb)
{
	s_info.sensors_update_cb = model_update_cb;
}

void model_sensors_unsubscirbe_event(void)
{
	s_info.sensors_update_cb = NULL;
}