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 <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
00103
00104
00105
00106
00107
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
00163
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