00001 #include "r2_gazebo_interface/RobotController.h"
00002
00003 using namespace gazebo;
00004
00005 RobotController::RobotController(physics::ModelPtr _modelPtr)
00006 : modelPtr(_modelPtr)
00007 {
00008 prevUpdateTime = _modelPtr->GetWorld()->GetSimTime();
00009 }
00010
00011 RobotController::~RobotController()
00012 {
00013 }
00014
00015 void RobotController::addJoint(physics::JointPtr _jointPtr, bool advancedMode)
00016 {
00017 boost::mutex::scoped_lock(pidMutex);
00018 JointControllerPtr jointPtr(new JointController(_jointPtr, advancedMode));
00019 joints[_jointPtr->GetName()] = jointPtr;
00020 }
00021
00022 void RobotController::addJointDependency(std::string _childName, std::string _parentName, double factor)
00023 {
00024 if (joints.find(_parentName) == joints.end())
00025 {
00026 ROS_WARN("GazeboInterface joint dependency not set because parent (%s) not found", _parentName.c_str());
00027 return;
00028 }
00029
00030 if (joints.find(_childName) == joints.end())
00031 {
00032 ROS_WARN("GazeboInterface joint dependency not set because child (%s) not found", _childName.c_str());
00033 return;
00034 }
00035
00036 boost::mutex::scoped_lock(pidMutex);
00037 dependencies.insert(std::make_pair(_parentName, std::make_pair(_childName, factor)));
00038
00039 if (_parentName != _childName)
00040 {
00041 joints[_childName]->setPassive();
00042 }
00043 }
00044
00045 void RobotController::setPosPid(const std::string& name, double _p, double _i, double _d,
00046 double _imax, double _imin, double _cmdMax, double _cmdMin)
00047 {
00048 if (joints.find(name) == joints.end())
00049 {
00050 ROS_DEBUG("GazeboInterface PosPID not set because joint (%s) not found", name.c_str());
00051 return;
00052 }
00053
00054 boost::mutex::scoped_lock(pidMutex);
00055 joints[name]->setPosPid(_p, _i, _d, _imax, _imin, _cmdMax, _cmdMin);
00056 }
00057
00058 void RobotController::setVelPid(const std::string& name, double _p, double _i, double _d,
00059 double _imax, double _imin, double _cmdMax, double _cmdMin)
00060 {
00061 if (joints.find(name) == joints.end())
00062 {
00063 ROS_WARN("GazeboInterface PosPID not set because joint (%s) not found", name.c_str());
00064 return;
00065 }
00066
00067 boost::mutex::scoped_lock(pidMutex);
00068 joints[name]->setVelPid(_p, _i, _d, _imax, _imin, _cmdMax, _cmdMin);
00069 }
00070
00071
00072 void RobotController::setJointPositions(std::map<std::string, double> posMap)
00073 {
00074
00075 bool is_paused = modelPtr->GetWorld()->IsPaused();
00076
00077 if (!is_paused)
00078 {
00079 modelPtr->GetWorld()->SetPaused(true);
00080 }
00081
00082
00083 std::map<std::string, bool> brakeStates;
00084
00085 for (std::map<std::string, JointControllerPtr>::iterator it = joints.begin(); it != joints.end(); ++it)
00086 {
00087 brakeStates[it->first] = it->second->getJointStatus().brakeReleased;
00088 it->second->releaseBrake();
00089 }
00090
00091
00092 for (std::map<std::string, double>::iterator posIt = posMap.begin(); posIt != posMap.end(); ++posIt)
00093 {
00094 std::pair<dependenciesType::iterator, dependenciesType::iterator> deps = dependencies.equal_range(posIt->first);
00095
00096 for (dependenciesType::iterator depIt = deps.first; depIt != deps.second; ++depIt)
00097 {
00098 posMap[depIt->second.first] = depIt->second.second * posIt->second;
00099 }
00100 }
00101
00102
00103 modelPtr->SetJointPositions(posMap);
00104
00105
00106 for (std::map<std::string, bool>::iterator it = brakeStates.begin(); it != brakeStates.end(); ++it)
00107 {
00108 joints[it->first]->releaseBrake(it->second);
00109 }
00110
00111
00112 modelPtr->GetWorld()->SetPaused(is_paused);
00113 }
00114
00115 void RobotController::getJointStates(std::map<std::string, JointState>& posMap)
00116 {
00117 if (posMap.empty())
00118 {
00119 JointState zero;
00120 zero.position = 0.;
00121 zero.velocity = 0.;
00122 zero.effort = 0.;
00123
00124
00125 for (std::map<std::string, JointControllerPtr>::const_iterator it = joints.begin(); it != joints.end(); ++it)
00126 {
00127 posMap[it->first] = zero;
00128 }
00129
00130 if (!posMap.empty())
00131 {
00132 getJointStates(posMap);
00133 }
00134 }
00135
00136 physics::Joint_V model_joints = modelPtr->GetJoints();
00137
00138
00139 for (physics::Joint_V::iterator j_it = model_joints.begin(); j_it != model_joints.end(); ++j_it)
00140 {
00141
00142 std::string name = (*j_it)->GetName();
00143 std::map<std::string, JointState>::iterator it = posMap.find(name);
00144
00145 if (it != posMap.end())
00146 {
00147 it->second.position = (*j_it)->GetAngle(0).Radian();
00148 it->second.velocity = (*j_it)->GetVelocity(0);
00149 it->second.effort = (*j_it)->GetForce((unsigned int)0);
00150 }
00151 }
00152 }
00153
00154 void RobotController::getJointTargets(std::map<std::string, JointState>& posMap)
00155 {
00156 if (posMap.empty())
00157 {
00158 JointState zero;
00159 zero.position = 0.;
00160 zero.velocity = 0.;
00161 zero.effort = 0.;
00162
00163
00164 for (std::map<std::string, JointControllerPtr>::const_iterator it = joints.begin(); it != joints.end(); ++it)
00165 {
00166 if (!it->second->isPassive())
00167 {
00168 posMap[it->first] = zero;
00169 }
00170 }
00171
00172 if (!posMap.empty())
00173 {
00174 getJointTargets(posMap);
00175 }
00176 }
00177
00178 for (std::map<std::string, JointControllerPtr>::const_iterator jIt = joints.begin(); jIt != joints.end(); ++jIt)
00179 {
00180 std::map<std::string, JointState>::iterator it = posMap.find(jIt->first);
00181
00182 if (it != posMap.end())
00183 {
00184 it->second.position = jIt->second->getPosTarget();
00185 it->second.velocity = jIt->second->getVelTarget();
00186 it->second.effort = jIt->second->getEffortTarget();
00187
00188
00189 dependenciesType::iterator depIt = dependencies.find(jIt->first);
00190
00191 if (depIt != dependencies.end())
00192 {
00193 it->second.position = it->second.position / depIt->second.second;
00194 }
00195
00196 }
00197 }
00198 }
00199
00200 void RobotController::getJointLimits(std::map<std::string, std::pair<double, double> >& limitsMap)
00201 {
00202 if (limitsMap.empty())
00203 {
00204
00205 for (std::map<std::string, JointControllerPtr>::const_iterator it = joints.begin(); it != joints.end(); ++it)
00206 {
00207 if (!it->second->isPassive())
00208 {
00209 limitsMap[it->first] = std::pair<double, double>(-3.14159, 3.14159);
00210 }
00211 }
00212
00213 if (!limitsMap.empty())
00214 {
00215 getJointLimits(limitsMap);
00216 }
00217 }
00218
00219 for (std::map<std::string, JointControllerPtr>::const_iterator jIt = joints.begin(); jIt != joints.end(); ++jIt)
00220 {
00221 std::map<std::string, std::pair<double, double> >::iterator it = limitsMap.find(jIt->first);
00222
00223 if (it != limitsMap.end())
00224 {
00225 jIt->second->getJointLimits(it->second.first, it->second.second);
00226 }
00227 }
00228 }
00229
00230 void RobotController::setJointPosTarget(const std::string& name, double target)
00231 {
00232 if (joints.find(name) == joints.end())
00233 {
00234
00235 return;
00236 }
00237
00238 boost::mutex::scoped_lock(pidMutex);
00239
00240 if (!joints[name]->isPassive())
00241 {
00242 joints[name]->setPosTarget(target);
00243 }
00244
00245
00246 std::pair<dependenciesType::iterator, dependenciesType::iterator> deps = dependencies.equal_range(name);
00247
00248 for (dependenciesType::iterator depIt = deps.first; depIt != deps.second; ++depIt)
00249 {
00250 joints[depIt->second.first]->setPosTarget(depIt->second.second * target);
00251 }
00252 }
00253
00254 void RobotController::setJointVelTarget(const std::string& name, double target)
00255 {
00256 if (joints.find(name) == joints.end())
00257 {
00258 ROS_WARN("GazeboInterface setJointVelTarget not set because joint (%s) not found", name.c_str());
00259 return;
00260 }
00261
00262 boost::mutex::scoped_lock(pidMutex);
00263
00264 if (!joints[name]->isPassive())
00265 {
00266 joints[name]->setVelTarget(target);
00267 }
00268
00269
00270 std::pair<dependenciesType::iterator, dependenciesType::iterator> deps = dependencies.equal_range(name);
00271
00272 for (dependenciesType::iterator depIt = deps.first; depIt != deps.second; ++depIt)
00273 {
00274 joints[depIt->second.first]->setVelTarget(depIt->second.second * target);
00275 }
00276 }
00277
00278 void RobotController::setJointEffortTarget(const std::string& name, double target)
00279 {
00280 if (joints.find(name) == joints.end())
00281 {
00282 ROS_WARN("GazeboInterface setJointEffortTarget not set because joint (%s) not found", name.c_str());
00283 return;
00284 }
00285
00286 boost::mutex::scoped_lock(pidMutex);
00287
00288 if (!joints[name]->isPassive())
00289 {
00290 joints[name]->setEffortTarget(target);
00291 }
00292
00293
00294 std::pair<dependenciesType::iterator, dependenciesType::iterator> deps = dependencies.equal_range(name);
00295
00296 for (dependenciesType::iterator depIt = deps.first; depIt != deps.second; ++depIt)
00297 {
00298 joints[depIt->second.first]->setEffortTarget(depIt->second.second * target);
00299 }
00300 }
00301
00302
00303 void RobotController::setJointControl(const r2_msgs::JointControl::ConstPtr& msg)
00304 {
00305 if (joints.find(msg->joint) == joints.end())
00306 {
00307 ROS_WARN("GazeboInterface setJointControl not set because joint (%s) not found", msg->joint.c_str());
00308 return;
00309 }
00310
00311 boost::mutex::scoped_lock(pidMutex);
00312 joints[msg->joint]->setJointControl(msg);
00313
00314
00315 std::pair<dependenciesType::iterator, dependenciesType::iterator> deps = dependencies.equal_range(msg->joint);
00316
00317 for (dependenciesType::iterator depIt = deps.first; depIt != deps.second; ++depIt)
00318 {
00319 r2_msgs::JointControl* jcPtr = new r2_msgs::JointControl(*msg);
00320 jcPtr->joint = depIt->second.first;
00321 r2_msgs::JointControl::ConstPtr depJointControlPtr(jcPtr);
00322 joints[depIt->second.first]->setJointControl(depJointControlPtr);
00323 }
00324 }
00325
00326 const r2_msgs::JointStatus& RobotController::getJointStatus(const std::string& name) const
00327 {
00328 std::map<std::string, JointControllerPtr>::const_iterator cit = joints.find(name);
00329
00330 if (cit == joints.end())
00331 {
00332 ROS_WARN("GazeboInterface getJointStatus failed because joint (%s) not found", name.c_str());
00333 }
00334
00335 return cit->second->getJointStatus();
00336 }
00337
00338 void RobotController::update()
00339 {
00340 common::Time currTime = modelPtr->GetWorld()->GetSimTime();
00341 common::Time stepTime = currTime - prevUpdateTime;
00342 prevUpdateTime = currTime;
00343
00344 for (std::map<std::string, JointControllerPtr>::iterator it = joints.begin(); it != joints.end(); ++it)
00345 {
00346 it->second->update(stepTime);
00347 }
00348 }
00349
00350 void RobotController::publishJointStatuses(ros::Publisher& rosPub) const
00351 {
00352 r2_msgs::JointStatusArray statusArray;
00353 statusArray.header.stamp = ros::Time::now();
00354
00355 for (std::map<std::string, JointControllerPtr>::const_iterator it = joints.begin(); it != joints.end(); ++it)
00356 {
00357 statusArray.status.push_back(it->second->getJointStatus());
00358 }
00359
00360 rosPub.publish(statusArray);
00361 }