Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArGripper.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArGripper.h"
00029 #include "ArCommands.h"
00030 
00037 AREXPORT ArGripper::ArGripper(ArRobot *robot, int gripperType) :
00038   myConnectCB(this, &ArGripper::connectHandler),
00039   myPacketHandlerCB(this, &ArGripper::packetHandler)
00040 {
00041   myRobot = robot;
00042   myType = gripperType;
00043   if (myRobot != NULL) 
00044   {
00045     myRobot->addPacketHandler(&myPacketHandlerCB, ArListPos::FIRST);
00046     myRobot->addConnectCB(&myConnectCB, ArListPos::LAST);
00047     if (myRobot->isConnected() && (myType == GRIPPAC || myType == QUERYTYPE))
00048       myRobot->comInt(ArCommands::GRIPPERPACREQUEST, 2);
00049   }
00050   myLastDataTime.setToNow();
00051 }
00052 
00053 AREXPORT ArGripper::~ArGripper()
00054 {
00055 }
00056 
00057 AREXPORT void ArGripper::connectHandler(void)
00058 {
00059   if (myRobot != NULL && (myType == GRIPPAC || myType == QUERYTYPE))
00060     myRobot->comInt(ArCommands::GRIPPERPACREQUEST, 2);
00061 }
00062 
00066 AREXPORT bool ArGripper::gripOpen(void)
00067 {
00068   if (myRobot != NULL)
00069     return myRobot->comInt(ArCommands::GRIPPER, 
00070                ArGripperCommands::GRIP_OPEN);
00071   else
00072     return false;
00073 }
00074 
00078 AREXPORT bool ArGripper::gripClose(void)
00079 {
00080   if (myRobot != NULL)
00081     return myRobot->comInt(ArCommands::GRIPPER, 
00082                ArGripperCommands::GRIP_CLOSE);
00083   else
00084     return false;
00085 }
00086 
00090 AREXPORT bool ArGripper::gripStop(void)
00091 {
00092   if (myRobot != NULL)
00093     return myRobot->comInt(ArCommands::GRIPPER, 
00094                ArGripperCommands::GRIP_STOP);
00095   else
00096     return false;
00097 }
00098 
00102 AREXPORT bool ArGripper::liftUp(void)
00103 {
00104   if (myRobot != NULL)
00105     return myRobot->comInt(ArCommands::GRIPPER, 
00106                ArGripperCommands::LIFT_UP);
00107   else
00108     return false;
00109 }
00110 
00114 AREXPORT bool ArGripper::liftDown(void)
00115 {
00116   if (myRobot != NULL)
00117     return myRobot->comInt(ArCommands::GRIPPER, 
00118                ArGripperCommands::LIFT_DOWN);
00119   else
00120     return false;
00121 }
00122 
00126 AREXPORT bool ArGripper::liftStop(void)
00127 {
00128   if (myRobot != NULL)
00129     return myRobot->comInt(ArCommands::GRIPPER, 
00130                ArGripperCommands::LIFT_STOP);
00131   else
00132     return false;
00133 }
00134 
00138 AREXPORT bool ArGripper::gripperStore(void)
00139 {
00140   if (myRobot != NULL)
00141     return myRobot->comInt(ArCommands::GRIPPER, 
00142                ArGripperCommands::GRIPPER_STORE);
00143   else
00144     return false;
00145 }
00146 
00150 AREXPORT bool ArGripper::gripperDeploy(void)
00151 {
00152   if (myRobot != NULL)
00153     return myRobot->comInt(ArCommands::GRIPPER, 
00154                ArGripperCommands::GRIPPER_DEPLOY);
00155   else
00156     return false;
00157 }
00158 
00162 AREXPORT bool ArGripper::gripperHalt(void)
00163 {
00164   if (myRobot != NULL)
00165     return myRobot->comInt(ArCommands::GRIPPER, 
00166                ArGripperCommands::GRIPPER_HALT);
00167   else
00168     return false;
00169 }
00170 
00174 AREXPORT bool ArGripper::gripPressure(int mSecIntervals)
00175 {
00176   if (myRobot == NULL)
00177     return false;
00178   
00179   if (myRobot->comInt(ArCommands::GRIPPER, ArGripperCommands::GRIP_PRESSURE) &&
00180       myRobot->comInt(ArCommands::GRIPPERVAL, mSecIntervals))
00181     return true;
00182   else
00183     return false;
00184 }
00185 
00189 AREXPORT bool ArGripper::liftCarry(int mSecIntervals)
00190 {
00191   if (myRobot == NULL)
00192     return false;
00193   
00194   if (myRobot->comInt(ArCommands::GRIPPER, ArGripperCommands::LIFT_CARRY) &&
00195       myRobot->comInt(ArCommands::GRIPPERVAL, mSecIntervals))
00196     return true;
00197   else
00198     return false;
00199 }
00200 
00204 AREXPORT bool ArGripper::isGripMoving(void) const
00205 {
00206   int d;
00207 
00208   if (myType == NOGRIPPER || myType == QUERYTYPE || myRobot == NULL)
00209   {
00210     return false;
00211   }
00212   else if (myType == GENIO || myType == GRIPPAC || myType == USERIO)
00213   {
00214     if (myType == GENIO || myType == USERIO)
00215       d = myRobot->getAnalogPortSelected() >> 8;
00216     else
00217       d = mySwitches;
00218 
00219     if (myType == USERIO && (d & ArUtil::BIT2)) // moving
00220       return true;
00221     else if (myType != USERIO && (d & ArUtil::BIT7)) // moving
00222       return true;
00223     else // not moving
00224       return false;
00225   }
00226   else
00227   {
00228     ArLog::log(ArLog::Terse, "ArGripper::isGripMoving: Gripper type unknown.");
00229     return false;
00230   }
00231 }
00232 
00236 AREXPORT bool ArGripper::isLiftMoving(void) const
00237 {
00238   int d;
00239 
00240   if (myType == NOGRIPPER || myType == QUERYTYPE || myRobot == NULL)
00241   {
00242     return false;
00243   }
00244   else if (myType == GENIO || myType == GRIPPAC || myType == USERIO)
00245   {
00246     if (myType == GENIO || myType == USERIO)
00247       d = myRobot->getAnalogPortSelected() >> 8;
00248     else
00249       d = mySwitches;
00250 
00251     if (d & ArUtil::BIT6) // moving
00252       return true;
00253     else // not moving
00254       return false;
00255   }
00256   else
00257   {
00258     ArLog::log(ArLog::Terse, "ArGripper::isLiftMoving: Gripper type unknown.");
00259     return false;
00260   }
00261     
00262 }
00263 
00269 AREXPORT int ArGripper::getPaddleState(void) const
00270 {
00271   int d;
00272   int ret = 0;
00273 
00274   if (myType == NOGRIPPER || myType == QUERYTYPE || myRobot == NULL)
00275   {
00276     return 0;
00277   }
00278   else if (myType == GENIO || myType == GRIPPAC || myType == USERIO)
00279   {
00280     if (myType == GENIO)
00281       d = myRobot->getAnalogPortSelected() >> 8;
00282     else if (myType == USERIO)
00283       d = myRobot->getDigIn();
00284     else
00285       d = mySwitches;
00286 
00287     if (!(d & ArUtil::BIT4))
00288       ret += 1;
00289     if (!(d & ArUtil::BIT5))
00290       ret += 2;
00291     return ret;
00292   }
00293   else
00294   {
00295     ArLog::log(ArLog::Terse, "ArGripper::getPaddleState: Gripper type unknown.");
00296     return 0;
00297   }
00298 }
00303 AREXPORT int ArGripper::getGripState(void) const
00304 {
00305   int d;
00306 
00307   if (myType == NOGRIPPER || myType == QUERYTYPE || myRobot == NULL)
00308   {
00309     return 0;
00310   }
00311   else if (myType == GENIO || myType == GRIPPAC || myType == USERIO)
00312   {
00313     if (myType == GENIO)
00314       d = myRobot->getAnalogPortSelected() >> 8;
00315     else if (myType == USERIO)
00316       d = myRobot->getDigIn();
00317     else
00318       d = mySwitches;
00319 
00320     if (!(d & ArUtil::BIT4) && !(d & ArUtil::BIT5)) // both
00321       return 2;
00322     else if (!(d & ArUtil::BIT0)) // inner
00323       return 1;
00324     else // between
00325       return 0;
00326   }
00327   else
00328   {
00329     ArLog::log(ArLog::Terse, "ArGripper::getGripState: Gripper type unknown.");
00330     return 0;
00331   }
00332 
00333 }
00334 
00339 AREXPORT int ArGripper::getBreakBeamState(void) const
00340 {
00341   int d;
00342 
00343   if (myType == NOGRIPPER || myType == QUERYTYPE || myRobot == NULL)
00344   {
00345     return 0;
00346   }
00347   else if (myType == GENIO || myType == GRIPPAC || myType == USERIO)
00348   {
00349     if (myType == GENIO)
00350       d = myRobot->getAnalogPortSelected() >> 8;
00351     else if (myType == USERIO)
00352       d = myRobot->getDigIn();
00353     else
00354       d = mySwitches;
00355 
00356     if ((d & ArUtil::BIT2) && (d & ArUtil::BIT3)) // both
00357       return 3;
00358     else if (d & ArUtil::BIT3) // inner
00359       return 1;
00360     else if (d & ArUtil::BIT2) // outter
00361       return 2;
00362     else // neither
00363       return 0;
00364   }
00365   else
00366   {
00367     ArLog::log(ArLog::Terse, 
00368            "ArGripper::getBreakBeamState: Gripper type unknown.");
00369     return 0;
00370   }
00371 }
00372 
00377 AREXPORT bool ArGripper::isLiftMaxed(void) const
00378 {
00379   int d = 0;
00380 
00381   if (myType == NOGRIPPER || myType == QUERYTYPE || myRobot == NULL)
00382   {
00383     return false;
00384   }
00385   else if (myType == GENIO || myType == GRIPPAC || myType == USERIO)
00386   {
00387     if (myType == GENIO)
00388       d = myRobot->getAnalogPortSelected() >> 8;
00389     else if (myType == USERIO)
00390       d = myRobot->getDigIn();
00391     else
00392       d = mySwitches;
00393     if (!(d & ArUtil::BIT1))
00394       return true;
00395     else
00396       return false;
00397   }
00398   else
00399   {
00400     ArLog::log(ArLog::Terse, "ArGripper::getLiftState: Gripper type unknown.");
00401     return false;
00402   }
00403 }
00404   
00405 
00406 AREXPORT void ArGripper::logState(void) const
00407 {
00408   char paddleBuf[128];
00409   char liftBuf[128];
00410   char breakBeamBuf[128];
00411   char buf[1024];
00412   int state;
00413 
00414   if (myType == NOGRIPPER)
00415   {
00416     ArLog::log(ArLog::Terse, "There is no gripper.");
00417     return;
00418   } 
00419   if (myType == QUERYTYPE)
00420   {
00421     ArLog::log(ArLog::Terse, "Querying gripper type.");
00422     return;
00423   }
00424   
00425   if (isLiftMaxed())
00426     sprintf(liftBuf, "maxed");
00427   else
00428     sprintf(liftBuf, "between");
00429 
00430   if (isLiftMoving())
00431     strcat(liftBuf, "_moving");
00432 
00433   state = getGripState();
00434   if (state == 1)
00435     sprintf(paddleBuf, "open");
00436   else if (state == 2)
00437     sprintf(paddleBuf, "closed");
00438   else
00439     sprintf(paddleBuf, "between");
00440 
00441   if (isGripMoving())
00442     strcat(paddleBuf, "_moving");
00443 
00444   state = getBreakBeamState();
00445   if (state == 0)
00446     sprintf(breakBeamBuf, "none");
00447   else if (state == 1)
00448     sprintf(breakBeamBuf, "inner");
00449   else if (state == 2)
00450     sprintf(breakBeamBuf, "outter");
00451   else if (state == 3)
00452     sprintf(breakBeamBuf, "both");
00453   
00454   sprintf(buf, "Lift: %15s  Grip: %15s  BreakBeam: %10s", liftBuf, paddleBuf,
00455       breakBeamBuf);
00456   if (myType == GRIPPAC)
00457     sprintf(buf, "%s TimeSince: %ld", buf, getMSecSinceLastPacket());
00458   ArLog::log(ArLog::Terse, buf);
00459   
00460 }
00461 
00462 AREXPORT bool ArGripper::packetHandler(ArRobotPacket *packet)
00463 {
00464   int type;
00465   
00466   if (packet->getID() != 0xE0)
00467     return false;
00468 
00469   myLastDataTime.setToNow();
00470   type = packet->bufToUByte();  
00471   mySwitches = packet->bufToUByte();
00472   myGraspTime = packet->bufToUByte();
00473 
00474   if (myType == QUERYTYPE)
00475   {
00476     if (type == 2)
00477     {
00478       ArLog::log(ArLog::Normal, 
00479          "Gripper:  querried, using General IO.");
00480       myType = GENIO;
00481     }
00482     else if (type == 1)
00483     {
00484       ArLog::log(ArLog::Normal, 
00485          "Gripper:  querried, using User IO.");
00486       myType = USERIO;
00487     }
00488     else
00489     {
00490       ArLog::log(ArLog::Normal, 
00491          "Gripper:  querried, the robot has no gripper.");
00492       myType = NOGRIPPER;
00493     }
00494     if (myRobot != NULL)
00495       myRobot->comInt(ArCommands::GRIPPERPACREQUEST, 0);
00496     return true;
00497   }
00498   if (myRobot != NULL && myType != GRIPPAC)
00499   {
00500     ArLog::log(ArLog::Verbose, 
00501            "Gripper: got another gripper packet after stop requested.");
00502     myRobot->comInt(ArCommands::GRIPPERPACREQUEST, 0);
00503   }
00504   return true;
00505 }
00506 
00511 AREXPORT int ArGripper::getType(void) const
00512 {
00513   return myType;
00514 }
00515 
00519 AREXPORT void ArGripper::setType(int type)
00520 {
00521   myType = type;
00522   if (myRobot != NULL && (myType == GRIPPAC || myType == QUERYTYPE))
00523     myRobot->comInt(ArCommands::GRIPPERPACREQUEST, 2);
00524 }
00525 
00529 AREXPORT long ArGripper::getMSecSinceLastPacket(void) const
00530 {
00531   return myLastDataTime.mSecSince();
00532 }
00533 
00541 AREXPORT int ArGripper::getGraspTime(void) const
00542 {
00543   return myGraspTime;
00544 }

Generated on Tue Feb 20 10:51:40 2007 for Aria by  doxygen 1.4.0