22 #include <sys/socket.h> 23 #include <arpa/inet.h> 36 #define RESULT_ERROR (-1) 73 previous_packet_counter_(0),
74 previous_packet_counter_valid_(false),
75 startOfChannelFound_(false),
83 uint32_t currentPacketSize,
84 int8_t* channelBuffer,
85 uint32_t channelBufferSize,
107 if ((*pos) + Length > channelBufferSize)
110 ROS_DEBUG(
"Channel buffer is too small.\n");
115 memcpy(channelBuffer+(*pos), currentPacketData+Start, Length);
124 double Input::slope(
const std::vector<double>& x,
const std::vector<double>& y) {
125 const double n = x.size();
126 const double s_x = std::accumulate(x.begin(), x.end(), 0.0);
127 const double s_y = std::accumulate(y.begin(), y.end(), 0.0);
128 const double s_xx = std::inner_product(x.begin(), x.end(), x.begin(), 0.0);
129 const double s_xy = std::inner_product(x.begin(), x.end(), y.begin(), 0.0);
130 const double a = (n * s_xy - s_x * s_y) / (n * s_xx - s_x * s_x);
138 const uint32_t distanceX_offset = 2052;
139 const uint32_t distanceY_offset = 6148;
140 const uint32_t distanceZ_offset = 10244;
141 const uint32_t confidence_offset = 14340;
142 const uint32_t amplitude_offset = 16388;
143 const uint32_t amplitude_normalization_offset = 18436;
144 const uint32_t camera_calibration_offset = 18464;
149 ROS_DEBUG(
"processChannel8: buf too small\n");
154 float* distanceX = (
float*)(&buf[distanceX_offset]);
155 float* distanceY = (
float*)(&buf[distanceY_offset]);
156 float* distanceZ = (
float*)(&buf[distanceZ_offset]);
157 uint16_t* confidence = (uint16_t*)(&buf[confidence_offset]);
158 uint16_t* amplitude = (uint16_t*)(&buf[amplitude_offset]);
159 float* amplitude_normalization = (
float*)(&buf[amplitude_normalization_offset]);
160 float* camera_calibration = (
float*)(&buf[camera_calibration_offset]);
165 std::vector<double> vx, vz;
166 for( uint32_t m =0; m<16; ++m)
168 for( uint32_t j =0; j<64; ++j)
170 uint32_t i = 64*m +j;
172 uint32_t pixelValid = confidence[i] & 1;
175 if (distanceX[i] > 0.2)
183 pcl::PointXYZI point;
184 point.x = distanceX[i] ;
185 point.y = distanceY[i] ;
186 point.z = distanceZ[i] - 1;
200 float normalization_factor = 1;
204 point.intensity = amplitude[i]*normalization_factor;
205 pc.points.push_back(point);
214 for(
int n = 0; n <4; n++)
215 ROS_DEBUG_STREAM(
"amplitude_normalization "<<n<<
" "<<amplitude_normalization[n]);
234 int Input::process(int8_t* udpPacketBuf,
const ssize_t rc, pcl::PointCloud<pcl::PointXYZI> & pc)
258 if (ph->
ChannelID == customerDataChannel)
308 sockfd_ = socket(PF_INET, SOCK_DGRAM, 0);
316 memset(&my_addr, 0,
sizeof(my_addr));
317 my_addr.sin_family = AF_INET;
318 my_addr.sin_port = htons(udp_port);
319 my_addr.sin_addr.s_addr = INADDR_ANY;
321 if (bind(
sockfd_, (sockaddr *)&my_addr,
sizeof(sockaddr)) == -1)
327 if (fcntl(
sockfd_,F_SETFL, O_NONBLOCK|FASYNC) < 0)
346 const uint32_t udpPacketBufLen = 2000;
347 int8_t udpPacketBuf[udpPacketBufLen];
349 struct pollfd fds[1];
351 fds[0].events = POLLIN;
352 static const int POLL_TIMEOUT = 1000;
377 int retval = poll(fds, 1, POLL_TIMEOUT);
381 ROS_ERROR(
"poll() error: %s", strerror(errno));
389 if ((fds[0].revents & POLLERR)
390 || (fds[0].revents & POLLHUP)
391 || (fds[0].revents & POLLNVAL))
393 ROS_ERROR(
"poll() reports O3M151 error");
396 }
while ((fds[0].revents & POLLIN) == 0);
399 ssize_t rc=recvfrom(
sockfd_,(
char*)udpPacketBuf,udpPacketBufLen,0,NULL,NULL);
404 int result =
process(udpPacketBuf, rc, pc);
427 std::string filename,
430 double repeat_delay):
432 packet_rate_(packet_rate)
445 ROS_INFO(
"Read input file only once.");
447 ROS_INFO(
"Read input file as quickly as possible.");
449 ROS_INFO(
"Delay %.3f seconds before repeating input file.",
456 ROS_FATAL(
"Error opening O3M151 socket dump file.");
472 struct pcap_pkthdr *header;
473 const u_char *pkt_data;
475 const uint32_t udpPacketBufLen = 2000;
476 int8_t udpPacketBuf[udpPacketBufLen];
480 bool keep_reading =
false;
482 if ((res = pcap_next_ex(
pcap_, &header, &pkt_data)) >= 0)
488 memcpy(udpPacketBuf, pkt_data+42, header->len);
490 result =
process(udpPacketBuf, header->len-42, pc);
491 ROS_DEBUG(
"result process %d", header->len);
501 ROS_WARN(
"Error %d reading O3M151 packet: %s",
502 res, pcap_geterr(
pcap_));
508 ROS_INFO(
"end of file reached -- done reading.");
514 ROS_INFO(
"end of file reached -- delaying %.3f seconds.",
519 if(keep_reading ==
false)
struct o3m151_driver::PacketHeader PacketHeader
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
struct o3m151_driver::ChannelHeader ChannelHeader
#define ROS_DEBUG_STREAM(args)
static const uint32_t customerDataChannel
#define ROS_INFO_STREAM(args)
static const size_t packet_size
struct o3m151_driver::ChannelEnd ChannelEnd