run_ctrl.c

00001 /*
00002   走行制御ライブラリ
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "run_ctrl.h"
00008 #include "tRunCtrl.h"
00009 #include "commandCtrl.h"
00010 #include "get_keyword.h"
00011 #include "connect_device.h"
00012 #include "serial_device.h"
00013 #include "tcpip_device.h"
00014 #include "structTables.h"
00015 #include "nodeAccess.h"
00016 #include <string.h>
00017 
00018 typedef struct {
00019   int straight_ref_vel;
00020   int straight_ref_acc;
00021   long rotate_ref_vel;
00022   long rotate_ref_acc;
00023   int follow_r;
00024 } runParams_t;
00025 
00026 
00027 extern void run_initCommandCtrl(int *Con, runCtrl_t *Run, nodeInfo_t *Node,
00028                                 runParams_t *Params, int *Unique_id);
00029 
00030 enum {
00031   SimulatorPort = 49763,
00032   SEND_COMMAND_SIZE = 256,
00033 };
00034 
00035 typedef struct {
00036   int x;
00037   int y;
00038   direction_t zt;
00039 } position_t;
00040 static position_t local_offset;
00041 
00042 typedef struct {
00043   int send_command_size;
00044   char send_command[SEND_COMMAND_SIZE];
00045 } runCommand_t;
00046 static runCommand_t cmd;
00047 
00048 
00049 static runCtrl_t Tbl;
00050 static nodeInfo_t Node;
00051 static runParams_t Params;
00052 static int Unique_id = 0;
00053 
00054 
00060 #ifndef RUN_CONFIG_FILE
00061 #define RUN_CONFIG_FILE "defaultargs"
00062 #endif
00063 
00064 
00068 #ifndef RUN_AUTO_PORT
00069 # ifdef LINUX
00070 #  define RUN_AUTO_PORT "/dev/ttyS"
00071 # else
00072 #  define RUN_AUTO_PORT "COM"
00073 # endif
00074 #endif
00075 
00076 
00077 static char *Error_device = "";
00078 static long Error_baudrate = 0;
00079 static char *Error_message = "connection device is not specified";
00080 static int fd = -1;
00081 
00082 
00083 static int searchConfigFile(const char *path) {
00084   enum {
00085     CONFIG_PATH_LENGTH = 256,
00086   };
00087   char file_path[CONFIG_PATH_LENGTH +1];
00088   int path_length = strlen(path);
00089   int file_length = strlen(RUN_CONFIG_FILE);
00090   long baudrate = RUN_BAUDRATE;
00091   char *baudrate_str;
00092   char *deviceName;
00093 
00094   if ((path_length + 1 + file_length + 1) < CONFIG_PATH_LENGTH) {
00095     strcpy(file_path, path);
00096     strcpy(&file_path[path_length], "/");
00097     strcpy(&file_path[path_length +1], RUN_CONFIG_FILE);
00098     file_path[path_length + 1 + file_length] = '\0';
00099 
00100     if ((baudrate_str = get_keyword(file_path, "run_baudrate"))) {
00101       baudrate = atoi(baudrate_str);
00102       free(baudrate_str);
00103     }
00104     deviceName = get_keyword(file_path, "run_port");
00105   }
00106   if (deviceName) {
00107     int ret_value = initConnectDevice(deviceName, baudrate, SERIAL);
00108     free(deviceName);
00109     if (ret_value >= 0) {
00110       return ret_value;
00111     }
00112   }
00113   return NO_FILE_ERROR;
00114 }
00115 
00116 
00117 static void initialization(void) {
00118   Params.straight_ref_vel = StraightVel;
00119   Params.straight_ref_acc = StraightAcc;
00120   Params.rotate_ref_vel = RotateVel;
00121   Params.rotate_ref_acc = RotateAcc;
00122   Params.follow_r = FollowRadius;
00123 
00124   initNodeInfo(&Node);
00125   registerStructInfo(&Node, (unsigned char*)&Tbl, RUN_CTRL_TARGET_ID);
00126   Tbl.version = 0;
00127   //registerCrdNortifyClient();
00128 
00129   run_initCommandCtrl(&fd, &Tbl, &Node, &Params, &Unique_id);
00130 }
00131 
00132 
00133 static int checkVersion(void) {
00134   initialization();
00135 
00136   long target_version = PACKAGE_NUM_VERSION -1;
00137   if (recvVersion(&target_version) < 0) {
00138     Error_message = "Transmit fail: please check connected device.";
00139     return -1;
00140   }
00141   if (target_version/10 != PACKAGE_NUM_VERSION/10) {
00142     fprintf(stderr,
00143             "warnning: RunCtrl version mismatch between PC(%d) and SH2(%ld)\n",
00144             PACKAGE_NUM_VERSION, target_version);
00145     exit(1);
00146   }
00147 #if 0
00148   int ret_value = sendPositionInit();
00149   if (ret_value < 0) {
00150     error_message = "Transmit fail: in sendPositionInit()";
00151     return ret_value;
00152   }
00153 #endif
00154   run_stop();
00155 
00156   //set_watchDogTimer(0);
00157   //initTicksInfo();
00158 
00159   // 現在自己位置で座標系を初期化
00160 
00161   // !!! とりあえずのコメントアウト。この処理は必要
00162   //run_adjustRunPosition(0, 0, deg(0));
00163 
00164   return 0;
00165 }
00166 
00167 
00168 static int connectDefault(void) {
00169   int ret_value = searchConfigFile(".");
00170   char *home;
00171   long baudrate = RUN_BAUDRATE;
00172   char *deviceName = NULL;
00173   char *baudrate_str = NULL;
00174 
00175   if ((ret_value >= 0) || (ret_value != NO_FILE_ERROR)) {
00176     return ret_value;
00177   }
00178 
00179   home = getenv("HOME");
00180   if (home) {
00181     ret_value = searchConfigFile(home);
00182     // !!! free する必要はあるのか?
00183     if ((ret_value >= 0) || (ret_value != NO_FILE_ERROR)) {
00184       return ret_value;
00185     }
00186   }
00187 
00188   if ((baudrate_str = getenv("RUN_BAUDRATE"))) {
00189     baudrate = atoi(baudrate_str);
00190   }
00191 
00192   deviceName = getenv("RUN_PORT");
00193   if (deviceName) {
00194     int ret_value;
00195     ret_value = initConnectDevice(deviceName, baudrate, SERIAL);
00196     if (ret_value >= 0) {
00197       fd = ret_value;
00198       ret_value = checkVersion();
00199     }
00200     return ret_value;
00201   }
00202   return -1;
00203 }
00204 
00205 
00206 int initRunCtrl(int argc, char *argv[]) {
00207   long baudrate = RUN_BAUDRATE;
00208   char *deviceName = NULL;
00209   int ret_value;
00210   int i;
00211 
00212   for (i = 1; i < argc; ++i) {
00213     if (!strncmp("--run_port=", argv[i], 11) && (strlen(argv[i]) > 11)) {
00214       deviceName = &argv[i][11];
00215 
00216     } else if (!strncmp("--run_baudrate=", argv[i], 15) &&
00217                (strlen(argv[i]) > 15)) {
00218       baudrate = atoi(&argv[i][15]);
00219 
00220     } else if (!strcmp("-s", argv[i]) || !strcmp("--simulator", argv[i])) {
00221       // シミュレータ(TCP/IP) への接続
00222       return initConnectDevice("localhost", SimulatorPort, TCP_IP);
00223     }
00224   }
00225   if (deviceName &&
00226       (!strcmp("auto", deviceName) || !strcmp("AUTO", deviceName))) {
00227     int id;
00228     for (id = 20; id >= 0; --id) {
00229       char checkDevice[16] = { '\0','\0','\0','\0','\0','\0','\0','\0',
00230                                '\0','\0','\0','\0','\0','\0','\0','\0' };
00231       sprintf(checkDevice, RUN_AUTO_PORT "%d", id);
00232       ret_value = initConnectDevice(checkDevice, baudrate, SERIAL);
00233       if (ret_value >= 0) {
00234         fd = ret_value;
00235         ret_value = checkVersion();
00236         break;
00237       }
00238     }
00239     if (ret_value < 0) {
00240       Error_device = RUN_AUTO_PORT;
00241       Error_baudrate = baudrate;
00242       Error_message = "auto dvice detection is fail";
00243     } else {
00244       Error_message = "success";
00245     }
00246     return ret_value;
00247   }
00248 
00249   if (!deviceName) {
00250     return connectDefault();
00251   }
00252   ret_value = initConnectDevice(deviceName, baudrate, SERIAL);
00253   if (ret_value >= 0) {
00254     fd = ret_value;
00255     ret_value = checkVersion();
00256   }
00257   return ret_value;
00258 }
00259 
00260 
00261 char* run_getError(void) {
00262   return "";
00263 }
00264 
00265 
00266 #include "move_ctrl.c"
00267 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1