#include <service_app.h>
#include "log.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 "lap_counter/lap_counter.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,
unsigned int f_value;
unsigned int r_value;
unsigned int dir_state;
+ const char *user_name;
guint idle_h;
} app_data;
+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)
{
return;
return floor(val + 0.5);
}
-static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
+static int __map_range_val(int v_min, int v_max, int d_min, int d_max, int val)
{
int rval = 0;
double slope = 0;
return rval;
}
-static int ___map_speed_val(int speed)
+static inline int ___map_speed_val(int speed)
{
- static const int motor_max = 4095;
- static const int motor_min = -4095;
- static const int speed_max = 1000;
- static const int speed_min = -1000;
-
- return __map_range_val(motor_max, motor_min,
- speed_max, speed_min, speed);
+ return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, -4095, 4095, speed);
}
-static int ___map_servo_val(int servo)
+static inline 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;
-
- return __map_range_val(motor_max, motor_min,
- servo_max, servo_min, servo);
+ return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, STERING_SERVO_CENTER - STERING_SERVO_RANGE, STERING_SERVO_CENTER + STERING_SERVO_RANGE, servo);
}
-
static int __driving_motors(int servo, int speed)
{
int val_speed;
return 0;
}
-static gboolean __message_dispatcher(gpointer user_data)
+static void __camera(int azimuth, int elevation)
{
- 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;
+ 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
}
-static void __recv_state_change(receiver_type_e type,
- receiver_state_e state, void* user_data)
-{
- app_data *ad = user_data;
- ret_if(!ad);
-
- _D("receiver type[%d] state changed[%d]", type, state);
-
- if (state == RECEIVER_STATE_CONNECTED) {
- if (!ad->idle_h)
- ad->idle_h = g_idle_add(__message_dispatcher, ad);
- } else {
- if (ad->idle_h) {
- g_source_remove(ad->idle_h);
- ad->idle_h = 0;
- }
- __driving_motors(0, 0);
+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;
}
-
- return;
}
-static void __conn_state_changed_cb(connection_state_e state,
- const char *ip, void* user_data)
+static void __user_name_received_cb(const char *name)
{
- app_data *ad = user_data;
-
- _D("connection state changed : %d", state);
-
- if (state == CONNECTION_STATE_CONNECTED) {
- receiver_start(RECEIVER_TYPE_UDP);
-
- } else {
- receiver_stop(RECEIVER_TYPE_UDP);
-
- if (ad->idle_h) {
- g_source_remove(ad->idle_h);
- ad->idle_h = 0;
- }
+ _D("User name received: %s", name);
+ lap_counter_set_user_name(name);
+ lap_counter_set_start_lap();
+}
- __driving_motors(0, 0);
+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;
}
- return;
+ 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();
+ lap_counter_init();
}
static bool service_app_create(void *data)
int ret = 0;
app_data *ad = data;
- receiver_init(RECEIVER_TYPE_UDP);
- receiver_set_state_changed_cb(RECEIVER_TYPE_UDP, __recv_state_change, ad);
-
- connection_manager_init();
- connection_manager_set_state_changed_cb(__conn_state_changed_cb, ad);
-
- message_queue_new();
+ _D("-----------------------=======================================================================-----------------------");
+ _D("-----------------------=======================================================================-----------------------");
+ _D("-----------------------============================== APP %s %s ==============================-----------------------", __DATE__, __TIME__);
+ _D("-----------------------=======================================================================-----------------------");
+ _D("-----------------------=======================================================================-----------------------");
/*
* if you want to use default configuration,
}
#endif
+ resource_lap_counter_init();
+
+ _initialize_components(ad);
+ cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
+
+ controller_connection_manager_set_command_received_cb(__command_received_cb);
+ controller_connection_manager_set_user_name_received_cb(__user_name_received_cb);
+
+ lap_counter_init();
return true;
}
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);
+ 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;
{
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);
+
+ resource_lap_counter_destroy();
+
if (ad->idle_h)
g_source_remove(ad->idle_h);
+ lap_counter_shutdown();
+ controller_connection_manager_release();
+ message_manager_shutdown();
- connection_manager_fini();
- receiver_fini(RECEIVER_TYPE_UDP);
+ cloud_communication_stop();
+ cloud_communication_fini();
+ config_shutdown();
+ net_util_fini();
resource_close_all();
log_file_close();