/** * Glove.h - DG5Glove Interface * CSE 490I Neurobotics Winter 2007 * Josh Storz */ #include #include #include #include #include "DG5GloveLib.h" #ifndef _GLOVE_H_ #define _GLOVE_H_ class Glove { public: Glove(); ~Glove(); // right or left handed glove static const int GLOVE_RIGHT = 1; static const int GLOVE_LEFT = 2; // finger indexes static const int FINGER_THUMB = 4; static const int FINGER_INDEX = 3; static const int FINGER_MIDDLE = 2; static const int FINGER_RING = 1; static const int FINGER_PINKY = 0; // button indexes static const int BUTTON_UP = 2; static const int BUTTON_ENTER = 1; static const int BUTTON_DOWN = 0; // glove dataframe struct GloveData { double finger_pos[5]; bool button_state[3]; // constructor, initial state GloveData() { for (int i = 0; i < 5; i++) finger_pos[i] = 0; for (int i = 0; i < 3; i++) button_state[i] = false; } }; // glove finger calibration struct GloveCalib { double min[5]; double max[5]; double offset[5]; GloveCalib() { // sane defaults for (int i = 0; i < 5; i++) { min[i] = -1; // calibration minimum bounds max[i] = 255; // calibration maximum bounds offset[i] = 0; // calibration offset (before scaling) } } }; // connection methods bool open(); bool close(); bool isConnected() { return (hcomm != NULL); } // settings void setComm(int port); int getComm(); void setCalib(Glove::GloveCalib calib); Glove::GloveCalib getCalib(); void setType(int type); int getType(); // data bool getState(Glove::GloveData *frame); // interactive methods bool calibrate(Glove::GloveCalib *calib); protected: // hardware level commands static const int CMD_LOW_PASS_OFF = 251; static const int CMD_LOW_PASS_ON = 252; static const int CMD_START = 253; static const int CMD_STOP = 254; static const int CMD_VERSION = 255; // message length (response from glove interface) static const int BLOCK_LENGTH = 60; // bounds on dataframe normalization static const int FINGER_MIN = 0; static const int FINGER_MAX = 255; // instance variables HINSTANCE glove; // dll HANDLE hcomm; // comm handle int comm_port; // comm port # GLOVE_STATE state; // dll state response GLOVE_SETTINGS settings; // dll settings structure unsigned char verify_code; // unused int glove_type; // right or left GloveCalib calib; // calibration (raw scaling and shifting) BYTE buffer[100]; // data input buffer // helper methods bool libLoaded() { return (glove != NULL); } // check if dll is loaded double normalize( int finger ) {return min(calib.max[finger], max(state.Finger[finger], calib.min[finger]) ); } // double normalize(int finger) { return min(FINGER_MAX, max(FINGER_MIN,(int)(((state.Finger[finger] + calib.offset[finger]) / (calib.max[finger] - calib.min[finger])) * (FINGER_MAX-FINGER_MIN)))); } // normalize input bool sendCmd(int cmd) { if (!libLoaded() || !isConnected()) return false; else return (bool)VHand_SendGloveCommand(hcomm, (BYTE)cmd); } // send hardware command }; #endif