42 #if ( defined(UNIX) && !defined(WIN32) )
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 <netinet/in.h>
74 vpSickLDMRS::vpSickLDMRS()
76 ip =
"131.254.12.119";
78 body =
new unsigned char [104000];
79 isFirstMeasure =
true;
93 vpSickLDMRS::~vpSickLDMRS()
108 bool vpSickLDMRS::setup(std::string ip,
int port)
112 return ( this->setup() );
120 bool vpSickLDMRS::setup()
122 struct sockaddr_in serv_addr;
128 socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
130 fprintf(stderr,
"Failed to create socket\n");
133 bzero(&serv_addr,
sizeof(serv_addr));
134 serv_addr.sin_family = AF_INET;
135 serv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
136 serv_addr.sin_port = htons(port);
139 res = connect(socket_fd, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) ;
140 if (errno == EINPROGRESS) {
144 FD_SET(static_cast<unsigned int>(socket_fd), &myset);
145 res = select(socket_fd+1, NULL, &myset, NULL, &tv);
146 if (res < 0 && errno != EINTR) {
147 fprintf(stderr,
"Error connecting to server %d - %s\n", errno, strerror(errno));
151 fprintf(stderr,
"ok");
154 fprintf(stderr,
"Timeout in select() - Cancelling!\n");
168 bool vpSickLDMRS::measure(
vpLaserScan laserscan[4])
170 unsigned int *uintptr;
171 unsigned short *ushortptr;
172 static unsigned char header[24];
173 ushortptr=(
unsigned short *)header;
174 uintptr=(
unsigned int *)header;
176 assert (
sizeof(header) == 24);
179 double time_second = 0;
181 if (isFirstMeasure) {
186 if (recv(socket_fd, header,
sizeof(header), MSG_WAITALL) == -1) {
192 if (ntohl(uintptr[0]) != vpSickLDMRS::MagicWordC2) {
193 printf(
"Error, wrong magic number !!!\n");
198 uint16_t msgtype = ntohs(ushortptr[7]);
199 uint32_t msgLength = ntohl(uintptr[2]);
201 ssize_t len = recv(socket_fd, body, msgLength, MSG_WAITALL);
202 if (len != (ssize_t)msgLength){
203 printf(
"Error, wrong msg length: %d of %d bytes.\n", (
int)len, msgLength);
207 if (msgtype!=vpSickLDMRS::MeasuredData){
215 unsigned short measurementId;
216 ushortptr = (
unsigned short *) body;
217 measurementId = ushortptr[0];
220 uintptr=(
unsigned int *) (body+6);
221 unsigned int seconds = uintptr[1];
222 unsigned int fractional=uintptr[0];
223 double startTimestamp = seconds + fractional / 4294967296.;
226 uintptr=(
unsigned int *) (body+14);
227 seconds = uintptr[1];
228 fractional=uintptr[0];
229 double endTimestamp = seconds + fractional / 4294967296.;
232 if (isFirstMeasure) {
233 time_offset = time_second - startTimestamp;
234 isFirstMeasure =
false;
237 startTimestamp += time_offset;
238 endTimestamp += time_offset;
241 unsigned short numSteps = ushortptr[11];
244 short startAngle = (short)ushortptr[12];
245 short stopAngle = (short)ushortptr[13];
250 unsigned short numPoints = ushortptr[14];
253 for (
int i=0; i < nlayers; i++) {
254 laserscan[i].
clear();
269 for (
int i=0; i < numPoints; i++) {
270 ushortptr = (
unsigned short *) (body+44+i*10);
271 unsigned char layer = ((
unsigned char) body[44+i*10])&0x0F;
272 unsigned char echo = ((
unsigned char) body[44+i*10])>>4;
276 hAngle = (2.f * M_PI / numSteps)*(
short) ushortptr[1];
277 rDist = 0.01 * ushortptr[2];
280 scanPoint.
setPolar(rDist, hAngle, vAngle[layer]);
281 laserscan[layer].
addPoint(scanPoint);
void setMeasurementId(const unsigned short &measurementId)
void setNumPoints(const unsigned short &numPoints)
void setStartAngle(const short &startAngle)
Implements a laser scan data structure that contains especially the list of scanned points that have ...
void setEndTimestamp(const double &endTimestamp)
Class that defines a single laser scanner point.
static double rad(double deg)
void setStopAngle(const short &stopAngle)
void setNumSteps(const unsigned short &numSteps)
void setPolar(double rDist, double hAngle, double vAngle)
void addPoint(const vpScanPoint &p)
void setStartTimestamp(const double &startTimestamp)
static double measureTimeSecond()