42 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
44 #include <visp/vpSickLDMRS.h>
45 #include <visp/vpMath.h>
46 #include <visp/vpDebug.h>
47 #include <visp/vpTime.h>
48 #include <sys/socket.h>
49 #include <sys/select.h>
50 #include <netinet/in.h>
76 vpSickLDMRS::vpSickLDMRS()
77 : socket_fd(-1), body(NULL), vAngle(), time_offset(0),
78 isFirstMeasure(true), maxlen_body(104000)
80 ip =
"131.254.12.119";
82 body =
new unsigned char [maxlen_body];
94 vpSickLDMRS::~vpSickLDMRS()
109 bool vpSickLDMRS::setup(std::string ip_address,
int com_port)
111 setIpAddress( ip_address );
113 return ( this->setup() );
121 bool vpSickLDMRS::setup()
123 struct sockaddr_in serv_addr;
129 socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
131 fprintf(stderr,
"Failed to create socket\n");
135 memset(&serv_addr, 0,
sizeof(serv_addr));
136 serv_addr.sin_family = AF_INET;
137 serv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
138 serv_addr.sin_port = htons(port);
141 res = connect(socket_fd, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) ;
142 if (errno == EINPROGRESS) {
146 FD_SET(static_cast<unsigned int>(socket_fd), &myset);
147 res = select(socket_fd+1, NULL, &myset, NULL, &tv);
148 if (res < 0 && errno != EINTR) {
149 fprintf(stderr,
"Error connecting to server %d - %s\n", errno, strerror(errno));
153 fprintf(stderr,
"ok");
156 fprintf(stderr,
"Timeout in select() - Cancelling!\n");
170 bool vpSickLDMRS::measure(
vpLaserScan laserscan[4])
172 unsigned int *uintptr;
173 unsigned short *ushortptr;
174 static unsigned char header[24];
175 ushortptr=(
unsigned short *)header;
176 uintptr=(
unsigned int *)header;
178 assert (
sizeof(header) == 24);
181 double time_second = 0;
183 if (isFirstMeasure) {
188 if (recv(socket_fd, header,
sizeof(header), MSG_WAITALL) == -1) {
194 if (ntohl(uintptr[0]) != vpSickLDMRS::MagicWordC2) {
195 printf(
"Error, wrong magic number !!!\n");
200 uint16_t msgtype = ntohs(ushortptr[7]);
201 uint32_t msgLength = ntohl(uintptr[2]);
203 ssize_t len = recv(socket_fd, body, msgLength, MSG_WAITALL);
204 if (len != (ssize_t)msgLength){
205 printf(
"Error, wrong msg length: %d of %d bytes.\n", (
int)len, msgLength);
209 if (msgtype!=vpSickLDMRS::MeasuredData){
217 unsigned short measurementId;
218 ushortptr = (
unsigned short *) body;
219 measurementId = ushortptr[0];
222 uintptr=(
unsigned int *) (body+6);
223 unsigned int seconds = uintptr[1];
224 unsigned int fractional=uintptr[0];
225 double startTimestamp = seconds + fractional / 4294967296.;
228 uintptr=(
unsigned int *) (body+14);
229 seconds = uintptr[1];
230 fractional=uintptr[0];
231 double endTimestamp = seconds + fractional / 4294967296.;
234 if (isFirstMeasure) {
235 time_offset = time_second - startTimestamp;
236 isFirstMeasure =
false;
239 startTimestamp += time_offset;
240 endTimestamp += time_offset;
243 unsigned short numSteps = ushortptr[11];
246 short startAngle = (short)ushortptr[12];
247 short stopAngle = (short)ushortptr[13];
252 unsigned short numPoints = ushortptr[14];
255 for (
int i=0; i < nlayers; i++) {
256 laserscan[i].
clear();
271 if (numPoints > USHRT_MAX-2)
274 for (
int i=0; i < numPoints; i++) {
275 ushortptr = (
unsigned short *) (body+44+i*10);
276 unsigned char layer = ((
unsigned char) body[44+i*10])&0x0F;
277 unsigned char echo = ((
unsigned char) body[44+i*10])>>4;
281 hAngle = (2.f * M_PI / numSteps)*(
short) ushortptr[1];
282 rDist = 0.01 * ushortptr[2];
285 scanPoint.
setPolar(rDist, hAngle, vAngle[layer]);
286 laserscan[layer].
addPoint(scanPoint);
error that can be emited by ViSP classes.
Implements a laser scan data structure that contains especially the list of scanned points that have ...
Class that defines a single laser scanner point.
void setStartAngle(const short &start_angle)
void setNumPoints(const unsigned short &num_points)
static double rad(double deg)
void setEndTimestamp(const double &end_timestamp)
void setNumSteps(const unsigned short &num_steps)
void addPoint(const vpScanPoint &p)
void setStartTimestamp(const double &start_timestamp)
static double measureTimeSecond()
void setMeasurementId(const unsigned short &id)
void setPolar(double r_dist, double h_angle, double v_angle)
void setStopAngle(const short &stop_angle)