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