usbl_sim.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: 14.02.2013.
00036  *********************************************************************/
00037 #include <labust/tritech/MTDevice.hpp>
00038 #include <labust/tritech/mtMessages.hpp>
00039 #include <labust/tritech/mmcMessages.hpp>
00040 
00041 #include <geometry_msgs/PointStamped.h>
00042 #include <auv_msgs/NavSts.h>
00043 #include <std_msgs/String.h>
00044 #include <std_msgs/Bool.h>
00045 #include <ros/ros.h>
00046 
00047 #include <boost/shared_ptr.hpp>
00048 #include <boost/bind.hpp>
00049 #include <boost/archive/binary_iarchive.hpp>
00050 #include <boost/archive/binary_oarchive.hpp>
00051 
00052 #include <queue>
00053 
00055 class USBLSim
00056 {
00057 public:
00058         USBLSim():
00059                 usblBusy(false),
00060                 useDevice(false),
00061                 sent(false),
00062                 navTime(4)
00063         {
00064                 ros::NodeHandle nh,ph("~");
00065                 std::string port("/dev/rfcomm0");
00066                 int baud(57600);
00067 
00068                 ph.param("port",port,port);
00069                 ph.param("baud",baud,baud);
00070                 ph.param("use_device",useDevice,useDevice);
00071                 ph.param("nav_time",navTime,navTime);
00072 
00073                 if (useDevice)
00074                 {
00075                         using namespace labust::tritech;
00076                         usbl.reset(new MTDevice(port,baud));
00077                         MTDevice::HandlerMap map;
00078                         map[MTMsg::mtMiniModemCmd] = boost::bind(&USBLSim::onReplyMsg,this, _1);
00079                         usbl->registerHandlers(map);
00080                 }
00081 
00082                 dataSub = nh.subscribe<std_msgs::String>("outgoing_data",       1, boost::bind(&USBLSim::onOutgoingMsg,this,_1));
00083                 dataPub = nh.advertise<std_msgs::String>("incoming_data",1);
00084                 usblTimeout = nh.advertise<std_msgs::Bool>("usbl_timeout",1);
00085                 usblNav = nh.advertise<geometry_msgs::PointStamped>("usbl_nav", 1);
00086                 simDiverState = nh.subscribe<auv_msgs::NavSts>("diver_sim",1, boost::bind(&USBLSim::onDiverSim, this, _1));
00087                 simVehicleState = nh.subscribe<auv_msgs::NavSts>("platform_sim",1, boost::bind(&USBLSim::onPlatformSim, this, _1));
00088         }
00089 
00090         void onReplyMsg(labust::tritech::MTMsgPtr tmsg)
00091         {
00092                 using namespace labust::tritech;
00093                 {
00094                         boost::mutex::scoped_lock lock(pingLock);
00095                         usblBusy = false;
00096                 }
00097                 usblCondition.notify_one();
00098 
00099                 boost::archive::binary_iarchive dataSer(*tmsg->data, boost::archive::no_header);
00100                 MMCMsg modem_data;
00101                 dataSer>>modem_data;
00102                 //std::cout<<"Modem data:";
00103                 //for(int i=0; i<modem_data.data.size(); ++i) std::cout<<int(modem_data.data[i])<<",";
00104                 //std::cout<<std::endl;
00105                 //std_msgs::String::Ptr modem(new std_msgs::String());
00106                 //modem->data.assign(modem_data.data.begin(), modem_data.data.end());
00107                 //Buffer one message like the acoustic modem does
00108                 std_msgs::String reply;
00109                 reply.data.assign(modem_data.data.begin(), modem_data.data.end());
00110                 reply_queue.push(reply);
00111                 if (reply_queue.size() > 1) reply_queue.pop();
00112                 ROS_INFO("Received data message.");
00113         }
00114 
00115         void onDiverSim(const auv_msgs::NavSts::ConstPtr msg)
00116         {
00117                 boost::mutex::scoped_lock l(vehicleStateMux);
00118                 vehicleState = *msg;
00119         }
00120 
00121         void onPlatformSim(const auv_msgs::NavSts::ConstPtr msg)
00122         {
00123                 if ((ros::Time::now() - lastNav).toSec() > navTime)
00124                 {
00125                         boost::mutex::scoped_lock l(vehicleStateMux);
00126                         geometry_msgs::PointStamped::Ptr point(new geometry_msgs::PointStamped());
00127                         point->header.stamp = ros::Time::now();
00128                         point->point.x = msg->position.north - vehicleState.position.north;
00129                         point->point.y = msg->position.east - vehicleState.position.east;
00130                         point->point.z = msg->position.depth - vehicleState.position.depth;
00131                         usblNav.publish(point);
00132                 }
00133         }
00134 
00135         void onOutgoingMsg(const std_msgs::String::ConstPtr msg)
00136         {
00137                 using namespace labust::tritech;
00138                 MTMsgPtr tmsg(new MTMsg());
00139                 tmsg->txNode = 85;
00140                 tmsg->rxNode = labust::tritech::Nodes::Surface;
00141                 tmsg->node = 85;
00142                 tmsg->msgType = MTMsg::mtMiniModemData;
00143 
00144                 MMCMsg mmsg;
00145                 mmsg.msgType = labust::tritech::mmcGetRangeTxRxBits48;
00146                 for (int i=0;i<msg->data.size();++i) mmsg.data[i] = msg->data[i];
00147 
00148                 boost::archive::binary_oarchive ar(*tmsg->data, boost::archive::no_header);
00149                 ar<<mmsg;
00150                 usblBusy = true;
00151 
00152                 if (useDevice)
00153                 {
00154                         usbl->send(tmsg);
00155                 }
00156                 else
00157                 {
00158                         onReplyMsg(tmsg);
00159                 }
00160                 ROS_INFO("Sent data message.");
00161 
00162                 //boost::mutex::scoped_lock lock(pingLock);
00163                 //while (usblBusy) usblCondition.wait(lock);
00164                 if (!reply_queue.empty())
00165                 {
00166                         dataPub.publish(reply_queue.front());
00167                         reply_queue.pop();
00168                         sent = true;
00169                 }
00170         }
00171 
00172         ros::Publisher dataPub, usblTimeout, usblNav;
00173         ros::Subscriber dataSub, simDiverState, simVehicleState;
00174         auv_msgs::NavSts vehicleState;
00175         boost::shared_ptr<labust::tritech::MTDevice> usbl;
00176         boost::mutex pingLock, vehicleStateMux;
00177         boost::condition_variable usblCondition;
00178         bool usblBusy, useDevice;
00179         std::queue<std_msgs::String> reply_queue;
00180         bool sent;
00181         double navTime;
00182         ros::Time lastNav;
00183 };
00184 
00185 int main(int argc, char* argv[])
00186 {
00187         ros::init(argc,argv,"usbl_sim");
00188         USBLSim usbl;
00189         ros::spin();
00190 
00191         return 0;
00192 }
00193 
00194 
00195 


usbl
Author(s): Gyula Nagy
autogenerated on Thu Jul 10 2014 10:34:21