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

ArSick.h

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 #ifndef ARSICK_H
00028 #define ARSICK_H
00029 
00030 #include "ariaTypedefs.h"
00031 #include "ArSickPacket.h"
00032 #include "ArSickPacketReceiver.h"
00033 #include "ArRobotPacket.h"
00034 #include "ArRangeDeviceThreaded.h"   
00035 #include "ArFunctor.h"
00036 #include "ArCondition.h"
00037 
00039 class ArSick : public ArRangeDeviceThreaded
00040 {
00041 public:
00042   enum BaudRate {
00043     BAUD9600, 
00044     BAUD19200, 
00045     BAUD38400 
00046   };
00047   enum Degrees {
00048     DEGREES180, 
00049     DEGREES100 
00050   };
00051   enum Increment {
00052     INCREMENT_ONE, 
00053     INCREMENT_HALF 
00054   };
00056   ArSick(size_t currentBufferSize = 361, 
00057                   size_t cumulativeBufferSize = 0,
00058                   const char *name = "laser",
00059                   bool addAriaExitCB = true);
00061   virtual ~ArSick();
00063   void configure(bool useSim = false, bool powerControl = true, 
00064                           bool laserFlipped = false, 
00065                           BaudRate baud = BAUD38400,
00066                           Degrees deg = DEGREES180, 
00067                           Increment incr = INCREMENT_ONE);
00069   void configureShort(bool useSim = false, 
00070                                BaudRate baud = BAUD38400,
00071                                Degrees deg = DEGREES180, 
00072                                Increment incr = INCREMENT_ONE);
00074   void setSensorPosition(double x, double y, double th);
00076   void setSensorPosition(ArPose pose);  
00078   ArPose getSensorPosition();
00080   double getSensorPositionX();
00082   double getSensorPositionY();
00084   double getSensorPositionTh();
00086   bool blockingConnect(void);
00088   bool asyncConnect(void);
00090   bool disconnect(bool doNotLockRobotForSim = false);
00092   void setDeviceConnection(ArDeviceConnection *conn);
00094   ArDeviceConnection *getDeviceConnection(void);
00096   bool isConnected(void) 
00097     { if (myState == STATE_CONNECTED) return true; else return false; }
00099   bool tryingToConnect(void) 
00100     { if (myState != STATE_CONNECTED && myState != STATE_NONE) 
00101       return true; else return false; }
00102 
00104   unsigned int getMinRange(void) { return myMinRange; }
00106   void setMinRange(unsigned int minRange) { myMinRange = minRange; }
00108   void addIgnoreReading(double ignoreReading) 
00109                { myIgnoreReadings.push_back(ignoreReading); }
00111   void clearIgnoreReadings(void) { myIgnoreReadings.clear(); }
00113   const std::list<double> *getIgnoreReadings(void) const
00114              { return &myIgnoreReadings; }
00116   void setFilterNearDist(double dist); 
00118   double getFilterNearDist(void);
00119 
00121   void setFilterCumulativeMaxDist(double dist) 
00122     { setMaxDistToKeepCumulative(dist); }
00124   double getFilterCumulativeMaxDist(void)
00125     { return getMaxDistToKeepCumulative(); }
00127   void setFilterCumulativeInsertMaxDist(double dist);
00129   double getFilterCumulativeInsertMaxDist(void);
00131   void setFilterCumulativeNearDist(double dist);
00133   double getFilterCumulativeNearDist(void);
00135   void setFilterCumulativeCleanDist(double dist);
00137   double getFilterCumulativeCleanDist(void);
00139   void setFilterCleanCumulativeInterval(int milliSeconds);
00141   int getFilterCleanCumulativeInterval(void);
00145   void setFilterCumulativeMaxAge(int seconds) 
00146     { setMaxSecondsToKeepCumulative(seconds); }
00150   int getFilterCumulativeMaxAge(void) 
00151     { return getMaxSecondsToKeepCumulative(); }
00152 
00154   bool runOnRobot(void);
00155 
00156 
00158   int getSickPacCount();
00159 
00161   void addConnectCB(ArFunctor *functor,
00162                              ArListPos::Pos position = ArListPos::LAST);
00164   void remConnectCB(ArFunctor *functor);
00165 
00167   void addFailedConnectCB(ArFunctor *functor, 
00168                                    ArListPos::Pos position = ArListPos::LAST);
00170   void remFailedConnectCB(ArFunctor *functor);
00171 
00173   void addDisconnectNormallyCB(ArFunctor *functor, 
00174                                 ArListPos::Pos position = ArListPos::LAST);
00176   void remDisconnectNormallyCB(ArFunctor *functor);
00177   
00179   void addDisconnectOnErrorCB(ArFunctor *functor, 
00180                                ArListPos::Pos position = ArListPos::LAST);
00182   void remDisconnectOnErrorCB(ArFunctor *functor);
00183 
00185   void addDataCB(ArFunctor *functor,
00186                                ArListPos::Pos position = ArListPos::LAST);
00188   void remDataCB(ArFunctor *functor);
00189 
00191   void setConnectionTimeoutTime(int mSecs);
00193   int getConnectionTimeoutTime(void);
00194 
00196   ArTime getLastReadingTime(void);
00197 
00198 
00200   bool isUsingSim(void);
00202   bool isControllingPower(void);
00204   bool isLaserFlipped(void);
00206   Degrees getDegrees(void);
00208   Increment getIncrement(void);
00209 
00210 
00212   void setIsUsingSim(bool usingSim);
00214   void setIsControllingPower(bool controlPower);
00216   void setIsLaserFlipped(bool laserFlipped);
00217 
00221   bool simPacketHandler(ArRobotPacket * packet);
00226   void sensorInterpCallback(void);
00230   bool internalConnectSim(void);
00232   int internalConnectHandler(void);
00236   virtual void * runThread(void *arg);
00240   void processPacket(ArSickPacket *packet, ArPose pose, 
00241                               ArPose encoderPose, unsigned int counter,
00242                               bool deinterlace, ArPose deinterlaceDelta);
00246   void runOnce(bool lockRobot);
00247   virtual void setRobot(ArRobot *robot);
00251   void dropConnection(void);
00255   void failedConnect(void);
00259   void madeConnection(void);
00263   void robotConnectCallback(void);
00265   virtual void applyTransform(ArTransform trans,
00266                                         bool doCumulative = true);
00267 protected:
00268   enum State {
00269     STATE_NONE, 
00270     STATE_INIT, 
00271     STATE_WAIT_FOR_POWER_ON, 
00272     STATE_CHANGE_BAUD, 
00273     STATE_CONFIGURE, 
00274     STATE_WAIT_FOR_CONFIGURE_ACK, 
00275     STATE_INSTALL_MODE, 
00276     STATE_WAIT_FOR_INSTALL_MODE_ACK, 
00277     STATE_SET_MODE, 
00278     STATE_WAIT_FOR_SET_MODE_ACK, 
00279     STATE_START_READINGS, 
00280     STATE_WAIT_FOR_START_ACK, 
00281     STATE_CONNECTED 
00282   };
00284   void filterReadings();
00286   void filterAddAndCleanCumulative(double x, double y, bool clean);
00288   void filterFarAndOldCumulative(void);
00290   void switchState(State state);
00292   std::list<double> myIgnoreReadings;
00293   State myState;
00294   unsigned int myMinRange;
00295   ArTime myStateStart;
00296   ArFunctorC<ArSick> myRobotConnectCB;
00297   ArRetFunctor1C<bool, ArSick, ArRobotPacket *> mySimPacketHandler;
00298   ArFunctorC<ArSick> mySensorInterpCB;
00299   std::list<ArSensorReading *>::iterator myIter;
00300   bool myStartConnect;
00301   bool myRunningOnRobot;
00302   int myTimeoutTime;
00303 
00304   // range buffers to hold current range set and assembling range set
00305   std::list<ArSensorReading *> *myAssembleReadings;
00306   std::list<ArSensorReading *> *myCurrentReadings;
00307 
00308   bool myProcessImmediately;
00309   bool myInterpolation;
00310   // list of packets, so we can process them from the sensor callback
00311   std::list<ArSickPacket *> myPackets;
00312 
00313   // a lock for the device (so we are mostly unlocked for people to get data, 
00314   // but so we don't have people trying to read multiple times from the same 
00315   // device
00316   ArMutex myConnLock;
00317 
00318   // whether we did a real configure or a short configure
00319   bool myRealConfigured;
00320   
00321   // these two are just for the sim packets
00322   unsigned int myWhichReading;
00323   unsigned int myTotalNumReadings;
00324 
00325   // some variables so we don't have to do a tedios if every time
00326   double myOffsetAmount;
00327   double myIncrementAmount;
00328 
00329   // packet stuff
00330   ArSickPacket myPacket;
00331   bool myRunInOwnThread;
00332   bool myUseSim;
00333   bool myLaserFlipped;
00334   ArPose mySensorPose;
00335   bool myPowerControl;
00336   BaudRate myBaud;
00337   Degrees myDegrees;
00338   Increment myIncrement;
00339   ArTime myLastReading;
00340   
00341   // stuff for the sim packet
00342   ArPose mySimPacketStart;
00343   ArTransform mySimPacketTrans;
00344   ArTransform mySimPacketEncoderTrans;
00345   unsigned int mySimPacketCounter;
00346 
00347   // packet count
00348   time_t myTimeLastSickPacket;
00349   int mySickPacCurrentCount;
00350   int mySickPacCount;
00351 
00352   // connection
00353   ArSickPacketReceiver mySickPacketReceiver;
00354   ArDeviceConnection *myConn;
00355   std::list<ArFunctor *> myConnectCBList;
00356   std::list<ArFunctor *> myFailedConnectCBList;
00357   std::list<ArFunctor *> myDisconnectNormallyCBList;
00358   std::list<ArFunctor *> myDisconnectOnErrorCBList;
00359   std::list<ArFunctor *> myDataCBList;
00360 
00361   ArTime myLastCleanedCumulative;
00362   // filtering parameters, see the docs in the acessor
00363   double myFilterNearDist;      
00364   double myFilterCumulativeInsertMaxDist; 
00365   double myFilterSquaredCumulativeInsertMaxDist;
00366   double myFilterCumulativeNearDist; 
00367   double myFilterSquaredCumulativeNearDist; 
00368   double myFilterCumulativeCleanDist; 
00369   double myFilterSquaredCumulativeCleanDist; 
00370   int myFilterCleanCumulativeInterval; 
00371 
00372   ArMutex myStateMutex;
00373   ArRetFunctor1C<bool, ArSick, bool> myAriaExitCB;
00374 };
00375 
00376 #endif //ARSICK_H

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