nav200.cc

00001 #include "nav200.h"
00002 
00003 
00004 
00005 Nav200::Nav200()
00006 {
00007 }
00008 
00009 
00010 Nav200::~Nav200()
00011 {
00012 }
00013 
00014 int Nav200::Initialise(Driver* sn2002, Device* opaque2, player_devaddr_t opaque_id2 )
00015 {
00016   this->sn200 = sn2002;
00017   this->opaque_id = opaque_id2;
00018   this->opaque = opaque2;
00019   bytesReceived = 0;
00020 
00021   return 0;
00022 }
00023 
00024 int Nav200::Terminate()
00025 {
00026   return 0;
00027 }
00028 
00029 /*****************
00030 * Standby mode
00031 *******************/
00032 
00033 bool Nav200::EnterStandby()
00034 {
00035   WriteCommand('S','A',0,NULL);
00036   if (ReadFromNav200() > 0)
00037   {
00038     if (packet.length==5 && packet.mode == 'S' && packet.function == 'A')
00039       return true;
00040   }
00041   return false;
00042 }
00043 
00044 int Nav200::GetVersionNumber()
00045 {
00046   WriteCommand('S','V',0,NULL);
00047   if (ReadFromNav200() > 0)
00048   {
00049     if (packet.length==8 && packet.mode == 'S' && packet.function == 'V')
00050     {
00051       //printf("Version: %d.%d.%d\n",packet.data[0],packet.data[1],packet.data[2]);
00052       return (*reinterpret_cast<int*> (packet.data))&0xFFFFFF;
00053     }
00054   }
00055   return 0;
00056 
00057 }
00058 
00059 char* Nav200::GetVersionString()
00060 {
00061   WriteCommand('S','T',0,NULL);
00062   if(ReadFromNav200()>0)
00063   {
00064     if(packet.mode == 'S' && packet.function == 'T')
00065     {
00066       packet.data[packet.dataLength]='\0';
00067       return(reinterpret_cast<char*>(packet.data));
00068     }
00069   }
00070   return NULL;
00071 }
00072 
00073 short Nav200::GetDeviceSerial()
00074 {
00075   WriteCommand('S','S',0,NULL);
00076   if(ReadFromNav200()>0)
00077   {
00078     if(packet.length==7 && packet.mode == 'S' && packet.function == 'S')
00079       return *reinterpret_cast<short*> (packet.data);
00080   }
00081   return 0;
00082 }
00083 
00084 bool Nav200::rotateDirection(uint8_t direction)
00085 {
00086   WriteCommand('S','U',1,&direction);
00087   if(ReadFromNav200()>0)
00088   {
00089     if(packet.length==6 && packet.mode == 'S' && packet.function == 'U' /*&& *reinterpret_cast<uint8_t*> (packet.data) == direction*/)
00090     {
00091       return true;
00092     }
00093   }
00094   return false;
00095 }
00096 
00097 bool Nav200::GetReflectorPosition(uint8_t layer, uint8_t number, PositionXY & reflector)
00098 {
00099   uint8_t data[2] = {layer,number};
00100   WriteCommand('S','R',2,data);
00101   if(ReadFromNav200()>0)
00102   {
00103     if(packet.length==15 && packet.mode == 'S' && packet.function == 'R' && packet.data[0]==layer && packet.data[1]==number)
00104     {
00105         reflector.x = *reinterpret_cast<int*> (packet.data+2);
00106         reflector.y = *reinterpret_cast<int*> (packet.data+6);
00107         return true;
00108     }
00109   }
00110   return false;
00111 }
00112 
00113 bool Nav200::ChangeReflectorPosition(uint8_t layer, uint8_t number, int newX, int newY)
00114 {
00115   uint8_t command[10];
00116   command[0] = layer;
00117   command[1] = number;
00118   *reinterpret_cast<int*> (&command[2]) = newX;
00119   *reinterpret_cast<int*> (&command[6]) = newY;
00120   WriteCommand('S','C',10,command);
00121   if(ReadFromNav200()>0)
00122   {
00123     if(packet.length==15 && packet.mode == 'S' && packet.function == 'C' && packet.data[0]==layer && packet.data[1]==number && *reinterpret_cast<int*> (packet.data+2) == newX && *reinterpret_cast<int*> (packet.data+6) == newY)
00124       return true;
00125   }
00126   return false;
00127 }
00128 
00129 bool Nav200::InsertReflectorPosition(uint8_t layer, uint8_t number, int X, int Y)
00130 {
00131   uint8_t command[10];
00132   command[0] = layer;
00133   command[1] = number;
00134   *reinterpret_cast<int*> (&command[2]) = X;
00135   *reinterpret_cast<int*> (&command[6]) = Y;
00136   WriteCommand('S','I',10,command);
00137   if(ReadFromNav200()>0)
00138   {
00139     if(packet.length==15 && packet.mode == 'S' && packet.function == 'I' && packet.data[0]==layer && packet.data[1]==number && *reinterpret_cast<int*> (packet.data+2) == X && *reinterpret_cast<int*> (packet.data+6) == Y)
00140       return true;
00141   }
00142   return false;
00143 }
00144 
00145 bool Nav200::DeleteReflectorPosition(uint8_t layer, uint8_t number, PositionXY & reflector)
00146 {
00147   uint8_t data[2] = {layer,number};
00148   WriteCommand('S','D',2,data);
00149   if(ReadFromNav200()>0)
00150   {
00151     if(packet.length==15 && packet.mode == 'S' && packet.function == 'D' && packet.data[0]==layer && packet.data[1]==number)
00152     {
00153         reflector.x = *reinterpret_cast<int*> (packet.data+2);
00154         reflector.y = *reinterpret_cast<int*> (packet.data+6);
00155 //         printf("deleted reflector is %d\n",reflector.x);
00156         return true;
00157     }
00158   }
00159   return false;
00160 }
00161 
00162 
00163 /*********************************
00164 * Read and set reflector radii
00165 ***********************************/
00166 int Nav200::GetReflectorRadius(uint8_t layer)
00167 {
00168 //   ReflectorSize reflector;
00169   WriteCommand('R','G',1,&layer);
00170   if(ReadFromNav200()>0)
00171   {
00172     if(packet.length==7 && packet.mode == 'R' && packet.function == 'G' && packet.data[0] == layer)
00173     {
00174       return packet.data[1];
00175     }
00176   }
00177   return -1;
00178 }
00179 
00180 bool Nav200::SetReflectorRadius(uint8_t layer, uint8_t radius)
00181 {
00182   uint8_t data[2] = {layer,radius};
00183   WriteCommand('R','S',2,data);
00184   if(ReadFromNav200()>0)
00185   {
00186     if(packet.length==7 && packet.mode == 'R' && packet.function == 'G' && packet.data[0] == layer && packet.data[1] == radius)
00187       return true;
00188   }
00189   return false;
00190 }
00191 
00192 /*****************
00193 * Mapping mode
00194 *******************/
00195 bool Nav200::EnterMapping()
00196 {
00197   WriteCommand('M','A',0,NULL);
00198   if(ReadFromNav200()>0)
00199   {
00200     if(packet.length==5 && packet.mode == 'M' && packet.function == 'A')
00201       return true;
00202   }
00203   return false;
00204 }
00205 
00206 int Nav200::StartMapping(uint8_t layer, int X, int Y, short orientation, uint8_t radius)
00207 {
00208   uint8_t data[12];
00209   data[0] = layer;
00210   *reinterpret_cast<int*> (&data[1]) = X;
00211   *reinterpret_cast<int*> (&data[5]) = Y;
00212   *reinterpret_cast<short*> (&data[9]) = orientation;
00213   data[11] = radius;
00214   WriteCommand('M','S',12,data);
00215   if(ReadFromNav200(100000000)>0)
00216   {
00217     if(packet.length==7 && packet.mode == 'M' && packet.function == 'S' && packet.data[0] == layer)
00218       return packet.data[1];
00219   }
00220   return -1;
00221 }
00222 
00223 int Nav200::StartMappingMeasurement(uint8_t layer, uint8_t scans, int X, int Y, short orientation, uint8_t radius)
00224 {
00225   uint8_t data[13];
00226   data[0] = layer;
00227   data[1] = scans;
00228   *reinterpret_cast<int*> (&data[2]) = X;
00229   *reinterpret_cast<int*> (&data[6]) = Y;
00230   *reinterpret_cast<short*> (&data[10]) = orientation;
00231   data[12] = radius;
00232   WriteCommand('M','M',13,data);
00233   if(ReadFromNav200(100000000)>0)
00234   {
00235     if(packet.length==7 && packet.mode == 'M' && packet.function == 'S' && packet.data[0] == layer)
00236       return packet.data[1];
00237   }
00238   return -1;
00239 }
00240 
00241 int Nav200::StartNegativeMappingMeasurement(uint8_t layer, uint8_t scans, int X, int Y, short orientation, uint8_t radius)
00242 {
00243   uint8_t data[13];
00244   data[0] = layer;
00245   data[1] = scans;
00246   *reinterpret_cast<int*> (&data[2]) = X;
00247   *reinterpret_cast<int*> (&data[6]) = Y;
00248   *reinterpret_cast<short*> (&data[10]) = orientation;
00249   data[12] = radius;
00250   WriteCommand('M','N',13,data);
00251   if(ReadFromNav200()>0)
00252   {
00253     if(packet.length==7 && packet.mode == 'M' && packet.function == 'S' && packet.data[0] == layer)
00254       return packet.data[1];
00255   }
00256   return -1;
00257 }
00258 
00259 bool Nav200::MappingPosition(uint8_t layer, uint8_t number, PositionXY & reflector)
00260 {
00261   uint8_t data[2] = {layer,number};
00262   WriteCommand('M','R',2,data);
00263   if(ReadFromNav200()>0)
00264   {
00265     if(packet.length==15 && packet.mode == 'M' && packet.function == 'R' && packet.data[0] == layer && packet.data[1] == number)
00266     {
00267         reflector.x = *reinterpret_cast<int*> (packet.data+2);
00268         reflector.y = *reinterpret_cast<int*> (packet.data+6);
00269         return true;
00270     }
00271   }
00272   return false;
00273 }
00274 
00275 /*****************
00276 * Positioning mode
00277 *******************/
00278 bool Nav200::EnterPositioning()
00279 {
00280   WriteCommand('P','A',0,NULL);
00281   if(ReadFromNav200()>0)
00282   {
00283     if(packet.length==5 && packet.mode == 'P' && packet.function == 'A')
00284       return true;
00285   }
00286   return false;
00287 }
00288 
00289 bool Nav200::EnterPositioningInput(uint8_t NumberOfMeasurements)
00290 {
00291   WriteCommand('P','N',1,&NumberOfMeasurements);
00292   if(ReadFromNav200()>0)
00293   {
00294     if(packet.length==5 && packet.mode == 'P' && packet.function == 'A')
00295       return true;
00296   }
00297   return false;
00298 }
00299 
00300 bool Nav200::GetPositionAuto(LaserPos & laserPosition)
00301 {
00302   WriteCommand('P','P',0,NULL);
00303 
00304   if(ReadFromNav200()>0)
00305   {
00306     if(packet.length==17 && packet.mode == 'P' && packet.function == 'P')
00307     {
00308       laserPosition.pos.x = *reinterpret_cast<int*> (packet.data);
00309       laserPosition.pos.y = *reinterpret_cast<int*> (packet.data+4);
00310       laserPosition.orientation = *reinterpret_cast<short*> (packet.data+8);
00311       laserPosition.quality = packet.data[10];
00312       laserPosition.number = packet.data[11];
00313       return true;
00314     }
00315   }
00316   return false;
00317 }
00318 
00319 bool Nav200::GetPositionSpeed(short speedX, short speedY, LaserPos & laserPosition)
00320 {
00321   uint8_t data[4];
00322   *reinterpret_cast<short*> (&data[0]) = speedX;
00323   *reinterpret_cast<short*> (&data[2]) = speedY;
00324   WriteCommand('P','v',4,data);
00325   if(ReadFromNav200()>0)
00326   {
00327     if(packet.length==17 && packet.mode == 'P' && packet.function == 'P')
00328     {
00329       laserPosition.pos.x = *reinterpret_cast<int*> (packet.data);
00330       laserPosition.pos.y = *reinterpret_cast<int*> (packet.data+4);
00331       laserPosition.orientation = *reinterpret_cast<short*> (packet.data+8);
00332       laserPosition.quality = packet.data[10];
00333       laserPosition.number = packet.data[11];
00334       return true;
00335     }
00336   }
00337   return false;
00338 }
00339 
00340 bool Nav200::GetPositionSpeedVelocity(short speedX, short speedY, short velocity, LaserPos & laserPosition)
00341 {
00342   uint8_t data[6];
00343   *reinterpret_cast<short*> (&data[0]) = speedX;
00344   *reinterpret_cast<short*> (&data[2]) = speedY;
00345   *reinterpret_cast<short*> (&data[4]) = velocity;
00346   WriteCommand('P','w',6,data);
00347   if(ReadFromNav200()>0)
00348   {
00349     if(packet.length==17 && packet.mode == 'P' && packet.function == 'P')
00350     {
00351       laserPosition.pos.x = *reinterpret_cast<int*> (packet.data);
00352       laserPosition.pos.y = *reinterpret_cast<int*> (packet.data+4);
00353       laserPosition.orientation = *reinterpret_cast<short*> (packet.data+8);
00354       laserPosition.quality = packet.data[10];
00355       laserPosition.number = packet.data[11];
00356       return true;
00357     }
00358   }
00359   return false;
00360 }
00361 
00362 
00363 bool Nav200::GetPositionSpeedVelocityAbsolute(short speedX, short speedY, short velocity, LaserPos & laserPosition)
00364 {
00365   uint8_t data[6];
00366   *reinterpret_cast<short*> (&data[0]) = speedX;
00367   *reinterpret_cast<short*> (&data[2]) = speedY;
00368   *reinterpret_cast<short*> (&data[4]) = velocity;
00369   WriteCommand('P','V',6,data);
00370   if(ReadFromNav200()>0)
00371   {
00372     if(packet.length==17 && packet.mode == 'P' && packet.function == 'P')
00373     {
00374       laserPosition.pos.x = *reinterpret_cast<int*> (packet.data);
00375       laserPosition.pos.y = *reinterpret_cast<int*> (packet.data+4);
00376       laserPosition.orientation = *reinterpret_cast<short*> (packet.data+8);
00377       laserPosition.quality = packet.data[10];
00378       laserPosition.number = packet.data[11];
00379       return true;
00380     }
00381   }
00382   return false;
00383 }
00384 
00385 bool Nav200::ChangeLayer(uint8_t layer)
00386 {
00387   WriteCommand('P','L',1,&layer);
00388   if(ReadFromNav200()>0)
00389   {
00390     if(packet.length==6 && packet.mode=='P' && packet.function=='L' && packet.data[0] == layer)
00391       return true;
00392   }
00393   return false;
00394 }
00395 
00396 bool Nav200::ChangeLayerDefPosition(uint8_t layer, int X, int Y, short orientation)
00397 {
00398   uint8_t data[11];
00399   data[0] = layer;
00400   *reinterpret_cast<int*> (&data[1]) = X;
00401   *reinterpret_cast<int*> (&data[5]) = Y;
00402   *reinterpret_cast<short*> (&data[9]) = orientation;
00403   WriteCommand('P','M',11,data);
00404   if(ReadFromNav200()>0)
00405   {
00406     if(packet.length==16 && packet.mode == 'P' && packet.function == 'M' && packet.data[0] == layer && *reinterpret_cast<int*> (packet.data+1) == X && *reinterpret_cast<int*> (packet.data+5) == Y && *reinterpret_cast<short*> (packet.data+9) == orientation)
00407       return true;
00408   }
00409   return false;
00410 }
00411 
00412 bool Nav200::SetActionRadii(int min, int max)
00413 {
00414   uint8_t data[8];
00415   *reinterpret_cast<int*> (&data[0]) = min;
00416   *reinterpret_cast<int*> (&data[4]) = max;
00417   WriteCommand('P','O',8,data);
00418   if(ReadFromNav200()>0)
00419   {
00420     if(packet.length==13 && packet.mode == 'P' && packet.function == 'O' && *reinterpret_cast<int*> (packet.data)==min && *reinterpret_cast<int*> (packet.data+4)==max)
00421       return true;
00422   }
00423   return false;
00424 }
00425 
00426 bool Nav200::SelectNearest(uint8_t N_nearest)
00427 {
00428   WriteCommand('P','C',1,&N_nearest);
00429   if(ReadFromNav200()>0)
00430   {
00431     if(packet.length==6 && packet.mode == 'P' && packet.function == 'C' && packet.data[0] == N_nearest)
00432       return true;
00433   }
00434   return false;
00435 }
00436 
00437 /*****************
00438 * upload mode
00439 *******************/
00440 bool Nav200::EnterUpload()
00441 {
00442   WriteCommand('U','A',0,NULL);
00443   if(ReadFromNav200()>0)
00444   {
00445     if(packet.length==5 && packet.mode == 'U' && packet.function == 'A')
00446       return true;
00447   }
00448   return false;
00449 }
00450 
00451 bool Nav200::GetUploadTrans(uint8_t layer, ReflectorData & reflector)
00452 {
00453   WriteCommand('U','R',1,&layer);
00454   if(ReadFromNav200()>0)
00455   {
00456     if(packet.length==15 && packet.mode == 'U' && packet.function == 'R' && packet.data[0] == layer)
00457     {
00458       reflector.layer = packet.data[0];
00459       reflector.number = packet.data[1];
00460       reflector.pos.x = *reinterpret_cast<int*> (packet.data+2);
00461       reflector.pos.y = *reinterpret_cast<int*> (packet.data+6);
00462       return true;
00463     }
00464   }
00465   return false;
00466 }
00467 
00468 /*****************
00469 * download mode
00470 ******************/
00471 bool Nav200::EnterDownload()
00472 {
00473   WriteCommand('D','A',0,NULL);
00474   if(ReadFromNav200()>0)
00475   {
00476     if(packet.length==5 && packet.mode == 'D' && packet.function == 'A')
00477       return true;
00478   }
00479   return false;
00480 }
00481 
00482 bool Nav200::DownloadReflector(uint8_t layer, uint8_t number, int X, int Y)
00483 {
00484   uint8_t data[10];
00485   data[0] = layer;
00486   data[1] = number;
00487   *reinterpret_cast<int*> (&data[2]) = X;
00488   *reinterpret_cast<int*> (&data[6]) = Y;
00489   WriteCommand('D','R',10,data);
00490   if(ReadFromNav200()>0)
00491   {
00492     if(packet.length == 7 && packet.mode == 'D' && packet.function == 'R' && packet.data[0] == layer && packet.data[1] == number)
00493     {
00494       return true;
00495     }
00496   }
00497   return false;
00498 }
00499 
00500 /*****************
00501 * Error Messages
00502 ******************/
00503 void Nav200::PrintErrorMsg(void)
00504 {
00505   fprintf(stderr,"ERROR: Last command entry %c:", error.F0); 
00506   switch(error.F1)
00507   {
00508     case 1:   fprintf(stderr,"User's software error: "); break;
00509     case 2:   fprintf(stderr,"Error class: Transputer: "); break;
00510     case 3:   fprintf(stderr,"Error in the connection between TPU and sensor: "); break;
00511     case 4:   fprintf(stderr,"Spurious measurement: "); break;
00512     case 5:   fprintf(stderr,"Spurious angular measurement: "); break;
00513     case 6:   fprintf(stderr,"Error in connection to reflector memory: "); break;
00514     case 7:   fprintf(stderr,"Error in connection between user computer and TPU (RS-232 interface): "); break;
00515   }
00516   switch(error.F2)
00517   {
00518     case 1:   fprintf(stderr,"input/output, telegram traffic. "); break;
00519     case 2:   fprintf(stderr,"standby mode. "); break;
00520     case 3:   fprintf(stderr,"handling reference reflectors. "); break;
00521     case 4:   fprintf(stderr,"download mode. "); break;
00522     case 5:   fprintf(stderr,"upload mode. "); break;
00523     case 6:   fprintf(stderr,"mapping mode. "); break;
00524     case 7:   fprintf(stderr,"positioning mode. "); break;
00525     case 8:   fprintf(stderr,"test mode. "); break;
00526     case 9:   fprintf(stderr,"navigation operation, general. "); break;
00527 
00528   }
00529   switch(error.F3)
00530   {
00531     case 1:   fprintf(stderr,"Unknown command.\n"); break;
00532     case 2:   fprintf(stderr,"Command(function) not implemented.\n"); break;
00533     case 3:   fprintf(stderr,"Wrong Command\n"); break;
00534     case 4:   fprintf(stderr,"Reflector not available\n"); break;
00535     case 5:   fprintf(stderr,"Wrong reflector number\n"); break;
00536     case 6:   fprintf(stderr,"Wrong data block\n"); break;
00537     case 7:   fprintf(stderr,"Input not possible\n"); break;
00538     case 8:   fprintf(stderr,"Invalid layer\n"); break;
00539     case 9:   fprintf(stderr,"No reflectors in layer\n"); break;
00540     case 10:   fprintf(stderr,"All reflectors transferred\n"); break;
00541     case 11:   fprintf(stderr,"Communication error\n"); break;
00542     case 12:   fprintf(stderr,"Error in the initialisation of reference reflectors\n"); break;
00543     case 13:   fprintf(stderr,"Wrong radii during input of effective range\n"); break;
00544     case 14:   fprintf(stderr,"Flash EPROM not functional\n"); break;
00545     case 15:   fprintf(stderr,"Wrong reflector radius\n"); break;
00546   }
00547 
00548 }
00549 
00551 // Write a packet to Nav200
00552 int Nav200::WriteCommand(char mode, char function, int dataLength, uint8_t * data)
00553 {
00554 /*  if (fd < 0)
00555     return -1;*/
00556 
00557   int length = dataLength+5;
00558   uint8_t * buffer = new uint8_t[length];
00559 
00560   // Create header
00561   buffer[0] = STX;
00562   buffer[1] = length;
00563   buffer[2] = mode;
00564   buffer[3] = function;
00565 
00566   // Copy data
00567   memcpy(buffer + 4, data, dataLength);
00568 
00569   // Create footer (CRC)
00570   buffer[length-1] = CreateCRC(buffer, 4 + dataLength);
00571 
00572 /*  // Make sure both input and output queues are empty
00573   tcflush(fd, TCIOFLUSH);*/
00574 
00575 /*  // switch to blocking IO for the write
00576   int flags = fcntl(fd, F_GETFL);
00577   if (flags < 0 || fcntl(fd,F_SETFL,flags &~O_NONBLOCK) < 0)
00578   {
00579     fprintf(stderr,"Error changing to blocking write (%d - %s), disabling\n",errno,strerror(errno));
00580     tcsetattr(fd, TCSANOW, &oldtio);
00581     fd = -1;
00582     delete [] buffer;
00583     return -1;
00584   }
00585 */
00586   player_opaque_data_t mData;
00587   mData.data_count = length;
00588   mData.data = buffer;
00589 
00590   // before we send a command to the NAV200, flush and data coming in from the remote file
00591   sn200->InQueue->SetFilter(opaque_id.host, opaque_id.robot, opaque_id.interf, opaque_id.index, PLAYER_MSGTYPE_DATA, PLAYER_OPAQUE_DATA_STATE);
00592   
00593   //puts("waiting for data");
00594   sn200->ProcessMessages();
00595   
00596   // before we send a command to the NAV200, flush and data coming in from the remote file
00597   if (bytesReceived)
00598   {
00599 
00600     do
00601     {
00602         PLAYER_WARN1("Buffer was not empty on NAV200 (%d), flushing",bytesReceived);
00603         bytesReceived = 0;
00604         usleep(100000);
00605         sn200->ProcessMessages();
00606     } while (bytesReceived != 0);
00607   }
00608   sn200->InQueue->ClearFilter();
00609   
00610   opaque->PutMsg(sn200->InQueue, PLAYER_MSGTYPE_CMD, PLAYER_OPAQUE_CMD_DATA, reinterpret_cast<void*>(&mData), 0, NULL);
00611   
00612 /*  if((length && (write(fd, buffer, length)) < length))
00613   {
00614     fprintf(stderr,"Error writing to FOB (%d - %s), disabling\n",errno,strerror(errno));
00615     tcsetattr(fd, TCSANOW, &oldtio);
00616     fd = -1;
00617     delete [] buffer;
00618     return -1;
00619   }*/
00620 
00621 /*  // restore flags
00622   if (fcntl(fd,F_SETFL,flags) < 0)
00623   {
00624     fprintf(stderr,"Error restoring file mode (%d - %s), disabling\n",errno,strerror(errno));
00625     tcsetattr(fd, TCSANOW, &oldtio);
00626     fd = -1;
00627     delete [] buffer;
00628     return -1;
00629   }*/
00630 
00631   delete [] buffer;
00632 
00633   return 0;
00634 }
00635 
00636 
00638 // Read a packet from the Nav200
00639 int Nav200::ReadFromNav200(int timeout_usec)
00640 {
00641   
00642   int dataLength = 0;
00643 
00644   struct timeval start, now;
00645   gettimeofday(&start,NULL);
00646   
00647   sn200->InQueue->SetFilter(opaque_id.host, opaque_id.robot, opaque_id.interf, opaque_id.index, PLAYER_MSGTYPE_DATA, PLAYER_OPAQUE_DATA_STATE);
00648   
00649   for (;;)
00650   {
00651     //puts("waiting for data");
00652     sn200->ProcessMessages();
00653     
00654     // do we have enough for a header?
00655     while (bytesReceived > 4)
00656     {
00657       //PLAYER_MSG4(2, "recieved STX %d data len %d mode %c fun %c", receivedBuffer[0], receivedBuffer[1], receivedBuffer[2], receivedBuffer[3]);
00658       if (receivedBuffer[0] != STX)
00659       { 
00660         bool found = false;
00661         // bad thing, we dont have the correct start to a message
00662         for(int i=1; i<bytesReceived; i++)
00663         { // find where STX is
00664           if(receivedBuffer[i]==STX)
00665           { // move so STX is at start
00666             memmove(receivedBuffer, receivedBuffer+i, bytesReceived-i);
00667             bytesReceived-=i;
00668             found = true;
00669             break;
00670           }
00671         }
00672         // If none of the data points are STX then all are (essentially) removed
00673         if (!found)
00674         {
00675                 bytesReceived = 0;
00676         }
00677         continue;
00678       }
00679   
00680       // we have valid start byte
00681       // get length
00682       dataLength = receivedBuffer[1];
00683   
00684       // check we have enough data for full length, if not then break
00685       if (dataLength > bytesReceived)
00686       {
00687         break;
00688       }
00689       // calc CRC and check it
00690       // if bad CRC, assume same as bad STX
00691       if (CreateCRC(receivedBuffer, dataLength)) // change that later!!
00692       {// bad thing, we dont have the correct start to a message
00693         fprintf(stderr,"bad CRC!!!\n");
00694         bool found = false;
00695         for(int i=1; i<bytesReceived; i++)
00696         { // find where STX is
00697           if(receivedBuffer[i]==STX)
00698           { // move so STX is at start
00699             memmove(receivedBuffer, receivedBuffer+i, bytesReceived-i);
00700             bytesReceived-=i;
00701             found = true;
00702             break;
00703           }
00704         }
00705         // If none of the data points are STX then all are (essentially) removed
00706         if (!found)
00707         {
00708                 bytesReceived = 0;
00709         }
00710         continue;
00711       }
00712       // if good CRC we actually have a packet so assemble our NAV200 message and return it to the application
00713       else
00714       {
00715           packet.header = receivedBuffer[0];
00716           packet.length = receivedBuffer[1];
00717           packet.mode = receivedBuffer[2];
00718           packet.function = receivedBuffer[3];
00719           packet.dataLength = packet.length-HEADER_SIZE-FOOTER_SIZE;
00720           memcpy(packet.data, receivedBuffer+4, packet.dataLength);
00721           packet.BCC = receivedBuffer[4+packet.dataLength];
00722     
00723           memmove(receivedBuffer, receivedBuffer+dataLength, bytesReceived-dataLength);
00724           bytesReceived-=dataLength;
00725           sn200->InQueue->ClearFilter();
00726           
00727           if(receivedBuffer[3]=='E'){
00728                   error.F0 = receivedBuffer[4];
00729                   error.F1 = receivedBuffer[5];
00730                   error.F2 = receivedBuffer[6];
00731                   error.F3 = receivedBuffer[7];
00732           
00733                   //check out what the error is and it out
00734                   PrintErrorMsg();
00735                   return -2;
00736         }
00737 
00738         return 1;
00739       }
00740     }
00741     gettimeofday(&now,NULL);
00742     if ((now.tv_sec - start.tv_sec) * 1e6 + now.tv_usec - start.tv_usec > timeout_usec)
00743     {
00744       fprintf(stderr,"Timed out waiting for packet %d uSecs passed\n",static_cast<int>((now.tv_sec - start.tv_sec) * 1e6 + now.tv_usec - start.tv_usec));
00745       sn200->InQueue->ClearFilter();
00746       return -1;
00747     }
00748     usleep(1000);
00749   }
00750   sn200->InQueue->ClearFilter();
00751   return 0;
00752 }
00753 
00754 
00755 uint8_t Nav200::CreateCRC(uint8_t* data, ssize_t len)
00756 {
00757   uint8_t result = 0;
00758   for (int ii = 0; ii < len; ++ii)
00759     result ^= data[ii];
00760   return result;
00761 }

Last updated 12 September 2005 21:38:45