Integrated controller connection manager
[apps/native/gear-racing-car.git] / src / app.c
index 5783915..f6d813f 100644 (file)
--- a/src/app.c
+++ b/src/app.c
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
+#include <math.h>
 #include <glib.h>
 #include <service_app.h>
 #include "log.h"
-#include "dc_motor.h"
 #include "resource.h"
+#include "receiver.h"
+#include "message.h"
+#include "connection_manager.h"
+#include "net-util.h"
+#include "config.h"
+#include "cloud/cloud_communication.h"
+#include "messages/message_manager.h"
+#include "controller_connection_manager.h"
+#include "command.h"
+
+#define ENABLE_MOTOR 1
+
+#define CONFIG_GRP_CAR "Car"
+#define CONFIG_KEY_ID "Id"
+#define CONFIG_KEY_NAME "Name"
+#define CLOUD_REQUESTS_FREQUENCY 15
 
 enum {
        DIR_STATE_S,
@@ -34,10 +51,11 @@ typedef struct app_data_s {
        unsigned int f_value;
        unsigned int r_value;
        unsigned int dir_state;
+       guint idle_h;
 } app_data;
 
-#define FRONT_PIN 21
-#define REAR_PIN 4
+static void _initialize_components(app_data *ad);
+static void _initialize_config();
 
 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
 {
@@ -51,6 +69,9 @@ static void service_app_region_changed(app_event_info_h event_info, void *user_d
 
 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
 {
+       _E("low battery! exit app");
+       service_app_exit();
+
        return;
 }
 
@@ -59,106 +80,213 @@ static void service_app_low_memory(app_event_info_h event_info, void *user_data)
        return;
 }
 
-static void ___________control_motor(app_data *ad)
+static inline double __map_round(double val)
 {
-       _D("control motor, state(%u), f_val(%u), r_val(%u)",
-                       ad->dir_state, ad->f_value, ad->r_value);
+       return floor(val + 0.5);
+}
 
+static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
+{
+       int rval = 0;
+       double slope = 0;
+       slope = 1.0 * (d_max - d_min) / (v_max - v_min);
 
-       switch (ad->dir_state) {
-               case DIR_STATE_F:
-                       if (ad->f_value)  {
-                               if (ad->r_value) {
-                                       ad->dir_state = DIR_STATE_S;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, 0);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, 0);
-                               } else {
-                                       ad->dir_state = DIR_STATE_B;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, -2000);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, -2000);
-                               }
-                       }
-                       break;
-               case DIR_STATE_B:
-                       if (ad->r_value)  {
-                               if (ad->f_value) {
-                                       ad->dir_state = DIR_STATE_S;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, 0);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, 0);
-                               } else {
-                                       ad->dir_state = DIR_STATE_F;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, 2000);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, 2000);
-                               }
-                       }
-                       break;
-               case DIR_STATE_S:
-                       if (!ad->f_value)  {
-                               ad->dir_state = DIR_STATE_F;
-                               dc_motor_speed_set(DC_MOTOR_ID_L, 2000);
-                               dc_motor_speed_set(DC_MOTOR_ID_R, 2000);
-                       } else if (!ad->r_value) {
-                               ad->dir_state = DIR_STATE_B;
-                               dc_motor_speed_set(DC_MOTOR_ID_L, -2000);
-                               dc_motor_speed_set(DC_MOTOR_ID_R, -2000);
-                       }
-                       break;
-       }
+       rval = d_min + __map_round(slope * (val - v_min));
 
-       return;
+       return rval;
 }
 
-static void _front_ioa_sensor_changed_cb(unsigned int value, void *data)
+static int ___map_speed_val(int speed)
 {
-       app_data *ad = data;
+       static const int motor_max = 4095;
+       static const int motor_min = -4095;
+       static const int speed_max = 1000;
+       static const int speed_min = -1000;
 
-       _D("FRONT has obstacle!");
+       return __map_range_val(motor_max, motor_min,
+               speed_max, speed_min, speed);
+}
 
-       ad->f_value = value;
+static int ___map_servo_val(int servo)
+{
+       static const int motor_max = 500;
+       static const int motor_min = 400;
+       static const int servo_max = 1000;
+       static const int servo_min = -1000;
 
-       ___________control_motor(ad);
+       return __map_range_val(motor_max, motor_min,
+               servo_max, servo_min, servo);
+}
 
-       return;
+
+static int __driving_motors(int servo, int speed)
+{
+       int val_speed;
+       int val_servo;
+
+       val_servo = ___map_servo_val(servo);
+       val_speed = ___map_speed_val(speed);
+
+       _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
+               servo, val_servo, speed, val_speed);
+#if ENABLE_MOTOR
+       resource_set_servo_motor_value(0, val_servo);
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, val_speed);
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, val_speed);
+#endif
+
+       return 0;
 }
 
-static void _back_ioa_sensor_changed_cb(unsigned int value, void *data)
+static gboolean __message_dispatcher(gpointer user_data)
 {
-       app_data *ad = data;
+       message_s *msg = NULL;
+
+       do {
+               msg = message_pop_from_inqueue();
+               if (msg) {
+                       switch (msg->cmd) {
+                       case MESSAGE_CMD_HELLO:
+                               /* TODO : say hello to sender */
+                               break;
+                       case MESSAGE_CMD_CALIBRATION:
+                               /* TODO : set calibration mode */
+                               break;
+                       case MESSAGE_CMD_DRIVE:
+                               /* TODO : driving car */
+                               __driving_motors(msg->servo, msg->speed);
+                               break;
+                       case MESSAGE_CMD_BYE:
+                               __driving_motors(0, 0);
+                               break;
+                       }
+               }
+               free(msg);
+       } while (msg);
+
+       return TRUE;
+}
+
+static void __camera(int azimuth, int elevation)
+{
+       //TODO: Camera steering
+}
+
+static void __command_received_cb(command_s command) {
+       switch(command.type) {
+       case COMMAND_TYPE_DRIVE:
+               __driving_motors(command.data.steering.direction, command.data.steering.speed);
+               break;
+       case COMMAND_TYPE_CAMERA:
+               __camera(command.data.camera_position.camera_azimuth, command.data.camera_position.camera_elevation);
+               break;
+       case COMMAND_TYPE_DRIVE_AND_CAMERA:
+               __driving_motors(command.data.steering_and_camera.direction, command.data.steering_and_camera.speed);
+               __camera(command.data.steering_and_camera.camera_azimuth, command.data.steering_and_camera.camera_elevation);
+               break;
+       case COMMAND_TYPE_NONE:
+               break;
+       default:
+               _E("Unknown command type");
+               break;
+       }
+}
+
+static void __conn_state_changed_cb(connection_state_e state,
+       const char *ip, void* user_data)
+{
+       app_data *ad = user_data;
+
+       _D("connection state changed : %d", state);
 
-       _D("BACK has obstacle!");
+       if (state == CONNECTION_STATE_CONNECTED) {
+               receiver_start(RECEIVER_TYPE_UDP);
 
-       ad->r_value = value;
+       } else {
+               receiver_stop(RECEIVER_TYPE_UDP);
 
-       ___________control_motor(ad);
+               if (ad->idle_h) {
+                       g_source_remove(ad->idle_h);
+                       ad->idle_h = 0;
+               }
 
+               __driving_motors(0, 0);
+       }
        return;
 }
 
+static void _initialize_config()
+{
+       config_init();
+       char *id = NULL;
+       char *name = NULL;
+       gboolean modified = false;
+       if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
+               char *uuid = g_uuid_string_random();
+               config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
+               g_free(uuid);
+               modified = true;
+       }
+       if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
+               config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
+               modified = true;
+       }
+       if (modified == true) {
+               config_save();
+       }
+       free(id);
+       free(name);
+}
+
+static void _initialize_components(app_data *ad)
+{
+       net_util_init();
+       _initialize_config();
+       cloud_communication_init();
+       message_manager_init();
+       controller_connection_manager_listen();
+}
+
 static bool service_app_create(void *data)
 {
        int ret = 0;
+       app_data *ad = data;
 
-       ret = dc_motor_init();
+       /*
+        * if you want to use default configuration,
+        * Do not need to call resource_set_motor_driver_L298N_configuration(),
+        *
+       */
+#if ENABLE_MOTOR
+       ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
+       if (ret) {
+               _E("resource_set_motor_driver_L298N_configuration()");
+               service_app_exit();
+       }
+       ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
        if (ret) {
-               _E("failed init motor, terminating this application");
+               _E("resource_set_motor_driver_L298N_configuration()");
                service_app_exit();
        }
+#endif
+
+       _initialize_components(ad);
+       cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
+
+       controller_connection_manager_set_command_received_cb(__command_received_cb);
 
        return true;
 }
 
 static void service_app_control(app_control_h app_control, void *data)
 {
-       app_data *ad = data;
-       int ret;
 
-       resource_read_infrared_obstacle_avoidance_sensor(FRONT_PIN, &ad->f_value);
-       resource_read_infrared_obstacle_avoidance_sensor(REAR_PIN, &ad->r_value);
-
-       resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(FRONT_PIN, _front_ioa_sensor_changed_cb, ad);
-       resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(REAR_PIN, _back_ioa_sensor_changed_cb, ad);
-
-       ___________control_motor(ad);
+#if ENABLE_MOTOR
+       /* set speed 0, to reduce delay of initializing motor driver */
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
+       resource_set_servo_motor_value(0, 450);
+#endif
 
        return;
 }
@@ -167,7 +295,19 @@ static void service_app_terminate(void *data)
 {
        app_data *ad = data;
 
-       dc_motor_fini();
+       if (ad->idle_h)
+               g_source_remove(ad->idle_h);
+
+
+       controller_connection_manager_release();
+       message_manager_shutdown();
+
+       cloud_communication_stop();
+       cloud_communication_fini();
+       config_shutdown();
+       net_util_fini();
+
+       resource_close_all();
        log_file_close();
 
        _D("Bye ~");
@@ -206,4 +346,3 @@ int main(int argc, char* argv[])
 
        return 0;
 }
-