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
35 #define STERING_SERVO_CENTER 340
36 #define STERING_SERVO_RANGE 125
38 #define MAX_UDP_INPUT 10000
40 #define CONFIG_GRP_CAR "Car"
41 #define CONFIG_KEY_ID "Id"
42 #define CONFIG_KEY_NAME "Name"
43 #define CLOUD_REQUESTS_FREQUENCY 15
44 #define AZIMUTH_SERVO_PIN 15
45 #define ELEVATION_SERVO_PIN 14
47 #define ELEVATION_MIN 200
48 #define ELEVATION_MAX 400
49 #define AZIMUTH_MIN 200
50 #define AZIMUTH_MAX 700
58 typedef struct app_data_s {
61 unsigned int dir_state;
65 static void _initialize_components(app_data *ad);
66 static void _initialize_config();
68 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
73 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
78 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
80 _E("low battery! exit app");
86 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
91 static inline double __map_round(double val)
93 return floor(val + 0.5);
96 static int __map_range_val(int v_min, int v_max, int d_min, int d_max, int val)
100 slope = 1.0 * (d_max - d_min) / (v_max - v_min);
102 rval = d_min + __map_round(slope * (val - v_min));
107 static inline int ___map_speed_val(int speed)
109 return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, -4095, 4095, speed);
112 static inline int ___map_servo_val(int servo)
114 return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, STERING_SERVO_CENTER - STERING_SERVO_RANGE, STERING_SERVO_CENTER + STERING_SERVO_RANGE, servo);
117 static int __driving_motors(int servo, int speed)
122 val_servo = ___map_servo_val(servo);
123 val_speed = ___map_speed_val(speed);
125 _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
126 servo, val_servo, speed, val_speed);
128 resource_set_servo_motor_value(0, val_servo);
129 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, val_speed);
130 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, val_speed);
136 static void __camera(int azimuth, int elevation)
138 int val_azimuth = __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, AZIMUTH_MIN, AZIMUTH_MAX, azimuth);
139 int val_elevation = __map_range_val(0, MAX_UDP_INPUT, ELEVATION_MIN, ELEVATION_MAX, elevation); // No need to look upside down
141 _D("camera - azimuth[%4d : %4d], elevation[%4d : %4d]", azimuth, val_azimuth, elevation, val_elevation);
143 resource_set_servo_motor_value(ELEVATION_SERVO_PIN, val_elevation);
144 resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, val_azimuth);
148 static void __command_received_cb(command_s command) {
149 switch(command.type) {
150 case COMMAND_TYPE_DRIVE:
151 __driving_motors(command.data.steering.direction, command.data.steering.speed);
153 case COMMAND_TYPE_CAMERA:
154 __camera(command.data.camera_position.camera_azimuth, command.data.camera_position.camera_elevation);
156 case COMMAND_TYPE_DRIVE_AND_CAMERA:
157 __driving_motors(command.data.steering_and_camera.direction, command.data.steering_and_camera.speed);
158 __camera(command.data.steering_and_camera.camera_azimuth, command.data.steering_and_camera.camera_elevation);
160 case COMMAND_TYPE_NONE:
163 _E("Unknown command type");
168 static void _initialize_config()
176 gboolean modified = false;
177 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
178 char *uuid = g_uuid_string_random();
179 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
183 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
184 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
187 if (modified == true) {
194 static void _initialize_components(app_data *ad)
197 _initialize_config();
198 cloud_communication_init();
199 message_manager_init();
200 controller_connection_manager_listen();
203 static bool service_app_create(void *data)
209 * if you want to use default configuration,
210 * Do not need to call resource_set_motor_driver_L298N_configuration(),
214 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
216 _E("resource_set_motor_driver_L298N_configuration()");
219 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
221 _E("resource_set_motor_driver_L298N_configuration()");
226 _initialize_components(ad);
227 cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
229 controller_connection_manager_set_command_received_cb(__command_received_cb);
234 static void service_app_control(app_control_h app_control, void *data)
238 /* set speed 0, to reduce delay of initializing motor driver */
239 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
240 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
241 resource_set_servo_motor_value(0, 450);
242 resource_set_servo_motor_value(ELEVATION_SERVO_PIN, ELEVATION_MIN);
243 resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
249 static void service_app_terminate(void *data)
253 resource_set_servo_motor_value(0, STERING_SERVO_CENTER);
254 resource_set_servo_motor_value(ELEVATION_SERVO_PIN, ELEVATION_MIN);
255 resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
258 g_source_remove(ad->idle_h);
261 controller_connection_manager_release();
262 message_manager_shutdown();
264 cloud_communication_stop();
265 cloud_communication_fini();
269 resource_close_all();
277 int main(int argc, char* argv[])
281 service_app_lifecycle_callback_s event_callback;
282 app_event_handler_h handlers[5] = {NULL, };
284 log_type_set(LOG_TYPE_DLOG);
286 ad = calloc(1, sizeof(app_data));
289 event_callback.create = service_app_create;
290 event_callback.terminate = service_app_terminate;
291 event_callback.app_control = service_app_control;
293 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
294 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
295 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
296 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
297 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
298 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
299 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
300 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
302 ret = service_app_main(argc, argv, &event_callback, ad);
304 _E("failed to start app");