00001
00009 #include "mcdc3006s/MCDC3006Slib/MCDC3006S.h"
00010
00011 #define AD_HOME getenv( "AD_HOME" )
00012
00013 #define TRUE 1
00014 #define FALSE 0
00015
00016 #define DEFAULT_SERIAL_DEVICE "/dev/brazo_derecho"
00017 #define DEFAULT_SEMFILE "/robot/etc/semDrive"
00018
00019 typedef int boolean;
00020
00021 void mainMenu(driverSensor_t * odo, driverConf_t * dc);
00022 int moveToPosition(long int position, boolean relative, driverSensor_t * odo, driverConf_t * dc);
00023 int moveVelocity(long int velocity, driverConf_t * dc);
00024
00025 int main(int argc, char * argv[])
00026 {
00027 driverSensor_t odo;
00028 driverConf_t dc;
00029
00030
00031
00032 dc.maxPos = 100000;
00033 dc.minPos = -100000;
00034 dc.maxVel = 1000;
00035 dc.maxAcc = 200;
00036 dc.maxDec = 200;
00037
00038 int baudrate = 19200;
00039
00040 char serialDevice[128];
00041 int initValue;
00042 key_t keySem;
00043
00044
00045
00046
00047 #ifdef DEBUG
00048 printf ("$AD_HOME is %s\n\r", AD_HOME);
00049 #endif
00050
00051 if (argc > 1)
00052 strcpy(serialDevice, argv[1]);
00053 else {
00054 strcpy(serialDevice, DEFAULT_SERIAL_DEVICE);
00055 #ifdef DEBUG
00056 printf ("SerialDevice is %s\n\r", serialDevice);
00057 #endif
00058 }
00059
00060 if (argc > 2)
00061 strcpy(dc.sem.semFile, argv[2]);
00062 else {
00063 strcpy(dc.sem.semFile, AD_HOME);
00064 strcat(dc.sem.semFile, DEFAULT_SEMFILE);
00065
00066 }
00067
00068 initValue = initCommunication(baudrate, serialDevice, &dc.RSd, dc.sem.semFile, &dc.sem.semID);
00069
00070 switch(initValue) {
00071 case -1:
00072 fprintf(stderr, "Could not establish connections with serial port at %s\n\r", serialDevice);
00073 return -1;
00074 break;
00075 case -2:
00076 fprintf(stderr, "Could not create semaphore file at %s\n\r", dc.sem.semFile);
00077 return -2;
00078 break;
00079 case -3:
00080 fprintf(stderr, "Could not set zero odometry\n\r");
00081 return -3;
00082 break;
00083 case -4:
00084 fprintf(stderr, "Semaphore related error\n\r");
00085 return -4;
00086 break;
00087 default:
00088 break;
00089 }
00090
00091 setDriverConf(dc);
00092
00093
00094 mainMenu(&odo, &dc);
00095
00096
00097 rmSem(dc.sem.semID);
00098
00099 return 0;
00100 }
00101
00106 void mainMenu(driverSensor_t * odo, driverConf_t * dc)
00107 {
00108 driverStatus_t drvStat;
00109
00110 int option = -1;
00111 int subOption;
00112 int exit = FALSE;
00113 int subOptionExit = FALSE;
00114
00115 long int param;
00116 long int oldPos;
00117
00118 do {
00119 printf("\n\r\n\r");
00120 printf("--- MAIN MENU ---\n\r");
00121 printf("Available options\n\r");
00122 printf("1.Set/Print Max/Min positions\n\r");
00123 printf("2.Set/Print Max velocity\n\r");
00124 printf("3.Set/Print Max acceleration\n\r");
00125 printf("4.Set/Print Max decceleration\n\r");
00126 printf("5.Print Odometry Data\n\r");
00127 printf("6.Move the driver\n\r");
00128 printf("7.Enable Driver\n\r");
00129 printf("8.Disable Driver\n\r");
00130 printf("9.Show Driver Status\n\r");
00131 printf("10.Calibrate the Driver\n\r");
00132 printf("0.Exit\n\r");
00133 printf("\n\r\n\r");
00134 printf("Choose an option:");
00135 scanf("%d", &option);
00136 printf("\n\r");
00137 if (option == 1) {
00138 do {
00139 printf("\n\r\n\r");
00140 printf("--- Change or Print Max and Min positions ---\n\r");
00141 printf("Available options:\n\r");
00142 printf("1.Set Max Pos\n\r");
00143 printf("2.Set Min Pos\n\r");
00144 printf("3.Print Max and Min Pos\n\r");
00145 printf("0.Back to Main menu\n\r");
00146 printf("Option:");
00147 scanf("%d", &subOption);
00148 printf("\n\r");
00149
00150 switch(subOption) {
00151 case 1:
00152 printf("Please write the max position: ");
00153 scanf("%ld", &dc->maxPos);
00154 printf("\n\r");
00155 if (setDriverMaxPos(dc->RSd, dc->sem.semID, dc->maxPos) == ERR_NOERR) {
00156 if (getDriverMaxPos(dc->RSd, dc->sem.semID) >= ERR_NOERR)
00157 printf("OK. Curent Max Position is: %ld\n\r", getDriverMaxPos(dc->RSd, dc->sem.semID));
00158 else
00159 fprintf(stderr, "There has been an error.\n\r");
00160 }
00161 else
00162 fprintf(stderr, "There has been an error.\n\r");
00163 subOptionExit = FALSE;
00164 break;
00165 case 2:
00166 printf("Please write the min position: ");
00167 scanf("%ld", ¶m);
00168 printf("\n\r");
00169 if (setDriverMinPos(dc->RSd, dc->sem.semID, param) == ERR_NOERR)
00170 printf("OK. Curent Min Position is: %ld \n\r", getDriverMinPos(dc->RSd, dc->sem.semID));
00171 else
00172 fprintf(stderr, "There has been an error.\n\r");
00173 subOptionExit = FALSE;
00174 break;
00175 case 3:
00176
00177 if (getDriverMaxPos(dc->RSd, dc->sem.semID) >= ERR_NOERR) {
00178 printf("MAX Pos: %ld \n\r", dc->maxPos);
00179 }
00180 else
00181 printf("There has been an error getting the max position\n\r");
00182
00183 printf("MIN Pos: %ld \n\r", getDriverMinPos(dc->RSd, dc->sem.semID));
00184 subOptionExit = FALSE;
00185 break;
00186 case 0:
00187 subOptionExit = TRUE;
00188 printf("subOption: %d. Option: %d\n\r", subOption, option);
00189 continue;
00190 default:
00191 printf("Incorrect Option.\n\r");
00192 subOptionExit = FALSE;
00193 break;
00194 }
00195 }
00196 while(subOptionExit != TRUE);
00197 }
00198 else if (option == 2) {
00199 do {
00200 printf("\n\r\n\r");
00201 printf("--- Change or Print Max velocity ---\n\r");
00202 printf("Available options:\n\r");
00203 printf("1.Set Max Velocity\n\r");
00204 printf("2.Print Max Velocities\n\r");
00205 printf("0.Back to Main menu\n\r");
00206 printf("Option:");
00207 scanf("%d", &subOption);
00208 printf("\n\r");
00209
00210 switch(subOption) {
00211 case 1:
00212 printf("Please write the max velocity: ");
00213 scanf("%ld", ¶m);
00214 printf("\n\r");
00215 if (setDriverMaxVel(dc->RSd, dc->sem.semID, param) == ERR_NOERR)
00216 printf("OK. Curent Max velocity is: %ld\n\r", getDriverMaxVel(dc->RSd, dc->sem.semID));
00217 else
00218 fprintf(stderr, "There has been an error.\n\r");
00219 subOptionExit = FALSE;
00220 break;
00221 case 2:
00222 printf("MAX Velocity: %ld \n\r", getDriverMaxVel(dc->RSd, dc->sem.semID));
00223 subOptionExit = FALSE;
00224 break;
00225 case 0:
00226 subOptionExit = TRUE;
00227 break;
00228 default:
00229 printf("Incorrect Option.\n\r");
00230 subOptionExit = FALSE;
00231 break;
00232 }
00233 }
00234 while(subOptionExit != TRUE);
00235
00236 }
00237 else if (option == 3) {
00238 do {
00239 printf("\n\r\n\r");
00240 printf("--- Change or Print Max acceleration ---\n\r");
00241 printf("Available options:\n\r");
00242 printf("1.Set Max Acceleration\n\r");
00243 printf("2.Print Max Acceleration\n\r");
00244 printf("0.Back to Main menu\n\r");
00245 printf("Option:");
00246 scanf("%d", &subOption);
00247 printf("\n\r");
00248
00249 switch(subOption) {
00250 case 1:
00251 printf("Please write the max acceleration: ");
00252 scanf("%ld", ¶m);
00253 printf("\n\r");
00254 if (setDriverMaxAcc(dc->RSd, dc->sem.semID, param) == ERR_NOERR)
00255 printf("OK. Curent Max acceleration is: %ld\n\r", getDriverMaxAcc(dc->RSd, dc->sem.semID));
00256 else
00257 fprintf(stderr, "There has been an error.\n\r");
00258 subOptionExit = FALSE;
00259 break;
00260 case 2:
00261 printf("MAX acceleration: %ld \n\r", getDriverMaxAcc(dc->RSd, dc->sem.semID));
00262 subOptionExit = FALSE;
00263 break;
00264 case 0:
00265 subOptionExit = TRUE;
00266 break;
00267 default:
00268 printf("Incorrect Option.\n\r");
00269 subOptionExit = FALSE;
00270 break;
00271 }
00272 }
00273 while(subOptionExit != TRUE);
00274 }
00275 else if (option == 4) {
00276 do {
00277 printf("\n\r\n\r");
00278 printf("--- Change or Print Max decceleration ---\n\r");
00279 printf("Available options:\n\r");
00280 printf("1.Set Max Decceleration\n\r");
00281 printf("2.Print Max Decceleration\n\r");
00282 printf("0.Back to Main menu\n\r");
00283 printf("Option:");
00284 scanf("%d", &subOption);
00285 printf("\n\r");
00286
00287 switch(subOption) {
00288 case 1:
00289 printf("Please write the max decceleration: ");
00290 scanf("%ld", ¶m);
00291 printf("\n\r");
00292 if (setDriverMaxDec(dc->RSd, dc->sem.semID, param) == ERR_NOERR)
00293 printf("OK. Curent Max acceleration is: %ld\n\r", getDriverMaxDec(dc->RSd, dc->sem.semID));
00294 else
00295 fprintf(stderr, "There has been an error.\n\r");
00296 subOptionExit = FALSE;
00297 break;
00298 case 2:
00299 printf("MAX acceleration: %ld \n\r", getDriverMaxDec(dc->RSd, dc->sem.semID));
00300 subOptionExit = FALSE;
00301 break;
00302 case 0:
00303 subOptionExit = TRUE;
00304 break;
00305 default:
00306 printf("Incorrect Option.\n\r");
00307 subOptionExit = FALSE;
00308 break;
00309 }
00310 }
00311 while(subOptionExit != TRUE);
00312 }
00313 else if (option == 5) {
00314 getDriverSensor(dc->RSd, dc->sem.semID, odo);
00315 printf("Odometry data:\n\r");
00316 printf("Current position: %ld \n\r", odo->p);
00317 printf("Current velocity: %ld \n\r", odo->v);
00318 }
00319 else if (option == 6) {
00320
00321 long int velo;
00322
00323 do {
00324 printf("\n\r\n\r");
00325 printf("--- Do Movements menu ---\n\r");
00326 printf("Available options:\n\r");
00327 printf("1.Move to an absolute position\n\r");
00328 printf("2.Move to a relative position\n\r");
00329 printf("3.Move by velocity.\n\r");
00330 printf("0.Back to Main menu\n\r");
00331 printf("Option:");
00332 scanf("%d", &subOption);
00333 printf("\n\r");
00334
00335 switch(subOption) {
00336
00337 case 1:
00338 printf("Please write the absolute position in pulses: ");
00339 scanf("%ld", ¶m);
00340 printf("\n\r");
00341
00342 printf("Trying to move to %ld\n\r", param);
00343 if (moveToPosition(param, FALSE, odo, dc) != ERR_NOERR)
00344 fprintf(stderr, "There has been an error.\n\r");
00345
00346 subOptionExit = FALSE;
00347 break;
00348 case 2:
00349 printf("Please write the relative position in pulses: ");
00350 scanf("%ld", ¶m);
00351 printf("\n\r");
00352
00353 printf("Trying to move to %ld\n\r", param);
00354 if (moveToPosition(param, TRUE, odo, dc) != ERR_NOERR)
00355 fprintf(stderr, "There has been an error.\n\r");
00356 subOptionExit = FALSE;
00357 break;
00358 case 3:
00359
00360 printf("Please write velocity in rpm: ");
00361 scanf("%ld", ¶m);
00362 printf("\n\r");
00363 printf("Trying to move at %ld rpm\n\r", param);
00364 if (moveVelocity(param, dc) != ERR_NOERR)
00365 fprintf(stderr, "There has been an error.\n\r");
00366 subOptionExit = FALSE;
00367 break;
00368 case 0:
00369 subOptionExit = TRUE;
00370 break;
00371 default:
00372 printf("Incorrect Option.\n\r");
00373 subOptionExit = FALSE;
00374 break;
00375 }
00376 }
00377 while(subOptionExit != TRUE);
00378 }
00379 else if (option == 7) {
00380
00381 if (enableDriver(dc->RSd, dc->sem.semID) != ERR_NOERR) {
00382 fprintf(stderr, "Error while attempting to enable the driver.\n\r");
00383 break;
00384 }
00385 printf("OK. Driver enabled\n\r");
00386
00387 }
00388 else if (option == 8) {
00389 if (disableDriver(dc->RSd, dc->sem.semID) != ERR_NOERR) {
00390 fprintf(stderr, "Error while attempting to disable the driver.\n\r");
00391 break;
00392 }
00393 printf("OK. Driver disabled\n\r");
00394 }
00395 else if (option == 9) {
00396 if (getDriverStatus(dc->RSd, dc->sem.semID, &drvStat) != ERR_NOERR) {
00397 fprintf(stderr, "Error reading information from the Driver.\n\r");
00398 break;
00399 }
00400 printf("Printing the driver status. 1 TRUE, 0 False\n\r");
00401 printf("Driver Disabled: %d\n\r", drvStat.disabled);
00402 printf("Current Limiting: %d\n\r", drvStat.curLimiting);
00403 printf("Over Temperature: %d\n\r", drvStat.overTemperature);
00404 printf("Over Voltage: %d\n\r", drvStat.overVoltage);
00405 printf("Limit Sensor Reached: %d\n\r\n", drvStat.sensorReached);
00406 }
00407 else if (option == 10) {
00408 if (calibrateDriver(dc->RSd, dc->sem.semID, 26535, 4000, 500, 10000) != ERR_NOERR) {
00409 fprintf(stderr, "Error calibrating the driver.\n\r");
00410 option = -1;
00411 break;
00412 }
00413 printf("Calibration succeeded\n\r");
00414 }
00415 else if (option != 0) {
00416 printf("Incorrect Option\n\r");
00417 exit = FALSE;
00418 break;
00419 }
00420 else {
00421 exit = TRUE;
00422 printf("Bye bye!\n\r");
00423 break;
00424 }
00425 }
00426 while(exit != TRUE);
00427 }
00428
00440 int moveToPosition(long int position, boolean relative, driverSensor_t * odo, driverConf_t * dc)
00441 {
00442 int aux;
00443 int counter = 0;
00444 int currentError, oldError;
00445
00446 if (relative == FALSE) {
00447 if (moveDriverAbsPos(dc->RSd, dc->sem.semID, position) == ERR_NOERR)
00448 printf("OK. Moving to absolute %ld\n\r", position);
00449 else {
00450 fprintf(stderr, "There has been an error. moveAbsPos() did not return ERR_NOERR\n\r");
00451 return -1;
00452 }
00453 }
00454 else {
00455 if (moveDriverRelPos(dc->RSd, dc->sem.semID, position) == ERR_NOERR)
00456 printf("OK. Moving to relative %ld\n\r", position);
00457 else {
00458 fprintf(stderr, "There has been an error. moveRelPos() did not return ERR_NOERR\n\r");
00459 return -1;
00460 }
00461
00462 }
00463
00464 getDriverSensor(dc->RSd, dc->sem.semID, odo);
00465 currentError = position - odo->p;
00466
00467 printf("OK. The driver has arrived to its destination!\n\r");
00468 printf("Current driver position: %ld\n\r", odo->p);
00469 printf("You introduced: %ld\n\r", position);
00470 return 0;
00471 }
00472
00481 int moveVelocity(long int velocity, driverConf_t * dc)
00482 {
00483 if (moveDriverVel(dc->RSd, dc->sem.semID, velocity) == 0)
00484 printf("OK. Moving at %ld rpm\n\r", velocity);
00485 else {
00486 fprintf(stderr, "There has been an error. moveVel() did not returned 0\n\r");
00487 return -1;
00488 }
00489
00490 return 0;
00491 }
00492
00493 int testMovementMonitor()
00494 {
00495 long int destination;
00496 short crashed;
00497 int timeOut;
00498 int range;
00499 int currLimit;
00500 }