2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Jeonghoon Park <jh1979.park@samsung.com>
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
10 * http://floralicense.org/license/
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.
24 #include <service_app.h>
29 #include "cloud/cloud_communication.h"
30 #include "messages/message_manager.h"
31 #include "controller_connection_manager.h"
34 #define ENABLE_MOTOR 1
36 #define CONFIG_GRP_CAR "Car"
37 #define CONFIG_KEY_ID "Id"
38 #define CONFIG_KEY_NAME "Name"
39 #define CLOUD_REQUESTS_FREQUENCY 15
47 typedef struct app_data_s {
50 unsigned int dir_state;
54 static void _initialize_components(app_data *ad);
55 static void _initialize_config();
57 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
62 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
67 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
69 _E("low battery! exit app");
75 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
80 static inline double __map_round(double val)
82 return floor(val + 0.5);
85 static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
89 slope = 1.0 * (d_max - d_min) / (v_max - v_min);
91 rval = d_min + __map_round(slope * (val - v_min));
96 static int ___map_speed_val(int speed)
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;
103 return __map_range_val(motor_max, motor_min,
104 speed_max, speed_min, speed);
107 static int ___map_servo_val(int servo)
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;
114 return __map_range_val(motor_max, motor_min,
115 servo_max, servo_min, servo);
118 static int __driving_motors(int servo, int speed)
123 val_servo = ___map_servo_val(servo);
124 val_speed = ___map_speed_val(speed);
126 _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
127 servo, val_servo, speed, val_speed);
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);
137 static void __camera(int azimuth, int elevation)
139 //TODO: Camera steering
142 static void __command_received_cb(command_s command) {
143 switch(command.type) {
144 case COMMAND_TYPE_DRIVE:
145 __driving_motors(command.data.steering.direction, command.data.steering.speed);
147 case COMMAND_TYPE_CAMERA:
148 __camera(command.data.camera_position.camera_azimuth, command.data.camera_position.camera_elevation);
150 case COMMAND_TYPE_DRIVE_AND_CAMERA:
151 __driving_motors(command.data.steering_and_camera.direction, command.data.steering_and_camera.speed);
152 __camera(command.data.steering_and_camera.camera_azimuth, command.data.steering_and_camera.camera_elevation);
154 case COMMAND_TYPE_NONE:
157 _E("Unknown command type");
162 static void _initialize_config()
168 gboolean modified = false;
169 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
170 char *uuid = g_uuid_string_random();
171 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
175 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
176 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
179 if (modified == true) {
186 static void _initialize_components(app_data *ad)
189 _initialize_config();
190 cloud_communication_init();
191 message_manager_init();
192 controller_connection_manager_listen();
195 static bool service_app_create(void *data)
201 * if you want to use default configuration,
202 * Do not need to call resource_set_motor_driver_L298N_configuration(),
206 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
208 _E("resource_set_motor_driver_L298N_configuration()");
211 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
213 _E("resource_set_motor_driver_L298N_configuration()");
218 _initialize_components(ad);
219 cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
221 controller_connection_manager_set_command_received_cb(__command_received_cb);
226 static void service_app_control(app_control_h app_control, void *data)
230 /* set speed 0, to reduce delay of initializing motor driver */
231 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
232 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
233 resource_set_servo_motor_value(0, 450);
239 static void service_app_terminate(void *data)
244 g_source_remove(ad->idle_h);
247 controller_connection_manager_release();
248 message_manager_shutdown();
250 cloud_communication_stop();
251 cloud_communication_fini();
255 resource_close_all();
263 int main(int argc, char* argv[])
267 service_app_lifecycle_callback_s event_callback;
268 app_event_handler_h handlers[5] = {NULL, };
270 log_type_set(LOG_TYPE_DLOG);
272 ad = calloc(1, sizeof(app_data));
275 event_callback.create = service_app_create;
276 event_callback.terminate = service_app_terminate;
277 event_callback.app_control = service_app_control;
279 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
280 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
281 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
282 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
283 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
284 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
285 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
286 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
288 ret = service_app_main(argc, argv, &event_callback, ad);
290 _E("failed to start app");