Go to the documentation of this file.00001 #include "robodyn_controllers/ModeArbiter.h"
00002 #include <iostream>
00003
00004 ModeArbiter::ModeArbiter()
00005 {
00006 }
00007
00008 ModeArbiter::~ModeArbiter()
00009 {
00010 }
00011
00012 void ModeArbiter::InitializeMaps(std::vector<std::string> mechanisms, std::vector<std::string> exJoints)
00013 {
00014
00015 std::string user = "user";
00016 std::string mode = "IK";
00017 std::string jntName;
00018
00019
00020 KDL::SegmentMap segments = tree.getSegments();
00021 KDL::SegmentMap::const_iterator it = segments.begin();
00022
00023
00024 while (it != segments.end())
00025 {
00026
00027 if (it->second.segment.getJoint().getType() != KDL::Joint::None)
00028 {
00029 jntName = it->second.segment.getJoint().getName();
00030 supervisorMap[jntName] = user;
00031 incumbentMap[jntName] = user;
00032 modeMap[jntName] = mode;
00033 }
00034
00035
00036 it++;
00037 }
00038
00039
00040 for (unsigned int i = 0; i < mechanisms.size(); i++)
00041 {
00042 supervisorMap[mechanisms[i]] = user;
00043 incumbentMap[mechanisms[i]] = user;
00044 modeMap[mechanisms[i]] = mode;
00045 }
00046
00047
00048 for (unsigned int i = 0; i < exJoints.size(); i++)
00049 {
00050 supervisorMap[exJoints[i]] = user;
00051 incumbentMap[exJoints[i]] = user;
00052 modeMap[exJoints[i]] = mode;
00053 }
00054
00055 grippers = exJoints;
00056
00057 return;
00058
00059 }
00060
00061 void ModeArbiter::ClearMaps()
00062 {
00063 supervisorMap.clear();
00064 incumbentMap.clear();
00065 modeMap.clear();
00066 }
00067
00068 bool ModeArbiter::GetMapElement(const std::string& map, const std::string& element, std::string& value)
00069 {
00070 bool ret = false;
00071 std::map<std::string, std::string>::const_iterator it;
00072
00073 if (map == "supervisor")
00074 {
00075 if ((it = supervisorMap.find(element)) != supervisorMap.end())
00076 {
00077 ret = true;
00078 value = it->second;
00079 }
00080 }
00081 else if (map == "incumbent")
00082 {
00083 if ((it = incumbentMap.find(element)) != incumbentMap.end())
00084 {
00085 ret = true;
00086 value = it->second;
00087 }
00088 }
00089 else
00090 {
00091 if ((it = modeMap.find(element)) != modeMap.end())
00092 {
00093 ret = true;
00094 value = it->second;
00095 }
00096 }
00097
00098 return ret;
00099 }
00100
00101 void ModeArbiter::AssignMaster(const std::string& name)
00102 {
00103 master = name;
00104 }
00105
00106 void ModeArbiter::GetMaster(std::string& name)
00107 {
00108 name = master;
00109 }
00110
00111 bool ModeArbiter::isValidHijack(const std::string& name)
00112 {
00113 std::map<std::string, std::string>::iterator it;
00114
00116 for (it = supervisorMap.begin(); it != supervisorMap.end(); ++it)
00117 {
00118 if (it->second == name)
00119 {
00120 return true;
00121 }
00122 }
00123
00124 return false;
00125 }
00126
00127 int ModeArbiter::AssignArbitrationProperties(const r2_msgs::Modes& modeMsg)
00128 {
00129 int ret = 0;
00130
00131 if (isValidCommand(modeMsg))
00132 {
00133 if (modeMsg.supervisor.size() > 0)
00134 {
00135 ret += AssignSupervisors(modeMsg);
00136 }
00137
00138 if (modeMsg.incumbent.size() > 0)
00139 {
00140 ret += AssignIncumbents(modeMsg);
00141 }
00142
00143 if (modeMsg.mode.size() > 0)
00144 {
00145 ret += AssignModes(modeMsg);
00146 }
00147
00148 if (!modeMsg.master.empty())
00149 {
00150 AssignMaster(modeMsg.master);
00151 }
00152 }
00153
00154 return ret;
00155 }
00156
00157 int ModeArbiter::AssignSupervisors(const r2_msgs::Modes& modeMsg)
00158 {
00159 int ret = 1;
00160 std::map<std::string, std::string>::iterator it;
00161
00162 if (modeMsg.joint_names.size() == 0)
00163 {
00164
00165 for (it = supervisorMap.begin(); it != supervisorMap.end(); it++)
00166 {
00167 it->second = modeMsg.supervisor[0];
00168 }
00169 }
00170 else
00171 {
00172 for (unsigned int i = 0; i < modeMsg.joint_names.size(); i++)
00173 {
00174 if ((it = supervisorMap.find(modeMsg.joint_names[i])) != supervisorMap.end())
00175 {
00176 it->second = modeMsg.supervisor[i];
00177 }
00178 }
00179 }
00180
00181 return ret;
00182 }
00183
00184 int ModeArbiter::AssignIncumbents(const r2_msgs::Modes& modeMsg)
00185 {
00186 int ret = 1;
00187 std::map<std::string, std::string>::iterator it;
00188
00189 if (modeMsg.joint_names.size() == 0)
00190 {
00191
00192 for (it = incumbentMap.begin(); it != incumbentMap.end(); it++ )
00193 {
00194 it->second = modeMsg.incumbent[0];
00195
00196 }
00197 }
00198 else
00199 {
00200 for (unsigned int i = 0; i < modeMsg.joint_names.size(); i++)
00201 {
00202 if ((it = incumbentMap.find(modeMsg.joint_names[i])) != incumbentMap.end())
00203 {
00204 it->second = modeMsg.incumbent[i];
00205 }
00206 }
00207 }
00208
00209 return ret;
00210 }
00211
00212 int ModeArbiter::AssignModes(const r2_msgs::Modes& modeMsg)
00213 {
00214 int ret = 1;
00215 std::map<std::string, std::string>::iterator it;
00216
00217 if (modeMsg.joint_names.size() == 0)
00218 {
00219
00220 for (it = modeMap.begin(); it != modeMap.end(); it++)
00221 {
00222 it->second = modeMsg.mode[0];
00223 }
00224 }
00225 else
00226 {
00227 for (unsigned int i = 0; i < modeMsg.joint_names.size(); i++)
00228 {
00229 if ((it = modeMap.find(modeMsg.joint_names[i])) != modeMap.end())
00230 {
00231 it->second = modeMsg.mode[i];
00232 }
00233 }
00234 }
00235
00236 return ret;
00237 }
00238
00239 bool ModeArbiter::isValidCommand(const std::string& originator, const std::vector<std::string>& jointNames)
00240 {
00241 bool ret = true;
00242 std::map<std::string, std::string>::const_iterator it;
00243
00244 if (jointNames.size() == 0)
00245 {
00246
00247 it = supervisorMap.begin();
00248
00249 while (it != supervisorMap.end())
00250 {
00251 if (!(it->second == originator || originator == master))
00252 {
00253 ret = false;
00254 break;
00255 }
00256
00257 it++;
00258 }
00259 }
00260 else
00261 {
00262
00263 for (unsigned int i = 0; i < jointNames.size(); i++)
00264 {
00265 if ((it = supervisorMap.find(jointNames[i])) != supervisorMap.end())
00266 {
00267 if (!(it->second == originator || originator == master))
00268 {
00269 ret = false;
00270 break;
00271 }
00272 }
00273 }
00274 }
00275
00276 return ret;
00277 }
00278
00279 bool ModeArbiter::isValidCommand(const r2_msgs::Modes& modeMsg)
00280 {
00281 return isValidCommand(modeMsg.originator, modeMsg.joint_names);
00282 }
00283
00284
00285 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledGains& gains)
00286 {
00287 return isValidCommand(gains.originator, gains.joint_names);
00288 }
00289
00290 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledJointState& js)
00291 {
00292 return isValidCommand(js.originator, js.name);
00293 }
00294
00295 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledPoseTrajectory& pt)
00296 {
00297 bool ret = true;
00298
00299 std::map<std::string, std::string>::const_iterator it = supervisorMap.begin();
00300
00301 while (it != supervisorMap.end())
00302 {
00303 if (!(it->second == pt.originator || pt.originator == master))
00304 {
00305 ret = false;
00306 break;
00307 }
00308
00309 it++;
00310
00311 }
00312
00313 return ret;
00314 }
00315
00316 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledControllerPoseSettings& ps)
00317 {
00318 bool ret = true;
00319
00320 std::map<std::string, std::string>::const_iterator it = supervisorMap.begin();
00321
00322 while (it != supervisorMap.end())
00323 {
00324 if (!(it->second == ps.originator || ps.originator == master))
00325 {
00326 ret = false;
00327 break;
00328 }
00329
00330 it++;
00331
00332 }
00333
00334 return ret;
00335 }
00336
00337 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledGripperPositionCommand& gp)
00338 {
00339 bool ret = true;
00340 std::map<std::string, std::string>::const_iterator it;
00341
00342 for (unsigned int i = 0; i < gp.name.size(); i++)
00343 {
00344 if ((it = supervisorMap.find(gp.name[i])) != supervisorMap.end())
00345 {
00346 if (!(it->second == gp.originator || gp.originator == master))
00347 {
00348 ret = false;
00349 break;
00350 }
00351 }
00352 else
00353 {
00354 ret = false;
00355 break;
00356 }
00357
00358 }
00359
00360 return ret;
00361 }
00362
00363 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledJointTrajectory& jt)
00364 {
00365 return isValidCommand(jt.originator, jt.joint_names);
00366 }
00367
00368 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledControllerJointSettings& js)
00369 {
00370 return isValidCommand(js.originator, js.joint_names);
00371 }
00372
00373 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledTrajectoryMonitorFactors& tmf)
00374 {
00375 return isValidCommand(tmf.originator, tmf.joint_names);
00376 }
00377
00378 bool ModeArbiter::isValidCommand(const r2_msgs::LabeledJointControlDataArray& jc)
00379 {
00380 bool ret = true;
00381 std::map<std::string, std::string>::const_iterator it;
00382
00383 for (unsigned int i = 0; i < jc.joint.size(); i++)
00384 {
00385 if ((it = supervisorMap.find(jc.joint[i])) != supervisorMap.end())
00386 {
00387 if (!(it->second == jc.originator || jc.originator == master))
00388 {
00389 ret = false;
00390 break;
00391 }
00392 }
00393 else
00394 {
00395 ret = false;
00396 break;
00397 }
00398
00399 }
00400
00401 return ret;
00402 }
00403
00404 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledGains& gainsIn, r2_msgs::Gains& gainsOut)
00405 {
00406 bool ret = false;
00407
00408 if (isValidCommand(gainsIn))
00409 {
00410 ret = true;
00411 gainsOut.joint_names = gainsIn.joint_names;
00412 gainsOut.naturalFreq = gainsIn.naturalFreq;
00413 gainsOut.dampingRatio = gainsIn.dampingRatio;
00414 gainsOut.K = gainsIn.K;
00415 gainsOut.D = gainsIn.D;
00416 gainsOut.I = gainsIn.I;
00417 gainsOut.windupLimit = gainsIn.windupLimit;
00418 }
00419
00420 return ret;
00421 }
00422
00423 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledJointState& jsIn, sensor_msgs::JointState& jsOut)
00424 {
00425 bool ret = false;
00426
00427 if (isValidCommand(jsIn))
00428 {
00429 ret = true;
00430 jsOut.header = jsIn.header;
00431 jsOut.name = jsIn.name;
00432 jsOut.position = jsIn.position;
00433 jsOut.velocity = jsIn.velocity;
00434 jsOut.effort = jsIn.effort;
00435 }
00436
00437 return ret;
00438 }
00439
00440 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledPoseTrajectory& ptIn, r2_msgs::PoseTrajectory& ptOut)
00441 {
00442 bool ret = false;
00443
00444 if (isValidCommand(ptIn))
00445 {
00446 ret = true;
00447 ptOut.header = ptIn.header;
00448 ptOut.nodes = ptIn.nodes;
00449 ptOut.node_priorities = ptIn.node_priorities;
00450 ptOut.refFrames = ptIn.refFrames;
00451 ptOut.points = ptIn.points;
00452 }
00453
00454 return ret;
00455 }
00456
00457 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledControllerPoseSettings& psIn, r2_msgs::ControllerPoseSettings& psOut)
00458 {
00459 bool ret = false;
00460
00461 if (isValidCommand(psIn))
00462 {
00463 ret = true;
00464 psOut.maxLinearVelocity = psIn.maxLinearVelocity;
00465 psOut.maxRotationalVelocity = psIn.maxRotationalVelocity;
00466 psOut.maxLinearAcceleration = psIn.maxLinearAcceleration;
00467 psOut.maxRotationalAcceleration = psIn.maxRotationalAcceleration;
00468 }
00469
00470 return ret;
00471 }
00472
00473 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledJointTrajectory& jtIn, trajectory_msgs::JointTrajectory& jtOut)
00474 {
00475 bool ret = false;
00476
00477 if (isValidCommand(jtIn))
00478 {
00479 ret = true;
00480 jtOut.header = jtIn.header;
00481 jtOut.joint_names = jtIn.joint_names;
00482 jtOut.points = jtIn.points;
00483 }
00484
00485 return ret;
00486 }
00487
00488 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledControllerJointSettings& jsIn, r2_msgs::ControllerJointSettings& jsOut)
00489 {
00490 bool ret = false;
00491
00492 if (isValidCommand(jsIn))
00493 {
00494 ret = true;
00495 jsOut.joint_names = jsIn.joint_names;
00496 jsOut.jointVelocityLimits = jsIn.jointVelocityLimits;
00497 jsOut.jointAccelerationLimits = jsIn.jointAccelerationLimits;
00498 }
00499
00500 return ret;
00501 }
00502
00503 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledGripperPositionCommand& gpIn, r2_msgs::GripperPositionCommand& gpOut)
00504 {
00505 bool ret = false;
00506
00507 if (isValidCommand(gpIn))
00508 {
00509 ret = true;
00510 gpOut.name = gpIn.name;
00511 gpOut.setpoint = gpIn.setpoint;
00512 gpOut.command = gpIn.command;
00513 gpOut.dutyCycle = gpIn.dutyCycle;
00514 }
00515
00516 return ret;
00517 }
00518
00519 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledJointControlDataArray& jcIn, r2_msgs::JointControlDataArray& jcOut)
00520 {
00521 bool ret = false;
00522 jcOut.joint.clear();
00523 jcOut.data.clear();
00524
00525 if (isValidCommand(jcIn))
00526 {
00527 ret = true;
00528
00529 for (unsigned int i = 0; i < jcIn.joint.size(); i++)
00530 {
00531
00532 if (std::find(grippers.begin(), grippers.end(), jcIn.joint[i]) == grippers.end())
00533 {
00534 jcOut.joint.push_back(jcIn.joint[i]);
00535 jcOut.data.push_back(jcIn.data[i]);
00536 }
00537 else
00538 {
00539 r2_msgs::JointControlData data = jcIn.data[i];
00540 data.controlMode.state = r2_msgs::JointControlMode::IGNORE;
00541 data.commandMode.state = r2_msgs::JointControlCommandMode::IGNORE;
00542 data.calibrationMode.state = r2_msgs::JointControlCalibrationMode::IGNORE;
00543 jcOut.joint.push_back(jcIn.joint[i]);
00544 jcOut.data.push_back(data);
00545 }
00546 }
00547 }
00548
00549 return ret;
00550 }
00551
00552 bool ModeArbiter::CreateCommandMessage(const r2_msgs::LabeledTrajectoryMonitorFactors& tmfIn, r2_msgs::TrajectoryMonitorFactors& tmfOut)
00553 {
00554 bool ret = false;
00555
00556 if (isValidCommand(tmfIn))
00557 {
00558 ret = true;
00559 tmfOut.joint_names = tmfIn.joint_names;
00560 tmfOut.distanceFactors = tmfIn.distanceFactors;
00561 tmfOut.torqueLimitFactors = tmfIn.torqueLimitFactors;
00562 }
00563
00564 return ret;
00565 }
00566
00567 void ModeArbiter::CreateTriggerMessages(std::vector<r2_msgs::Modes>& triggerMsgs)
00568 {
00569 r2_msgs::Modes tempMsg;
00570 std::list<std::string> supervisors;
00571
00572 triggerMsgs.clear();
00573
00574 std::map<std::string, std::string>::const_iterator it = supervisorMap.begin();
00575
00576 while (it != supervisorMap.end())
00577 {
00578 supervisors.push_back(it->second);
00579 it++;
00580 }
00581
00582 supervisors.unique();
00583
00584 std::list<std::string>::iterator itl = supervisors.begin();
00585
00586 for (; itl != supervisors.end(); ++itl)
00587 {
00588 tempMsg.header.stamp = ros::Time::now();
00589 tempMsg.joint_names.clear();
00590 tempMsg.supervisor.clear();
00591 tempMsg.incumbent.clear();
00592
00593 std::string super = *itl;
00594 tempMsg.supervisor.push_back(super);
00595
00596 std::map<std::string, std::string>::const_iterator its = supervisorMap.begin();
00597
00598 while (its != supervisorMap.end())
00599 {
00600 if (its->second == super)
00601 {
00602 tempMsg.joint_names.push_back(its->first);
00603 }
00604
00605 its++;
00606 }
00607
00608
00609 if (tempMsg.joint_names.size() > 0)
00610 {
00611 tempMsg.incumbent.push_back(incumbentMap[tempMsg.joint_names[0]]);
00612 }
00613
00614 triggerMsgs.push_back(tempMsg);
00615 }
00616 }