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 #include "netft_example_controllers/netft_example_controller.h"
00036 #include "pluginlib/class_list_macros.h"
00037 #include <math.h>
00038 #include <boost/foreach.hpp>
00039 #include <string>
00040 #include <algorithm>
00041
00042 PLUGINLIB_DECLARE_CLASS(netft_example_controllers, NetFTExampleController, netft_example_controllers::NetFTExampleController, pr2_controller_interface::Controller)
00043
00044
00045 namespace netft_example_controllers
00046 {
00047
00048 NetFTExampleController::NetFTExampleController() :
00049 max_force_(0.0),
00050 max_torque_(0.0),
00051 analog_in_(NULL),
00052 pub_cycle_count_(0),
00053 should_publish_(false)
00054 {
00055
00056 }
00057
00058
00059 NetFTExampleController::~NetFTExampleController()
00060 {
00061
00062 }
00063
00064
00065 bool NetFTExampleController::init(pr2_mechanism_model::RobotState *robot, ros::NodeHandle &node)
00066 {
00067 if (!robot)
00068 return false;
00069
00070 std::string analog_in_name;
00071 if (!node.getParam("analog_in_name", analog_in_name))
00072 {
00073 ROS_ERROR("NetFTExampleController: No \"analog_in_name\" found on parameter namespace: %s",
00074 node.getNamespace().c_str());
00075 return false;
00076 }
00077
00078 pr2_hardware_interface::HardwareInterface* hw = robot->model_->hw_;
00079 analog_in_ = hw->getAnalogIn(analog_in_name);
00080 if (analog_in_ == NULL)
00081 {
00082 ROS_ERROR("NetFTExampleController: Cannot find AnalogIn named \"%s\"",
00083 analog_in_name.c_str());
00084 BOOST_FOREACH(const pr2_hardware_interface::AnalogInMap::value_type &v, hw->analog_ins_)
00085 {
00086 ROS_INFO("AnalogIn : %s", v.first.c_str());
00087 }
00088 return false;
00089 }
00090 ROS_INFO("NetFTExampleController: Using AnalogIn named \"%s\"", analog_in_name.c_str());
00091
00092
00093 pub_.init(node, "force_torque_stats", 2);
00094
00095 return true;
00096 }
00097
00098
00099 void NetFTExampleController::starting()
00100 {
00101
00102 }
00103
00104
00105 void NetFTExampleController::update()
00106 {
00107 if (analog_in_->state_.state_.size() != 6)
00108 {
00109 ROS_ERROR_THROTTLE(5.0, "NetFTExampleController: AnalogInput is has unexpected size %d",
00110 int(analog_in_->state_.state_.size()));
00111 return;
00112 }
00113
00114 double fx = analog_in_->state_.state_[0];
00115 double fy = analog_in_->state_.state_[1];
00116 double fz = analog_in_->state_.state_[2];
00117 double tx = analog_in_->state_.state_[3];
00118 double ty = analog_in_->state_.state_[4];
00119 double tz = analog_in_->state_.state_[5];
00120
00121 double abs_force = sqrt( fx*fx + fy*fy + fz*fz );
00122 double abs_torque = sqrt( tx*tx + ty*ty + tz*tz );
00123 max_force_ = std::max(max_force_, abs_force);
00124 max_torque_ = std::max(max_torque_, abs_torque);
00125
00126
00127 if (++pub_cycle_count_ > 10)
00128 {
00129 should_publish_ = true;
00130 pub_cycle_count_ = 0;
00131 }
00132
00133 if (should_publish_ && pub_.trylock())
00134 {
00135 should_publish_ = false;
00136 pub_.msg_.abs_force = abs_force;
00137 pub_.msg_.max_force = max_force_;
00138 pub_.msg_.abs_torque = abs_torque;
00139 pub_.msg_.max_torque = max_torque_;
00140 pub_.unlockAndPublish();
00141 }
00142 }
00143
00144 }