33 #include <angles/angles.h> 44 verbose_ = debug_mode;
46 MeasurementQueue_ =
new std::vector<MeasurementQueueElement_t >;
56 sockfd_ = socket (AF_INET, SOCK_STREAM, 0);
61 memset (&serv_addr_, 0,
sizeof (serv_addr_));
62 serv_addr_.sin_port = htons (portno_);
63 serv_addr_.sin_family = AF_INET;
64 #if defined (HAVE_GETADDRINFO) 66 if (getaddrinfo (hostname_, NULL, NULL, &(addr_ptr)))
68 ROS_ERROR (
"getaddrinfo() failed with error");
72 assert (addr_ptr->ai_addr);
73 if ((addr_ptr->ai_addr->sa_family) != AF_INET)
75 ROS_ERROR (
"unsupported internet address family");
78 serv_addr.sin_addr.s_addr = (
reinterpret_cast<struct sockaddr_in*
> (addr_ptr->ai_addr))->sin_addr.s_addr;
79 freeaddrinfo (addr_ptr);
82 server_ = gethostbyname (hostname_);
83 if ((server_) == NULL)
85 memcpy (&(serv_addr_.sin_addr.s_addr), server_->h_addr, server_->h_length);
89 if (connect (sockfd_, reinterpret_cast<struct sockaddr*> (&serv_addr_),
sizeof (serv_addr_)) < 0)
101 return (close (sockfd_));
113 if (ReadAnswer () != 0)
115 ExtendedRIS_ = onoff;
128 if (ReadAnswer () != 0)
130 MeanFilterNumScans_ = num_scans;
140 snprintf (cmd,
CMD_BUFFER_SIZE,
"sWN FLrang %+f %+f", (
float)range_min, (
float)range_max);
143 if (ReadAnswer () != 0)
145 RangeFilterBottomLimit_ = range_min;
146 RangeFilterTopLimit_ = range_max;
159 if (ReadAnswer () != 0)
161 FilterMask_ = filter_mask;
170 char* tmp = (
char*) malloc (strlen (ip) + 1);
171 unsigned char* _ip = (
unsigned char*) malloc (4);
174 _ip[0] = atoi (strtok (tmp,
"."));
175 for (
int i = 1; i < 4; i++)
176 _ip[i] = atoi (strtok (NULL,
"."));
188 snprintf (cmd,
CMD_BUFFER_SIZE,
"sMN SetAccessMode %d %s", userlevel, password);
190 return (ReadConfirmationAndAnswer ());
198 char *mac = (
char*) malloc (20);
202 SendCommand (
"sRN EImac ");
203 if (ReadAnswer () != 0)
206 strtok ((
char*) buffer_,
" ");
209 for (
int i = 0; i < 6; i++)
211 tmp = strtok (NULL,
"-");
212 strncpy (&mac[index], tmp, 2);
227 unsigned char* ip_str;
228 ip_str = ParseIP (ip);
231 snprintf (cmd, 80,
"sWN EIip %X %X %X %X", ip_str[0], ip_str[1], ip_str[2], ip_str[3]);
235 return (ReadAnswer ());
243 unsigned char* gw_str;
244 gw_str = ParseIP (gw);
247 snprintf (cmd,
CMD_BUFFER_SIZE,
"sWN EIgate %X %X %X %X", gw_str[0], gw_str[1], gw_str[2], gw_str[3]);
251 return (ReadAnswer ());
259 unsigned char* mask_str;
260 mask_str = ParseIP (mask);
263 snprintf (cmd,
CMD_BUFFER_SIZE,
"sWN EImask %X %X %X %X", mask_str[0], mask_str[1], mask_str[2], mask_str[3]);
267 return (ReadAnswer ());
280 return (ReadAnswer ());
288 const char* cmd =
"sMN mDCreset ";
291 return (ReadAnswer ());
299 const char* cmd =
"sMN Run";
302 return (ReadConfirmationAndAnswer ());
309 float angle_start,
float angle_range)
312 snprintf (cmd,
CMD_BUFFER_SIZE,
"sMN mSCconfigbyang 04 %s %+f 01 %+f %+f",
313 password, ang_res, angle_start, angle_range);
316 return (ReadConfirmationAndAnswer ());
323 float angle_start,
float angle_range)
326 snprintf (cmd,
CMD_BUFFER_SIZE,
"sMN mSCconfigbyfreq 04 %s %+f 01 %+f %+f",
327 password, freq, angle_start, angle_range);
330 return (ReadConfirmationAndAnswer ());
337 float angle_start,
float angle_range)
340 snprintf (cmd,
CMD_BUFFER_SIZE,
"sMN mSCsetscanconfig %+.2f %+.2f %+.2f %+.2f",
341 freq, ang_res, angle_start, angle_range);
344 int error = ReadConfirmationAndAnswer ();
349 strtok ((
char*)buffer_,
" "); strtok (NULL,
" ");
350 int ErrorCode = strtol (strtok (NULL,
" "), NULL, 16);
351 long int sf = strtol (strtok (NULL,
" "), NULL, 16);
352 long int re = strtol (strtok (NULL,
" "), NULL, 16);
354 if ((ErrorCode != 0) && (verbose_ > 0))
355 printf (
">> Warning: got an error code %d\n", ErrorCode);
357 scanning_frequency_ = sf;
361 printf (
">> Measured value quality is: %ld [5-10]\n",
362 strtol (strtok (NULL,
" "), NULL, 16));
381 return (ReadConfirmationAndAnswer ());
386 sensor_msgs::LaserScan
389 sensor_msgs::LaserScan scan;
391 char cs_read = 0, cs_calc = 0;
395 memset (buffer_, 0, 256);
396 if (!MeasurementQueue_->empty ())
399 ROS_DEBUG (
">>> Reading from queue...\n");
400 memcpy (buffer_, (
char*) MeasurementQueue_->front ().string, MeasurementQueue_->front ().length + 1);
401 free (MeasurementQueue_->front ().string);
402 MeasurementQueue_->erase (MeasurementQueue_->begin ());
407 ROS_DEBUG (
">>> Queue empty. Reading from socket...\n");
408 n_ = read (sockfd_, buffer_, 8);
412 ROS_DEBUG (
">>> E: error reading from socket!\n");
415 if (buffer_[0] != 0x02 || buffer_[1] != 0x02 || buffer_[2] != 0x02 || buffer_[3] != 0x02)
418 ROS_DEBUG (
">>> E: error expected 4 bytes STX's!\n");
419 n_ = read (sockfd_, buffer_, 255);
424 length = ( (buffer_[4] << 24) | (buffer_[5] << 16) | (buffer_[6] << 8) | (buffer_[7]) );
427 n_ = read (sockfd_, &buffer_[current], length-current);
429 }
while (current < length);
432 int ret = read (sockfd_, &cs_read, 1);
435 ROS_ERROR (
"LMS400 didnt get any data in read %d",ret);
439 for (
int i = 0; i < length; i++)
440 cs_calc ^= buffer_[i];
442 if (cs_calc != cs_read)
445 ROS_WARN (
">>> E: checksums do not match!\n");
463 uint16_t distance = 0;
464 uint8_t remission = 0;
468 scan.angle_min = angles::from_degrees (min_angle);
469 scan.angle_max = angles::from_degrees (max_angle);
470 scan.angle_increment = angles::from_degrees (resolution);
471 scan.range_min = 0.7;
472 scan.range_max = 3.6;
481 if (meas_header.
Format == 0x20 || meas_header.
Format == 0x21)
483 memcpy (&distance, (
void *)&buffer_[index],
sizeof (uint16_t) );
484 index +=
sizeof (uint16_t);
486 if (meas_header.
Format == 0x20 || meas_header.
Format == 0x22)
488 memcpy (&remission, (
void *)&buffer_[index],
sizeof (uint8_t) );
489 index +=
sizeof (uint8_t);
498 scan.header.frame_id =
"lms400_tilt_laser";
513 return (ReadConfirmationAndAnswer ());
523 AssembleCommand ((
unsigned char *) cmd, strlen (cmd));
525 n_ = write (sockfd_, command_, commandlength_);
537 memset (buffer_, 0, 256);
538 n_ = read (sockfd_, buffer_, 8);
542 if (buffer_[0] != 0x02 || buffer_[1] != 0x02 || buffer_[2] != 0x02 || buffer_[3] != 0x02)
545 ROS_WARN (
"> E: expected 4 bytes STX's!");
546 n_ = read (sockfd_, buffer_, 255);
551 int length = ( (buffer_[4] << 24) | (buffer_[5] << 16) | (buffer_[6] << 8) | (buffer_[7]) );
555 n_ = read (sockfd_, &buffer_[current], length-current);
557 }
while (current < length);
559 bufferlength_ = length;
560 if ((verbose_ > 0) && (buffer_[0] != 0x20))
561 ROS_DEBUG (
">> Received: \"%s\"\n", buffer_);
564 if (strncmp ((
const char*)buffer_,
"sFA", 3) == 0)
566 strtok ((
char*)buffer_,
" ");
567 ROS_DEBUG (
">> E: Got an error message with code 0x%s\n", strtok (NULL,
" "));
572 int ret = read (sockfd_, &cs_read, 1);
575 ROS_ERROR (
"LMS400 didnt get any data in read %d",ret);
579 if (buffer_[0] ==
's')
581 else if (buffer_[0] == 0x20)
582 return (ReadResult ());
586 ROS_DEBUG (
">>>> ReadResult: probably found a data packet!\n>>>> %s\n", buffer_);
588 unsigned char* tmp = (
unsigned char*) malloc (bufferlength_ + 1);
589 memcpy (tmp, buffer_, bufferlength_ + 1);
593 MeasurementQueue_->push_back (q);
595 return (ReadResult ());
606 return (ReadResult ());
615 if (buffer_[0] ==
's' && buffer_[1] ==
'F' && buffer_[2] ==
'A')
618 return (ReadResult ());
626 unsigned char checksum = 0;
633 command_[4] = (len >> 24) & 0xff;
634 command_[5] = (len >> 16) & 0xff;
635 command_[6] = (len >> 8) & 0xff;
636 command_[7] = (len ) & 0xff;
638 for (index = 0; index < len; index++)
640 command_[index + 8] = cmd[index];
641 checksum ^= cmd[index];
643 command_[8 + len] = checksum;
644 command_[9 + len] = 0x00;
646 commandlength_ = 9 + len;
int AssembleCommand(unsigned char *command, int len)
int ReadConfirmationAndAnswer()
int SetScanningFrequency(const char *password, float freq, float angle_start, float angle_range)
int SendCommand(const char *cmd)
const int CMD_BUFFER_SIZE
int TerminateConfiguration()
int SetResolutionAndFrequency(float freq, float ang_res, float angle_start, float angle_range)
int SetMeanFilterParameters(int num_scans)
unsigned char * ParseIP(char *ip)
int SetNetmask(char *mask)
int SetPort(uint16_t port)
sensor_msgs::LaserScan ReadMeasurement()
int GetMACAddress(char **macadress)
int StartMeasurement(bool intensity=true)
int EnableFilters(int filter_mask)
int SetAngularResolution(const char *password, float ang_res, float angle_start, float angle_range)
int SetRangeFilterParameters(float range_min, float range_max)
int SetUserLevel(int8_t userlevel, const char *password)