dsa.h
Go to the documentation of this file.
00001 //======================================================================
00027 //======================================================================
00028 
00029 #ifndef DSA_h_
00030 #define DSA_h_
00031 
00032 #include "sdhlibrary_settings.h"
00033 
00034 //----------------------------------------------------------------------
00035 // System Includes - include with <>
00036 //----------------------------------------------------------------------
00037 
00038 #include <assert.h>
00039 
00040 //----------------------------------------------------------------------
00041 // Project Includes - include with ""
00042 //----------------------------------------------------------------------
00043 
00044 #include "sdhexception.h"
00045 
00046 #include "dbg.h"
00047 #if SDH_USE_VCC
00048 # include "rs232-vcc.h"
00049 #else
00050 # include "rs232-cygwin.h"
00051 #endif
00052 #include "simpletime.h"
00053 #include "crc.h"
00054 
00055 //----------------------------------------------------------------------
00056 // Defines, enums, unions, structs
00057 //----------------------------------------------------------------------
00058 
00059 NAMESPACE_SDH_START
00060 
00061 #define DSA_MAX_PREAMBLE_SEARCH (2*3*(6*(14+13)) + 16)
00062 
00063 //----------------------------------------------------------------------
00064 // Global variables (declarations)
00065 //----------------------------------------------------------------------
00066 
00067 
00068 //----------------------------------------------------------------------
00069 // External functions (function declarations)
00070 //----------------------------------------------------------------------
00071 
00072 
00073 //----------------------------------------------------------------------
00074 // Function prototypes (function declarations)
00075 //----------------------------------------------------------------------
00076 
00077 
00078 //----------------------------------------------------------------------
00079 // Class declarations
00080 //----------------------------------------------------------------------
00081 
00082 
00083 
00084 
00088 class VCC_EXPORT cDSAException: public cSDHLibraryException
00089 {
00090 public:
00091   cDSAException( cMsg const & _msg )
00092     : cSDHLibraryException( "cDSAException", _msg )
00093   {}
00094 };
00095 //======================================================================
00096 
00098 
00111 class VCC_EXPORT cDSA
00112 {
00113  public:
00115     typedef UInt16 tTexel;
00116 
00118     enum eDSAErrorCode
00119     {
00120         E_SUCCESS,
00121         E_NOT_AVAILABLE,
00122         E_NO_SENSOR,
00123         E_NOT_INITIALIZED,
00124         E_ALREADY_RUNNING,
00125         E_FEATURE_NOT_SUPPORTED,
00126         E_INCONSISTENT_DATA,
00127         E_TIMEOUT,
00128         E_READ_ERROR,
00129         E_WRITE_ERROR,
00130         E_INSUFFICIENT_RESOURCES,
00131         E_CHECKSUM_ERROR,
00132         E_CMD_NOT_ENOUGH_PARAMS,
00133         E_CMD_UNKNOWN,
00134         E_CMD_FORMAT_ERROR,
00135         E_ACCESS_DENIED,
00136         E_ALREADY_OPEN,
00137         E_CMD_FAILED,
00138         E_CMD_ABORTED,
00139         E_INVALID_HANDLE,
00140         E_DEVICE_NOT_FOUND,
00141         E_DEVICE_NOT_OPENED,
00142         E_IO_ERROR,
00143         E_INVALID_PARAMETER,
00144         E_INDEX_OUT_OF_BOUNDS,
00145         E_CMD_PENDING,
00146         E_OVERRUN,
00147         E_RANGE_ERROR
00148     };
00149 
00150 
00152 #if SDH_USE_VCC
00153 #pragma pack(push,1)  // for VCC (MS Visual Studio) we have to set the necessary 1 byte packing with this pragma
00154 #endif
00155     struct sControllerInfo
00156     {
00157         UInt16 error_code;
00158         UInt32 serial_no;
00159         UInt8  hw_version;
00160         UInt16 sw_version;
00161         UInt8  status_flags;
00162         UInt8  feature_flags;
00163         UInt8  senscon_type;
00164         UInt8  active_interface;
00165         UInt32 can_baudrate;
00166         UInt16 can_id;
00167     }   SDH__attribute__((__packed__)); // for gcc we have to set the necessary 1 byte packing with this attribute
00168 
00170     struct sSensorInfo
00171     {
00172         UInt16 error_code;
00173         UInt16 nb_matrices;
00174         UInt16 generated_by;
00175         UInt8  hw_revision;
00176         UInt32 serial_no;
00177         UInt8  feature_flags;
00178     } SDH__attribute__((__packed__)); // for gcc we have to set the necessary 1 byte packing with this attribute
00179 
00181     struct sMatrixInfo
00182     {
00183         UInt16 error_code;
00184         float  texel_width;
00185         float  texel_height;
00186         UInt16 cells_x;
00187         UInt16 cells_y;
00188         UInt8  uid[6];
00189         UInt8  reserved[2];
00190         UInt8  hw_revision;
00191 
00192         float  matrix_center_x;
00193         float  matrix_center_y;
00194         float  matrix_center_z;
00195 
00196         float  matrix_theta_x;
00197         float  matrix_theta_y;
00198         float  matrix_theta_z;
00199         float  fullscale;
00200         UInt8  feature_flags;
00201     } SDH__attribute__((__packed__)); // for gcc we have to set the necessary 1 byte packing with this attribute
00202 
00204     struct sSensitivityInfo
00205     {
00206         UInt16 error_code;  
00207         UInt8  adj_flags;   
00208 
00209 
00210 
00211         float  cur_sens;    
00212 
00213         float  fact_sens;   
00214 
00215 
00216     } SDH__attribute__((__packed__)); // for gcc we have to set the necessary 1 byte packing with this attribute
00217 
00218 #if SDH_USE_VCC
00219 #pragma pack(pop)   // for VCC (MS Visual Studio) restore normal packing
00220 #endif
00221 
00222 
00235     struct VCC_EXPORT sTactileSensorFrame
00236     {
00237         UInt32  timestamp;  
00238         UInt8   flags;      
00239         tTexel* texel;      
00240 
00242         sTactileSensorFrame( void )
00243         {
00244             texel = NULL;
00245         }
00246     };
00247 
00249     struct sContactInfo
00250     {
00251         double force;  
00252         double area;   
00253         double cog_x;  
00254         double cog_y;  
00255     };
00256 
00257 
00258  protected:
00259 
00261 #if SDH_USE_VCC
00262 #pragma pack(push,1)    // for VCC (MS Visual Studio) we have to set the necessary 1 byte packing with this pragma
00263 #endif
00264      struct sResponse {
00265         UInt8   packet_id;
00266         UInt16  size;
00267         UInt8*  payload;
00268         Int32   max_payload_size;
00269 
00271         sResponse( UInt8* _payload, int _max_payload_size )
00272         {
00273             payload = _payload;
00274             max_payload_size = _max_payload_size;
00275         }
00276     } SDH__attribute__((__packed__));  // for gcc we have to set the necessary 1 byte packing with this attribute
00277 #if SDH_USE_VCC
00278 #pragma pack(pop)   // for VCC (MS Visual Studio) restore normal packing
00279 #endif
00280 
00281     friend VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA::sResponse const &response );
00282 
00284     cDBG dbg;
00285 
00287     cRS232 comm_interface;
00288 
00290     bool do_RLE;
00291 
00293     sControllerInfo controller_info;
00294 
00296     sSensorInfo     sensor_info;
00297 
00299     sMatrixInfo*    matrix_info;
00300 
00302     sTactileSensorFrame frame;
00303 
00305     int nb_cells;
00306 
00308     int* texel_offset;
00309 
00311     long read_timeout_us;
00312 
00313     cSimpleTime start_pc;
00314     UInt32      start_dsa;
00315 
00317     tTexel contact_area_cell_threshold;
00318 
00320     tTexel contact_force_cell_threshold;
00321 
00323     double force_factor;
00324 
00325 
00329     double calib_pressure;  // N/(mm*mm)
00331     double calib_voltage;   // "what the DSA reports:" ~mV
00332 
00333 
00334     void WriteCommandWithPayload( UInt8 command, UInt8* payload, UInt16 payload_len )
00335     throw (cDSAException*, cSDHErrorCommunication*);
00336 
00337     inline void WriteCommand( UInt8 command )
00338     {
00339         WriteCommandWithPayload( command, NULL, 0 );
00340     }
00341     //----------------------------------------------------------------------
00342 
00353     void ReadResponse( sResponse* response, UInt8 command_id )
00354     throw (cDSAException*, cSDHErrorCommunication*);
00355 
00359     void ReadControllerInfo( sControllerInfo* _controller_info )
00360     throw (cDSAException*, cSDHErrorCommunication*);
00361 
00362 
00366     void ReadSensorInfo( sSensorInfo* _sensor_info )
00367     throw (cDSAException*, cSDHErrorCommunication*);
00368 
00369 
00373     void ReadMatrixInfo( sMatrixInfo* _matrix_info  )
00374     throw (cDSAException*, cSDHErrorCommunication*);
00375 
00376 
00380     void ReadFrame( sTactileSensorFrame* frame_p  )
00381     throw (cDSAException*, cSDHErrorCommunication*);
00382 
00383 
00388     void QueryControllerInfo( sControllerInfo* _controller_info  )
00389     throw (cDSAException*, cSDHErrorCommunication*);
00390 
00391 
00396     void QuerySensorInfo( sSensorInfo* _sensor_info )
00397     throw (cDSAException*, cSDHErrorCommunication*);
00398 
00399 
00404     void QueryMatrixInfo( sMatrixInfo* _matrix_info, int matrix_no )
00405     throw (cDSAException*, cSDHErrorCommunication*);
00406 
00407 
00411     void QueryMatrixInfos( void )
00412     throw (cDSAException*, cSDHErrorCommunication*);
00413 
00414 
00418     void ParseFrame( sResponse* response, sTactileSensorFrame* frame_p )
00419     throw (cDSAException*);
00420 
00421 
00422  public:
00442     cDSA( int debug_level=0, int port=1, char const* device_format_string="/dev/ttyS%d" );
00443 
00444 
00446     ~cDSA();
00447 
00448 
00450     inline sControllerInfo const & GetControllerInfo( void ) const
00451     {
00452         return controller_info;
00453     }
00454     //-----------------------------------------------------------------
00455 
00457     inline sSensorInfo const & GetSensorInfo( void ) const
00458     {
00459         return sensor_info;
00460     }
00461     //-----------------------------------------------------------------
00462 
00464     inline sMatrixInfo const & GetMatrixInfo( int m ) const
00465     {
00466         assert( 0 <= m  &&  m <= (int ) sensor_info.nb_matrices );
00467         return matrix_info[m];
00468     }
00469     //-----------------------------------------------------------------
00470 
00472     inline sTactileSensorFrame const & GetFrame() const
00473     {
00474         return frame;
00475     }
00476     //-----------------------------------------------------------------
00477 
00479     inline sTactileSensorFrame const & UpdateFrame()
00480     {
00481         ReadFrame( &frame );
00482         return frame;
00483     }
00484     //-----------------------------------------------------------------
00485 
00487     void Open(void)
00488     throw (cDSAException*, cSDHErrorCommunication*);
00489 
00491     void Close(void)
00492     throw (cDSAException*, cSDHErrorCommunication*);
00493 
00494 
00523     void SetFramerate( UInt16 framerate, bool do_RLE=true, bool do_data_acquisition=true )
00524     throw (cDSAException*, cSDHErrorCommunication*);
00525 
00538     void SetFramerateRetries( UInt16 framerate, bool do_RLE=true, bool do_data_acquisition=true, unsigned int retries=0, bool ignore_exceptions=false )
00539     throw (cDSAException*, cSDHErrorCommunication*);
00540 
00541 
00556     sSensitivityInfo GetMatrixSensitivity( int matrix_no )
00557     throw (cDSAException*, cSDHErrorCommunication*);
00558 
00559 
00576     void SetMatrixSensitivity( int matrix_no,
00577                                double sensitivity,
00578                                bool do_all_matrices=false,
00579                                bool do_reset=false,
00580                                bool do_persistent=false )
00581     throw (cDSAException*, cSDHErrorCommunication*);
00582 
00583 
00600     void SetMatrixThreshold( int matrix_no,
00601                              UInt16 threshold,
00602                              bool do_all_matrices=false,
00603                              bool do_reset=false,
00604                              bool do_persistent=false )
00605     throw (cDSAException*, cSDHErrorCommunication*);
00606 
00616     UInt16 GetMatrixThreshold( int matrix_no )
00617     throw (cDSAException*, cSDHErrorCommunication*);
00618 
00622     tTexel GetTexel( int m, int x, int y ) const;
00623 
00624 
00625 
00629     inline int GetMatrixIndex( int fi, int part )
00630     {
00631         return fi * 2 + part;
00632     }
00633 
00634 
00638     inline unsigned long GetAgeOfFrame( sTactileSensorFrame* frame_p )
00639     {
00640         return ((unsigned long) (start_pc.Elapsed()*1000.0)) - (frame_p->timestamp - start_dsa);
00641     }
00642 
00643     double GetContactArea( int m );
00644 
00645  private:
00646     double VoltageToPressure( double voltage );
00647 
00648     void ReadAndCheckErrorResponse( char const* msg, UInt8 command_id )
00649     throw (cDSAException*, cSDHErrorCommunication*);
00650 
00651 
00652  public:
00653     sContactInfo GetContactInfo( int m );
00654 
00655 
00662     static char const* ErrorCodeToString( eDSAErrorCode error_code );
00663     static char const* ErrorCodeToString( UInt16 error_code )
00664     {
00665         return ErrorCodeToString( (eDSAErrorCode) error_code );
00666     }
00667 
00668  private:
00670      bool acquiring_single_frame;
00671 
00683      void FlushInput( long timeout_us_first, long timeout_us_subsequent );
00684 }; // end of class cDSA
00685 //----------------------------------------------------------------------
00686 //----------------------------------------------------------------------
00687 
00688 
00689 VCC_EXPORT std::ostream &operator<<( std::ostream &stream,  cDSA::sControllerInfo const &controller_info );
00690 
00691 
00692 VCC_EXPORT std::ostream &operator<<( std::ostream &stream,  cDSA::sSensorInfo const &sensor_info );
00693 
00694 
00695 VCC_EXPORT std::ostream &operator<<( std::ostream &stream,  cDSA::sMatrixInfo const &matrix_info );
00696 
00697 
00698 VCC_EXPORT std::ostream &operator<<( std::ostream &stream,  cDSA::sResponse const &response );
00699 
00700 
00701 VCC_EXPORT std::ostream &operator<<( std::ostream &stream,  cDSA const &dsa );
00702 
00703 
00704 NAMESPACE_SDH_END
00705 
00706 #endif
00707 
00708 
00709 //======================================================================
00710 /*
00711 {
00712   Here are some settings for the emacs/xemacs editor (and can be safely ignored):
00713   (e.g. to explicitely set C++ mode for *.h header files)
00714 
00715   Local Variables:
00716   mode:C
00717   mode:ELSE
00718   End:
00719 }
00720 */
00721 //======================================================================}


schunk_sdh
Author(s): Mathias Luedtke , Florian Weisshardt
autogenerated on Thu Aug 27 2015 15:07:03