New servo ranges
[apps/native/gear-racing-car.git] / src / app.c
index 0de042d..60c5c20 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 "resource.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 STERING_SERVO_CENTER 340
+#define STERING_SERVO_RANGE 125
+
+#define MAX_UDP_INPUT 10000
+
+#define CONFIG_GRP_CAR "Car"
+#define CONFIG_KEY_ID "Id"
+#define CONFIG_KEY_NAME "Name"
+#define CLOUD_REQUESTS_FREQUENCY 15
+#define AZIMUTH_SERVO_PIN 15
+#define ELEVATION_SERVO_PIN 14
+
+#define ELEVATION_MIN 200
+#define ELEVATION_MAX 400
+#define AZIMUTH_MIN 200
+#define AZIMUTH_MAX 700
 
 enum {
        DIR_STATE_S,
@@ -33,10 +59,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)
 {
@@ -50,6 +77,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;
 }
 
@@ -58,107 +88,129 @@ static void service_app_low_memory(app_event_info_h event_info, void *user_data)
        return;
 }
 
-static int __servo_motor_test(void)
+static inline double __map_round(double val)
 {
-       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, 400);
-       sleep(1);
-       resource_set_servo_motor_value(0, 500);
-       sleep(1);
-       resource_set_servo_motor_value(0, 450);
-       sleep(1);
-       return 0;
+       return floor(val + 0.5);
 }
 
-static void ___________control_motor(app_data *ad)
+static int __map_range_val(int v_min, int v_max, int d_min, int d_max, int val)
 {
-       _D("control motor, state(%u), f_val(%u), r_val(%u)",
-                       ad->dir_state, ad->f_value, ad->r_value);
+       int rval = 0;
+       double slope = 0;
+       slope = 1.0 * (d_max - d_min) / (v_max - v_min);
 
+       rval = d_min + __map_round(slope * (val - v_min));
 
-       switch (ad->dir_state) {
-       case DIR_STATE_F:
-               if (ad->f_value)  {
-                       if (ad->r_value) {
-                               ad->dir_state = DIR_STATE_S;
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
-                       } else {
-                               ad->dir_state = DIR_STATE_B;
-                               __servo_motor_test();
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, -2000);
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, -2000);
-                       }
-               }
-               break;
-       case DIR_STATE_B:
-               if (ad->r_value)  {
-                       if (ad->f_value) {
-                               ad->dir_state = DIR_STATE_S;
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
-                       } else {
-                               ad->dir_state = DIR_STATE_F;
-                               __servo_motor_test();
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 2000);
-                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 2000);
-                       }
-               }
-               break;
-       case DIR_STATE_S:
-               if (!ad->f_value)  {
-                       ad->dir_state = DIR_STATE_F;
-                       __servo_motor_test();
-                       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 2000);
-                       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 2000);
-               } else if (!ad->r_value) {
-                       ad->dir_state = DIR_STATE_B;
-                       __servo_motor_test();
-                       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, -2000);
-                       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, -2000);
-               }
-               break;
-       }
+       return rval;
+}
 
-       return;
+static inline int ___map_speed_val(int speed)
+{
+       return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, -4095, 4095, speed);
 }
 
-static void _front_ioa_sensor_changed_cb(unsigned int value, void *data)
+static inline int ___map_servo_val(int servo)
 {
-       app_data *ad = data;
+       return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, STERING_SERVO_CENTER - STERING_SERVO_RANGE, STERING_SERVO_CENTER + STERING_SERVO_RANGE, servo);
+}
 
-       _D("FRONT has obstacle!");
+static int __driving_motors(int servo, int speed)
+{
+       int val_speed;
+       int val_servo;
 
-       ad->f_value = value;
+       val_servo = ___map_servo_val(servo);
+       val_speed = ___map_speed_val(speed);
 
-       ___________control_motor(ad);
+       _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;
+       return 0;
 }
 
-static void _back_ioa_sensor_changed_cb(unsigned int value, void *data)
+static void __camera(int azimuth, int elevation)
 {
-       app_data *ad = data;
-
-       _D("BACK has obstacle!");
+       int val_azimuth = __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, AZIMUTH_MIN, AZIMUTH_MAX, azimuth);
+       int val_elevation = __map_range_val(0, MAX_UDP_INPUT, ELEVATION_MIN, ELEVATION_MAX, elevation); // No need to look upside down
+
+       _D("camera - azimuth[%4d : %4d], elevation[%4d : %4d]", azimuth, val_azimuth, elevation, val_elevation);
+#if ENABLE_MOTOR
+       resource_set_servo_motor_value(ELEVATION_SERVO_PIN, val_elevation);
+       resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, val_azimuth);
+#endif
+}
 
-       ad->r_value = value;
+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;
+       }
+}
 
-       ___________control_motor(ad);
+static void _initialize_config()
+{
+       net_util_init();
+
+       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);
+}
 
-       return;
+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;
 
        /*
         * 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()");
@@ -169,28 +221,27 @@ static bool service_app_create(void *data)
                _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;
 
+#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_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);
+       resource_set_servo_motor_value(0, 450);
+       resource_set_servo_motor_value(ELEVATION_SERVO_PIN, ELEVATION_MIN);
+       resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
+#endif
 
        return;
 }
@@ -199,6 +250,23 @@ static void service_app_terminate(void *data)
 {
        app_data *ad = data;
 
+       resource_set_servo_motor_value(0, STERING_SERVO_CENTER);
+       resource_set_servo_motor_value(ELEVATION_SERVO_PIN, ELEVATION_MIN);
+       resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
+
+       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 ~");
@@ -237,4 +305,3 @@ int main(int argc, char* argv[])
 
        return 0;
 }
-