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
00034
00035
00036
00037 #include <boost/asio.hpp>
00038 #include <labust/tools/GeoUtilities.hpp>
00039 #include <ros/ros.h>
00040 #include <auv_msgs/NavSts.h>
00041 #include <geometry_msgs/PointStamped.h>
00042 #include <boost/thread.hpp>
00043
00044 #include <string>
00045
00046 struct SharedData
00047 {
00048 SharedData():socket(io){};
00049 boost::asio::io_service io;
00050 boost::asio::ip::udp::socket socket;
00051 boost::asio::ip::udp::endpoint remote;
00052 ros::Publisher trackPoint;
00053 uint8_t buffer[10000];
00054 };
00055
00056 void onData(SharedData& shared, const auv_msgs::NavSts::ConstPtr data)
00057 {
00058 std::pair<double, double> location = labust::tools::meter2deg(data->position.north, data->position.east,
00059 data->origin.latitude);
00060 std::vector<int32_t> buffer(2);
00061 int32_t latdeg = int32_t((data->origin.latitude + location.first)*1000000);
00062 int32_t londeg = int32_t((data->origin.longitude + location.second)*1000000);
00063 buffer[0] = htonl(latdeg);
00064
00065 buffer[1] = htonl(londeg);
00066
00067
00068 uint8_t* p=reinterpret_cast<uint8_t*>(buffer.data());
00069 uint32_t len = buffer.size()*sizeof(int32_t);
00070
00071
00072
00073
00074
00075 shared.socket.send_to(boost::asio::buffer(p, len), shared.remote);
00076
00077
00078 std::cout<<"Send data lat:"<<latdeg<<std::endl;
00079 std::cout<<"Send data lon:"<<londeg<<std::endl;
00080 }
00081
00082 void handle_receive(SharedData& shared, const boost::system::error_code& e, size_t length);
00083 void start_receive(SharedData& shared)
00084 {
00085 shared.socket.async_receive(boost::asio::buffer(shared.buffer, sizeof(shared.buffer)),
00086 boost::bind(&handle_receive,boost::ref(shared),_1,_2));
00087 }
00088
00089 void handle_receive(SharedData& shared, const boost::system::error_code& e, size_t length)
00090 {
00091 if (!e)
00092 {
00093 int32_t* pos = reinterpret_cast<int32_t*>(&shared.buffer[0]);
00094
00095 geometry_msgs::PointStamped point;
00096 point.header.frame_id = "worldLatLon";
00097 point.point.x = htonl(pos[0])/10000.;
00098 point.point.y = htonl(pos[1])/10000.;
00099 point.point.z = 0;
00100
00101 shared.trackPoint.publish(point);
00102 }
00103 else
00104 {
00105 ROS_ERROR("Error in communication.");
00106 }
00107
00108 start_receive(shared);
00109 }
00110
00111 int main(int argc, char* argv[])
00112 {
00113 ros::init(argc,argv,"coop_node");
00114 ros::NodeHandle nh,ph("~");
00115
00116 SharedData shared;
00117 ros::Subscriber measIn = nh.subscribe<auv_msgs::NavSts>("meas",1,boost::bind(&onData,boost::ref(shared),_1));
00118 shared.trackPoint = nh.advertise<geometry_msgs::PointStamped>("extPoint",1);
00119
00120 using namespace boost::asio::ip;
00121
00122 std::string ip("127.0.0.1"),port("8692"), port_local("33346");
00123 ph.param("ip", ip,ip);
00124 ph.param("port", port,port);
00125 ph.param("port_local", port_local,port_local);
00126 shared.socket.open(udp::v4());
00127
00128 udp::resolver resolver(shared.io);
00129
00130 shared.remote = *resolver.resolve(udp::resolver::query(ip,port));
00131 udp::resolver::iterator it = resolver.resolve(udp::resolver::query(
00132 "150.145.4.103",port_local));
00133 boost::system::error_code error = boost::asio::error::host_not_found;
00134 while (error && (it != udp::resolver::iterator()))
00135 {
00136 shared.socket.bind(*it++,error);
00137 }
00138
00139 start_receive(shared);
00140 boost::thread t(boost::bind(&boost::asio::io_service::run, &shared.io));
00141
00142 ros::spin();
00143 shared.io.stop();
00144
00145 return 0;
00146 }
00147
00148
00149
00150