modem_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: 14.02.2013.
00036  *********************************************************************/
00037 #include <labust/tritech/MTDevice.hpp>
00038 #include <labust/tritech/mtMessages.hpp>
00039 #include <labust/tritech/USBLMessages.hpp>
00040 #include <labust/tritech/DiverMsg_adv.hpp>
00041 
00042 #include <std_msgs/String.h>
00043 #include <ros/ros.h>
00044 
00045 #include <boost/bind.hpp>
00046 #include <boost/archive/binary_iarchive.hpp>
00047 #include <boost/archive/binary_oarchive.hpp>
00048 
00049 void onMsg(labust::tritech::MTDevice* modem, const std_msgs::String::ConstPtr msg)
00050 {
00051         using namespace labust::tritech;
00052 
00053         MTMsgPtr tmsg(new MTMsg());
00054         tmsg->txNode = 255;
00055         tmsg->rxNode = labust::tritech::Nodes::SlaveModem;
00056         tmsg->node = labust::tritech::Nodes::SlaveModem;
00057         tmsg->msgType = MTMsg::mtMiniModemCmd;
00058 
00059         MMCMsg mmsg;
00060         mmsg.msgType = labust::tritech::mmcSetRepBits;
00061 
00062         switch (msg->data.size())
00063         {
00064         case 0: mmsg.data[0] = 0; break;
00065         case 1: mmsg.data[0] = 8; break;
00066         case 2: mmsg.data[0] = 16; break;
00067         case 3: mmsg.data[0] = 24; break;
00068         case 4: mmsg.data[0] = 32; break;
00069         case 5: mmsg.data[0] = 40; break;
00070         case 6: mmsg.data[0] = 48; break;
00071         default:break;
00072         }
00073 
00074         for (int i=0;i<6;++i)   mmsg.data[i+1] = msg->data[i];
00075 
00076         std::ostream out();
00077         boost::archive::binary_oarchive ar(*tmsg->data, boost::archive::no_header);
00078         ar<<mmsg;
00079 
00080         modem->send(tmsg);
00081 
00082         std::cout<<"send message."<<std::endl;
00083 };
00084 
00085 void onData(ros::Publisher* modemOut, labust::tritech::MTMsgPtr tmsg)
00086 {
00087         using namespace labust::tritech;
00088         //static DiverMsg msg;
00089 
00090         boost::archive::binary_iarchive dataSer(*tmsg->data, boost::archive::no_header);
00091         MMCMsg data;
00092         dataSer>>data;
00093 
00094         //uint64_t* pt = reinterpret_cast<uint64_t*>(&data.data[1]);
00095         //msg.unpack<DiverMsg::AutoTopside>(*pt);
00096 
00097         ROS_INFO("Received USBL message type %d. Bits=%d",data.msgType);
00098 
00099         std::cout<<"Received datammcRangedRepByte package:";
00100         for (int i=0; i<data.data.size(); ++i)
00101         {
00102                 std::cout<<int(data.data[i])<<",";
00103         }
00104         std::cout<<std::endl;
00105 
00106         std_msgs::String modem;
00107         modem.data.assign(data.data.begin(), data.data.end());
00108         modemOut->publish(modem);
00109 }
00110 
00111 int main(int argc, char* argv[])
00112 {
00113         using namespace labust::tritech;
00114 
00115         ros::init(argc,argv,"modem_node");
00116 
00117         ros::NodeHandle nh,ph("~");
00118 
00119         std::string port("/dev/ttyUSB9");
00120         int baud(57600);
00121 
00122         ph.param("port",port,port);
00123         ph.param("baud",baud,baud);
00124 
00125         MTDevice modem(port,baud);
00126 
00127         ros::Subscriber inMsg = nh.subscribe<std_msgs::String>(
00128                         "modem_send",   1,boost::bind(&onMsg,&modem,_1));
00129         ros::Publisher pub = nh.advertise<std_msgs::String>("modem_rcvd",       1);
00130 
00131         MTDevice::HandlerMap map;
00132         map[MTMsg::mtMiniModemData] = boost::bind(&onData,&pub, _1);
00133         modem.registerHandlers(map);
00134 
00135         ros::spin();
00136 
00137         return 0;
00138 }
00139 
00140 
00141 


usbl
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:36:29