Go to the documentation of this file.00001
00027 #include <ros/ros.h>
00028 #include <boost/shared_ptr.hpp>
00029 #include <diagnostic_updater/publisher.h>
00030 #include <sensor_msgs/LaserScan.h>
00031
00032 #include "odva_ethernetip/socket/tcp_socket.h"
00033 #include "odva_ethernetip/socket/udp_socket.h"
00034 #include "omron_os32c_driver/os32c.h"
00035 #include "omron_os32c_driver/range_and_reflectance_measurement.h"
00036
00037 using std::cout;
00038 using std::endl;
00039 using boost::shared_ptr;
00040 using sensor_msgs::LaserScan;
00041 using eip::socket::TCPSocket;
00042 using eip::socket::UDPSocket;
00043 using namespace omron_os32c_driver;
00044 using namespace diagnostic_updater;
00045
00046
00047 int main(int argc, char *argv[])
00048 {
00049 ros::init(argc, argv, "os32c");
00050 ros::NodeHandle nh;
00051
00052
00053 string host, frame_id, local_ip;
00054 double start_angle, end_angle, expected_frequency, frequency_tolerance, timestamp_min_acceptable,
00055 timestamp_max_acceptable;
00056 ros::param::param<std::string>("~host", host, "192.168.1.1");
00057 ros::param::param<std::string>("~local_ip", local_ip, "0.0.0.0");
00058 ros::param::param<std::string>("~frame_id", frame_id, "laser");
00059 ros::param::param<double>("~start_angle", start_angle, OS32C::ANGLE_MAX);
00060 ros::param::param<double>("~end_angle", end_angle, OS32C::ANGLE_MIN);
00061 ros::param::param<double>("~expected_frequency", expected_frequency, 12.856);
00062 ros::param::param<double>("~frequency_tolerance", frequency_tolerance, 0.1);
00063 ros::param::param<double>("~timestamp_min_acceptable", timestamp_min_acceptable, -1);
00064 ros::param::param<double>("~timestamp_max_acceptable", timestamp_max_acceptable, -1);
00065
00066
00067 ros::Publisher laserscan_pub = nh.advertise<LaserScan>("scan", 1);
00068
00069
00070 Updater updater;
00071 updater.setHardwareID(host);
00072 DiagnosedPublisher<LaserScan> diagnosed_publisher(laserscan_pub, updater, FrequencyStatusParam(&expected_frequency,
00073 &expected_frequency,
00074 frequency_tolerance),
00075 TimeStampStatusParam(timestamp_min_acceptable,
00076 timestamp_max_acceptable));
00077
00078 boost::asio::io_service io_service;
00079 shared_ptr<TCPSocket> socket = shared_ptr<TCPSocket>(new TCPSocket(io_service));
00080 shared_ptr<UDPSocket> io_socket = shared_ptr<UDPSocket>(new UDPSocket(io_service, 2222, local_ip));
00081 OS32C os32c(socket, io_socket);
00082
00083 try
00084 {
00085 os32c.open(host);
00086 }
00087 catch (std::runtime_error ex)
00088 {
00089 ROS_FATAL_STREAM("Exception caught opening session: " << ex.what());
00090 return -1;
00091 }
00092
00093 try
00094 {
00095 os32c.setRangeFormat(RANGE_MEASURE_50M);
00096 os32c.setReflectivityFormat(REFLECTIVITY_MEASURE_TOT_4PS);
00097 os32c.selectBeams(start_angle, end_angle);
00098 }
00099 catch (std::invalid_argument ex)
00100 {
00101 ROS_FATAL_STREAM("Invalid arguments in sensor configuration: " << ex.what());
00102 return -1;
00103 }
00104
00105 try
00106 {
00107 os32c.startUDPIO();
00108 os32c.sendMeasurmentReportConfigUDP();
00109 }
00110 catch (std::logic_error ex)
00111 {
00112 ROS_FATAL_STREAM("Could not start UDP IO: " << ex.what());
00113 return -1;
00114 }
00115
00116 int ctr = 10;
00117 sensor_msgs::LaserScan laserscan_msg;
00118 os32c.fillLaserScanStaticConfig(&laserscan_msg);
00119 laserscan_msg.header.frame_id = frame_id;
00120
00121 while (ros::ok())
00122 {
00123 try
00124 {
00125
00126 MeasurementReport report = os32c.receiveMeasurementReportUDP();
00127 OS32C::convertToLaserScan(report, &laserscan_msg);
00128
00129
00130 laserscan_msg.header.stamp = ros::Time::now();
00131 laserscan_msg.header.seq++;
00132 diagnosed_publisher.publish(laserscan_msg);
00133
00134
00135 updater.update();
00136
00137
00138
00139 if (++ctr > 10)
00140 {
00141 os32c.sendMeasurmentReportConfigUDP();
00142 ctr = 0;
00143 }
00144 }
00145 catch (std::runtime_error ex)
00146 {
00147 ROS_ERROR_STREAM("Exception caught requesting scan data: " << ex.what());
00148 }
00149 catch (std::logic_error ex)
00150 {
00151 ROS_ERROR_STREAM("Problem parsing return data: " << ex.what());
00152 }
00153
00154 ros::spinOnce();
00155 }
00156
00157 os32c.closeConnection(0);
00158 os32c.close();
00159 return 0;
00160 }