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