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/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
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
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 }