mcdc3006s.cpp
Go to the documentation of this file.
00001 
00026 #include "maggie_motor_drivers/mcdc3006s.h"
00027 
00029 
00030 Mcdc3006s::Mcdc3006s()
00031 {
00032 }
00033 
00035 
00036 Mcdc3006s::~Mcdc3006s()
00037 {
00038     _comm.endCommunication();
00039 }
00040 
00042 
00043 int Mcdc3006s::init(int baudrate, char *dev, char *sem)
00044 {
00045     int error = 0;
00046     if ((error = _comm.initCommunication(baudrate, &dev[0], &sem[0])) < 0) {
00047         ROS_ERROR("[MCDC3006S] initCommunication with devices failed");
00048         ROS_WARN("[MCDC3006S] baudrate=%d, serialDevice=%s, semFile=%s", baudrate, dev, sem);
00049 
00050         return error;
00051     }
00052 
00053     return ERR_NOERR;
00054 }
00055 
00057 
00058 int Mcdc3006s::enable_driver()
00059 {
00060     char command[] = "EN\n\r\0";
00061 
00062     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00063         ROS_ERROR("[MCDC3006S] EnableDriver() --> Error");
00064 
00065         return ERR_WRI;
00066     }
00067 
00068     return ERR_NOERR;
00069 }
00070 
00072 
00073 int Mcdc3006s::disable_driver()
00074 {
00075     char command[] = "DI\n\r\0";
00076 
00077     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00078         ROS_ERROR("[MCDC3006S] DisableDriver() --> Error");
00079 
00080         return ERR_WRI;
00081     }
00082 
00083     return ERR_NOERR;
00084 }
00085 
00087 
00088 int Mcdc3006s::get_config(driverConf_t *dc)
00089 {
00090     (*dc).maxPos = get_max_pos();
00091     if (dc->maxPos == ERR_COM) {
00092         ROS_ERROR("[MCDC3006S] getDriverConf() --> Error reading the configuration from the driver");
00093         return ERR_COM;
00094     }
00095 
00096     (*dc).minPos = get_min_pos();
00097     if (dc->minPos == ERR_COM) {
00098         ROS_ERROR("[MCDC3006S] getDriverConf() --> Error reading the configuration from the driver");
00099         return ERR_COM;
00100     }
00101 
00102     (*dc).maxVel = get_max_vel();
00103     if (dc->maxVel == ERR_COM) {
00104         ROS_ERROR("[MCDC3006S] getDriverConf() --> Error reading the configuration from the driver");
00105         return ERR_COM;
00106     }
00107 
00108     (*dc).maxAcc = get_max_acc();
00109     if (dc->maxAcc == ERR_COM) {
00110         ROS_ERROR("[MCDC3006S] getDriverConf() --> Error reading the configuration from the driver");
00111         return ERR_COM;
00112     }
00113 
00114     (*dc).maxDec = get_max_dec();
00115     if (dc->maxDec == ERR_COM) {
00116         ROS_ERROR("[MCDC3006S] getDriverConf() --> Error reading the configuration from the driver");
00117         return ERR_COM;
00118     }
00119 
00120     (*dc).cCLimit = get_cur_lim();
00121     if (dc->maxAcc == ERR_COM) {
00122         ROS_ERROR("[MCDC3006S] getDriverConf() --> Error reading the configuration from the driver");
00123 
00124         return ERR_COM;
00125     }
00126 
00127     return ERR_NOERR;
00128 }
00129 
00131 
00132 long int Mcdc3006s::get_max_pos()
00133 {
00134     char command[SP_MSG_SIZE], response[SP_MSG_SIZE];
00135 
00136     sprintf(command, "GPL\r");
00137 
00138     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00139         ROS_ERROR("[MCDC3006S] getDriverMaxPos() --> Error communicating with driver");
00140 
00141         return ERR_COM;
00142     }
00143 
00144     // Converting the response of the driver to a long int.
00145     return atol(response);
00146 }
00147 
00149 
00150 long int Mcdc3006s::get_min_pos()
00151 {
00152     static char command[SP_MSG_SIZE];
00153     char response[SP_MSG_SIZE];
00154     long int minPos;
00155 
00156     sprintf(command, "GNL\r");
00157 
00158     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00159         ROS_ERROR("[MCDC3006S] getDriverMinPos() --> Error when communicating with the driver");
00160 
00161         return ERR_COM;
00162     }
00163 
00164     minPos = atol(response);
00165 
00166     return minPos;
00167 }
00168 
00170 
00171 long int Mcdc3006s::get_max_vel()
00172 {
00173     char command[SP_MSG_SIZE]; // "GSP\n\r\0";
00174     char response[SP_MSG_SIZE];
00175 
00176     sprintf(command, "GSP\n\r\0");
00177 
00178     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00179         ROS_ERROR("[MCDC3006S] getDriverMaxVel() --> Error communicating with driver");
00180 
00181         return ERR_COM;
00182     }
00183 
00184     // Converting the response of the driver to a long int.
00185     return (atol(response));
00186 }
00187 
00189 
00190 long int Mcdc3006s::get_max_acc()
00191 {
00192     //  long int maxAcc = 0.0;
00193     char command[SP_MSG_SIZE];
00194     char response[SP_MSG_SIZE];
00195 
00196     strcpy(command, "GAC\n\r\0");
00197     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00198         ROS_ERROR("[MCDC3006S] getDriverMaxAcc() --> Error communicating with driver");
00199 
00200         return ERR_COM;
00201     }
00202 
00203     // Converting the response of the driver to a long int.
00204     return (atol(response));
00205 }
00206 
00208 
00209 long int Mcdc3006s::get_max_dec()
00210 {
00211     char command[SP_MSG_SIZE];
00212     char response[SP_MSG_SIZE];
00213 
00214     strcpy(command, "GDEC\n\r\0");
00215 
00216     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00217         ROS_ERROR("[MCDC3006S] getDriverMaxDec() --> Error communicating with driver");
00218 
00219         return ERR_COM;
00220     }
00221 
00222     // Converting the response of the driver to a long int.
00223     return (atol(response));
00224 }
00225 
00227 
00228 int Mcdc3006s::get_cur_lim()
00229 {
00230     static char command[SP_MSG_SIZE];
00231     char response[SP_MSG_SIZE];
00232 
00233     sprintf(command, "GCC\n\r\0");
00234 
00235     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00236         ROS_ERROR("[MCDC3006S] getDriverCurLim() --> Error communicating with the driver");
00237 
00238         return ERR_COM;
00239     }
00240 
00241     return (atoi(response));
00242 }
00243 
00245 
00246 int Mcdc3006s::get_peak_cur_lim()
00247 {
00248     static char command[SP_MSG_SIZE];
00249     char response[SP_MSG_SIZE];
00250 
00251     sprintf(command, "GPC\n\r\0");
00252 
00253     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00254         ROS_ERROR("[MCDC3006S] getDriverPCurLim() --> Error communicating with the driver");
00255 
00256         return ERR_COM;
00257     }
00258 
00259     return (atoi(response));
00260 }
00261 
00263 
00264 int Mcdc3006s::get_status(driverStatus_t * drvStatus)
00265 {
00266     char command[SP_MSG_SIZE];
00267     char response[SP_MSG_SIZE];
00268 
00269     sprintf(command, "GST\n\r\0");          //
00270     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00271         ROS_ERROR(
00272             "[MCDC3006S] getDriverStatus() --> Error when communicating with the driver. Could not establish driver status (GST).");
00273 
00274         return (ERR_COM);
00275     }
00276     // Now response has the driver "status" information
00277     drvStatus->disabled = atoi(response) & ENABLED_MASK;
00278 
00279     sprintf(command, "OST\n\r\0");          //
00280     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00281         ROS_ERROR(
00282             "[MCDC3006S] getDriverStatus() --> Error when communicating with the driver. Could not establish driver Operative Status (OST).");
00283 
00284         return (ERR_COM);
00285     } // Now response has the driver "fault status" information
00286 
00287     drvStatus->curLimiting = atoi(response) & CURRENT_LIMITING_MASK;
00288     drvStatus->overVoltage = atoi(response) & OVERVOLTAGE_MASK;
00289     drvStatus->overTemperature = atoi(response) & OVERTEMPERATURE_MASK;
00290     drvStatus->sensorReached = atoi(response) & DRIVER_INPUT_4_MASK;
00291 
00292     drvStatus->curLimiting = (drvStatus->curLimiting > 0) ?
00293         TRUE : FALSE;
00294     drvStatus->disabled = (drvStatus->disabled > 0) ?
00295         TRUE : FALSE;
00296     drvStatus->overTemperature = (drvStatus->overTemperature > 0) ?
00297         TRUE : FALSE;
00298     drvStatus->overVoltage = (drvStatus->overVoltage > 0) ?
00299         FALSE : FALSE;
00300     drvStatus->sensorReached = (drvStatus->sensorReached > 0) ?
00301         FALSE : FALSE;
00302 
00303     return ERR_NOERR;
00304 }
00305 
00307 
00308 int Mcdc3006s::get_sensor(driverSensor_t *sensor)
00309 {
00310     char command[SP_MSG_SIZE];
00311     char response[SP_MSG_SIZE];
00312 
00313     int status = ERR_NOERR;
00314 
00315     bzero((void *) response, sizeof(response));
00316 
00317     // Get current Position
00318     sprintf(command, "POS\n\r\0");
00319     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00320         ROS_ERROR(
00321             "[MCDC3006S] getDriverOdometry() --> Error when communicating with the driver. Could not establish current position");
00322         return (ERR_COM);
00323     }
00324     (*sensor).p = atol(response);
00325 
00326     // Get current velocity --> Current target velocity in rpm
00327     sprintf(command, "GV\n\r\0");
00328     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00329         ROS_ERROR(
00330             "[MCDC3006S] getDriverOdometry() --> Error when communicating with the driver. Could not establish current velocity");
00331         return (ERR_COM);
00332     }
00333     (*sensor).v = atol(response);
00334 
00335     // Get current instant current in mA
00336     sprintf(command, "GRC\n\r\0");
00337     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00338         ROS_ERROR(
00339             "[MCDC3006S] getDriverOdometry() --> Error when communicating with the driver. Could not establish current instant current");
00340         return (ERR_COM);
00341     }
00342     (*sensor).i = atol(response);
00343 
00344     return (ERR_NOERR);
00345 }
00346 
00348 
00349 int Mcdc3006s::get_instant_pos(long int *position)
00350 {
00351     char command[SP_MSG_SIZE];
00352     char response[SP_MSG_SIZE];
00353     int status = ERR_NOERR;
00354 
00355     bzero((void *) response, sizeof(response));
00356     sprintf(command, "POS\n\r\0");          // Get current Position
00357     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00358         ROS_ERROR(
00359             "[MCDC3006S] getDriverInstantPos() --> Error when communicating with the driver. Could not establish current position");
00360 
00361         return (ERR_COM);
00362     }
00363     *position = atol(response);
00364 
00365     return ERR_NOERR;
00366 }
00367 
00369 
00370 int Mcdc3006s::get_instant_vel(long int *velocity)
00371 {
00372     char command[SP_MSG_SIZE];
00373     char response[SP_MSG_SIZE];
00374     int status = ERR_NOERR;
00375 
00376     bzero((void *) response, sizeof(response));
00377     sprintf(command, "GV\n\r\0");           // Get Velocity
00378     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00379         ROS_ERROR(
00380             "[MCDC3006S] getDriverInstantVel() --> Error when communicating with the driver. Could not establish current instant current");
00381 
00382         return (ERR_COM);
00383     }
00384     *velocity = atol(response);
00385 
00386     return (ERR_NOERR);
00387 }
00388 
00390 
00391 int Mcdc3006s::get_instant_current(int *current)
00392 {
00393     char command[SP_MSG_SIZE];
00394     char response[SP_MSG_SIZE];
00395     int status = ERR_NOERR;
00396 
00397     bzero((void *) response, sizeof(response));
00398     sprintf(command, "GRC\n\r\0");          // Get current instant current in mA
00399     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00400         ROS_ERROR(
00401             "[MCDC3006S] getDriverInstantCurrent() --> Error when communicating with the driver. Could not establish current instant current");
00402 
00403         return (ERR_COM);
00404     }
00405     *current = atol(response);
00406 
00407     return (ERR_NOERR);
00408 }
00409 
00411 
00412 int Mcdc3006s::set_config(driverConf_t dc)
00413 {
00414     if (set_min_pos(dc.minPos) < ERR_NOERR) {
00415         ROS_ERROR("[MCDC3006S] setDriverConf() --> Error setting the minimum position");
00416 
00417         return ERR_CONF;
00418     }
00419     if (set_max_pos(dc.maxPos) < ERR_NOERR) {
00420         ROS_ERROR("[MCDC3006S] setDriverConf() --> Error setting the minimum position");
00421 
00422         return ERR_CONF;
00423     }
00424     if (set_max_vel(dc.maxVel) < ERR_NOERR) {
00425         ROS_ERROR("[MCDC3006S] setDriverConf() --> Error setting the minimum position");
00426 
00427         return ERR_CONF;
00428     }
00429     if (set_max_acc(dc.maxAcc) < ERR_NOERR) {
00430         ROS_ERROR("[MCDC3006S] setDriverConf() --> Error setting the minimum position");
00431 
00432         return ERR_CONF;
00433     }
00434     if (set_max_dec(dc.maxDec) < ERR_NOERR) {
00435         ROS_ERROR("[MCDC3006S] setDriverConf() --> Error setting the minimum position");
00436 
00437         return ERR_CONF;
00438     }
00439 
00440     return ERR_NOERR;
00441 }
00442 
00444 
00445 int Mcdc3006s::set_max_pos(long int maxPos)
00446 {
00447     char command[SP_MSG_SIZE];
00448 
00449     if (maxPos <= 0) {
00450         fprintf(stderr, "setDriveMaxPos --> ERROR. Out of range: "
00451                 "maxPos must be higher than 0 (You entered %ld).\n\r",
00452                 maxPos);
00453 
00454         return ERR_OUTOFRANGE;
00455     }
00456     sprintf(command, "LL%ld\n\r\0", maxPos);
00457 
00458     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00459         ROS_ERROR("[MCDC3006S] setDriverMaxPos() --> Error writing to the driver");
00460 
00461         return ERR_WRI;
00462     }
00463 
00464     if (activate_limits(ACTIVATE) < ERR_NOERR) {
00465         ROS_ERROR("[MCDC3006S] setDriverMaxPos() --> Error activating the driver");
00466 
00467         return ERR_POSLIMIT;
00468     }
00469 
00470     return ERR_NOERR;
00471 }
00472 
00474 
00475 int Mcdc3006s::set_min_pos(long int minPos)
00476 {
00477     char command[SP_MSG_SIZE];
00478 
00479     if (minPos >= 0) {
00480         fprintf(stderr, "setDriveMinPos --> ERROR. minPos must be lower than 0 (You entered %ld).\n\r", minPos);
00481 
00482         return ERR_OUTOFRANGE;
00483     }
00484 
00485     sprintf(command, "LL%ld\n\r\0", minPos);
00486 
00487     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00488         ROS_ERROR("[MCDC3006S] setDriverMinPos() --> Error writing to the driver");
00489 
00490         return ERR_WRI;
00491     }
00492 
00493     if (activate_limits(ACTIVATE) < ERR_NOERR) {
00494         ROS_ERROR("[MCDC3006S] setDriverMaxPos() --> Error activating the driver");
00495 
00496         return ERR_POSLIMIT;
00497     }
00498 
00499     return ERR_NOERR;
00500 }
00501 
00503 
00504 int Mcdc3006s::set_max_vel(long int maxVel)
00505 {
00506     char command[SP_MSG_SIZE];
00507 
00508     sprintf(command, "SP%ld\n\r\0", maxVel);
00509 
00510     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00511         ROS_ERROR("[MCDC3006S] setDriverMaxVel() --> Error writing to the driver");
00512 
00513         return ERR_WRI;
00514     }
00515 
00516     return ERR_NOERR;
00517 }
00518 
00520 
00521 int Mcdc3006s::set_max_acc(long int maxAcc)
00522 {
00523     char command[SP_MSG_SIZE];
00524 
00525     sprintf(command, "AC%ld\n\r\0", maxAcc);
00526 
00527     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00528         ROS_ERROR("[MCDC3006S] setDriverMaxAcc() --> Error writing to the driver");
00529 
00530         return ERR_WRI;
00531     }
00532 
00533     return ERR_NOERR;
00534 }
00535 
00537 
00538 int Mcdc3006s::set_max_dec(long int maxDec)
00539 {
00540     char command[SP_MSG_SIZE];
00541 
00542     sprintf(command, "DEC%ld\n\r\0", maxDec);
00543 
00544     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00545         ROS_ERROR("[MCDC3006S] setDriverMaxDec() --> Error writing to the driver");
00546 
00547         return ERR_WRI;
00548     }
00549 
00550     return ERR_NOERR;
00551 }
00552 
00554 
00555 int Mcdc3006s::set_cur_lim(int cl)
00556 {
00557     char command[SP_MSG_SIZE];
00558 
00559     sprintf(command, "LCC%d\n\r\0", cl);
00560 
00561     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00562         ROS_ERROR("[MCDC3006S] setDriverCurLim() --> Error writing to the driver");
00563 
00564         return ERR_WRI;
00565     }
00566 
00567     return ERR_NOERR;
00568 }
00569 
00571 
00572 int Mcdc3006s::set_peak_cur_lim(int pcl)
00573 {
00574     char command[SP_MSG_SIZE];
00575 
00576     sprintf(command, "LPC%d\n\r\0", pcl);
00577 
00578     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00579         ROS_ERROR("[MCDC3006S] setDriverPCurLim() --> Error writing to the driver");
00580 
00581         return ERR_WRI;
00582     }
00583 
00584     return ERR_NOERR;
00585 }
00586 
00588 
00589 int Mcdc3006s::set_baudrate(int baud)
00590 {
00591     int c = -1;
00592     int n;
00593     char command[SP_MSG_SIZE];
00594     for (n = 1; n < 10; n++) {
00595         if (baud == 600 * n) {
00596             c = 1;
00597             break;
00598         }
00599     }
00600     if (c) {
00601         sprintf(command, "BAUD%d\n\r", baud);
00602     }
00603     else {
00604         fprintf(stderr, "driverMCDC3006S - setDriverBaud ERROR %d not supported\n\r", baud);
00605 
00606         return ERR_OUTOFRANGE;
00607     }
00608     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00609         ROS_ERROR("[MCDC3006S] setDriverCurLim() --> Error writing to the driver\n\r");
00610 
00611         return ERR_WRI;
00612     }
00613 
00614     return ERR_NOERR;
00615 }
00616 
00618 
00619 int Mcdc3006s::move_abs_pos(long int pos)
00620 {
00621     char command[SP_MSG_SIZE]; // = "LA";
00622 
00623     sprintf(command, "LA%ld\n\r\0", pos);
00624     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00625         ROS_ERROR("[MCDC3006S] moveDriverAbsPos() --> Error writing to the driver\n\r");
00626 
00627         return ERR_WRI;
00628     }
00629 
00630     sprintf(command, "M\n\r\0");
00631     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00632         ROS_ERROR("[MCDC3006S] moveDriverAbsPos() --> Error writing to the driver\n\r");
00633 
00634         return ERR_WRI;
00635     }
00636 
00637     return ERR_NOERR;
00638 }
00639 
00641 
00642 int Mcdc3006s::move_rel_pos(long int pos)
00643 {
00644     char command[SP_MSG_SIZE]; // = "LR";
00645 
00646     sprintf(command, "LR%ld\n\r\0", pos);
00647     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00648         ROS_ERROR("[MCDC3006S] moveDriverRelPos() --> Error writing to the driver\n\r");
00649 
00650         return ERR_WRI;
00651     }
00652 
00653     sprintf(command, "M\n\r\0");
00654     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00655         ROS_ERROR("[MCDC3006S] moveDriverRelPos() --> Error writing to the driver\n\r");
00656 
00657         return ERR_WRI;
00658     }
00659 
00660     return ERR_NOERR;
00661 }
00662 
00664 
00665 int Mcdc3006s::move_vel(long int vel)
00666 {
00667     char command[SP_MSG_SIZE];
00668 
00669     sprintf(command, "V%ld\n\r\0", vel);
00670     if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00671         ROS_ERROR("[MCDC3006S] moveDriverVel() --> Error writing to the driver\n\r");
00672 
00673         return ERR_WRI;
00674     }
00675 
00676     return ERR_NOERR;
00677 }
00678 
00680 
00681 int Mcdc3006s::save_to_flash()
00682 {
00683     char command[SP_MSG_SIZE], response[SP_MSG_SIZE];
00684 
00685     sprintf(command, "EEPSAV\n\r\0");
00686     if (_comm.askToRS232(command, strlen(command), response) < ERR_NOERR) {
00687         ROS_ERROR("[MCDC3006S] saveToFlash() --> Error. cannot communicate with the driver");
00688 
00689         return ERR_COM;
00690     }
00691     fprintf(stderr, "driverMCDC30065S - saveToFlash : %s\n", response);
00692 
00693     return ERR_NOERR;
00694 }
00695 
00697 
00698 int Mcdc3006s::activate_limits(int action)
00699 {
00700     char command[SP_MSG_SIZE];
00701 
00702     if (action == ACTIVATE || action == DEACTIVATE) {
00703         sprintf(command, "APL%d\n\r", action);
00704 
00705         if (_comm.writeToRS232(command, strlen(command)) < ERR_NOERR) {
00706             ROS_ERROR("[MCDC3006S] activateLimits() --> Error. cannot write to the driver");
00707 
00708             return ERR_WRI;
00709         }
00710 
00711         return ERR_NOERR;
00712     }
00713     ROS_ERROR("[MCDC3006S] activateLimits() --> Error Out of range");
00714 
00715     return ERR_OUTOFRANGE;
00716 }
00717 
00719 
00720 int Mcdc3006s::set_home_position(long int home)
00721 {
00722     char command[SP_MSG_SIZE];
00723     sprintf(command, "HO%ld\n\r", home);
00724 
00725     if (_comm.writeToRS232(command, strlen(command))) {
00726         ROS_ERROR("[MCDC3006S] setDriverHomePosition() --> Error\n\r");
00727 
00728         return ERR_WRI;
00729     }
00730 
00731     return ERR_NOERR;
00732 }
00733 
00735 
00736 int Mcdc3006s::calibrate(int limit)
00737 {
00738     // This is the input 4 of the driver. In this input is connected the sensor.
00739     char calibrationCommand[SP_MSG_SIZE];
00740     char calibrationResponse[SP_MSG_SIZE];
00741 
00742     int status = 1;
00743     struct timeval before, now;
00744 
00745     // Configuring the driver parameters
00746     // LCC: Load Continous Current LPC: Load peak current
00747     sprintf(calibrationCommand, "LCC%d\n\r", CALIBRATION_CURRENT_LIMIT);
00748     if (_comm.writeToRS232(calibrationCommand, strlen(calibrationCommand)) < ERR_NOERR) {
00749         ROS_ERROR("[MCDC3006S] calibrateDriver() --> Error\n\r");
00750 
00751         return ERR_NOHOME;
00752     }
00753 
00754     sprintf(calibrationCommand, "HL8\n\r");
00755     if (_comm.writeToRS232(calibrationCommand, strlen(calibrationCommand)) < ERR_NOERR) {
00756         ROS_ERROR("[MCDC3006S] calibrateDriver() --> Error\n\r");
00757 
00758         return ERR_NOHOME;
00759     }
00760 
00761     sprintf(calibrationCommand, "V%d\n\r", CALIBRATION_VELOCITY);
00762     if (_comm.writeToRS232(calibrationCommand, strlen(calibrationCommand)) < ERR_NOERR) {
00763         ROS_ERROR("[MCDC3006S] calibrateDriver() --> Error\n\r");
00764 
00765         return ERR_NOHOME;
00766     }
00767     gettimeofday(&before, 0);
00768 
00769     do {
00770         char str_tmp[SP_MSG_SIZE];
00771         char calibrationResponse[SP_MSG_SIZE];
00772 
00773         sprintf(str_tmp, "OST\n\r\0");
00774         _comm.askToRS232(str_tmp, strlen(str_tmp), calibrationResponse); 
00775 
00776         gettimeofday(&now, 0);
00777         if (atoi(calibrationResponse) & DRIVER_INPUT_4_MASK) {
00778             status = ERR_NOERR; // Sensor Reached OK.
00779         }
00780         else if (atoi(calibrationResponse) & CURRENT_LIMITING_MASK) {
00781             ROS_ERROR(
00782                 "[MCDC3006S] calibrateDriver() --> Error Calibrating the driver. Current Limit Reached Could not establish home position");
00783             status = ERR_CURLIM; // Error calibrating the driver (limit current reached)
00784 
00785         }
00786         else if (now.tv_sec - before.tv_sec > CALIBRATION_TIMEOUT) {
00787             ROS_ERROR(
00788                 "[MCDC3006S] calibrateDriver() --> Error Calibrating the driver. Timeout. Could not establish home position");
00789             status = ERR_TIMEOUT; // Timeout reached before arriving to the sensor
00790         }
00791     }
00792     while(status == 1);
00793 
00794     // Assuring that the driver stops at this point
00795     char str_tmp1[SP_MSG_SIZE];
00796     sprintf(str_tmp1, "V0\n\r\0");
00797     _comm.writeToRS232(str_tmp1, strlen(str_tmp1));
00798 
00799     // Moving the driver to 0.
00800     if (status == ERR_NOERR) {
00801         ROS_DEBUG("[MCDC3006S] calibrateDriver() --> Going to home position");
00802 
00803         // move driver to the requested position (in pulses)
00804         move_rel_pos(limit);
00805 
00806         sleep(3);
00807 
00808         //set home position
00809         if (set_home_position(0) == 0) {
00810             move_rel_pos(0);
00811         }
00812         else {
00813             ROS_ERROR(
00814                 "[MCDC3006S] calibrateDriver() --> Error Calibrating the driver. Could not establish home position");
00815             status = ERR_NOHOME;
00816         }
00817     }
00818 
00819     return (status);
00820 }
00821 


maggie_motor_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:06:39