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 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;
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
149 _D("camera - azimuth[%4d : %4d], elevation[%4d : %4d]", azimuth, val_azimuth, elevation, val_elevation);
151 resource_set_servo_motor_value(14, val_elevation);
152 resource_set_servo_motor_value(15, val_azimuth);
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);
161 case COMMAND_TYPE_CAMERA:
162 __camera(command.data.camera_position.camera_azimuth, command.data.camera_position.camera_elevation);
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);
168 case COMMAND_TYPE_NONE:
171 _E("Unknown command type");
176 static void _initialize_config()
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);
191 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
192 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
195 if (modified == true) {
202 static void _initialize_components(app_data *ad)
205 _initialize_config();
206 cloud_communication_init();
207 message_manager_init();
208 controller_connection_manager_listen();
211 static bool service_app_create(void *data)
217 * if you want to use default configuration,
218 * Do not need to call resource_set_motor_driver_L298N_configuration(),
222 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
224 _E("resource_set_motor_driver_L298N_configuration()");
227 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
229 _E("resource_set_motor_driver_L298N_configuration()");
234 _initialize_components(ad);
235 cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
237 controller_connection_manager_set_command_received_cb(__command_received_cb);
242 static void service_app_control(app_control_h app_control, void *data)
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);
255 static void service_app_terminate(void *data)
260 g_source_remove(ad->idle_h);
263 controller_connection_manager_release();
264 message_manager_shutdown();
266 cloud_communication_stop();
267 cloud_communication_fini();
271 resource_close_all();
279 int main(int argc, char* argv[])
283 service_app_lifecycle_callback_s event_callback;
284 app_event_handler_h handlers[5] = {NULL, };
286 log_type_set(LOG_TYPE_DLOG);
288 ad = calloc(1, sizeof(app_data));
291 event_callback.create = service_app_create;
292 event_callback.terminate = service_app_terminate;
293 event_callback.app_control = service_app_control;
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);
304 ret = service_app_main(argc, argv, &event_callback, ad);
306 _E("failed to start app");