01d8c7341a770a57713af32e4262b54af363b4d7
[apps/native/gear-racing-car.git] / src / app.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jeonghoon Park <jh1979.park@samsung.com>
5  *
6  * Licensed under the Flora License, Version 1.1 (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
9  *
10  * http://floralicense.org/license/
11  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <math.h>
23 #include <glib.h>
24 #include <service_app.h>
25 #include "log.h"
26 #include "resource.h"
27 #include "net-util.h"
28 #include "config.h"
29 #include "cloud/cloud_communication.h"
30 #include "messages/message_manager.h"
31 #include "controller_connection_manager.h"
32 #include "command.h"
33
34 #define ENABLE_MOTOR 1
35
36 #define CONFIG_GRP_CAR "Car"
37 #define CONFIG_KEY_ID "Id"
38 #define CONFIG_KEY_NAME "Name"
39 #define CLOUD_REQUESTS_FREQUENCY 15
40
41 enum {
42         DIR_STATE_S,
43         DIR_STATE_F,
44         DIR_STATE_B,
45 };
46
47 typedef struct app_data_s {
48         unsigned int f_value;
49         unsigned int r_value;
50         unsigned int dir_state;
51         guint idle_h;
52 } app_data;
53
54 static void _initialize_components(app_data *ad);
55 static void _initialize_config();
56
57 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
58 {
59         return;
60 }
61
62 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
63 {
64         return;
65 }
66
67 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
68 {
69         _E("low battery! exit app");
70         service_app_exit();
71
72         return;
73 }
74
75 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
76 {
77         return;
78 }
79
80 static inline double __map_round(double val)
81 {
82         return floor(val + 0.5);
83 }
84
85 static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
86 {
87         int rval = 0;
88         double slope = 0;
89         slope = 1.0 * (d_max - d_min) / (v_max - v_min);
90
91         rval = d_min + __map_round(slope * (val - v_min));
92
93         return rval;
94 }
95
96 static int ___map_speed_val(int speed)
97 {
98         static const int motor_max = 4095;
99         static const int motor_min = -4095;
100         static const int speed_max = 1000;
101         static const int speed_min = -1000;
102
103         return __map_range_val(motor_max, motor_min,
104                 speed_max, speed_min, speed);
105 }
106
107 static int ___map_servo_val(int servo)
108 {
109         static const int motor_max = 500;
110         static const int motor_min = 400;
111         static const int servo_max = 1000;
112         static const int servo_min = -1000;
113
114         return __map_range_val(motor_max, motor_min,
115                 servo_max, servo_min, servo);
116 }
117
118 static int __driving_motors(int servo, int speed)
119 {
120         int val_speed;
121         int val_servo;
122
123         val_servo = ___map_servo_val(servo);
124         val_speed = ___map_speed_val(speed);
125
126         _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
127                 servo, val_servo, speed, val_speed);
128 #if ENABLE_MOTOR
129         resource_set_servo_motor_value(0, val_servo);
130         resource_set_motor_driver_L298N_speed(MOTOR_ID_1, val_speed);
131         resource_set_motor_driver_L298N_speed(MOTOR_ID_2, val_speed);
132 #endif
133
134         return 0;
135 }
136
137 static void __camera(int azimuth, int elevation)
138 {
139         static const int camer_input_min = -1000;
140         static const int camer_input_max = 1000;
141         static const int camer_azimuth_min = 200;
142         static const int camer_azimuth_max = 700;
143         static const int camer_elevation_min = 200;
144         static const int camer_elevation_max = 400;
145
146         int val_azimuth = __map_range_val(camer_input_min, camer_input_max, camer_azimuth_min, camer_azimuth_max, azimuth);
147         int val_elevation = __map_range_val(0, camer_input_max, camer_elevation_min, camer_elevation_max, elevation); // No need to look upside down
148
149         _D("camera - azimuth[%4d : %4d], elevation[%4d : %4d]", azimuth, val_azimuth, elevation, val_elevation);
150 #if ENABLE_MOTOR
151         resource_set_servo_motor_value(14, val_elevation);
152         resource_set_servo_motor_value(15, val_azimuth);
153 #endif
154 }
155
156 static void __command_received_cb(command_s command) {
157         switch(command.type) {
158         case COMMAND_TYPE_DRIVE:
159                 __driving_motors(command.data.steering.direction, command.data.steering.speed);
160                 break;
161         case COMMAND_TYPE_CAMERA:
162                 __camera(command.data.camera_position.camera_azimuth, command.data.camera_position.camera_elevation);
163                 break;
164         case COMMAND_TYPE_DRIVE_AND_CAMERA:
165                 __driving_motors(command.data.steering_and_camera.direction, command.data.steering_and_camera.speed);
166                 __camera(command.data.steering_and_camera.camera_azimuth, command.data.steering_and_camera.camera_elevation);
167                 break;
168         case COMMAND_TYPE_NONE:
169                 break;
170         default:
171                 _E("Unknown command type");
172                 break;
173         }
174 }
175
176 static void _initialize_config()
177 {
178         net_util_init();
179
180         config_init();
181
182         char *id = NULL;
183         char *name = NULL;
184         gboolean modified = false;
185         if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
186                 char *uuid = g_uuid_string_random();
187                 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
188                 g_free(uuid);
189                 modified = true;
190         }
191         if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
192                 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
193                 modified = true;
194         }
195         if (modified == true) {
196                 config_save();
197         }
198         free(id);
199         free(name);
200 }
201
202 static void _initialize_components(app_data *ad)
203 {
204         net_util_init();
205         _initialize_config();
206         cloud_communication_init();
207         message_manager_init();
208         controller_connection_manager_listen();
209 }
210
211 static bool service_app_create(void *data)
212 {
213         int ret = 0;
214         app_data *ad = data;
215
216         /*
217          * if you want to use default configuration,
218          * Do not need to call resource_set_motor_driver_L298N_configuration(),
219          *
220         */
221 #if ENABLE_MOTOR
222         ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
223         if (ret) {
224                 _E("resource_set_motor_driver_L298N_configuration()");
225                 service_app_exit();
226         }
227         ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
228         if (ret) {
229                 _E("resource_set_motor_driver_L298N_configuration()");
230                 service_app_exit();
231         }
232 #endif
233
234         _initialize_components(ad);
235         cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
236
237         controller_connection_manager_set_command_received_cb(__command_received_cb);
238
239         return true;
240 }
241
242 static void service_app_control(app_control_h app_control, void *data)
243 {
244
245 #if ENABLE_MOTOR
246         /* set speed 0, to reduce delay of initializing motor driver */
247         resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
248         resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
249         resource_set_servo_motor_value(0, 450);
250 #endif
251
252         return;
253 }
254
255 static void service_app_terminate(void *data)
256 {
257         app_data *ad = data;
258
259         if (ad->idle_h)
260                 g_source_remove(ad->idle_h);
261
262
263         controller_connection_manager_release();
264         message_manager_shutdown();
265
266         cloud_communication_stop();
267         cloud_communication_fini();
268         config_shutdown();
269         net_util_fini();
270
271         resource_close_all();
272         log_file_close();
273
274         _D("Bye ~");
275
276         return;
277 }
278
279 int main(int argc, char* argv[])
280 {
281         app_data *ad = NULL;
282         int ret = 0;
283         service_app_lifecycle_callback_s event_callback;
284         app_event_handler_h handlers[5] = {NULL, };
285
286         log_type_set(LOG_TYPE_DLOG);
287
288         ad = calloc(1, sizeof(app_data));
289         retv_if(!ad, -1);
290
291         event_callback.create = service_app_create;
292         event_callback.terminate = service_app_terminate;
293         event_callback.app_control = service_app_control;
294
295         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
296                 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
297         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
298                 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
299         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
300                 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
301         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
302                 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
303
304         ret = service_app_main(argc, argv, &event_callback, ad);
305         if (ret)
306                 _E("failed to start app");
307
308         return 0;
309 }