00001
00002
00004
00005 #ifndef BHAND_SUPERVISORY_REALTIME_H
00006 #define BHAND_SUPERVISORY_REALTIME_H
00007
00036 #include "BHandDriver.h"
00037 #include "BHandHardware.h"
00038 #include "BHandCommands.h"
00039
00040 #include "bhand_motors.h"
00041
00042
00043 #define POCO_NO_UNWINDOWS
00044
00045
00046 #include "Poco/Thread.h"
00047 #include "Poco/Runnable.h"
00048 #include "Poco/Mutex.h"
00049 #include "Poco/Event.h"
00050
00051 using Poco::Thread;
00052 using Poco::Runnable;
00053 using Poco::Event;
00054
00055
00056
00057
00058
00059
00061
00062 enum BHRequest {
00063 BHREQ_EXIT = 1,
00064 BHREQ_REALTIME,
00065 BHREQ_SUPERVISE,
00066 BHREQ_CLEAR
00067 };
00068
00070
00071 enum BHSyncMode {
00072 BHMODE_SYNC = 1,
00073 BHMODE_ASYNCNOW,
00074 BHMODE_ASYNCWAIT,
00075 BHMODE_RETURN
00076 };
00077
00079
00080 enum BHAPIErrorCode {
00081 BHERR_BHANDEXISTS = -1,
00082 BHERR_OPENCOMMPORT = -2,
00083 BHERR_GETCOMMSTATE = -3,
00084 BHERR_SETCOMMSTATE = -4,
00085 BHERR_SETCOMMTIMEOUT = -5,
00086 BHERR_THREADSTART = -6,
00087 BHERR_THREADPRIORITY = -7,
00088 BHERR_WRITECOM = -8,
00089 BHERR_READCOM = -9,
00090 BHERR_BADRESPONSE = -10,
00091 BHERR_CLEARBUFFER = -11,
00092 BHERR_TIMEOUT = -12,
00093 BHERR_NOTCOMPLETED = -13,
00094 BHERR_PENDING = -14,
00095 BHERR_NOTINITIALIZED = -15,
00096 BHERR_BADPARAMETER = -16,
00097 BHERR_LONGSTRING = -17,
00098 BHERR_OUTOFRANGE = -18,
00099 BHERR_MOTORINACTIVE = -19,
00100 BHERR_PORTOUTOFRANGE = -20
00101 };
00102
00103
00111 class BHandSupervisoryRealtime : public Runnable
00112 {
00113 public:
00114
00116
00118
00119 BHandSupervisoryRealtime(BHand *bhand, int priority = THREAD_PRIORITY_TIME_CRITICAL);
00120 ~BHandSupervisoryRealtime();
00121
00122
00124
00126
00127 int InitSoftware(int port, int priority = THREAD_PRIORITY_TIME_CRITICAL);
00128 int Init(int port, int priority, BHCommunication comm, bool async = false);
00129 bool IsInitialized() { return driverIsOpen(); }
00130
00131
00133
00135
00136 int ComRequest(int requestNumber);
00137 int ComWaitForCompletion(unsigned int timeout);
00138 bool ComIsPending();
00139
00140 int ComGetError();
00141 BHandSupervisoryResult * GetResult();
00142
00143
00145
00147
00148 #ifdef BH8_262_HARDWARE
00149
00150 int ComInitialize(int comport, int priority = THREAD_PRIORITY_TIME_CRITICAL);
00151 int ComOpen(int comport, int baudrate = 9600);
00152 bool ComIsOpen();
00153 void ComClose();
00154
00155 int ComSetBaudrate(int baud);
00156
00157 int ComSetTimeouts(unsigned int readInterval,
00158 unsigned int readMultiplier, unsigned int readConstant,
00159 unsigned int writeMultiplier, unsigned int writeConstant);
00160 int ComSetTimeouts(unsigned int readMultiplier, unsigned int readConstant,
00161 unsigned int writeMultiplier, unsigned int writeConstant);
00162
00163 bool ComClear(bool rxOnly = false);
00164 int ComRead(char *rxBuf, int rxNumBytes);
00165 int ComWrite(const char *txBuf, int txNumBytes);
00166
00167 #endif
00168
00169 void SetWaitCallbackFunc(BHCallback waitCallbackFunc);
00170
00172
00174
00175 int InitHand(const char *motor);
00176 int Reset(bool *responseReceived = 0);
00177
00179
00181
00182 int Close(const char *motor);
00183 int Open(const char *motor);
00184
00185 int GoToDefault(const char *motor);
00186 int GoToDifferentPositions(int value1, int value2, int value3, int value4);
00187 int GoToHome(const char *motor = "");
00188 int GoToPosition(const char *motor, int value);
00189
00190 int StepClose(const char *motor);
00191 int StepClose(const char *motor, int stepAmount);
00192 int StepOpen(const char *motor);
00193 int StepOpen(const char *motor, int stepAmount);
00194
00195 int TorqueClose(const char *motor);
00196 int TorqueOpen(const char *motor);
00197
00199
00201
00202 int Get(const char *motor, const char *propertyName, int *result);
00203 int Set(const char *motor, const char *propertyName, int value);
00204
00205 int PGet(const char *propertyName, int *result);
00206 int PSet(const char *propertyName, int value);
00207
00208 int Default(const char *motor);
00209 int Load(const char *motor);
00210 int Save(const char *motor);
00211
00212 int Temperature(int *result);
00213
00215
00217
00218 int Command(const char *send, char *receive = 0);
00219
00220 int Delay(unsigned int msec);
00221
00222 int StopMotor(const char *motor);
00223
00224 #ifdef BH8_262_HARDWARE
00225
00226 const char * Response();
00227 const char * Buffer();
00228 #endif
00229
00231
00233
00234 int Baud(unsigned int baud);
00235
00236
00238
00240
00241 void setSyncMode(BHSyncMode syncMode) { this->syncMode = syncMode; }
00242 BHSyncMode getSyncMode() { return (BHSyncMode)syncMode; }
00243
00244 void setRequestTimeout(unsigned int timeout) { requestTimeout = timeout; }
00245 unsigned int getRequestTimeout() { return requestTimeout; }
00246
00247 void setpCallback(BHCallback funcPtr) { pCallback = funcPtr; }
00248 BHCallback getpCallback() { return pCallback; }
00249
00250
00252
00254
00255 int RTStart(const char *motor, BHMotorProtection motorProtection = BHMotorTSTOPProtect);
00256 int RTUpdate(bool control = true, bool feedback = true);
00257 int RTAbort();
00258
00260
00262
00263 int RTSetFlags(const char *motor, bool LCV, int LCVC, bool LCPG,
00264 bool LFV, int LFVC, bool LFS, bool LFAP, bool LFDP, int LFDPC);
00265 int RTSetFlags(const char *motor, bool LCV, int LCVC, bool LCPG, bool LCT,
00266 bool LFV, int LFVC, bool LFS, bool LFAP, bool LFDP, int LFDPC,
00267 bool LFBP, bool LFAIN, bool LFDPD, bool LFT);
00268
00269 int RTUpdate(const char *motor, const char *property, int *values);
00270
00272
00274
00275 int RTSetVelocity(const char motor, int velocity);
00276 int RTSetGain(const char motor, int gain);
00277 int RTSetTorque(const char motor, int torque);
00278 int RTSetPosition(const char motor, int position);
00279
00281
00283
00284 char RTGetVelocity(const char motor);
00285 unsigned char RTGetStrain(const char motor);
00286 int RTGetPosition(const char motor);
00287 char RTGetDeltaPos(const char motor);
00288 int RTGetBreakawayPosition(const char motor);
00289 int RTGetTemp();
00290 unsigned char RTGetAIN(const char motor);
00291 void RTGetPPS(const char motor, int *pps, int ppsElements);
00292
00293
00294
00295
00296 #ifdef BHAND_SUPERVISORY_PRIVATE_VARIABLES
00297 protected:
00298 #endif
00299
00301
00303
00325 int syncMode;
00326
00336 unsigned int requestTimeout;
00337
00366 BHCallback pCallback;
00367
00368 protected:
00369
00370 BHand * getBHand() { return m_bhand; }
00371
00372 private:
00373
00375
00377
00378 void run();
00379
00380
00382
00384
00385 bool driverIsOpen();
00386 bool serialDriverIsOpen();
00387
00388 void createThread(int priority);
00389
00390
00392
00394
00395 int validate(BHMotors motors, const char *propertyName, int value);
00396 int validate(const char *propertyName, int value);
00397
00398
00400
00402
00403 bool m_initialized;
00404
00405 BHand *m_bhand;
00406 BHandDriver *m_driver;
00407
00408 Thread m_thread;
00409
00410 Event m_requestPending;
00411 Event m_requestComplete;
00412
00413 int m_request;
00414 int m_comErr;
00415
00416 };
00417
00418 #endif