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

ArGripper.cpp

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

Generated on Wed Oct 19 12:56:35 2005 for Aria by  doxygen 1.4.0