GripperEnvironment.cpp
Go to the documentation of this file.
00001 #include "robodyn_mechanisms/GripperEnvironment.h"
00002 
00003 GripperEnvironment::GripperEnvironment()
00004 {
00005 }
00006 
00007 GripperEnvironment::~GripperEnvironment()
00008 {
00009 }
00010 
00011 void GripperEnvironment::initializeMap(const std::vector<std::string>& grippers)
00012 {
00013     // Initialize zeros in the right ways
00014     GripperEnvInfo initData;
00015 
00016     initData.environment       = r2_msgs::GripperEnvironment::NONE;
00017     initData.gripperLocked     = 0;
00018     initData.userCommand       = r2_msgs::GripperEnvironment::NONE;
00019     initData.highForceSubState = 0;
00020     initData.frame             = KDL::Frame();
00021     initData.prevFrame         = KDL::Frame();
00022 
00023     for (unsigned int i = 0; i < grippers.size(); i++)
00024     {
00025         gripperEnvMap[grippers[i]] = initData;
00026     }
00027 
00028 }
00029 
00030 void GripperEnvironment::setComponentNames(const std::string& handrail, const std::string& seatTrack)
00031 {
00032     handrailComponent  = handrail;
00033     seatTrackComponent = seatTrack;
00034 }
00035 
00036 bool GripperEnvironment::getMapEntry(const std::string& name, GripperEnvInfo& info)
00037 {
00038     if ((envIt = gripperEnvMap.find(name)) != gripperEnvMap.end())
00039     {
00040         info = envIt->second;
00041         return true;
00042     }
00043 
00044     return false;
00045 }
00046 
00047 std::string GripperEnvironment::getBaseFrame()
00048 {
00049     if (!baseFrameList.empty())
00050     {
00051         return baseFrameList[0];
00052     }
00053 
00054     return "";
00055 }
00056 
00057 int GripperEnvironment::getGoalStatusArraySize()
00058 {
00059     return goalStatuses.status_list.size();
00060 }
00061 
00062 void GripperEnvironment::setUserCommand(const std::string gripperName, const int& cmd)
00063 {
00064     if ((envIt = gripperEnvMap.find(gripperName)) != gripperEnvMap.end())
00065     {
00066         envIt->second.userCommand = cmd;
00067     }
00068 }
00069 
00070 void GripperEnvironment::populateGripperStateInfo(const r2_msgs::GripperPositionState& gripperState)
00071 {
00072     // Iterate through all end effectors in message
00073     for (unsigned int i = 0; i < gripperState.name.size(); i++)
00074     {
00075         if ((envIt = gripperEnvMap.find(gripperState.name[i])) != gripperEnvMap.end())
00076         {
00077             if (gripperState.name.size() == gripperState.locked.size())
00078             {
00079                 envIt->second.gripperLocked = gripperState.locked[i];
00080             }
00081         }
00082     }
00083 }
00084 
00085 void GripperEnvironment::populateBaseFrameInfo(const r2_msgs::StringArray& baseFrames)
00086 {
00087     baseFrameList = baseFrames.data;
00088 }
00089 
00090 void GripperEnvironment::populatePoseStateInfo(const r2_msgs::PoseState& poseCmd)
00091 {
00092     if (baseFrameList.empty())
00093     {
00094         return;
00095     }
00096 
00097     // iterate through map
00098     for (envIt = gripperEnvMap.begin(); envIt != gripperEnvMap.end(); envIt++)
00099     {
00100         try
00101         {
00102             RosMsgConverter::PoseStateToFrame(poseCmd, baseFrameList[0], GripperToBaseFrame(envIt->first), tempFrame);
00103         }
00104         catch (std::logic_error& e)
00105         {
00106             NasaCommonLogging::Logger::log("gov.nasa.robodyn.GripperEnvironment", diagnostic_msgs::DiagnosticStatus::WARN, "Bad pose message");
00107             return;
00108         }
00109 
00110         envIt->second.prevFrame = envIt->second.frame;
00111         envIt->second.frame     = tempFrame;
00112     }
00113 }
00114 
00115 void GripperEnvironment::populateGoalStatus(const actionlib_msgs::GoalStatusArray& goalStats)
00116 {
00117     goalStatuses.status_list.clear();
00118 
00119     if (findAttachType(goalStats.header.frame_id) == r2_msgs::GripperEnvironment::HANDRAIL ||
00120             findAttachType(goalStats.header.frame_id) == r2_msgs::GripperEnvironment::SEATTRACK)
00121     {
00122         for (unsigned int i = 0; i < goalStats.status_list.size(); i++)
00123         {
00124             // clear this vector after using it in transitions
00125             if (goalStats.status_list[i].status == actionlib_msgs::GoalStatus::SUCCEEDED)
00126             {
00127                 goalStatuses.status_list.push_back(goalStats.status_list[i]);
00128             }
00129         }
00130     }
00131 }
00132 
00133 void GripperEnvironment::createGripperEnvironmentMsg(r2_msgs::GripperEnvironment& gripEnv)
00134 {
00135     gripEnv.header.stamp = ros::Time::now();
00136     gripEnv.name.clear();
00137     gripEnv.environment.clear();
00138 
00139     for (envIt = gripperEnvMap.begin(); envIt != gripperEnvMap.end(); envIt++)
00140     {
00141         gripEnv.name.push_back(envIt->first);
00142 
00143         gripEnv.environment.push_back(envIt->second.environment);
00144     }
00145 }
00146 
00147 void GripperEnvironment::update()
00148 {
00149     for (envIt = gripperEnvMap.begin(); envIt != gripperEnvMap.end(); envIt++)
00150     {
00151         switch (envIt->second.environment)
00152         {
00153             case r2_msgs::GripperEnvironment::NONE:
00154                 if (baseFrameList.size() > 0)
00155                 {
00156                     if (isBaseFrame(envIt))
00157                     {
00158                         transToHighForce(r2_msgs::GripperEnvironment::HANDRAIL, envIt);
00159                     }
00160                     else
00161                     {
00162                         transToFreespace(envIt);
00163                     }
00164                 }
00165 
00166                 break;
00167 
00168             case r2_msgs::GripperEnvironment::FREESPACE:
00169                 if (checkForHighForce(envIt, type))
00170                 {
00171                     transToHighForce(type, envIt);
00172                 }
00173 
00174                 break;
00175 
00176             case r2_msgs::GripperEnvironment::HANDRAIL:
00177             case r2_msgs::GripperEnvironment::SEATTRACK:
00178                 if (checkForFreespaceUserCmd(envIt))
00179                 {
00180                     transToFreespace(envIt);
00181                     break;
00182                 }
00183 
00184                 if (envIt->second.highForceSubState == 0)
00185                 {
00187                     if (checkForMovement(envIt))
00188                     {
00189                         transToFreespace(envIt);
00190                     }
00191                     else
00192                     {
00193                         if (isGripperLocked(envIt))
00194                         {
00195                             envIt->second.highForceSubState = 1;
00196                         }
00197                     }
00198                 }
00199                 else
00200                 {
00202                     if (!isGripperLocked(envIt))
00203                     {
00204                         transToFreespace(envIt);
00205                     }
00206                 }
00207 
00208                 break;
00209 
00210             default:
00211                 break;
00212         }
00213     }
00214 }
00215 
00216 void GripperEnvironment::transToHighForce(const int& attachType, std::map<std::string, GripperEnvInfo>::iterator& gripIt)
00217 {
00218     gripIt->second.environment = attachType;
00219 
00220     if (isGripperLocked(gripIt))
00221     {
00222         gripIt->second.highForceSubState = 1;
00223     }
00224     else
00225     {
00226         gripIt->second.highForceSubState = 0;
00227 
00228         if (baseFrameList.size() > 0)
00229         {
00230             baseFrame = baseFrameList[0];
00231         }
00232         else
00233         {
00234             baseFrame = "r2/robot_world";
00235         }
00236     }
00237 }
00238 
00239 void GripperEnvironment::transToFreespace(std::map<std::string, GripperEnvInfo>::iterator& gripIt)
00240 {
00241     gripIt->second.environment       = r2_msgs::GripperEnvironment::FREESPACE;
00242     gripIt->second.highForceSubState = 0;
00243 }
00244 
00245 bool GripperEnvironment::checkForHighForce(std::map<std::string, GripperEnvInfo>::iterator& gripIt, int& attachType)
00246 {
00247     if (checkForHighForceUserCmd(gripIt, attachType))
00248     {
00249         return true;
00250     }
00251     else if (checkGoalStatus(gripIt->first, attachType))
00252     {
00253         return true;
00254     }
00255     else
00256     {
00257         attachType = r2_msgs::GripperEnvironment::NONE;
00258         return false;
00259     }
00260 }
00261 
00262 bool GripperEnvironment::checkForFreespaceUserCmd(std::map<std::string, GripperEnvInfo>::iterator& gripIt)
00263 {
00264     if (gripIt->second.userCommand == r2_msgs::GripperEnvironment::FREESPACE)
00265     {
00266         gripIt->second.userCommand = r2_msgs::GripperEnvironment::NONE;
00267         return true;
00268     }
00269 
00270     return false;
00271 }
00272 
00273 bool GripperEnvironment::checkForHighForceUserCmd(std::map<std::string, GripperEnvInfo>::iterator& gripIt, int& attachType)
00274 {
00275     attachType = gripIt->second.userCommand;
00276 
00277     if (gripIt->second.userCommand == r2_msgs::GripperEnvironment::HANDRAIL || gripIt->second.userCommand == r2_msgs::GripperEnvironment::SEATTRACK)
00278     {
00279         gripIt->second.userCommand = r2_msgs::GripperEnvironment::NONE;
00280         return true;
00281     }
00282     else
00283     {
00284         return false;
00285     }
00286 }
00287 
00288 bool GripperEnvironment::checkGoalStatus(const std::string& gripper, int& attachType)
00289 {
00290     attachType = r2_msgs::GripperEnvironment::NONE;
00291 
00292     for (unsigned int i = 0; i < goalStatuses.status_list.size(); i++)
00293     {
00294         if (FindSide(goalStatuses.status_list[i].goal_id.id) == FindSide(gripper))
00295         {
00296             if ((attachType = findAttachType(goalStatuses.status_list[i].goal_id.id)) == r2_msgs::GripperEnvironment::NONE)
00297             {
00298                 return false;
00299             }
00300 
00301             goalStatuses.status_list.clear();
00302             return true;
00303         }
00304     }
00305 
00306     return false;
00307 }
00308 
00309 bool GripperEnvironment::checkForMovement(const std::map<std::string, GripperEnvInfo>::iterator& gripIt)
00310 {
00311     if (baseFrameList.size() > 0)
00312     {
00313         if (baseFrame != baseFrameList[0])
00314         {
00315             return true;
00316         }
00317     }
00318     else
00319     {
00320         return true;
00321     }
00322 
00324     frameDiff = KDL::diff(gripIt->second.frame, gripIt->second.prevFrame);
00325 
00326     for (unsigned int i = 0; i < 3; i++)
00327     {
00328         if (std::abs(frameDiff.vel[i]) > 0.01 || std::abs(frameDiff.rot[i]) > 0.01)
00329         {
00330             return true;
00331         }
00332     }
00333 
00334     return false;
00335 }
00336 
00337 bool GripperEnvironment::isGripperLocked(const std::map<std::string, GripperEnvInfo>::iterator& gripIt)
00338 {
00339     return gripIt->second.gripperLocked;
00340 }
00341 
00342 bool GripperEnvironment::isBaseFrame(const std::map<std::string, GripperEnvInfo>::iterator& gripIt)
00343 {
00344     if (isGripperLocked(gripIt))
00345     {
00346         if (std::find(baseFrameList.begin(), baseFrameList.end(), GripperToBaseFrame(gripIt->first)) != baseFrameList.end())
00347         {
00348             return true;
00349         }
00350     }
00351 
00352     return false;
00353 }
00354 
00355 int GripperEnvironment::findAttachType(const std::string& input)
00356 {
00357     std::size_t pos = input.find("Handrail");
00358 
00359     if (pos != std::string::npos)
00360     {
00361         return r2_msgs::GripperEnvironment::HANDRAIL;
00362     }
00363     else
00364     {
00365         pos = input.find("Seattrack");
00366 
00367         if (pos != std::string::npos)
00368         {
00369             return r2_msgs::GripperEnvironment::SEATTRACK;
00370         }
00371         else
00372         {
00373             return r2_msgs::GripperEnvironment::NONE;
00374         }
00375     }
00376 
00377     return r2_msgs::GripperEnvironment::NONE;
00378 }


robodyn_mechanisms
Author(s):
autogenerated on Thu Jun 6 2019 21:22:48