Libbarrett  1.2.4
include/barrett/products/puck.h
00001 
00026 /*
00027  * @file puck.h
00028  * @date 08/20/2010
00029  * @author Dan Cody
00030  *  
00031  */
00032 
00033 #ifndef BARRETT_PRODUCTS_PUCK_H_
00034 #define BARRETT_PRODUCTS_PUCK_H_
00035 
00036 
00037 #include <stdexcept>
00038 #include <vector>
00039 
00040 #include <barrett/bus/abstract/communications_bus.h>
00041 
00042 
00043 namespace barrett {
00044 
00045 
00046 class Puck {
00047 
00048 public:
00049         enum RoleOption {
00050                 RO_MagEncOnSerial = 0x0100,
00051                 RO_MagEncOnHall = 0x0200,
00052                 RO_MagEncOnEnc = 0x400,
00053                 RO_Strain = 0x0800,
00054                 RO_Tact = 0x1000,
00055                 RO_IMU = 0x2000,
00056                 RO_OpticalEncOnEnc = 0x4000
00057         };
00058         enum PuckType {
00059                 PT_Monitor, PT_Safety, PT_Motor, PT_ForceTorque, PT_Unknown
00060         };
00061 
00062         static const char* getPuckTypeStr(enum PuckType pt) {
00063                 return puckTypeStrs[pt];
00064         }
00065 
00066 // include the generated file containing the list of available properties
00067 #       include <barrett/products/detail/property_list.h>
00068         static const char* getPropertyStr(enum Property prop);
00069         static enum Property getPropertyEnum(const char* str) throw(std::invalid_argument);
00070         static enum Property getPropertyEnumNoThrow(const char* str);
00071 
00072 
00073 public:
00074         Puck(const bus::CommunicationsBus& bus, int id);
00075         ~Puck();
00076 
00077         void wake();
00078 
00079         int getProperty(enum Property prop, bool realtime = false) const;
00080         template<typename Parser> void getProperty(enum Property prop,
00081                         typename Parser::result_type* result, bool realtime = false) const;
00082         void setProperty(enum Property prop, int value, bool blocking = false) const {
00083                 setProperty(bus, id, getPropertyId(prop), value, blocking);
00084         }
00085 
00086         void saveProperty(enum Property prop) const;
00087         void saveAllProperties() const;
00088         void resetProperty(enum Property prop) const;
00089 
00090         bool respondsToProperty(enum Property prop) const {
00091                 return respondsToProperty(prop, effectiveType, vers);
00092         }
00093         int getPropertyId(enum Property prop) const throw(std::runtime_error) {
00094                 return getPropertyId(prop, effectiveType, vers);
00095         }
00096         int getPropertyIdNoThrow(enum Property prop) const {
00097                 return getPropertyIdNoThrow(prop, effectiveType, vers);
00098         }
00099 
00100         void updateRole();
00101         void updateStatus();
00102 
00103         const bus::CommunicationsBus& getBus() const { return bus; }
00104         int getId() const { return id; }
00105         int getVers() const { return vers; }
00106         int getRole() const { return role; }
00107         bool hasOption(enum RoleOption ro) const { return role & ro; }
00108         enum PuckType getType() const { return type; }
00109         enum PuckType getEffectiveType() const { return effectiveType; }
00110 
00111 
00112         static void wake(std::vector<Puck*> pucks);
00113 
00114         static int getProperty(const bus::CommunicationsBus& bus, int id, int propId, bool realtime = false);
00115         template<typename Parser> static void getProperty(
00116                         const bus::CommunicationsBus& bus, int id, int propId, typename Parser::result_type* result, bool realtime = false);
00117         static int tryGetProperty(const bus::CommunicationsBus& bus, int id, int propId,
00118                         int* result, double timeout_s = 0.001);
00119         template<typename Parser> static int tryGetProperty(
00120                         const bus::CommunicationsBus& bus, int id, int propId, typename Parser::result_type* result,
00121                         double timeout_s = 0.001);
00122         static void setProperty(const bus::CommunicationsBus& bus, int id, int propId,
00123                         int value, bool blocking = false);
00124 
00125         static int sendGetPropertyRequest(const bus::CommunicationsBus& bus, int id, int propId);
00126         static int receiveGetPropertyReply(const bus::CommunicationsBus& bus, int id, int propId,
00127                         int* result, bool blocking, bool realtime);
00128         template<typename Parser> static int receiveGetPropertyReply(
00129                         const bus::CommunicationsBus& bus, int id, int propId, typename Parser::result_type* result, bool blocking, bool realtime);
00130 
00131         static bool respondsToProperty(enum Property prop, enum PuckType pt, int fwVers) {
00132                 return getPropertyIdNoThrow(prop, pt, fwVers) != -1;
00133         }
00134         static int getPropertyId(enum Property prop, enum PuckType pt, int fwVers)
00135                         throw(std::runtime_error);
00136         static int getPropertyIdNoThrow(enum Property prop, enum PuckType pt, int fwVers);
00137 
00138 
00139         static const int DEFAULT_IPNM = 2700;
00140 
00141         static const int MIN_ID = 1;
00142         static const int MAX_ID = 31;
00143         static const int HOST_ID = 0;  // the Node ID of the control PC
00144         static const int NODE_ID_WIDTH = 5;
00145         static const int NODE_ID_MASK = 0x1f;
00146 
00147         static int nodeId2BusId(int id) {
00148                 return (id & TO_MASK) | (HOST_ID << NODE_ID_WIDTH);
00149         }
00150         static int busId2NodeId(int busId) {
00151                 return (busId & FROM_MASK) >> NODE_ID_WIDTH;
00152         }
00153         static int encodeBusId(int fromId, int toId) {
00154                 return (toId & TO_MASK) | ((fromId & NODE_ID_MASK) << NODE_ID_WIDTH);
00155         }
00156         static void decodeBusId(int busId, int* fromId, int* toId) {
00157                 *fromId = (busId & FROM_MASK) >> NODE_ID_WIDTH;
00158                 *toId = busId & TO_MASK;
00159         }
00160 
00161         static const int GROUP_MASK = 0x400;
00162         static const int FROM_MASK = 0x3e0;
00163         static const int TO_MASK = 0x41f;
00164 
00165         static const int SET_MASK = 0x80;
00166         static const int PROPERTY_MASK = 0x7f;
00167 
00168         static const double WAKE_UP_TIME = 1.0;  // seconds
00169         static const double TURN_OFF_TIME = 0.01;  // seconds
00170 
00171         struct StandardParser {
00172                 static int busId(int id, int propId);
00173 
00174                 typedef int result_type;
00175                 static int parse(int id, int propId, result_type* result, const unsigned char* data, size_t len);
00176         };
00177 
00178 
00179 protected:
00180         // From puck2:PARSE.H
00181         enum {
00182                 STATUS_RESET, STATUS_ERR, STATUS_READY
00183         };
00184 
00185         static const int ROLE_MASK = 0x1f;
00186         enum {
00187                 ROLE_TATER,
00188                 ROLE_GIMBALS,
00189                 ROLE_SAFETY,
00190                 ROLE_WRAPTOR,
00191                 ROLE_TRIGGER,
00192                 ROLE_BHAND,
00193                 ROLE_FORCE
00194         };
00195 
00196 
00197         const bus::CommunicationsBus& bus;
00198         int id;
00199         int vers, role;
00200         enum PuckType type, effectiveType;
00201 
00202 private:
00203         template<typename Parser>
00204         static int getPropertyHelper(const bus::CommunicationsBus& bus,
00205                         int id, int propId, typename Parser::result_type* result, bool blocking, bool realtime, double timeout_s);
00206 
00207         static const char puckTypeStrs[][12];
00208 };
00209 
00210 
00211 }
00212 
00213 
00214 // include template definitions
00215 #include <barrett/products/detail/puck-inl.h>
00216 
00217 
00218 #endif /* BARRETT_PRODUCTS_PUCK_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines