mrp2_serial.cpp
Go to the documentation of this file.
00001 #include "mrp2_hardware/mrp2_serial.h"
00002 
00003 static const double MILLION = 1000000.0;
00004 static const double BILLION = 1000000000.0;
00005 
00006 MRP2_Serial::MRP2_Serial (std::string port_name, uint32_t baudrate, std::string mode, bool simple)
00007  : _port_name(port_name), _baudrate(baudrate), _mode(mode), simple_(simple)
00008 {
00009   std::fill_n(speeds, 2, 0);
00010   e_stop = false;
00011   dir_left =false;
00012   dir_right=true;
00013   tempDataIndex = 0;
00014   seekForChar = true;
00015   line_ok_ = true;
00016   startChar = '$';
00017   serial_port.open_port(_port_name, _baudrate, _mode);
00018   use_usb_ = false;
00019   read_timeout_ = 0.02;
00020 
00021 }
00022 
00023 MRP2_Serial::MRP2_Serial (uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr, bool simple)
00024  : vendor_id_(vendor_id), product_id_(product_id), ep_in_addr_(ep_in_addr), ep_out_addr_(ep_out_addr), simple_(simple)
00025 {
00026   std::fill_n(speeds, 2, 0);
00027   e_stop = false;
00028   dir_left =false;
00029   dir_right=true;
00030   tempDataIndex = 0;
00031   seekForChar = true;
00032   line_ok_ = true;
00033   startChar = '$';
00034   usb_port.open_device(vendor_id_, product_id_, ep_in_addr_, ep_out_addr_);
00035   use_usb_ = true;
00036   read_timeout_ = 0.02;
00037 }
00038 
00039 
00040 MRP2_Serial::~MRP2_Serial ()
00041 {
00042   
00043 }
00044 
00045 void
00046 MRP2_Serial::update ()
00047 {
00048 }
00049 
00050 void
00051 MRP2_Serial::set_speeds(int32_t left_speed, int32_t right_speed)
00052 {
00053   uint8_t send_array[20];
00054 
00055   left_speed *= -1;
00056   right_speed *= -1;
00057 
00058   unsigned char s1 = (unsigned char)(left_speed >> 24);
00059   unsigned char s2 = (unsigned char)(left_speed >> 16);
00060   unsigned char s3 = (unsigned char)(left_speed >> 8);
00061   unsigned char s4 = (unsigned char)left_speed;
00062 
00063   unsigned char s5 = (unsigned char)(right_speed >> 24);
00064   unsigned char s6 = (unsigned char)(right_speed >> 16);
00065   unsigned char s7 = (unsigned char)(right_speed >> 8);
00066   unsigned char s8 = (unsigned char)right_speed;
00067   
00068   send_array[0] = '$';
00069   send_array[1] = setSPEEDS;
00070   send_array[2] = 8;
00071   send_array[3] = checksum_check_array(send_array, 3);
00072   send_array[4] = s1;
00073   send_array[5] = s2;
00074   send_array[6] = s3;
00075   send_array[7] = s4;
00076   send_array[8] = s5;
00077   send_array[9] = s6;
00078   send_array[10] = s7;
00079   send_array[11] = s8;
00080   send_array[12] = checksum_check_array(send_array, 12);
00081 
00082   send_and_get_reply(setSPEEDS, send_array, 13, true);
00083 
00084 }
00085 
00086 void 
00087 MRP2_Serial::set_speed_l(int32_t left_speed)
00088 {
00089   uint8_t send_array[20];
00090 
00091   left_speed *= -1;
00092 
00093   unsigned char s1 = (unsigned char)(left_speed >> 24);
00094   unsigned char s2 = (unsigned char)(left_speed >> 16);
00095   unsigned char s3 = (unsigned char)(left_speed >> 8);
00096   unsigned char s4 = (unsigned char)left_speed;
00097   
00098   send_array[0] = '$';
00099   send_array[1] = setSPEED_L;
00100   send_array[2] = 4;
00101   send_array[3] = checksum_check_array(send_array, 3);
00102   send_array[4] = s1;
00103   send_array[5] = s2;
00104   send_array[6] = s3;
00105   send_array[7] = s4;
00106   send_array[8] = checksum_check_array(send_array, 8);
00107 
00108   send_and_get_reply(setSPEED_L, send_array, 9, true);
00109 
00110 }
00111 
00112 void 
00113 MRP2_Serial::set_speed_r(int32_t right_speed)
00114 {
00115   uint8_t send_array[20];
00116 
00117   right_speed *= -1;
00118 
00119   unsigned char s5 = (unsigned char)(right_speed >> 24);
00120   unsigned char s6 = (unsigned char)(right_speed >> 16);
00121   unsigned char s7 = (unsigned char)(right_speed >> 8);
00122   unsigned char s8 = (unsigned char)right_speed;
00123   
00124   send_array[0] = '$';
00125   send_array[1] = setSPEED_R;
00126   send_array[2] = 4;
00127   send_array[3] = checksum_check_array(send_array, 3);
00128   send_array[4] = s5;
00129   send_array[5] = s6;
00130   send_array[6] = s7;
00131   send_array[7] = s8;
00132   send_array[8] = checksum_check_array(send_array, 8);
00133 
00134   send_and_get_reply(setSPEED_R, send_array, 9, true);
00135 
00136 }
00137 
00138 void
00139 MRP2_Serial::set_param_pid(char side, char param, float value)
00140 {
00141 
00142   //printf("Float value: %f\n", value);
00143   uint8_t send_array[20];
00144   
00145   union {
00146     float f;
00147     unsigned char bytes[4];
00148   } val;
00149 
00150   val.f = value;
00151 
00152 
00153   unsigned char fl_array[4];
00154   memcpy(fl_array, &val, 4);
00155 
00156 
00157   if(side == 'L')
00158   {
00159     switch(param)
00160     {
00161       case 'P':
00162         send_array[1] = setPARAM_KP_L;
00163         break;
00164       case 'I':
00165         send_array[1] = setPARAM_KI_L;
00166         break;
00167       case 'D':
00168         send_array[1] = setPARAM_KD_L;
00169         break;
00170       default:
00171         break;
00172     }
00173   }else if(side == 'R')
00174   {
00175     switch(param)
00176     {
00177       case 'P':
00178         send_array[1] = setPARAM_KP_R;
00179         break;
00180       case 'I':
00181         send_array[1] = setPARAM_KI_R;
00182         break;
00183       case 'D':
00184         send_array[1] = setPARAM_KD_R;
00185         break;
00186       default:
00187         break;
00188     }
00189   }
00190 
00191   //printf("Side: %c, param: %c:\n", side, param);
00192   //print_array(fl_array, 4);
00193 
00194   send_array[0] = '$';
00195   send_array[2] = 4;
00196   send_array[3] = checksum_check_array(send_array, 3);
00197   send_array[4] = fl_array[3];
00198   send_array[5] = fl_array[2];
00199   send_array[6] = fl_array[1];
00200   send_array[7] = fl_array[0];
00201   send_array[8] = checksum_check_array(send_array, 8);
00202 
00203   send_and_get_reply(send_array[1], send_array, 9, true);
00204 }
00205 
00206 void
00207 MRP2_Serial::set_param_imax(char side, uint32_t value)
00208 {
00209   uint8_t send_array[20];
00210   
00211   if(side == 'L'){
00212     send_array[1] = setPARAM_IMAX_L;
00213   }else if(side == 'R'){
00214     send_array[1] = setPARAM_IMAX_R;
00215   }
00216 
00217   send_array[0] = '$';  
00218   send_array[2] = 4;
00219   send_array[3] = checksum_check_array(send_array, 3);
00220   send_array[4] = (unsigned char)(value >> 24);
00221   send_array[5] = (unsigned char)(value >> 16);
00222   send_array[6] = (unsigned char)(value >> 8);
00223   send_array[7] = (unsigned char)value;
00224   send_array[8] = checksum_check_array(send_array, 8);
00225 
00226   send_and_get_reply(send_array[1], send_array, 9, true);
00227 
00228 }
00229 
00230 void
00231 MRP2_Serial::set_maxspeed_fwd(uint32_t value)
00232 {
00233   uint8_t send_array[20];
00234 
00235   send_array[0] = '$';
00236   send_array[1] = setMAXSPEED_FWD;  
00237   send_array[2] = 4;
00238   send_array[3] = checksum_check_array(send_array, 3);
00239   send_array[4] = (unsigned char)(value >> 24);
00240   send_array[5] = (unsigned char)(value >> 16);
00241   send_array[6] = (unsigned char)(value >> 8);
00242   send_array[7] = (unsigned char)value;
00243   send_array[8] = checksum_check_array(send_array, 8);
00244 
00245   send_and_get_reply(setMAXSPEED_FWD, send_array, 9, true);
00246 }
00247 
00248 void
00249 MRP2_Serial::set_maxspeed_rev(uint32_t value)
00250 {
00251   uint8_t send_array[20];
00252 
00253   send_array[0] = '$';
00254   send_array[1] = setMAXSPEED_REV;  
00255   send_array[2] = 4;
00256   send_array[3] = checksum_check_array(send_array, 3);
00257   send_array[4] = (unsigned char)(value >> 24);
00258   send_array[5] = (unsigned char)(value >> 16);
00259   send_array[6] = (unsigned char)(value >> 8);
00260   send_array[7] = (unsigned char)value;
00261   send_array[8] = checksum_check_array(send_array, 8);
00262 
00263   send_and_get_reply(setMAXSPEED_REV, send_array, 9, true);
00264 }
00265 
00266 void
00267 MRP2_Serial::set_max_accel(uint32_t value)
00268 {
00269   uint8_t send_array[20];
00270 
00271   send_array[0] = '$';
00272   send_array[1] = setMAXACCEL;  
00273   send_array[2] = 4;
00274   send_array[3] = checksum_check_array(send_array, 3);
00275   send_array[4] = (unsigned char)(value >> 24);
00276   send_array[5] = (unsigned char)(value >> 16);
00277   send_array[6] = (unsigned char)(value >> 8);
00278   send_array[7] = (unsigned char)value;
00279   send_array[8] = checksum_check_array(send_array, 8);
00280 
00281 
00282   send_and_get_reply(setMAXACCEL, send_array, 9, true);
00283 }
00284 
00285 void
00286 MRP2_Serial::set_estop(bool value)
00287 {
00288   uint8_t send_array[20];
00289 
00290   send_array[0] = '$';
00291   send_array[1] = setESTOP;  
00292   send_array[2] = 1;
00293   send_array[3] = checksum_check_array(send_array, 3);
00294   send_array[4] = (unsigned char)value;
00295   send_array[5] = checksum_check_array(send_array, 6);
00296 
00297   send_and_get_reply(setESTOP, send_array, 6, true);
00298 }
00299 
00300 void
00301 MRP2_Serial::clear_diag(int diag)
00302 {
00303   uint8_t send_array[20];
00304   send_array[0] = '$';
00305   send_array[1] = clearDIAG;
00306   send_array[2] = 1;
00307   send_array[3] = checksum_check_array(send_array, 3);
00308   send_array[4] = diag; 
00309   send_array[5] = checksum_check_array(send_array, 5);
00310 
00311   send_and_get_reply(clearDIAG, send_array, 6, true);
00312 }
00313 
00314 std::vector<int> 
00315 MRP2_Serial::get_speeds(bool update)
00316 {
00317   if (update) {
00318     uint8_t send_array[2];
00319     send_array[0] = '$';
00320     send_array[1] = getSPEEDS;
00321     send_and_get_reply(getSPEEDS, send_array, 2, false);
00322   }
00323   return _speeds;
00324 }
00325 
00326 int 
00327 MRP2_Serial::get_speed_l(bool update)
00328 {
00329   if (update) {
00330     uint8_t send_array[2];
00331     send_array[0] = '$';
00332     send_array[1] = getSPEED_L;
00333     send_and_get_reply(getSPEED_L, send_array, 2, false);
00334   }
00335   return _speed_l;
00336 }
00337 
00338 int 
00339 MRP2_Serial::get_speed_r(bool update)
00340 {
00341   if (update) {
00342     uint8_t send_array[2];
00343     send_array[0] = '$';
00344     send_array[1] = getSPEED_R;
00345     send_and_get_reply(getSPEED_R, send_array, 2, false);
00346   }
00347   return _speed_r;
00348 }
00349 
00350 float
00351 MRP2_Serial::get_param_pid(char side, char param, bool update)
00352 {
00353   uint8_t send_array[2];
00354 
00355   send_array[0] = '$';
00356 
00357   if(side == 'L')
00358   {
00359     if(param == 'P'){
00360       if(update){
00361         send_array[1] = getPARAM_KP_L;
00362         send_and_get_reply(send_array[1], send_array, 2, false);
00363       }
00364       return _Kp_l;
00365     }else if(param == 'I'){
00366       if(update){
00367         send_array[1] = getPARAM_KI_L;
00368         send_and_get_reply(send_array[1], send_array, 2, false);
00369       }
00370       return _Ki_l;
00371     }else if(param == 'D'){
00372       if(update){
00373         send_array[1] = getPARAM_KD_L;
00374         send_and_get_reply(send_array[1], send_array, 2, false);
00375       }
00376       return _Kd_l;
00377     }
00378   }
00379   else if(side == 'R'){
00380     if(param == 'P'){
00381       if(update){
00382         send_array[1] = getPARAM_KP_R;
00383         send_and_get_reply(send_array[1], send_array, 2, false);
00384       }
00385       return _Kp_r;
00386     }else if(param == 'I'){
00387       if(update){
00388         send_array[1] = getPARAM_KI_R;
00389         send_and_get_reply(send_array[1], send_array, 2, false);
00390       }
00391       return _Ki_r;
00392     }else if(param == 'D'){
00393       if(update){
00394         send_array[1] = getPARAM_KD_R;
00395         send_and_get_reply(send_array[1], send_array, 2, false);
00396       }
00397       return _Kd_r;
00398     }
00399   }
00400 
00401 }
00402 
00403 std::vector<int>
00404 MRP2_Serial::get_param_imax(char side, bool update)
00405 {
00406   if (update) {
00407     uint8_t send_array[2];
00408 
00409     send_array[0] = '$';
00410 
00411     if(side == 'L')
00412     {
00413       send_array[1] = getPARAM_IMAX_L;
00414     }else if(side =='R')
00415     {
00416       send_array[1] = getPARAM_IMAX_R;
00417     }
00418     
00419     send_and_get_reply(send_array[1], send_array, 2, false);
00420   }
00421   return _imax;
00422 }
00423 
00424 int
00425 MRP2_Serial::get_maxspeed_fwd(bool update)
00426 {
00427   if (update) {
00428     uint8_t send_array[2];
00429     send_array[0] = '$';
00430     send_array[1] = getMAXSPEED_FWD;
00431     send_and_get_reply(getMAXSPEED_FWD, send_array, 2, false);
00432   }
00433   return _maxspeed_fwd;
00434 }
00435 
00436 int
00437 MRP2_Serial::get_maxspeed_rev(bool update)
00438 {
00439   if (update) {
00440     uint8_t send_array[2];
00441     send_array[0] = '$';
00442     send_array[1] = getMAXSPEED_REV;
00443     send_and_get_reply(getMAXSPEED_REV, send_array, 2, false);
00444   }
00445   return _maxspeed_rev;
00446 }
00447 
00448 int
00449 MRP2_Serial::get_maxaccel(bool update)
00450 {
00451   if (update) {
00452     uint8_t send_array[2];
00453     send_array[0] = '$';
00454     send_array[1] = getMAXACCEL;
00455     send_and_get_reply(getMAXACCEL, send_array, 2, false);
00456   }
00457   return _maxaccel;
00458 }
00459 
00460 int
00461 MRP2_Serial::get_batt_volt(bool update)
00462 {
00463   if (update) {
00464     uint8_t send_array[2];
00465     send_array[0] = '$';
00466     send_array[1] = getBATT_VOLT;
00467     send_and_get_reply(getBATT_VOLT, send_array, 2, false);
00468   }
00469   return _batt_volt;
00470 }
00471 
00472 int
00473 MRP2_Serial::get_batt_current(bool update)
00474 {
00475   if (update) {
00476     uint8_t send_array[2];
00477     send_array[0] = '$';
00478     send_array[1] = getBATT_CURRENT;
00479     send_and_get_reply(getBATT_CURRENT, send_array, 2, false);
00480   }
00481   return _batt_current;
00482 }
00483 
00484 int
00485 MRP2_Serial::get_batt_soc(bool update)
00486 {
00487   if (update) {
00488     uint8_t send_array[2];
00489     send_array[0] = '$';
00490     send_array[1] = getBATT_SOC;
00491     send_and_get_reply(getBATT_SOC, send_array, 2, false);
00492   }
00493   return _batt_soc;
00494 }
00495 
00496 std::vector<int> 
00497 MRP2_Serial::get_positions(bool update)
00498 {
00499   if (update) {
00500     uint8_t send_array[2];
00501     send_array[0] = '$';
00502     send_array[1] = getPOSITIONS;
00503     send_and_get_reply(getPOSITIONS, send_array, 2, false);
00504   }
00505   return _positions;
00506 }
00507 
00508 int 
00509 MRP2_Serial::get_position_l(bool update)
00510 {
00511   if (update) {
00512     uint8_t send_array[2];
00513     send_array[0] = '$';
00514     send_array[1] = getPOSITION_L;
00515     send_and_get_reply(getPOSITION_L, send_array, 2, false);
00516   }
00517   return _position_l;
00518 }
00519 
00520 int 
00521 MRP2_Serial::get_position_r(bool update)
00522 {
00523   if (update) {
00524     uint8_t send_array[2];
00525     send_array[0] = '$';
00526     send_array[1] = getPOSITION_R;
00527     send_and_get_reply(getPOSITION_R, send_array, 2, false);
00528   }
00529   return _position_r;
00530 }
00531 
00532 std::vector<int> 
00533 MRP2_Serial::get_bumpers(bool update)
00534 {
00535   if (update) {
00536     uint8_t send_array[2];
00537     send_array[0] = '$';
00538     send_array[1] = getBUMPERS;
00539     send_and_get_reply(getBUMPERS, send_array, 2, false);
00540   }
00541   return _bumpers;
00542 }
00543 
00544 void 
00545 MRP2_Serial::reset_positions()
00546 {
00547   uint8_t send_array[10];
00548   
00549   send_array[0] = '$';
00550   send_array[1] = resetPOSITIONS;
00551   send_array[2] = 0;
00552   send_array[3] = checksum(4);
00553 
00554   send_and_get_reply(resetPOSITIONS, send_array, 4, true);
00555 }
00556 
00557 void 
00558 MRP2_Serial::reset_position_l()
00559 {
00560   uint8_t send_array[10];
00561   
00562   send_array[0] = '$';
00563   send_array[1] = resetPOSITION_L;
00564   send_array[2] = 0;
00565   send_array[3] = checksum(4);
00566 
00567   send_and_get_reply(resetPOSITION_L, send_array, 4, true);
00568 }
00569 
00570 void 
00571 MRP2_Serial::reset_position_r()
00572 {
00573   uint8_t send_array[10];
00574   
00575   send_array[0] = '$';
00576   send_array[1] = resetPOSITION_R;
00577   send_array[2] = 0;
00578   send_array[3] = checksum(4);
00579 
00580   send_and_get_reply(resetPOSITION_R, send_array, 4, true);
00581 }
00582 
00583 bool
00584 MRP2_Serial::get_estop(bool update)
00585 {
00586   if (update) {
00587     uint8_t send_array[2];
00588     send_array[0] = '$';
00589     send_array[1] = getESTOP;
00590     send_and_get_reply(getESTOP, send_array, 2, false);
00591   }
00592   return _estop;
00593 }
00594 
00595 void 
00596 MRP2_Serial::update_diag()
00597 {
00598   uint8_t send_array[2];
00599   send_array[0] = '$';
00600   send_array[1] = getDIAG;
00601   send_and_get_reply(getSPEED_R, send_array, 2, false);
00602 }
00603 
00604 bool
00605 MRP2_Serial::get_diag(int diag)
00606 {
00607   switch(diag){
00608       case DIAG_MOTOR_STALL_L:
00609         return _diag_motor_stall_l;
00610     break;
00611       case DIAG_MOTOR_STALL_R:
00612         return _diag_motor_stall_r;
00613     break;
00614       case DIAG_BATT_LOW:
00615         return _diag_batt_low;
00616     break;
00617       case DIAG_BATT_HIGH:
00618         return _diag_batt_high;
00619     break;
00620       case DIAG_MOTOR_DRVR_ERR:
00621         return _diag_motor_drvr_err;
00622     break;
00623       case DIAG_AUX_LIGHTS_ERR:
00624         return _diag_aux_lights_err;
00625     break;
00626   }
00627 }
00628 
00629 int 
00630 MRP2_Serial::get_batt_cell_capacity(bool update)
00631 {
00632   if (update) {
00633     uint8_t send_array[2];
00634     send_array[0] = '$';
00635     send_array[1] = getBATT_CELL_CAPACITY;
00636     send_and_get_reply(getBATT_CELL_CAPACITY, send_array, 2, false);
00637   }
00638   return _batt_cell_capacity;
00639 }
00640 
00641 void
00642 MRP2_Serial::set_bumper_estop(bool value)
00643 {
00644   uint8_t send_array[20];
00645 
00646   send_array[0] = '$';
00647   send_array[1] = setBUMPER_ESTOP;  
00648   send_array[2] = 1;
00649   send_array[3] = checksum_check_array(send_array, 3);
00650   send_array[4] = (unsigned char)value;
00651   send_array[5] = checksum_check_array(send_array, 6);
00652 
00653   send_and_get_reply(setBUMPER_ESTOP, send_array, 6, true);
00654 }
00655 
00656 bool 
00657 MRP2_Serial::get_bumper_estop(bool update)
00658 {
00659   if (update) {
00660     uint8_t send_array[2];
00661     send_array[0] = '$';
00662     send_array[1] = getBUMPER_ESTOP;
00663     send_and_get_reply(getBUMPER_ESTOP, send_array, 2, false);
00664   }
00665   return _bumper_estop;
00666 }
00667     
00668 bool
00669 MRP2_Serial::get_estop_button(bool update)
00670 {
00671   if (update) {
00672     uint8_t send_array[2];
00673     send_array[0] = '$';
00674     send_array[1] = getESTOP_BTN;
00675     send_and_get_reply(getESTOP_BTN, send_array, 2, false);
00676   }
00677   return _estop_btn;
00678 }
00679 
00680 std::vector<int> 
00681 MRP2_Serial::get_sonars(bool update)
00682 {
00683   if (update) {
00684     uint8_t send_array[2];
00685     send_array[0] = '$';
00686     send_array[1] = getSONARS;
00687     send_and_get_reply(getSONARS, send_array, 2, false);
00688   }
00689   return _sonars;
00690 }
00691 
00692 int
00693 MRP2_Serial::send_and_get_reply(uint8_t _command, uint8_t *send_array, int send_size, bool is_ack)
00694 {
00695   struct timeval  tv1, tv2;
00696   struct timespec ctv1, ctv2;
00697 
00698   double _time_diff = 0;
00699   int _ret_val = 0;
00700 
00701   gettimeofday(&tv1, NULL);
00702   
00703   //printf("%ld.%06ld - SAGR called: %d\n", tv1.tv_sec, tv1.tv_usec, _command);
00704 
00705   while (!line_ok_)
00706   {
00707     usleep(1);
00708   }
00709 
00710   line_ok_ = false;
00711 
00712   gettimeofday(&tv1, NULL);
00713 
00714   //printf("%ld.%06ld - Serial send called: %d\n", tv1.tv_sec, tv1.tv_usec, _command);
00715 
00716   if(use_usb_)
00717     int ret = usb_port.write_bytes(send_array,send_size);
00718   else
00719     int ret = serial_port.send_buf(send_array,send_size);
00720 
00721   gettimeofday(&tv1, NULL);
00722   gettimeofday(&tv2, NULL);
00723 
00724   clock_gettime(CLOCK_MONOTONIC, &ctv1);
00725   clock_gettime(CLOCK_MONOTONIC, &ctv2);
00726 
00727   //printf("%ld.%06ld - Command sent: %d\n", ctv1.tv_sec, ctv1.tv_nsec, _command);
00728 
00729   if (is_ack)
00730   {
00731     _ack_data = 0;
00732     while (_ack_data != _command && _time_diff < read_timeout_)
00733     {
00734       _ret_val = read_serial(ACK);
00735       //gettimeofday(&tv2, NULL);
00736       //_time_diff = (double) (tv2.tv_usec - tv1.tv_usec) / MILLION + (double) (tv2.tv_sec - tv1.tv_sec);
00737       clock_gettime(CLOCK_MONOTONIC, &ctv2);
00738       _time_diff = ctv2.tv_sec - ctv1.tv_sec + (ctv2.tv_nsec - ctv1.tv_nsec) / BILLION;
00739       usleep(1);
00740     }
00741     line_ok_ = true;
00742     /*if(_ack_data != _command && _time_diff >= _time_out)
00743       printf("ACK Timeout!, Command: %d\n", _command);
00744     else if (_ack_data == _command && _time_diff < _time_out)
00745       printf("%ld.%06ld - Command reply received: %d\n", ctv2.tv_sec, ctv2.tv_nsec, _command);*/
00746   }
00747   else
00748   {
00749     while (_ret_val == 0 && _time_diff < read_timeout_)
00750     {
00751 
00752         _ret_val = read_serial(_command);
00753         //gettimeofday(&tv2, NULL);
00754         //_time_diff = (double) (tv2.tv_usec - tv1.tv_usec) / MILLION + (double) (tv2.tv_sec - tv1.tv_sec);
00755         clock_gettime(CLOCK_MONOTONIC, &ctv2);
00756         _time_diff = ctv2.tv_sec - ctv1.tv_sec + (ctv2.tv_nsec - ctv1.tv_nsec) / BILLION;
00757         usleep(1);
00758     }
00759     line_ok_ = true;
00760     /*if(_ret_val == 0 && _time_diff >= _time_out)
00761       printf("Read Timeout!, Command: %d\n", _command);
00762     else if (_ret_val != 0 && _time_diff < _time_out)
00763       printf("%ld.%06ld - Command reply received: %d\n", ctv2.tv_sec, ctv2.tv_nsec, _command);*/
00764 
00765   }
00766   
00767   
00768   return _ret_val;
00769 }
00770 
00771 bool
00772 MRP2_Serial::is_available ()
00773 {
00774   return line_ok_;
00775 }
00776 
00777 int
00778 MRP2_Serial::read_serial (uint8_t _command_to_read)
00779 {
00780   uint8_t inData[24] = {0};
00781   int recievedData = 0;
00782 
00783   if(use_usb_)
00784     recievedData = usb_port.read_bytes(inData,24);
00785   else
00786     recievedData = serial_port.poll_comport(inData, 24);
00787 
00788   /*if(_command_to_read == getSONARS && recievedData > 0)
00789     print_array(inData, recievedData);*/
00790 
00791   if (recievedData == 0)
00792     return 0;
00793 
00794   if (simple_)
00795     return process_simple(inData, recievedData, _command_to_read);
00796   else
00797     return process(inData, recievedData, _command_to_read);
00798 }
00799 
00800 int 
00801 MRP2_Serial::process_simple (uint8_t *inData, int recievedData, uint8_t _command_to_read)
00802 {
00803   int ret_val_ = 0;
00804 
00805   uint8_t* ret_data_ = new uint8_t[recievedData];
00806   memcpy( ret_data_, inData, recievedData * sizeof(uint8_t) );
00807 
00808   int data_len = first_validator(ret_data_);
00809   if(second_validator(ret_data_, data_len) != -1)
00810   {
00811       ret_val_ = execute_command(ret_data_);
00812   }
00813   return ret_val_;
00814 }
00815 
00816 int 
00817 MRP2_Serial::process (uint8_t *inData, int recievedData, uint8_t _command_to_read)
00818 {
00819   int startIndex = 0;
00820   int proc_length = 0;
00821   int while_cnt = 0;
00822   int _ret_val = 0;
00823 
00824   if (recievedData > 0) 
00825   {
00826 
00827       if(tempDataIndex == 0)
00828       {
00829           memcpy(tempData, inData, recievedData); 
00830           tempDataIndex = recievedData;
00831       }else{ 
00832           memcpy(&tempData[tempDataIndex], &inData[0], recievedData); 
00833           tempDataIndex += recievedData;
00834       }
00835 
00836 
00837       tempDataIndex = find_message_start(tempData,tempDataIndex);
00838   }
00839 
00840   if (tempDataIndex > 3 && tempData[0] == startChar) 
00841   {
00842       
00843       int data_len = first_validator(tempData);
00844 
00845 
00846       if (tempDataIndex < data_len+5)
00847       {
00848         return _ret_val;
00849       }
00850 
00851 
00852       if (data_len != -1)
00853       {
00854         if (tempData[1] != _command_to_read)
00855         {
00856           tempData[0] = '0';
00857           return _ret_val;          
00858         }
00859       }
00860 
00861       if(data_len == 0 && tempDataIndex >= 3)
00862       {
00863           _ret_val = execute_command(tempData);
00864           tempData[0] = '0'; 
00865           tempDataIndex = find_message_start(tempData,tempDataIndex);
00866       }else if(data_len > 0 && tempDataIndex >= data_len+5)
00867       {
00868           if(second_validator(tempData, data_len) != -1)
00869           {
00870 
00871               _ret_val = execute_command(tempData);
00872 
00873           }
00874           tempData[0] = '0'; 
00875           tempDataIndex = find_message_start(tempData,tempDataIndex);
00876 
00877       }else if(data_len == -1){
00878 
00879           tempData[0] = '0'; 
00880           tempDataIndex = find_message_start(tempData,tempDataIndex);
00881       }
00882 
00883   }
00884 
00885   return _ret_val;
00886 
00887 }
00888 
00889 void 
00890 MRP2_Serial::array_chopper(uint8_t *buf, int start, int end) {
00891   int k = 0;
00892   for (int i = start; i < start+end; i++)
00893   {
00894     tempData[k] = buf[i];
00895     k++;
00896   }
00897 };
00898 
00899 unsigned char 
00900 MRP2_Serial::checksum(int size)
00901 {
00902   uint8_t ret = 0;
00903   for(int i=0; i<size; i++)
00904   {
00905     ret = ret + sendArray[i];
00906   }
00907   if(size > 3)
00908   {
00909     return (ret & 255) - 1; 
00910   }
00911   return ret & 255 ;
00912 }
00913 
00914 unsigned char 
00915 MRP2_Serial::checksum_check_array(uint8_t *arr, int size)
00916 {
00917   uint8_t ret = 0;
00918   for(int i=0; i<size; i++)
00919   {
00920     ret = ret + arr[i];
00921   }
00922   if(size > 3)
00923   {
00924     ret = (ret & 0xFF) - 1; 
00925   }
00926   else{
00927     ret = ret & 0xFF; 
00928   }
00929   return ret;
00930 }
00931 
00932 bool 
00933 MRP2_Serial::checksum_match(uint8_t *buf, int size) {
00934   uint8_t checksum = 0;
00935   int i = 0;
00936   for(i = 0; i<size; i++) 
00937   {
00938       checksum = checksum + buf[i];
00939   }
00940   if(size > 3)
00941   {
00942     checksum = (checksum & 0xFF) - 1; 
00943     
00944   }else{
00945     checksum = checksum & 0xFF; 
00946   }
00947 
00948   if(checksum == buf[size])    
00949   {
00950       return true;
00951   }    
00952   return false;
00953 };
00954 
00955 int 
00956 MRP2_Serial::first_validator(uint8_t *buf) {
00957   if(checksum_match(buf, 3))
00958     {
00959         return buf[2];
00960     }
00961     return -1;
00962 };
00963 
00964 int 
00965 MRP2_Serial::second_validator(uint8_t *buf, int data_len) {
00966   int total_len = data_len + 5;
00967   if(checksum_match(buf, 4 + data_len))
00968   {
00969       return 1;
00970   }
00971   return -1;
00972 };
00973 
00974 int 
00975 MRP2_Serial::find_message_start(uint8_t *buf,  int lastIndex) {
00976   int start = 0;
00977   for (start = 0; start < lastIndex ; start++)
00978   {
00979       if(buf[start] == startChar)
00980       {
00981           break;
00982       }
00983   }
00984   
00985   lastIndex = lastIndex - start;
00986 
00987   array_chopper(buf, start, lastIndex+1);
00988   
00989   return lastIndex;
00990 };
00991 
00992 int 
00993 MRP2_Serial::execute_command(uint8_t *buf) {
00994 
00995   union{
00996     float f;
00997     uint8_t bytes[4];
00998   }val;
00999 
01000   if(buf[1] == getBUMPERS)
01001   {
01002     _bumpers.clear();
01003     _bumpers.push_back((buf[4] >> 3) & 0x01); // front left
01004     _bumpers.push_back((buf[4] >> 2) & 0x01); // front right
01005     _bumpers.push_back((buf[4] >> 1) & 0x01); // rear left
01006     _bumpers.push_back(buf[4] & 0x01);        // rear right
01007 
01008   }
01009 
01010   if(buf[1] == 52)
01011   {
01012     _ack_data = buf[4];
01013   }
01014 
01015   if(buf[1] == getSPEEDS)
01016   {
01017       int l_speed = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24); 
01018       int r_speed= buf[8] + (buf[9] << 8) + (buf[10] << 16) + (buf[11] << 24); 
01019       
01020       _speed_l = l_speed*-1;
01021       _speed_r = r_speed*-1;
01022       _speeds.clear();
01023       _speeds.push_back(_speed_l);
01024       _speeds.push_back(_speed_r);
01025 
01026   }
01027 
01028   if(buf[1] == getSPEED_L)
01029   {
01030       int l_speed = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24); 
01031       
01032       _speed_l = l_speed*-1;
01033       _speeds.clear();
01034       _speeds.push_back(_speed_l);
01035       _speeds.push_back(_speed_r);
01036 
01037   }
01038 
01039   if(buf[1] == getSPEED_R)
01040   {
01041       int r_speed = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24); 
01042       
01043       _speed_r = r_speed*-1;
01044       _speeds.clear();
01045       _speeds.push_back(_speed_l);
01046       _speeds.push_back(_speed_r);
01047 
01048   }
01049 
01050   if(buf[1] == getPARAM_KP_L)
01051   {
01052     //printf("KP_L: 4:%d, 5:%d, 6:%d, 7:%d\n", buf[4], buf[5], buf[6], buf[7]);
01053 
01054     val.bytes[0] = buf[4];
01055     val.bytes[1] = buf[5];
01056     val.bytes[2] = buf[6];
01057     val.bytes[3] = buf[7];
01058 
01059     _Kp_l = val.f;
01060     //printf("kp_l:%f\n", _Kp_l);
01061   }
01062 
01063   if(buf[1] == getPARAM_KI_L)
01064   {
01065     val.bytes[0] = buf[4];
01066     val.bytes[1] = buf[5];
01067     val.bytes[2] = buf[6];
01068     val.bytes[3] = buf[7];
01069     _Ki_l = val.f;
01070      //printf("ki_l:%f\n", _Ki_l);
01071   }
01072 
01073   if(buf[1] == getPARAM_KD_L)
01074   {
01075     val.bytes[0] = buf[4];
01076     val.bytes[1] = buf[5];
01077     val.bytes[2] = buf[6];
01078     val.bytes[3] = buf[7];
01079     _Kd_l = val.f;
01080      //printf("kd_l:%f\n", _Kd_l);
01081   }
01082 
01083   if(buf[1] == getPARAM_KP_R)
01084   {
01085     val.bytes[0] = buf[4];
01086     val.bytes[1] = buf[5];
01087     val.bytes[2] = buf[6];
01088     val.bytes[3] = buf[7];
01089     _Kp_r = val.f;
01090      //printf("kp_r:%f\n", _Kp_r);
01091   }
01092 
01093   if(buf[1] == getPARAM_KI_R)
01094   {
01095     val.bytes[0] = buf[4];
01096     val.bytes[1] = buf[5];
01097     val.bytes[2] = buf[6];
01098     val.bytes[3] = buf[7];
01099     _Ki_r = val.f;
01100     //printf("ki_r:%f\n", _Ki_r);
01101   }
01102 
01103   if(buf[1] == getPARAM_KD_R)
01104   {
01105     val.bytes[0] = buf[4];
01106     val.bytes[1] = buf[5];
01107     val.bytes[2] = buf[6];
01108     val.bytes[3] = buf[7];
01109     _Kd_r = val.f;
01110     //printf("kd_r:%f\n", _Kd_r);
01111   }
01112 
01113   if(buf[1] == getPARAM_IMAX_L)
01114   {
01115     _imax_l = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01116     _imax.clear();
01117     _imax.push_back(_imax_l);
01118     _imax.push_back(_imax_r);
01119   }
01120 
01121   if(buf[1] == getPARAM_IMAX_R)
01122   {
01123     _imax_r = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01124     _imax.clear();
01125     _imax.push_back(_imax_l);
01126     _imax.push_back(_imax_r);
01127   }
01128 
01129   if(buf[1] == getMAXSPEED_FWD)
01130   {
01131     _maxspeed_fwd = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01132   }
01133 
01134   if(buf[1] == getMAXSPEED_REV)
01135   {
01136     _maxspeed_rev = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01137   }
01138 
01139   if(buf[1] == getMAXACCEL)
01140   {
01141     _maxaccel = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01142   }
01143 
01144   if(buf[1] == getBATT_VOLT)
01145   {
01146     _batt_volt = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01147   }
01148 
01149   if(buf[1] == getBATT_CURRENT)
01150   {
01151     _batt_current = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01152   }
01153 
01154   if(buf[1] == getBATT_SOC)
01155   {
01156     _batt_soc = buf[4] + (buf[5] << 8) + (buf[6] << 16) + (buf[7] << 24);
01157   }
01158 
01159   if(buf[1] == getPOSITION_L)
01160   {
01161 
01162       int l_position = buf[5] + (buf[6] << 8) + (buf[7] << 16) + (buf[8] << 24);
01163       if(buf[4] == 1)
01164       {
01165         l_position *= -1;
01166       }
01167         
01168       _position_l = l_position;
01169       _positions.clear();
01170       _positions.push_back(_position_l);
01171       _positions.push_back(_position_r);
01172 
01173   }
01174 
01175   if(buf[1] == getPOSITION_R)
01176   {
01177 
01178       int r_position = buf[5] + (buf[6] << 8) + (buf[7] << 16) + (buf[8] << 24);
01179       if(buf[4] == 1)
01180       {
01181         r_position *= -1;
01182       }
01183         
01184       _position_r = r_position;
01185       _positions.clear();
01186       _positions.push_back(_position_l);
01187       _positions.push_back(_position_r);
01188 
01189   }
01190 
01191   if(buf[1] == getESTOP)
01192   {
01193     _estop = buf[4];
01194   }
01195 
01196   if(buf[1] == getDIAG)
01197   {
01198     _diag_motor_stall_l = buf[5];
01199     _diag_motor_stall_r = buf[6];
01200     _diag_batt_low = buf[7];
01201     _diag_batt_high = buf[8];
01202     _diag_motor_drvr_err = buf[9];
01203     _diag_aux_lights_err = buf[10];
01204   }
01205 
01206   if(buf[1] == getBATT_CELL_CAPACITY)
01207   {
01208     _batt_cell_capacity = buf[5] + (buf[6] << 8) + (buf[7] << 16) + (buf[8] << 24);
01209   }
01210 
01211   if(buf[1] == getBUMPER_ESTOP)
01212   {
01213     _bumper_estop = buf[4];
01214   }
01215 
01216   if(buf[1] == getESTOP_BTN)
01217   {
01218     _estop_btn = buf[4];
01219   }
01220 
01221   if(buf[1] == getSONARS)
01222   {
01223     _sonars.clear();
01224     _sonars.push_back(buf[4] + (buf[5] << 8));
01225     _sonars.push_back(buf[6] + (buf[7] << 8));
01226     _sonars.push_back(buf[8] + (buf[9] << 8));
01227     _sonars.push_back(buf[10] + (buf[11] << 8));
01228     _sonars.push_back(buf[12] + (buf[13] << 8));
01229     _sonars.push_back(buf[14] + (buf[15] << 8));
01230     _sonars.push_back(buf[16] + (buf[17] << 8));
01231   }
01232 
01233   return buf[1];
01234 
01235 };
01236 
01237 void 
01238 MRP2_Serial::print_array(uint8_t *buf, int length) {
01239   printf("Array: ");
01240   int i = 0;
01241   for (i = 0; i < length; i++)
01242   {
01243       //printf("%02x ",buf[i] );
01244     printf("%d ",buf[i] );
01245   }
01246   printf("\n");
01247 };
01248 
01249 void 
01250 MRP2_Serial::set_read_timeout(double timeout){
01251   read_timeout_ = timeout;
01252 }
01253 
01254 double 
01255 MRP2_Serial::get_read_timeout(void){
01256   return read_timeout_;
01257 }


mrp2_hardware
Author(s): Akif Hacinecipoglu
autogenerated on Thu Jun 6 2019 21:43:37