00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00044
00045
00046 #ifndef DSA_h_
00047 #define DSA_h_
00048
00049 #include "sdhlibrary_settings.h"
00050
00051
00052
00053
00054
00055 #include <assert.h>
00056
00057
00058
00059
00060
00061 #include "sdhexception.h"
00062
00063 #include "dbg.h"
00064 #if SDH_USE_VCC
00065 # include "rs232-vcc.h"
00066 #else
00067 # include "rs232-cygwin.h"
00068 #endif
00069 #include "simpletime.h"
00070 #include "crc.h"
00071
00072
00073
00074
00075
00076 NAMESPACE_SDH_START
00077
00078 #define DSA_MAX_PREAMBLE_SEARCH (2*3*(6*(14+13)) + 16)
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00105 class VCC_EXPORT cDSAException: public cSDHLibraryException
00106 {
00107 public:
00108 cDSAException(cMsg const & _msg)
00109 : cSDHLibraryException("cDSAException", _msg)
00110 {}
00111 };
00112
00113
00115
00128 class VCC_EXPORT cDSA
00129 {
00130 public:
00132 typedef UInt16 tTexel;
00133
00135 enum eDSAErrorCode
00136 {
00137 E_SUCCESS,
00138 E_NOT_AVAILABLE,
00139 E_NO_SENSOR,
00140 E_NOT_INITIALIZED,
00141 E_ALREADY_RUNNING,
00142 E_FEATURE_NOT_SUPPORTED,
00143 E_INCONSISTENT_DATA,
00144 E_TIMEOUT,
00145 E_READ_ERROR,
00146 E_WRITE_ERROR,
00147 E_INSUFFICIENT_RESOURCES,
00148 E_CHECKSUM_ERROR,
00149 E_CMD_NOT_ENOUGH_PARAMS,
00150 E_CMD_UNKNOWN,
00151 E_CMD_FORMAT_ERROR,
00152 E_ACCESS_DENIED,
00153 E_ALREADY_OPEN,
00154 E_CMD_FAILED,
00155 E_CMD_ABORTED,
00156 E_INVALID_HANDLE,
00157 E_DEVICE_NOT_FOUND,
00158 E_DEVICE_NOT_OPENED,
00159 E_IO_ERROR,
00160 E_INVALID_PARAMETER,
00161 E_INDEX_OUT_OF_BOUNDS,
00162 E_CMD_PENDING,
00163 E_OVERRUN,
00164 E_RANGE_ERROR
00165 };
00166
00167
00169 #if SDH_USE_VCC
00170 #pragma pack(push,1) // for VCC (MS Visual Studio) we have to set the necessary 1 byte packing with this pragma
00171 #endif
00172 struct sControllerInfo
00173 {
00174 UInt16 error_code;
00175 UInt32 serial_no;
00176 UInt8 hw_version;
00177 UInt16 sw_version;
00178 UInt8 status_flags;
00179 UInt8 feature_flags;
00180 UInt8 senscon_type;
00181 UInt8 active_interface;
00182 UInt32 can_baudrate;
00183 UInt16 can_id;
00184 } SDH__attribute__((__packed__));
00185
00187 struct sSensorInfo
00188 {
00189 UInt16 error_code;
00190 UInt16 nb_matrices;
00191 UInt16 generated_by;
00192 UInt8 hw_revision;
00193 UInt32 serial_no;
00194 UInt8 feature_flags;
00195 } SDH__attribute__((__packed__));
00196
00198 struct sMatrixInfo
00199 {
00200 UInt16 error_code;
00201 float texel_width;
00202 float texel_height;
00203 UInt16 cells_x;
00204 UInt16 cells_y;
00205 UInt8 uid[6];
00206 UInt8 reserved[2];
00207 UInt8 hw_revision;
00208
00209 float matrix_center_x;
00210 float matrix_center_y;
00211 float matrix_center_z;
00212
00213 float matrix_theta_x;
00214 float matrix_theta_y;
00215 float matrix_theta_z;
00216 float fullscale;
00217 UInt8 feature_flags;
00218 } SDH__attribute__((__packed__));
00219
00221 struct sSensitivityInfo
00222 {
00223 UInt16 error_code;
00224 UInt8 adj_flags;
00225
00226
00227
00228 float cur_sens;
00229
00230 float fact_sens;
00231
00232
00233 } SDH__attribute__((__packed__));
00234
00235 #if SDH_USE_VCC
00236 #pragma pack(pop) // for VCC (MS Visual Studio) restore normal packing
00237 #endif
00238
00239
00252 struct VCC_EXPORT sTactileSensorFrame
00253 {
00254 UInt32 timestamp;
00255 UInt8 flags;
00256 tTexel* texel;
00257
00259 sTactileSensorFrame(void)
00260 {
00261 texel = NULL;
00262 }
00263 };
00264
00266 struct sContactInfo
00267 {
00268 double force;
00269 double area;
00270 double cog_x;
00271 double cog_y;
00272 };
00273
00274
00275 protected:
00276
00278 #if SDH_USE_VCC
00279 #pragma pack(push,1) // for VCC (MS Visual Studio) we have to set the necessary 1 byte packing with this pragma
00280 #endif
00281 struct sResponse
00282 {
00283 UInt8 packet_id;
00284 UInt16 size;
00285 UInt8* payload;
00286 Int32 max_payload_size;
00287
00289 sResponse(UInt8* _payload, int _max_payload_size)
00290 {
00291 payload = _payload;
00292 max_payload_size = _max_payload_size;
00293 }
00294 } SDH__attribute__((__packed__));
00295 #if SDH_USE_VCC
00296 #pragma pack(pop) // for VCC (MS Visual Studio) restore normal packing
00297 #endif
00298
00299 friend VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA::sResponse const &response);
00300
00302 cDBG dbg;
00303
00305 cRS232 comm_interface;
00306
00308 bool do_RLE;
00309
00311 sControllerInfo controller_info;
00312
00314 sSensorInfo sensor_info;
00315
00317 sMatrixInfo* matrix_info;
00318
00320 sTactileSensorFrame frame;
00321
00323 int nb_cells;
00324
00326 int* texel_offset;
00327
00329 long read_timeout_us;
00330
00331 cSimpleTime start_pc;
00332 UInt32 start_dsa;
00333
00335 tTexel contact_area_cell_threshold;
00336
00338 tTexel contact_force_cell_threshold;
00339
00341 double force_factor;
00342
00343
00347 double calib_pressure;
00349 double calib_voltage;
00350
00351
00352 void WriteCommandWithPayload(UInt8 command, UInt8* payload, UInt16 payload_len)
00353 throw (cDSAException*, cSDHErrorCommunication*);
00354
00355 inline void WriteCommand(UInt8 command)
00356 {
00357 WriteCommandWithPayload(command, NULL, 0);
00358 }
00359
00360
00371 void ReadResponse(sResponse* response, UInt8 command_id)
00372 throw (cDSAException*, cSDHErrorCommunication*);
00373
00377 void ReadControllerInfo(sControllerInfo* _controller_info)
00378 throw (cDSAException*, cSDHErrorCommunication*);
00379
00380
00384 void ReadSensorInfo(sSensorInfo* _sensor_info)
00385 throw (cDSAException*, cSDHErrorCommunication*);
00386
00387
00391 void ReadMatrixInfo(sMatrixInfo* _matrix_info)
00392 throw (cDSAException*, cSDHErrorCommunication*);
00393
00394
00398 void ReadFrame(sTactileSensorFrame* frame_p)
00399 throw (cDSAException*, cSDHErrorCommunication*);
00400
00401
00406 void QueryControllerInfo(sControllerInfo* _controller_info)
00407 throw (cDSAException*, cSDHErrorCommunication*);
00408
00409
00414 void QuerySensorInfo(sSensorInfo* _sensor_info)
00415 throw (cDSAException*, cSDHErrorCommunication*);
00416
00417
00422 void QueryMatrixInfo(sMatrixInfo* _matrix_info, int matrix_no)
00423 throw (cDSAException*, cSDHErrorCommunication*);
00424
00425
00429 void QueryMatrixInfos(void)
00430 throw (cDSAException*, cSDHErrorCommunication*);
00431
00432
00436 void ParseFrame(sResponse* response, sTactileSensorFrame* frame_p)
00437 throw (cDSAException*);
00438
00439
00440 public:
00460 cDSA(int debug_level = 0, int port = 1, char const* device_format_string = "/dev/ttyS%d");
00461
00462
00464 ~cDSA();
00465
00466
00468 inline sControllerInfo const & GetControllerInfo(void) const
00469 {
00470 return controller_info;
00471 }
00472
00473
00475 inline sSensorInfo const & GetSensorInfo(void) const
00476 {
00477 return sensor_info;
00478 }
00479
00480
00482 inline sMatrixInfo const & GetMatrixInfo(int m) const
00483 {
00484 assert(0 <= m && m <= (int) sensor_info.nb_matrices);
00485 return matrix_info[m];
00486 }
00487
00488
00490 inline sTactileSensorFrame const & GetFrame() const
00491 {
00492 return frame;
00493 }
00494
00495
00497 inline sTactileSensorFrame const & UpdateFrame()
00498 {
00499 ReadFrame(&frame);
00500 return frame;
00501 }
00502
00503
00505 void Open(void)
00506 throw (cDSAException*, cSDHErrorCommunication*);
00507
00509 void Close(void)
00510 throw (cDSAException*, cSDHErrorCommunication*);
00511
00512
00541 void SetFramerate(UInt16 framerate, bool do_RLE = true, bool do_data_acquisition = true)
00542 throw (cDSAException*, cSDHErrorCommunication*);
00543
00556 void SetFramerateRetries(UInt16 framerate, bool do_RLE = true, bool do_data_acquisition = true, unsigned int retries = 0, bool ignore_exceptions = false)
00557 throw (cDSAException*, cSDHErrorCommunication*);
00558
00559
00574 sSensitivityInfo GetMatrixSensitivity(int matrix_no)
00575 throw (cDSAException*, cSDHErrorCommunication*);
00576
00577
00594 void SetMatrixSensitivity(int matrix_no,
00595 double sensitivity,
00596 bool do_all_matrices = false,
00597 bool do_reset = false,
00598 bool do_persistent = false)
00599 throw (cDSAException*, cSDHErrorCommunication*);
00600
00601
00618 void SetMatrixThreshold(int matrix_no,
00619 UInt16 threshold,
00620 bool do_all_matrices = false,
00621 bool do_reset = false,
00622 bool do_persistent = false)
00623 throw (cDSAException*, cSDHErrorCommunication*);
00624
00634 UInt16 GetMatrixThreshold(int matrix_no)
00635 throw (cDSAException*, cSDHErrorCommunication*);
00636
00640 tTexel GetTexel(int m, int x, int y) const;
00641
00642
00643
00647 inline int GetMatrixIndex(int fi, int part)
00648 {
00649 return fi * 2 + part;
00650 }
00651
00652
00656 inline unsigned long GetAgeOfFrame(sTactileSensorFrame* frame_p)
00657 {
00658 return ((unsigned long)(start_pc.Elapsed() * 1000.0)) - (frame_p->timestamp - start_dsa);
00659 }
00660
00661 double GetContactArea(int m);
00662
00663 private:
00664 double VoltageToPressure(double voltage);
00665
00666 void ReadAndCheckErrorResponse(char const* msg, UInt8 command_id)
00667 throw (cDSAException*, cSDHErrorCommunication*);
00668
00669
00670 public:
00671 sContactInfo GetContactInfo(int m);
00672
00673
00680 static char const* ErrorCodeToString(eDSAErrorCode error_code);
00681 static char const* ErrorCodeToString(UInt16 error_code)
00682 {
00683 return ErrorCodeToString((eDSAErrorCode) error_code);
00684 }
00685
00686 private:
00688 bool acquiring_single_frame;
00689
00701 void FlushInput(long timeout_us_first, long timeout_us_subsequent);
00702 };
00703
00704
00705
00706
00707 VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA::sControllerInfo const &controller_info);
00708
00709
00710 VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA::sSensorInfo const &sensor_info);
00711
00712
00713 VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA::sMatrixInfo const &matrix_info);
00714
00715
00716 VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA::sResponse const &response);
00717
00718
00719 VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cDSA const &dsa);
00720
00721
00722 NAMESPACE_SDH_END
00723
00724 #endif
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739