Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpSickLDMRS.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
43 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
44 
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <netdb.h>
48 #include <netinet/in.h>
49 #include <string.h>
50 #include <sys/select.h>
51 #include <sys/socket.h>
52 #include <visp3/core/vpDebug.h>
53 #include <visp3/core/vpMath.h>
54 #include <visp3/core/vpTime.h>
55 #include <visp3/sensor/vpSickLDMRS.h>
56 //#include <strings.h>
57 #include <assert.h>
58 #include <limits.h>
59 #include <math.h>
60 #include <stdlib.h>
61 
62 BEGIN_VISP_NAMESPACE
69 vpSickLDMRS::vpSickLDMRS()
70  : socket_fd(-1), body(nullptr), vAngle(), time_offset(0), isFirstMeasure(true), maxlen_body(104000)
71 {
72  ip = "131.254.12.119";
73  port = 12002;
74  body = new unsigned char[maxlen_body];
75 
76  vAngle.resize(4); // Vertical angle of the 4 layers
77  vAngle[0] = vpMath::rad(-1.2);
78  vAngle[1] = vpMath::rad(-0.4);
79  vAngle[2] = vpMath::rad(0.4);
80  vAngle[3] = vpMath::rad(1.2);
81 }
82 
86 vpSickLDMRS::~vpSickLDMRS()
87 {
88  if (body)
89  delete[] body;
90 }
91 
101 bool vpSickLDMRS::setup(const std::string &ip_address, int com_port)
102 {
103  setIpAddress(ip_address);
104  setPort(com_port);
105  return (this->setup());
106 }
107 
113 bool vpSickLDMRS::setup()
114 {
115  struct sockaddr_in serv_addr;
116  int res;
117  struct timeval tv;
118  fd_set myset;
119 
120  // Create the TCP socket
121  socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
122  if (socket_fd < 0) {
123  fprintf(stderr, "Failed to create socket\n");
124  return false;
125  }
126  // bzero(&serv_addr, sizeof(serv_addr));
127  memset(&serv_addr, 0, sizeof(serv_addr));
128  serv_addr.sin_family = AF_INET; // Internet/IP
129  serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); // IP address
130  serv_addr.sin_port = htons(port); // server port
131 
132  // Establish connection
133  res = connect(socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
134  if (errno == EINPROGRESS) {
135  tv.tv_sec = 3;
136  tv.tv_usec = 0;
137  FD_ZERO(&myset);
138  FD_SET(static_cast<unsigned int>(socket_fd), &myset);
139  res = select(socket_fd + 1, nullptr, &myset, nullptr, &tv);
140  if (res < 0 && errno != EINTR) {
141  fprintf(stderr, "Error connecting to server %d - %s\n", errno, strerror(errno));
142  return false;
143  }
144  else if (res > 0) {
145  fprintf(stderr, "ok");
146  }
147  else {
148  fprintf(stderr, "Timeout in select() - Cancelling!\n");
149  return false;
150  }
151  }
152 
153  return true;
154 }
155 
162 bool vpSickLDMRS::measure(vpLaserScan laserscan[4])
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 
241  // get the number of points of this measurement
242  unsigned short numPoints = ushortptr[14];
243 
244  int nlayers = 4;
245  for (int i = 0; i < nlayers; i++) {
246  laserscan[i].clear();
247  laserscan[i].setMeasurementId(measurementId);
248  laserscan[i].setStartTimestamp(startTimestamp);
249  laserscan[i].setEndTimestamp(endTimestamp);
250  laserscan[i].setNumSteps(numSteps);
251  laserscan[i].setStartAngle(startAngle);
252  laserscan[i].setStopAngle(stopAngle);
253  laserscan[i].setNumPoints(numPoints);
254  }
255 
256  // decode the measured points
257  double hAngle; // horizontal angle in rad
258  double rDist; // radial distance in meters
259  vpScanPoint scanPoint;
260 
261  if (numPoints > USHRT_MAX - 2)
262  throw(vpException(vpException::ioError, "Out of range number of point"));
263 
264  for (int i = 0; i < numPoints; i++) {
265  ushortptr = (unsigned short *)(body + 44 + i * 10);
266  unsigned char layer = ((unsigned char)body[44 + i * 10]) & 0x0F;
267  unsigned char echo = ((unsigned char)body[44 + i * 10]) >> 4;
268  // unsigned char flags = (unsigned char) body[44+i*10+1];
269 
270  if (echo == 0) {
271  hAngle = (2.f * M_PI / numSteps) * (short)ushortptr[1];
272  rDist = 0.01 * ushortptr[2]; // cm to meters conversion
273 
274  // vpTRACE("layer: %d d: %f hangle: %f", layer, rDist, hAngle);
275  scanPoint.setPolar(rDist, hAngle, vAngle[layer]);
276  laserscan[layer].addPoint(scanPoint);
277  }
278  }
279  return true;
280 }
281 END_VISP_NAMESPACE
282 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ ioError
I/O error.
Definition: vpException.h:67
Implements a laser scan data structure that contains especially the list of scanned points that have ...
Definition: vpLaserScan.h:65
void setNumPoints(const unsigned short &num_points)
Definition: vpLaserScan.h:112
void setStopAngle(const short &stop_angle)
Definition: vpLaserScan.h:110
void setMeasurementId(const unsigned short &id)
Definition: vpLaserScan.h:100
void setNumSteps(const unsigned short &num_steps)
Definition: vpLaserScan.h:106
void setStartTimestamp(const double &start_timestamp)
Definition: vpLaserScan.h:102
void addPoint(const vpScanPoint &p)
Definition: vpLaserScan.h:89
void clear()
Definition: vpLaserScan.h:91
void setEndTimestamp(const double &end_timestamp)
Definition: vpLaserScan.h:104
void setStartAngle(const short &start_angle)
Definition: vpLaserScan.h:108
static double rad(double deg)
Definition: vpMath.h:129
Class that defines a single laser scanner point.
Definition: vpScanPoint.h:76
void setPolar(double r_dist, double h_angle, double v_angle)
Definition: vpScanPoint.h:107
VISP_EXPORT double measureTimeSecond()