input.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (C) 2007, 2009, 2010, 2015 Austin Robot Technology, Patrick Beeson, Jack O'Quin
  2. // All rights reserved.
  3. //
  4. // Software License Agreement (BSD License 2.0)
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following
  14. // disclaimer in the documentation and/or other materials provided
  15. // with the distribution.
  16. // * Neither the name of {copyright_holder} nor the names of its
  17. // contributors may be used to endorse or promote products derived
  18. // from this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. // POSSIBILITY OF SUCH DAMAGE.
  32. /** \file
  33. *
  34. * Input classes for the Velodyne HDL-64E 3D LIDAR:
  35. *
  36. * Input -- base class used to access the data independently of
  37. * its source
  38. *
  39. * InputSocket -- derived class reads live data from the device
  40. * via a UDP socket
  41. *
  42. * InputPCAP -- derived class provides a similar interface from a
  43. * PCAP dump
  44. */
  45. #include <unistd.h>
  46. #include <string>
  47. #include <sstream>
  48. #include <sys/socket.h>
  49. #include <arpa/inet.h>
  50. #include <poll.h>
  51. #include <errno.h>
  52. #include <fcntl.h>
  53. #include <sys/file.h>
  54. #include "input.h"
  55. namespace velodyne_driver
  56. {
  57. ////////////////////////////////////////////////////////////////////////
  58. // InputSocket class implementation
  59. ////////////////////////////////////////////////////////////////////////
  60. bool InputSocket::init(std::string ip, uint16_t port)
  61. {
  62. m_port = port;
  63. m_devip_str = ip;
  64. m_sockfd = -1;
  65. if (!m_devip_str.empty())
  66. {
  67. inet_aton(m_devip_str.c_str(), &m_devip);
  68. }
  69. // connect to Velodyne UDP port
  70. LOG(INFO) << "Opening UDP socket: port " << m_port;
  71. m_sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  72. if (m_sockfd == -1)
  73. {
  74. perror("socket"); // TODO: ROS_ERROR errno
  75. return false;
  76. }
  77. sockaddr_in my_addr; // my address information
  78. memset(&my_addr, 0, sizeof(my_addr)); // initialize to zeros
  79. my_addr.sin_family = AF_INET; // host byte order
  80. my_addr.sin_port = htons(m_port); // port in network byte order
  81. my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill in my IP
  82. if (bind(m_sockfd, (sockaddr *)&my_addr, sizeof(sockaddr)) == -1)
  83. {
  84. perror("bind"); // TODO: ROS_ERROR errno
  85. return false;
  86. }
  87. if (fcntl(m_sockfd, F_SETFL, O_NONBLOCK | FASYNC) < 0)
  88. {
  89. perror("non-block");
  90. return false;
  91. }
  92. LOG(INFO) << "Velodyne socket fd is " << m_sockfd;
  93. return true;
  94. }
  95. void InputSocket::uninit()
  96. {
  97. (void)close(m_sockfd);
  98. }
  99. /** @brief Get one velodyne packet. */
  100. int InputSocket::getPacket(unsigned char* pkt)
  101. {
  102. struct pollfd fds[1];
  103. fds[0].fd = m_sockfd;
  104. fds[0].events = POLLIN;
  105. static const int POLL_TIMEOUT = 1000; // one second (in msec)
  106. sockaddr_in sender_address;
  107. socklen_t sender_address_len = sizeof(sender_address);
  108. while (true)
  109. {
  110. // Unfortunately, the Linux kernel recvfrom() implementation
  111. // uses a non-interruptible sleep() when waiting for data,
  112. // which would cause this method to hang if the device is not
  113. // providing data. We poll() the device first to make sure
  114. // the recvfrom() will not block.
  115. //
  116. // Note, however, that there is a known Linux kernel bug:
  117. //
  118. // Under Linux, select() may report a socket file descriptor
  119. // as "ready for reading", while nevertheless a subsequent
  120. // read blocks. This could for example happen when data has
  121. // arrived but upon examination has wrong checksum and is
  122. // discarded. There may be other circumstances in which a
  123. // file descriptor is spuriously reported as ready. Thus it
  124. // may be safer to use O_NONBLOCK on sockets that should not
  125. // block.
  126. // poll() until input available
  127. do
  128. {
  129. int retval = poll(fds, 1, POLL_TIMEOUT);
  130. if (retval < 0) // poll() error?
  131. {
  132. if (errno != EINTR)
  133. LOG(ERROR) << m_port << " poll() error:" << strerror(errno);
  134. return -1;
  135. }
  136. if (retval == 0) // poll() timeout?
  137. {
  138. LOG_EVERY_N(WARNING, 3000) << "Velodyne poll() timeout "<<m_port;
  139. return -1;
  140. }
  141. if ((fds[0].revents & POLLERR) || (fds[0].revents & POLLHUP) || (fds[0].revents & POLLNVAL)) // device error?
  142. {
  143. LOG(ERROR) << "poll() reports Velodyne error " << m_port;
  144. return -1;
  145. }
  146. } while ((fds[0].revents & POLLIN) == 0);
  147. // Receive packets that should now be available from the
  148. // socket using a blocking read.
  149. ssize_t nbytes = recvfrom(m_sockfd, pkt,
  150. PACKET_SIZE, 0,
  151. (sockaddr *)&sender_address,
  152. &sender_address_len);
  153. if (nbytes < 0)
  154. {
  155. if (errno != EWOULDBLOCK)
  156. {
  157. perror("recvfail");
  158. LOG(INFO) << "recvfail";
  159. return -1;
  160. }
  161. }
  162. else if ((size_t)nbytes == PACKET_SIZE)
  163. {
  164. // read successful,
  165. // if packet is not from the lidar scanner we selected by IP,
  166. // continue otherwise we are done
  167. if (m_devip_str != "" && sender_address.sin_addr.s_addr != m_devip.s_addr)
  168. continue;
  169. else
  170. {
  171. // memcpy(&packet, pkt, PACKET_SIZE);
  172. break; //done
  173. }
  174. }
  175. LOG(INFO) << "incomplete Velodyne packet read: " << nbytes << " bytes";
  176. }
  177. return 0;
  178. }
  179. } // velodyne namespace