BVSonarRos.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 #include <labust/blueview/BVSonarRos.hpp>
00035 #include <aidnav_msgs/MBSonar.h>
00036 #include <sensor_msgs/image_encodings.h>
00037 #include <pluginlib/class_list_macros.h>
00038 
00039 using namespace labust::blueview;
00040 
00041 PLUGINLIB_DECLARE_CLASS(bvt_sdk,BVSonarRos,labust::blueview::BVSonarRos, nodelet::Nodelet)
00042 
00043 BVSonarRos::BVSonarRos():
00044                 sonar(BVFactory::makeBVSonar()),
00045                 pingRate(100)
00046 {}
00047 
00048 BVSonarRos::~BVSonarRos()
00049 {
00050         worker.join();
00051 };
00052 
00053 void BVSonarRos::onInit()
00054 {
00055         phandle = this->getPrivateNodeHandle();
00056         nhandle = this->getNodeHandle();
00057 
00058         std::string sonarType("FILE");
00059         std::string sonarAddress("swimmer.son");
00060         std::string magtopicName("bvsonar_img");
00061         std::string colortopicName("bvsonar_cimg");
00062         std::string colormap("jet");
00063         std::string colormapDir(".");
00064         int headNumber(0), soundSpeed(1475);
00065 
00066         phandle.param("Type",sonarType,sonarType);
00067         phandle.param("Address",sonarAddress,sonarAddress);
00068         phandle.param("HeadNumber",headNumber,headNumber);
00069         phandle.param("MagnitudeTopicName",magtopicName,magtopicName);
00070         phandle.param("ColorTopicName",colortopicName,colortopicName);
00071         phandle.param("Colormap",colormap,colormap);
00072         phandle.param("ColormapDir",colormapDir,colormapDir);
00073         phandle.param("PingRate",pingRate,pingRate);
00074         phandle.param("SoundSpeed", soundSpeed, soundSpeed);
00075 
00076         //Sonar configuration
00077         sonar = BVFactory::makeBVSonar();
00078         mapper = BVFactory::makeBVColorMapper(colormapDir + "/" + colormap + ".cmap");
00079 
00080         if (int error = BVTSonar_Open(sonar.get(),sonarType.c_str(),sonarAddress.c_str()))
00081         {
00082                 throw std::invalid_argument(BVTError_GetString(error));
00083         }
00084         if (int error = BVTSonar_GetHead(sonar.get(),headNumber,&head))
00085         {
00086                 throw std::invalid_argument(BVTError_GetString(error));
00087         }
00088         //Sonar calibration
00089         BVTHead_SetSoundSpeed(head,soundSpeed);
00090 
00091         //Topics configuration
00092         imageTopic = nhandle.advertise<aidnav_msgs::MBSonar>(magtopicName,1);
00093         cImageTopic = nhandle.advertise<aidnav_msgs::MBSonar>(colortopicName,1);
00094 
00095         //Configure the dynamic reconfigure server
00096   server.setCallback(boost::bind(&BVSonarRos::dynrec, this, _1, _2));
00097 
00098   worker=boost::thread(boost::bind(&BVSonarRos::run,this));
00099 }
00100 
00101 void BVSonarRos::dynrec(bvt_sdk::BVSonarConfig& config, uint32_t level)
00102 {
00103         this->config = config;
00104 }
00105 
00106 void BVSonarRos::run()
00107 {
00108         this->runFileAcquisition();
00109 }
00110 
00111 void BVSonarRos::runFileAcquisition()
00112 {
00113         size_t pingNum = BVTHead_GetPingCount(head);
00114         size_t counter = 0;
00115         ros::Rate loop_rate(pingRate);
00116         BVTHead_SetImageRes(head,BVTHEAD_RES_HIGH);
00117         BVTHead_SetImageType(head,BVTHEAD_IMAGE_RTHETA);
00118         this->config.double_param = BVTHead_GetMaximumRange(head);
00119         server.setConfigMax(config);
00120         this->config.double_param = BVTHead_GetStopRange(head);
00121   server.setConfigDefault(this->config);
00122         while (nhandle.ok())
00123         {
00124                 BVTHead_SetRange(head,0,this->config.double_param);
00125                 BVPingPtr ping(BVFactory::getBVPing(head,counter));
00126 
00127                 BVMagImagePtr image(BVFactory::getBVMagImage(ping));
00128                 BVColorImagePtr cimage(BVFactory::getBVColorImage(image,mapper));
00129 
00130                 //Add navigation data reading.
00131                 aidnav_msgs::MBSonarPtr msg(new aidnav_msgs::MBSonar());
00132                 msg->image.height = BVTMagImage_GetHeight(image.get());
00133                 msg->image.width = BVTMagImage_GetWidth(image.get());
00134                 msg->image.encoding = sensor_msgs::image_encodings::MONO16;
00135                 msg->image.step = 2*msg->image.width;
00136                 msg->image.is_bigendian = 0;
00137                 msg->range_res = BVTMagImage_GetRangeResolution(image.get());
00138                 msg->origin.z = 0;
00139                 msg->origin.x = BVTMagImage_GetWidth(image.get())/2;
00140                 msg->origin.y = BVTMagImage_GetOriginRow(image.get());
00141 
00142                 unsigned char* datap = reinterpret_cast<unsigned char*>(BVTMagImage_GetBits(image.get()));
00143                 msg->image.data.assign(datap,datap + msg->image.step*msg->image.height);
00144                 imageTopic.publish(msg);
00145 
00146                 msg.reset(new aidnav_msgs::MBSonar());
00147                 msg->image.height = BVTColorImage_GetHeight(cimage.get());
00148                 msg->image.width = BVTColorImage_GetWidth(cimage.get());
00149                 msg->image.encoding = sensor_msgs::image_encodings::BGRA8;
00150                 msg->image.step = 4*msg->image.width;
00151                 datap = reinterpret_cast<unsigned char*>(BVTColorImage_GetBits(cimage.get()));
00152                 msg->image.data.assign(datap,datap + msg->image.step*msg->image.height);
00153                 cImageTopic.publish(msg);
00154 
00155                 ++counter;
00156                 counter %= pingNum;
00157 
00158                 ros::spinOnce();
00159                 loop_rate.sleep();
00160         }
00161 }
00162 
00163 
00164 


bvt_sdk
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:37:04