Apollo 11.0
自动驾驶开放平台
apollo::drivers::lslidar::driver::InputSocket类 参考

Live lslidar input from socket. 更多...

#include <input.h>

类 apollo::drivers::lslidar::driver::InputSocket 继承关系图:
apollo::drivers::lslidar::driver::InputSocket 的协作图:

Public 成员函数

 InputSocket (uint16_t port=MSOP_DATA_PORT_NUMBER, std::string lidar_ip="192.168.1.200", int packet_size=1212)
 
virtual ~InputSocket ()
 
virtual int GetPacket (LslidarPacket *pkt)
 
- Public 成员函数 继承自 apollo::drivers::lslidar::driver::Input
 Input (uint16_t portport=MSOP_DATA_PORT_NUMBER, std::string lidar_ip="192.168.1.200", int packet_size=1212)
 
virtual ~Input ()
 

额外继承的成员函数

- Protected 属性 继承自 apollo::drivers::lslidar::driver::Input
int port_
 
int sockfd_
 
uint64_t pointcloudTimeStamp
 
in_addr devip_
 
int packet_size_
 

详细描述

Live lslidar input from socket.

在文件 input.h53 行定义.

构造及析构函数说明

◆ InputSocket()

apollo::drivers::lslidar::driver::InputSocket::InputSocket ( uint16_t  port = MSOP_DATA_PORT_NUMBER,
std::string  lidar_ip = "192.168.1.200",
int  packet_size = 1212 
)

在文件 input.cc48 行定义.

48 :
49 Input(port, lidar_ip, packet_size) {
50 port_ = port;
51 sockfd_ = -1;
52 sockfd_ = socket(PF_INET, SOCK_DGRAM, 0);
53 if (-1 == sockfd_) {
54 AERROR << "socket open error";
55 return;
56 }
57
58 inet_aton(lidar_ip.c_str(), &devip_);
59 sockaddr_in myAddress; // my address information
60 memset(&myAddress, 0, sizeof(myAddress)); // initialize to zeros
61 myAddress.sin_family = AF_INET; // host byte order
62 myAddress.sin_port = htons(port); // port in network byte order
63 myAddress.sin_addr.s_addr = INADDR_ANY; // automatically fill in my IP
64
65 if (bind(sockfd_,
66 reinterpret_cast<sockaddr *>(&myAddress),
67 sizeof(sockaddr))
68 == -1) {
69 AERROR << "bind error, port:" << port;
70 return;
71 }
72
73 if (fcntl(sockfd_, F_SETFL, O_NONBLOCK | FASYNC) < 0) {
74 AERROR << "fcntl error";
75 return;
76 }
77}
Input(uint16_t portport=MSOP_DATA_PORT_NUMBER, std::string lidar_ip="192.168.1.200", int packet_size=1212)
Definition input.cc:35
#define AERROR
Definition log.h:44

◆ ~InputSocket()

apollo::drivers::lslidar::driver::InputSocket::~InputSocket ( void  )
virtual

在文件 input.cc79 行定义.

79 {
80 (void)close(sockfd_);
81}

成员函数说明

◆ GetPacket()

int apollo::drivers::lslidar::driver::InputSocket::GetPacket ( LslidarPacket pkt)
virtual

重载 apollo::drivers::lslidar::driver::Input .

在文件 input.cc83 行定义.

83 {
84 double time1 = apollo::cyber::Time().Now().ToSecond();
85 struct pollfd fds[1];
86 fds[0].fd = sockfd_;
87 fds[0].events = POLLIN;
88 static const int POLL_TIMEOUT = 3000; // one second (in msec)
89
90 sockaddr_in sender_address;
91 socklen_t sender_address_len = sizeof(sender_address);
92
93 while (true) {
94 // Receive packets that should now be available from the
95 // socket using a blocking read.
96 // poll() until input available
97 do {
98 int retval = poll(fds, 1, POLL_TIMEOUT);
99 if (retval < 0) {
100 if (errno != EINTR)
101 AERROR << "poll() error: " << strerror(errno);
102
103 return 1;
104 }
105 if (retval == 0) {
106 AERROR << "lslidar poll() timeout, port: " << port_;
107 return 1;
108 }
109 if ((fds[0].revents & POLLERR) || (fds[0].revents & POLLHUP)
110 || (fds[0].revents & POLLNVAL)) {
111 AWARN << "poll() reports lslidar error";
112 return 1;
113 }
114 } while ((fds[0].revents & POLLIN) == 0);
115 uint8_t bytes[1212];
116 ssize_t nbytes = recvfrom(
117 sockfd_,
118 bytes,
120 0,
121 reinterpret_cast<sockaddr *>(&sender_address),
122 &sender_address_len);
123 if (nbytes < 0) {
124 if (errno != EWOULDBLOCK) {
125 AERROR << "recvfail";
126 return 1;
127 }
128 } else if ((size_t)nbytes == size_t(packet_size_)) {
129 if (sender_address.sin_addr.s_addr != devip_.s_addr) {
130 AERROR << "lidar IP parameter set error,please reset in config "
131 "file";
132 continue;
133 } else {
134 pkt->set_data(bytes, packet_size_);
135 break;
136 }
137 }
138 AERROR << "incomplete lslidar packet read: " << nbytes << " bytes";
139 }
140
141 // Average the times at which we begin and end reading. Use that to
142 // estimate when the scan occurred.
143 double time2 = apollo::cyber::Time().Now().ToSecond();
144 AINFO << apollo::cyber::Time((time2 + time1) / 2.0).ToNanosecond();
145 return 0;
146}
Cyber has builtin time type Time.
Definition time.h:31
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
@ POLL_TIMEOUT

该类的文档由以下文件生成: