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

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

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