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

ArVCC4.h

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 #ifndef ARVCC4_H
00027 #define ARVCC4_H
00028 
00029 #include "ariaTypedefs.h"
00030 #include "ArBasePacket.h"
00031 #include "ArPTZ.h"
00032 #include "ariaUtil.h"
00033 #include "ArCommands.h"
00034 #include "ArSerialConnection.h"
00035 
00036 // maximum number of bytes expected for a response from the camera
00037 #define MAX_RESPONSE_BYTES 14
00038 
00039 // the state timeout when using bidirectional communication
00040 // This is big because it may have to wait for a power on or
00041 // power off command to complete, which take ~4 seconds.
00042 #define BIDIRECTIONAL_TIMEOUT 5000
00043 
00044 // The number of ms to wait for a timeout for unidirectional communication.
00045 // This is how long the usertask will wait before assuming that the camera
00046 // has processed the last command.
00047 #define UNIDIRECTIONAL_TIMEOUT 300
00048 
00049 // how often to request position information from the camera if using
00050 // bidirectional communication (in ms)
00051 #define AUTO_UPDATE_TIME 2000
00052 
00053 // accuracy of camera movements.  This sets how different the current 
00054 // position and the desired position must be in order for a command to be
00055 // sent to the camera.
00056 #define TOLERANCE .1
00057 
00100 
00101 class ArVCC4Commands
00102 {
00103 public:
00104   enum Command {
00105     DELIM = 0x00, 
00106     DEVICEID = 0x30, 
00107     PANSLEW = 0x50, 
00108     TILTSLEW = 0x51, 
00109     STOP = 0x53, 
00110     INIT = 0x58, 
00111     SLEWREQ = 0x59, 
00112     ANGLEREQ = 0x5c, 
00113     PANTILT = 0x62, 
00114     SETRANGE = 0x64, 
00115     PANTILTREQ = 0x63, 
00116     INFRARED = 0x76, 
00117     PRODUCTNAME = 0x87, 
00118     LEDCONTROL = 0x8E, 
00119     CONTROL = 0x90, 
00120     POWER = 0xA0, 
00121     AUTOFOCUS = 0xA1, 
00122     ZOOMSTOP = 0xA2, 
00123     GAIN = 0xA5, 
00124     FOCUS = 0xB0, 
00125     ZOOM = 0xB3, 
00126     ZOOMREQ = 0xB4, 
00127     IRCUTFILTER = 0xB5, 
00128     DIGITALZOOM = 0xB7, 
00129     FOOTER = 0xEF, 
00130     RESPONSE = 0xFE, 
00131     HEADER = 0xFF 
00132   };
00133 
00134 };
00135 
00137 
00142 class ArVCC4Packet: public ArBasePacket
00143 {
00144 public:
00146   AREXPORT ArVCC4Packet(ArTypes::UByte2 bufferSize = 30);
00148   AREXPORT virtual ~ArVCC4Packet();
00149 
00150   AREXPORT virtual void byte2ToBuf(ArTypes::Byte4 val);
00151 
00152   AREXPORT virtual void finalizePacket(void);
00153 
00154 protected:
00155 };
00156 
00157 class ArVCC4 : public ArPTZ
00158 {
00159 public:
00160   // the states for communication
00161   enum CommState {
00162     COMM_UNKNOWN,
00163     COMM_BIDIRECTIONAL,
00164     COMM_UNIDIRECTIONAL
00165   };
00166 
00167   enum CameraType {
00168     CAMERA_VCC4,
00169     CAMERA_C50I
00170   };
00171 
00173   AREXPORT ArVCC4(ArRobot *robot, bool inverted = false, CommState commDirection = COMM_UNKNOWN, bool autoUpdate = true, bool disableLED = false, CameraType cameraType = CAMERA_VCC4);
00175   AREXPORT virtual ~ArVCC4();
00176 
00177   AREXPORT virtual bool power(bool state) { myPowerStateDesired = state; return true; }
00178   AREXPORT bool getPower(void) { return myPowerState; }
00179   AREXPORT virtual bool init(void) { myInitRequested = true; return true; }
00180   AREXPORT virtual void reset(void) { ArPTZ::reset(); init(); }
00181 
00183   AREXPORT bool isInitted(void) { return myCameraIsInitted; }
00184   AREXPORT virtual void connectHandler(void);
00185   AREXPORT virtual bool packetHandler(ArBasePacket *packet);
00186 
00187   AREXPORT virtual bool pan(double deg) { return panTilt(deg, myTiltDesired); }
00188   AREXPORT virtual bool panRel(double deg) { return panTilt(myPanDesired + deg, myTiltDesired); }
00189   AREXPORT virtual bool tilt(double deg) { return panTilt(myPanDesired, deg); }
00190   AREXPORT virtual bool tiltRel(double deg) { return panTilt(myPanDesired, myTiltDesired + deg); }
00191   AREXPORT virtual bool panTiltRel(double pdeg, double tdeg) { return panTilt(myPanDesired + pdeg, myTiltDesired + tdeg); }
00192 
00193   AREXPORT virtual double getMaxPosPan(void) const 
00194     { if (myInverted) return invert(MIN_PAN); else return MAX_PAN; }
00195   AREXPORT virtual double getMaxNegPan(void) const 
00196     { if (myInverted) return invert(MAX_PAN); else return MIN_PAN; }
00197   AREXPORT virtual double getMaxPosTilt(void) const 
00198     { if (myInverted) return invert(MIN_TILT); else return MAX_TILT; }
00199   AREXPORT virtual double getMaxNegTilt(void) const
00200     { if (myInverted) return invert(MAX_TILT); else return MIN_TILT; }
00201 
00205   AREXPORT void getRealPanTilt(void) { myRealPanTiltRequested = true; }
00206 
00210   AREXPORT void getRealZoomPos(void) { myRealZoomRequested = true; }
00211 
00212   AREXPORT virtual bool canZoom(void) const { return true; }
00213 
00214   AREXPORT virtual bool panTilt(double pdeg, double tdeg);
00215   AREXPORT virtual bool zoom(int deg);
00218   AREXPORT bool digitalZoom(int deg);
00219 
00223   AREXPORT void addErrorCB(ArFunctor *functor, ArListPos::Pos position);
00224 
00226   AREXPORT void remErrorCB(ArFunctor *functor);
00227 
00229   AREXPORT bool haltPanTilt(void) { myHaltPanTiltRequested = true; return true; }
00231   AREXPORT bool haltZoom(void) { myHaltZoomRequested = true; return true; }
00232 
00234   AREXPORT bool panSlew(double deg) { myPanSlewDesired = deg; return true; }
00236   AREXPORT bool tiltSlew(double deg) { myTiltSlewDesired = deg; return true; }
00237 
00239   AREXPORT void preparePacket(ArVCC4Packet *packet);
00240 
00241   AREXPORT virtual double getPan(void) const { return myPanDesired; }
00242   AREXPORT virtual double getTilt(void) const { return myTiltDesired; }
00243   AREXPORT virtual int getZoom(void) const { return myZoomDesired; }
00244   AREXPORT double getDigitalZoom(void) const { return myDigitalZoomDesired; }
00245 
00246   AREXPORT virtual bool canGetRealPanTilt(void) const { return true; }
00247   AREXPORT virtual bool canGetRealZoom(void) const { return true; }
00248   AREXPORT virtual bool canSetFocus(void) const { return false; }
00251   AREXPORT virtual bool autoFocus(void) { myFocusModeDesired = 0; return true;}
00253   AREXPORT virtual bool focusNear(void) { myFocusModeDesired = 2; return true;}
00255   AREXPORT virtual bool focusFar(void) { myFocusModeDesired = 3; return true; }
00256 
00258   AREXPORT double getPanSlew(void) { return myPanSlewDesired; }
00260   AREXPORT double getMaxPanSlew(void) { return MAX_PAN_SLEW; }
00262   AREXPORT double getMinPanSlew(void) { return MIN_PAN_SLEW; }
00263 
00265   AREXPORT double getTiltSlew(void) { return myTiltSlewDesired; }
00267   AREXPORT double getMaxTiltSlew(void) { return MAX_TILT_SLEW; }
00269   AREXPORT double getMinTiltSlew(void) { return MIN_TILT_SLEW; }
00270 
00271   AREXPORT virtual int getMaxZoom(void) const;
00272   AREXPORT virtual int getMinZoom(void) const { return MIN_ZOOM; }
00273 
00274   AREXPORT virtual bool canGetFOV(void) { return true; }
00276   AREXPORT virtual double getFOVAtMaxZoom(void) { return myFOVAtMaxZoom; }
00278   AREXPORT virtual double getFOVAtMinZoom(void) { return myFOVAtMinZoom; }
00279 
00280 
00282   AREXPORT bool wasError(void) { return myWasError; }
00283 
00285   AREXPORT void enableAutoUpdate(void) { myAutoUpdate = true; }
00286   AREXPORT void disableAutoUpdate(void) { myAutoUpdate = false; }
00287   AREXPORT bool getAutoUpdate(void) { return myAutoUpdate; }
00288 
00291   AREXPORT void setLEDControlMode(int controlMode) { myDesiredLEDControlMode = controlMode; }
00293   AREXPORT void enableIRLEDs(void) { myDesiredIRLEDsMode = true; }
00295   AREXPORT void disableIRLEDs(void) { myDesiredIRLEDsMode = false; }
00297   AREXPORT bool getIRLEDsEnabled(void) { return myIRLEDsEnabled; }
00299   AREXPORT void enableIRFilterMode(void) { myDesiredIRFilterMode = true; }
00301   AREXPORT void disableIRFilterMode(void) { myDesiredIRFilterMode = false; }
00303   AREXPORT bool getIRFilterModeEnabled (void) { return myIRFilterModeEnabled; }
00304 protected:
00305 
00306   // preset limits on movements.  Based on empirical data
00307   enum Param {
00308     MAX_PAN = 98,       // 875 units is max pan assignment
00309     MIN_PAN = -98,      // -875 units is min pan assignment
00310     MAX_TILT = 88,      // 790 units is max tilt assignment
00311     MIN_TILT = -30,     // -267 units is min tilt assignment
00312     MAX_PAN_SLEW = 90,      // 800 positions per sec (PPS)
00313     MIN_PAN_SLEW = 1,       // 8 positions per sec (PPS)
00314     MAX_TILT_SLEW = 69,     // 662 positions per sec (PPS)
00315     MIN_TILT_SLEW = 1,      // 8 positions per sec (PPS)
00316     MAX_ZOOM_OPTIC = 1960,
00317     MIN_ZOOM = 0
00318   };
00319 
00320   // the various error states that the camera can return
00321   enum Error {
00322     CAM_ERROR_NONE = 0x30, 
00323     CAM_ERROR_BUSY = 0x31, 
00324     CAM_ERROR_PARAM = 0x35, 
00325     CAM_ERROR_MODE = 0x39,  
00326     CAM_ERROR_UNKNOWN = 0xFF 
00327  };
00328 
00329   // the states of the FSM
00330   enum State {
00331     UNINITIALIZED,
00332     STATE_UNKNOWN,
00333     INITIALIZING,
00334     SETTING_CONTROL_MODE,
00335     SETTING_INIT_TILT_RATE,
00336     SETTING_INIT_PAN_RATE,
00337     SETTING_INIT_RANGE,
00338     POWERING_ON,
00339     POWERING_OFF,
00340     POWERED_OFF,
00341     POWERED_ON,
00342     AWAITING_INITIAL_POWERON,
00343     AWAITING_INITIAL_INIT,
00344     AWAITING_ZOOM_RESPONSE,
00345     AWAITING_PAN_TILT_RESPONSE,
00346     AWAITING_STOP_PAN_TILT_RESPONSE,
00347     AWAITING_STOP_ZOOM_RESPONSE,
00348     AWAITING_PAN_SLEW_RESPONSE,
00349     AWAITING_TILT_SLEW_RESPONSE,
00350     AWAITING_POS_REQUEST,
00351     AWAITING_ZOOM_REQUEST,
00352     AWAITING_LED_CONTROL_RESPONSE,
00353     AWAITING_IRLEDS_RESPONSE,
00354     AWAITING_IRFILTER_RESPONSE,
00355     AWAITING_PRODUCTNAME_REQUEST,
00356     AWAITING_DIGITAL_ZOOM_RESPONSE,
00357     AWAITING_FOCUS_RESPONSE,
00358     STATE_DELAYED_SWITCH,
00359     STATE_ERROR
00360   };
00361 
00362   // flips the sign if needed
00363   double invert(double before) const
00364     { if (myInverted) return -before; else return before; }
00365   bool myInverted;
00366 
00367   // true if there was an error during the last cycle
00368   bool myWasError;
00369 
00370   // the camera name.  "C50i" for C50i, and "VC-C" for VC-C4
00371   std::string myProductName;
00372 
00373   ArRobot *myRobot;
00374   ArDeviceConnection *myConn;
00375   ArBasePacket *newPacket;
00376   ArVCC4Packet myPacket;
00377 
00378   // timers for watching for timeouts
00379   ArTime myStateTime;
00380   ArTime myPacketTime;
00381   ArTime myIdleTime;
00382 
00383   // gets set to true if using an aux port vs computer serial port
00384   bool myUsingAuxPort;
00385 
00386   // delay variable, if delaying before switching to the next state
00387   int myStateDelayTime;
00388 
00389   // what type of communication the camera is using
00390   CommState myCommType;
00391 
00392   // used to read data if the camera is attached directly to a computer
00393   virtual ArBasePacket* readPacket(void);
00394 
00395   // the functor to add as a usertask
00396   ArFunctorC<ArVCC4> myTaskCB;
00397 
00398   // the actual task to be added as a usertask
00399   void camTask(void);
00400 
00401   // true when a response has been received from the camera, but has
00402   // not yet been acted on by the state machine
00403   bool myResponseReceived;
00404 
00405   bool myWaitingOnStop;
00406   bool myWaitingOnPacket;
00407 
00408   // the state of the state machine
00409   State myState;
00410   State myPreviousState;
00411   State myNextState;
00412 
00413   // used to switch between states in the state machine
00414   void switchState(State state, int delayTime = 0);
00415 
00416   // the max time before a state times out, and the time for a packet response
00417   // to timeout.  The difference being that a packet reponse can be received
00418   // immediately, but it could say that the camera is busy, meaning the state
00419   // has not yet completed
00420   int myStateTimeout;
00421   int myPacketTimeout;
00422 
00423   // request a packet from the microcontroller of size num bytes.
00424   // most camera responses are 6 bytes, so just use the default
00425   void requestBytes(int num = 6);
00426 
00427   // the buffer to store the incoming packet data in
00428   unsigned char myPacketBuf[50];
00429   int myPacketBufLen;
00430 
00431   // how many bytes we're still expecting to receive from the controller
00432   int myBytesLeft;
00433 
00434   // these all send commands to the camera.
00435   bool sendPanTilt(void);
00436   bool sendZoom(void);
00437   bool sendPanSlew(void);
00438   bool sendTiltSlew(void);
00439   bool sendPower(void);
00440   bool sendHaltPanTilt(void);
00441   bool sendHaltZoom(void);
00442   bool sendRealPanTiltRequest(void);
00443   bool sendRealZoomRequest(void);
00444   bool sendDigitalZoom(void);
00445   bool sendFocus(void);
00446   
00447   // this is currently not used because it doesn't work right
00448   bool sendProductNameRequest(void);
00449 
00450   // the camera type is used to specify VC-C4 vs. C50i
00451   CameraType myCameraType;
00452   bool myRequestProductName;
00453 
00454   bool sendLEDControlMode(void);
00455   bool sendCameraNameRequest(void);
00456   int myDesiredLEDControlMode;
00457 
00458   bool sendIRFilterControl(void);
00459   bool sendIRLEDControl(void);
00460   bool myIRLEDsEnabled;
00461   bool myDesiredIRLEDsMode;
00462   bool myIRFilterModeEnabled;
00463   bool myDesiredIRFilterMode;
00464 
00465   // These should only be used by the state machine to initialize the 
00466   // camera for the first time
00467   bool setDefaultRange(void);
00468   bool setControlMode(void);
00469   bool sendInit(void);
00470 
00471   // process the packet data for a camera response that has accurate
00472   // pan/tilt positional information in it, and the product name
00473   void processGetPanTiltResponse(void);
00474   void processGetZoomResponse(void);
00475   void processGetProductNameResponse(void);
00476 
00477   // true if autoupdating of camera's position should be used  
00478   bool myAutoUpdate;
00479 
00480   // cycle for stepping through various autoupdate resquests from the camera
00481   int myAutoUpdateCycle;
00482 
00483   // returns true if there is no reponse to a packet within the timeout
00484   // or also if the state times out.  The argument will overrid the default
00485   // timeout periods
00486   bool timeout(int mSec = 0);
00487 
00488   // internal reperesenstation of pan, tilt, and zoom positions
00489   double myPan;
00490   double myTilt;
00491   int myZoom;
00492   int myDigitalZoom;
00493   int myFocusMode;
00494 
00495   // used to store the returned positional values when requesting the true
00496   // position from the camera
00497   double myPanResponse;
00498   double myTiltResponse;
00499   int myZoomResponse;
00500 
00501   // the returned product name
00502   char myProductNameResponse[4];
00503 
00504   // the positions that were last sent to the camera.  These are needed
00505   // because the desired positions can change between time a command is
00506   // sent and before it succeeds.
00507   double myPanSent;
00508   double myTiltSent;
00509   int myZoomSent;
00510   double myPanSlewSent;
00511   double myTiltSlewSent;
00512 
00513   // internal representation of pan and tilt slew
00514   double myPanSlew;
00515   double myTiltSlew;
00516 
00517   // where the user has requested the camera move to
00518   double myPanDesired;
00519   double myTiltDesired;
00520   int myZoomDesired;
00521   int myDigitalZoomDesired;
00522   int myFocusModeDesired;
00523 
00524   // the pan an tilt slew that the user requested
00525   double myPanSlewDesired;
00526   double myTiltSlewDesired;
00527 
00528   // internal mirror of camera power state, and whether it's be initted
00529   bool myPowerState;
00530   bool myCameraIsInitted;
00531 
00532   // whether the user wants the camera on or off, or initialized
00533   bool myPowerStateDesired;
00534   bool myInitRequested;
00535 
00536   // whether the user has requested to halt movement
00537   bool myHaltZoomRequested;
00538   bool myHaltPanTiltRequested;
00539 
00540   // whether the camera has been initialized since instance inception
00541   bool myCameraHasBeenInitted;
00542 
00543   // true if the user has requested to update the camera's postion
00544   // from the data returned from the camera
00545   bool myRealPanTiltRequested;
00546   bool myRealZoomRequested;
00547 
00548   // the error state from the last packet received
00549   unsigned int myError;
00550 
00551   // our FOV numbers (these should change if we use the digital zoom)
00552   double myFOVAtMaxZoom;
00553   double myFOVAtMinZoom;
00554 
00555   // run through the list or error callbacks
00556   void throwError();
00557 
00558   // the list of error callbacks to step through when a error occurs
00559   std::list<ArFunctor *> myErrorCBList;
00560 };
00561 
00562 #endif // ARVCC4_H
00563 

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