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