ViSP  2.8.0
All Classes Functions Variables Enumerations Enumerator Friends Modules Pages
vpSickLDMRS.cpp
1 /****************************************************************************
2  *
3  * $Id: vpSickLDMRS.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Sick LD-MRS laser driver on UNIX platform.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 #if ( defined(UNIX) && !defined(WIN32) )
43 
44 #include <visp/vpSickLDMRS.h>
45 #include <visp/vpMath.h>
46 #include <visp/vpDebug.h>
47 #include <visp/vpTime.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <netdb.h>
53 #include <string.h>
54 #include <strings.h>
55 #include <math.h>
56 #include <assert.h>
57 #include <stdlib.h>
58 
59 
60 
74 vpSickLDMRS::vpSickLDMRS()
75 {
76  ip = "131.254.12.119";
77  port = 12002;
78  body = new unsigned char [104000];
79  isFirstMeasure = true;
80  time_offset = 0;
81 
82  vAngle.resize(4); // Vertical angle of the 4 layers
83  vAngle[0] = vpMath::rad(-1.2);
84  vAngle[1] = vpMath::rad(-0.4);
85  vAngle[2] = vpMath::rad( 0.4);
86  vAngle[3] = vpMath::rad( 1.2);
87 
88 }
89 
93 vpSickLDMRS::~vpSickLDMRS()
94 {
95  if (body)
96  delete [] body;
97 }
98 
108 bool vpSickLDMRS::setup(std::string ip, int port)
109 {
110  setIpAddress( ip );
111  setPort( port );
112  return ( this->setup() );
113 }
114 
120 bool vpSickLDMRS::setup()
121 {
122  struct sockaddr_in serv_addr;
123  int res;
124  struct timeval tv;
125  fd_set myset;
126 
127  // Create the TCP socket
128  socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
129  if (socket_fd < 0) {
130  fprintf(stderr, "Failed to create socket\n");
131  return false;
132  }
133  bzero(&serv_addr, sizeof(serv_addr));
134  serv_addr.sin_family = AF_INET; // Internet/IP
135  serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); // IP address
136  serv_addr.sin_port = htons(port); // server port
137 
138  // Establish connection
139  res = connect(socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) ;
140  if (errno == EINPROGRESS) {
141  tv.tv_sec = 3;
142  tv.tv_usec = 0;
143  FD_ZERO(&myset);
144  FD_SET(static_cast<unsigned int>(socket_fd), &myset);
145  res = select(socket_fd+1, NULL, &myset, NULL, &tv);
146  if (res < 0 && errno != EINTR) {
147  fprintf(stderr, "Error connecting to server %d - %s\n", errno, strerror(errno));
148  return false;
149  }
150  else if (res > 0) {
151  fprintf(stderr,"ok");
152  }
153  else {
154  fprintf(stderr, "Timeout in select() - Cancelling!\n");
155  return false;
156  }
157  }
158 
159  return true;
160 }
161 
168 bool vpSickLDMRS::measure(vpLaserScan laserscan[4])
169 {
170  unsigned int *uintptr;
171  unsigned short *ushortptr;
172  static unsigned char header[24];
173  ushortptr=(unsigned short *)header;
174  uintptr=(unsigned int *)header;
175 
176  assert (sizeof(header) == 24);
177  //std::cout << "size " << sizeof(header) << std::endl;
178 
179  double time_second = 0;
180 
181  if (isFirstMeasure) {
182  time_second = vpTime::measureTimeSecond();
183  }
184 
185  // read the 24 bytes header
186  if (recv(socket_fd, header, sizeof(header), MSG_WAITALL) == -1) {
187  printf("recv\n");
188  perror("recv");
189  return false;
190  }
191 
192  if (ntohl(uintptr[0]) != vpSickLDMRS::MagicWordC2) {
193  printf("Error, wrong magic number !!!\n");
194  return false;
195  }
196 
197  // get the message body
198  uint16_t msgtype = ntohs(ushortptr[7]);
199  uint32_t msgLength = ntohl(uintptr[2]);
200 
201  ssize_t len = recv(socket_fd, body, msgLength, MSG_WAITALL);
202  if (len != (ssize_t)msgLength){
203  printf("Error, wrong msg length: %d of %d bytes.\n", (int)len, msgLength);
204  return false;
205  }
206 
207  if (msgtype!=vpSickLDMRS::MeasuredData){
208  //printf("The message in not relative to measured data !!!\n");
209  return true;
210  }
211 
212  // decode measured data
213 
214  // get the measurement number
215  unsigned short measurementId;
216  ushortptr = (unsigned short *) body;
217  measurementId = ushortptr[0];
218 
219  // get the start timestamp
220  uintptr=(unsigned int *) (body+6);
221  unsigned int seconds = uintptr[1];
222  unsigned int fractional=uintptr[0];
223  double startTimestamp = seconds + fractional / 4294967296.; // 4294967296. = 2^32
224 
225  // get the end timestamp
226  uintptr=(unsigned int *) (body+14);
227  seconds = uintptr[1];
228  fractional=uintptr[0];
229  double endTimestamp = seconds + fractional / 4294967296.; // 4294967296. = 2^32
230 
231  // compute the time offset to bring the measures in the Unix time reference
232  if (isFirstMeasure) {
233  time_offset = time_second - startTimestamp;
234  isFirstMeasure = false;
235  }
236 
237  startTimestamp += time_offset;
238  endTimestamp += time_offset;
239 
240  // get the number of steps per scanner rotation
241  unsigned short numSteps = ushortptr[11];
242 
243  // get the start/stop angle
244  short startAngle = (short)ushortptr[12];
245  short stopAngle = (short)ushortptr[13];
246 // std::cout << "angle in [" << startAngle << "; " << stopAngle
247 // << "]" << std::endl;
248 
249  // get the number of points of this measurement
250  unsigned short numPoints = ushortptr[14];
251 
252  int nlayers = 4;
253  for (int i=0; i < nlayers; i++) {
254  laserscan[i].clear();
255  laserscan[i].setMeasurementId(measurementId);
256  laserscan[i].setStartTimestamp(startTimestamp);
257  laserscan[i].setEndTimestamp(endTimestamp);
258  laserscan[i].setNumSteps(numSteps);
259  laserscan[i].setStartAngle(startAngle);
260  laserscan[i].setStopAngle(stopAngle);
261  laserscan[i].setNumPoints(numPoints);
262  }
263 
264  // decode the measured points
265  double hAngle; // horizontal angle in rad
266  double rDist; // radial distance in meters
267  vpScanPoint scanPoint;
268 
269  for (int i=0; i < numPoints; i++) {
270  ushortptr = (unsigned short *) (body+44+i*10);
271  unsigned char layer = ((unsigned char) body[44+i*10])&0x0F;
272  unsigned char echo = ((unsigned char) body[44+i*10])>>4;
273  //unsigned char flags = (unsigned char) body[44+i*10+1];
274 
275  if (echo==0) {
276  hAngle = (2.f * M_PI / numSteps)*(short) ushortptr[1];
277  rDist = 0.01 * ushortptr[2]; // cm to meters conversion
278 
279  //vpTRACE("layer: %d d: %f hangle: %f", layer, rDist, hAngle);
280  scanPoint.setPolar(rDist, hAngle, vAngle[layer]);
281  laserscan[layer].addPoint(scanPoint);
282  }
283  }
284  return true;
285 }
286 
287 #endif
void setMeasurementId(const unsigned short &measurementId)
Definition: vpLaserScan.h:106
void setNumPoints(const unsigned short &numPoints)
Definition: vpLaserScan.h:130
void setStartAngle(const short &startAngle)
Definition: vpLaserScan.h:122
Implements a laser scan data structure that contains especially the list of scanned points that have ...
Definition: vpLaserScan.h:66
void clear()
Definition: vpLaserScan.h:97
void setEndTimestamp(const double &endTimestamp)
Definition: vpLaserScan.h:114
Class that defines a single laser scanner point.
Definition: vpScanPoint.h:74
static double rad(double deg)
Definition: vpMath.h:100
void setStopAngle(const short &stopAngle)
Definition: vpLaserScan.h:126
void setNumSteps(const unsigned short &numSteps)
Definition: vpLaserScan.h:118
void setPolar(double rDist, double hAngle, double vAngle)
Definition: vpScanPoint.h:108
void addPoint(const vpScanPoint &p)
Definition: vpLaserScan.h:93
void setStartTimestamp(const double &startTimestamp)
Definition: vpLaserScan.h:110
static double measureTimeSecond()
Definition: vpTime.cpp:225