laser_scan.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *  Software License Agreement (BSD License)
00003 *  Copyright (c) 2013, Intelligent Robotics Lab, DLUT.
00004 *  All rights reserved.
00005 *  Author:Zhao Cilang,Yan Fei,Zhuang Yan.
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *   * Redistributions of source code must retain the above copyright
00011 *     notice, this list of conditions and the following disclaimer.
00012 *   * Redistributions in binary form must reproduce the above
00013 *     copyright notice, this list of conditions and the following
00014 *     disclaimer in the documentation and/or other materials provided
00015 *     with the distribution.
00016 *   * Neither the name of the Intelligent Robotics Lab nor the names of its
00017 *     contributors may be used to endorse or promote products derived
00018 *     from this software without specific prior written permission.
00019 *
00020 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 *  POSSIBILITY OF SUCH DAMAGE.
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   //create a publisher
00055   ros::init (argc, argv, "sicklms200");
00056   ros::NodeHandle nh;
00057   ros::Publisher sicklms = nh.advertise < sensor_msgs::LaserScan > ("/scan", 1000);
00058 
00059   //create a socket 
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   //connect to the nport
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   //create a LaserScan message,and assign the value
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);  //receive the data from laser
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++)        //find the head of one original message,so we can get one intact laser data.
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++)        //get the intact laser data from the original message,one intact laser data has 361 points.unit(m).
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);   //publish the ros_message
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 }


sick_laser
Author(s): Zhao Cilang,Yan Fei,Zhuang Yan/zhuang@dlut.edu.cn
autogenerated on Sun Jan 5 2014 11:05:03