Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034 #include <netinet/in.h>
00035 #include <arpa/inet.h>
00036 #include <unistd.h>
00037 #include <fcntl.h>
00038 #include <sys/stat.h>
00039 #include <sys/types.h>
00040 #include <sys/socket.h>
00041
00042 #include <ros/ros.h>
00043 #include <sensor_msgs/LaserScan.h>
00044
00045 #define PORT 4001 //The port of Nport
00046 #define IP "192.168.1.213" //The IP of Nport
00047
00048 int main (int argc, char **argv)
00049 {
00050 int n_socket;
00051 struct sockaddr_in socket_addr;
00052 char buf[2048];
00053
00054
00055 ros::init (argc, argv, "sicklms200");
00056 ros::NodeHandle nh;
00057 ros::Publisher sicklms = nh.advertise < sensor_msgs::LaserScan > ("/scan", 1000);
00058
00059
00060 if ((n_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0)
00061 {
00062 perror ("socket create error!");
00063
00064 return 1;
00065 }
00066 else
00067 {
00068 ROS_INFO ("socket create successfully\n");
00069 ROS_INFO ("socket id is: %d\n", n_socket);
00070 }
00071
00072 bzero (&socket_addr, sizeof (struct sockaddr_in));
00073 socket_addr.sin_family = AF_INET;
00074 socket_addr.sin_port = htons (PORT);
00075 socket_addr.sin_addr.s_addr = inet_addr (IP);
00076
00077
00078 if (connect (n_socket, (struct sockaddr *) (&socket_addr), sizeof (struct sockaddr)) < 0)
00079 {
00080 perror ("connect error");
00081
00082 return 1;
00083 }
00084 else
00085 {
00086 ROS_INFO ("connect successfully\n");
00087 ROS_INFO ("IP is: %s\n", IP);
00088 ROS_INFO ("PORT is:%d\n", PORT);
00089 }
00090
00091 bzero (buf, sizeof (buf));
00092
00093 unsigned int point_num = 361;
00094 double laser_frequency = 40;
00095
00096
00097 sensor_msgs::LaserScan sickscan;
00098 sickscan.header.frame_id = "laser_frame";
00099 sickscan.angle_min = -1.57;
00100 sickscan.angle_max = 1.57;
00101 sickscan.angle_increment = 3.14 / point_num;
00102 sickscan.time_increment = (1 / laser_frequency) / point_num;
00103 sickscan.range_min = 0.0;
00104 sickscan.range_max = 8000.0;
00105
00106 sickscan.ranges.resize (point_num);
00107 sickscan.intensities.resize (point_num);
00108 for (int i = 0; i < (int)point_num; i++)
00109 {
00110 sickscan.intensities[i] = 0.0;
00111 }
00112
00113 ros::Rate r (40.0);
00114 bool is_found = false;
00115 int nrecv_all = 0;
00116 int head = 0;
00117
00118 while (ros::ok ())
00119 {
00120 int nrecv = recv (n_socket, buf + nrecv_all, sizeof (buf) - nrecv_all, 0);
00121 if (nrecv == -1)
00122 {
00123 perror ("receive error");
00124 break;
00125 }
00126 else
00127 {
00128 nrecv_all += nrecv;
00129 if (nrecv_all < 732)
00130 {
00131 continue;
00132 }
00133 else
00134 {
00135 for (head = 0; head < nrecv_all; head++)
00136 {
00137 if (0x02 == (unsigned char) buf[head]
00138 && 0x80 == (unsigned char) buf[head + 1]
00139 && 0xD6 == (unsigned char) buf[head + 2] && 0x02 == (unsigned char) buf[head + 3])
00140 {
00141 is_found = true;
00142 break;
00143 }
00144 }
00145
00146 if (is_found && (nrecv_all - head > 732))
00147 {
00148 for (int i = head + 7, n = 0; i < head + 7 + 722; i += 2, n++)
00149 {
00150 sickscan.ranges[n] = *((short int *) (buf + i))/1000.0;
00151 }
00152 ros::Time scan_time = ros::Time::now ();
00153 sickscan.header.stamp = scan_time;
00154
00155 sicklms.publish (sickscan);
00156 r.sleep ();
00157
00158 for (int i = head + 732, i_ = 0; i < nrecv_all; i++, i_++)
00159 {
00160 buf[i_] = buf[i];
00161 }
00162 nrecv_all = nrecv_all - head - 732;
00163 }
00164 }
00165 }
00166 }
00167
00168 close (n_socket);
00169
00170 return 0;
00171 }