AsyncMerger.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  *  Created on: 06.05.2013.
00035  *  Author: Dula Nad
00036  *********************************************************************/
00037 #include <labust/control/AsyncMerger.hpp>
00038 #include <std_msgs/Float32.h>
00039 
00040 #include <algorithm>
00041 
00042 using namespace labust::control;
00043 
00044 
00045 AsyncMergerBase::AsyncMergerBase():
00046                 Ts(0.3)
00047 {
00048         this->reset_axes();
00049         ros::NodeHandle nh,ph("~");
00050         ph.param("sampling_time",Ts,Ts);
00051         timer = nh.createTimer(ros::Duration(Ts), &AsyncMergerBase::checkArrived, this);
00052         loop_time = nh.advertise<std_msgs::Float32>("loop_time",1);
00053 }
00054 
00055 void AsyncMergerBase::reset_axes()
00056 {
00057         for(int i=0; i<6; ++i)
00058         {
00059                 outaxis[i] = true;
00060                 outval[i] = 0;
00061         }
00062 }
00063 
00064 void AsyncMergerBase::copyToVector(const auv_msgs::BodyForceReq::ConstPtr& topic, bool* axis, double* val)
00065 {
00066         labust::tools::disableAxisToVector(topic->disable_axis, axis);
00067         labust::tools::pointToVector(topic->wrench.force,val);
00068         labust::tools::pointToVector(topic->wrench.torque,val,3);
00069 }
00070 
00071 void AsyncMergerBase::copyFromVector(const auv_msgs::BodyForceReq::Ptr& topic, bool* axis, double* val)
00072 {
00073         labust::tools::vectorToDisableAxis(axis,topic->disable_axis);
00074         labust::tools::vectorToPoint(val,topic->wrench.force);
00075         labust::tools::vectorToPoint(val,topic->wrench.torque,3);
00076 }
00077 
00078 void AsyncMergerBase::copyToVector(const auv_msgs::BodyVelocityReq::ConstPtr& topic, bool* axis, double* val)
00079 {
00080         labust::tools::disableAxisToVector(topic->disable_axis, axis);
00081         labust::tools::pointToVector(topic->twist.linear,val);
00082         labust::tools::pointToVector(topic->twist.angular,val,3);
00083 }
00084 
00085 void AsyncMergerBase::copyFromVector(const auv_msgs::BodyVelocityReq::Ptr& topic, bool* axis, double* val)
00086 {
00087         labust::tools::vectorToDisableAxis(axis, topic->disable_axis);
00088         labust::tools::vectorToPoint(val,topic->twist.linear);
00089         labust::tools::vectorToPoint(val,topic->twist.angular,3);
00090 }
00091 
00092 bool AsyncMergerBase::allArrived()
00093 {
00094         bool all(true);
00095         for(ControllerMap::const_iterator it=controllers.begin();
00096                         it!=controllers.end(); ++it)
00097         {
00098                 ROS_INFO("Status: %s : %d",it->first.c_str(), it->second);
00099                 all = all & it->second;
00100         }
00101         return all;
00102 }
00103 
00104 void AsyncMergerBase::clearAll()
00105 {
00106         std::vector <std::string> toremove;
00107 
00108         //Reset arrived flag
00109         for(ControllerMap::iterator it=controllers.begin();
00110                         it!=controllers.end(); ++it)
00111         {
00112                 if (!it->second) toremove.push_back(it->first);
00113                 it->second = false;
00114         }
00115         //Reset axes
00116         this->reset_axes();
00117 
00118         for (int i=0; i<toremove.size(); ++i)
00119         {
00120                 ROS_WARN("AsyncMerge: removing unresponsive controller %s", toremove[i].c_str());
00121                 controllers.erase(toremove[i]);
00122         }
00123 }
00124 
00128 void AsyncMergerBase::handleRequester(const std::string& requester)
00129 {
00130         controllers[requester] = true;
00131         if (allArrived())
00132         {
00133                 ROS_INFO("All controller inputs arrived in time.");
00134                 this->publishFinal();
00135                 lastAll = ros::Time::now();
00136                 clearAll();
00137         }
00138 }
00139 
00140 void AsyncMergerBase::checkArrived(const ros::TimerEvent& event)
00141 {
00142         boost::mutex::scoped_lock l(cnt_mux);
00143         double dT = (ros::Time::now() - lastAll).toSec();
00144         if ((dT > Ts) && controllers.size())
00145         {
00146                 ROS_WARN("Not all controller inputs arrived in time, dT=%f",dT);
00147                 this->publishFinal();
00148                 this->clearAll();
00149         }
00150 }
00151 
00152 
00153 int main(int argc, char* argv[])
00154 {
00155         ros::init(argc,argv,"merger");
00156 
00157         ros::NodeHandle ph("~");
00158         bool merge_tau(false);
00159         bool merge_nu(false);
00160         ph.param("merge_tau", merge_tau, false);
00161         ph.param("merge_nu", merge_nu, false);
00162 
00163         AsyncMergerBase::Ptr m;
00164         if (merge_tau)  m.reset(new AsyncMerger<auv_msgs::BodyForceReq>());
00165         if (merge_nu) m.reset(new AsyncMerger<auv_msgs::BodyVelocityReq>());
00166 
00167         ros::spin();
00168 
00169         return 0;
00170 }


labust_control
Author(s): Gyula Nagy
autogenerated on Fri Aug 28 2015 11:22:42