coop_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, LABUST, UNIZG-FER
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the LABUST nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  *  Author: Dula Nad
00035  *  Created: 27.05.2013.
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         //buffer[1] = htonl(int32_t((data->origin.latitude-latdeg)*10000));
00065         buffer[1] = htonl(londeg);
00066         //buffer[3] = htonl(int32_t((data->origin.longitude-londeg)*10000));
00067 
00068         uint8_t* p=reinterpret_cast<uint8_t*>(buffer.data());
00069         uint32_t len =  buffer.size()*sizeof(int32_t);
00070         //      uint8_t out[len];
00071         //      for(int32_t i=0; i<buffer.size();++i)
00072         //      {
00073         //              out[i] = p[len-i-1];
00074         //      }
00075         shared.socket.send_to(boost::asio::buffer(p, len), shared.remote);
00076         //std::string test = "Test\n";
00077         //shared.socket.send_to(boost::asio::buffer(test), shared.remote);
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         //Get thruster configuration
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         //Create a name resolver
00128         udp::resolver resolver(shared.io);
00129         //Resolve query and get the iterator
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         //t.join();
00145         return 0;
00146 }
00147 
00148 
00149 
00150 


cart2
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:37:19