demo-dsa.cpp
Go to the documentation of this file.
1 //======================================================================
37 //======================================================================
38 
40 
41 //----------------------------------------------------------------------
42 // System Includes - include with <>
43 //----------------------------------------------------------------------
44 
45 //#include <getopt.h>
46 //#include <assert.h>
47 
48 #include <iostream>
49 #include <vector>
50 
51 using namespace std;
52 
53 //----------------------------------------------------------------------
54 // Project Includes - include with ""
55 //---------------------------------------------------------------------
56 
57 #include "sdh/sdh.h"
58 #include "sdh/dsa.h"
59 #include "sdh/basisdef.h"
60 #include "sdh/util.h"
61 #include "sdhoptions.h"
62 
63 //----------------------------------------------------------------------
64 // Defines, enums, unions, structs,
65 //----------------------------------------------------------------------
66 
68 
69 
70 //----------------------------------------------------------------------
71 // Global variables
72 //----------------------------------------------------------------------
73 
82 char const* __help__ =
84  "Simple demo to test cDSA class of SDHLibrary-cpp.\n"
85  "\n"
86  "Remarks:\n"
87  "- You must specify at least one of these options to see some output:\n"
88  " -f | --fullframe \n"
89  " -r | --resulting\n"
90  " -c | --controllerinfo \n"
91  " -s | --sensorinfoinfo\n"
92  " -m | --matrixinfo=N\n"
93  " \n"
94  "- Example usage:\n"
95  " - Read a single full frame from tactile sensors connected to port 3 = COM4:\n"
96  " > demo-dsa --dsaport=3 -f\n"
97  " \n"
98  " - Read a single full frame from tactile sensors connected via TCP/IP (ethernet):\n"
99  " The tactile sensors have IP-Address 192.168.1.42 and use TCP port 13000.\n"
100  " (Requires at least SDH-firmware v0.0.3.2)\n"
101  " > demo-dsa --dsa_tcp=192.168.1.42:13000 -f\n"
102  " \n"
103  " - Read full frames continuously once per second from tactile sensors\n"
104  " connected to port 3 = COM4:\n"
105  " > demo-dsa --dsaport=3 -f -r 1\n"
106  " \n"
107  " - Read full frames continuously 10 times per second from tactile sensors\n"
108  " connected to port 3 = COM4:\n"
109  " > demo-dsa --dsaport=3 -f -r 10\n"
110  " \n"
111  " - Read full frames continuously as fast as possible (DSA push-mode)\n"
112  " from tactile sensors connected to port 3 = COM4:\n"
113  " > demo-dsa --dsaport=3 -f -r 30\n"
114  " \n"
115  " - Read a single full frame from tactile sensors connected to USB\n"
116  " to RS232 converter 0:\n"
117  " > demo-dsa --dsa_rs_device=/dev/ttyUSB0 -f \n"
118  " \n"
119  " - Read the sensor, controller, matrix 0 infos \n"
120  " from tactile sensors connected to port 3 = COM4:\n"
121  " > demo-dsa --dsaport=3 -s -c -m 0 \n"
122  " \n"
123  " - Get the version info of both the joint controllers and the tactile \n"
124  " sensor firmware from an SDH connected via Ethernet.\n"
125  " The joint controllers and the tactile sensors have a common IP-Address,\n"
126  " here 192.168.1.42. The SDH controller is attached to the \n"
127  " default TCP port 23 and the tactile sensors to the default TCP port 13000.\n"
128  " (Requires at least SDH-firmware v0.0.3.2)\n"
129  " > demo-dsa --tcp=192.168.1.42 --dsa_tcp -v\n"
130  " \n"
131  " - Get the version info of both the joint controllers and the tactile \n"
132  " sensor firmware from an SDH connected to \n"
133  " - port 2 = COM3 (joint controllers) and \n"
134  " - port 3 = COM4 (tactile sensor controller) \n"
135  " > demo-dsa -p 2 --dsaport=3 -v\n"
136  " - Set the sensitivity of all tactile sensor matrixes to 0.75 temporarily.\n"
137  " The value will be used only temporarily (until reset or power cycle). \n"
138  " > demo-dsa --dsaport=3 --sensitivity=0.75 \n"
139  " \n"
140  " - Set the sensitivity of all tactile sensor matrixes to 0.75 persistently.\n"
141  " The value will be stored persistently (i.e. will remain after reset or \n"
142  " power cycle). \n"
143  " > demo-dsa --dsaport=3 --sensitivity=0.75 --persistent\n"
144  " \n"
145  " - Reset the sensitivity of all tactile sensor matrixes to factory default.\n"
146  " > demo-dsa --dsaport=3 --sensitivity=0.75 --reset\n"
147  " \n"
148  " - Set the sensitivity of tactile sensor matrices 1 and 4 to individual\n"
149  " values temporarily.\n"
150  " The value will be used only temporarily (until reset or power cycle).\n"
151  " Sensor 1 (distal sensor of finger 1) will be set to 0.1\n"
152  " Sensor 4 (proximal sensor of finger 3) will be set to 0.4\n"
153  " > demo-dsa --dsaport=3 --sensitivity1=0.1 --sensitivity4=0.4 \n"
154 
155  " - Like for the sensitivity the thresholds can be adjusted via \n"
156  " the --threshold=VALUE parameter.\n"
157  "\n"
158  "- Known bugs:\n"
159  " - see the bug description for \"cDSAException: Checksum Error on Windows\n"
160  " console\" in the Related Pages->Bug List section of the doxygen\n"
161  " documentation\n"
162  ;
163 
164 char const* __author__ = "Dirk Osswald: dirk.osswald@de.schunk.com";
165 char const* __url__ = "http://www.schunk.com";
166 char const* __version__ = "$Id: demo-dsa.cpp 10351 2013-06-18 16:28:14Z Osswald2 $";
167 char const* __copyright__ = "Copyright (c) 2008 SCHUNK GmbH & Co. KG";
168 
169 char const* usage =
170  "usage: demo-dsa [options]\n"
171  ;
172 
173 // end of doxygen name group sdhlibrary_cpp_demo_dsa_cpp_vars
174 // @}
175 //----------------------------------------------------------------------
176 
177 
178 
179 int main( int argc, char **argv )
180 {
182 
183  int i;
184 
185  //---------------------
186  // handle command line options: set defaults first then overwrite by parsing actual command line
187  cSDHOptions options( "general sdhcom_common dsacom dsaother dsaadjust" );
188 
189  options.Parse( argc, argv, __help__, "demo-dsa", __version__, cSDH::GetLibraryName(), cSDH::GetLibraryRelease() );
190 
191  //
192  //---------------------
193 
194  //---------------------
195  // initialize debug message printing:
196  cDBG cdbg( options.debug_level > 0, "red", options.debuglog );
197  g_sdh_debug_log = options.debuglog;
198 
199  cdbg << "Debug messages of " << argv[0] << " are printed like this.\n";
200 
201  // reduce debug level for subsystems
202  options.debug_level-=1;
203  //---------------------
204 
205  //---------------------
206  // start actual processing:
207  cDSA* ts = NULL;
208  try
209  {
210 
211  if ( options.dsa_use_tcp )
212  {
213  cdbg << "debug_level=" << options.debug_level << " tcp_adr=" << options.tcp_adr << " dsa_tcp_port=" << options.dsa_tcp_port << "\n";
214  ts = new cDSA( options.debug_level, options.tcp_adr.c_str(), options.dsa_tcp_port, options.timeout );
215  }
216  else
217  {
218  cdbg << "debug_level=" << options.debug_level << " dsaport=" << options.dsaport << " dsa_rs_device=" << options.dsa_rs_device << "\n";
219  ts = new cDSA( options.debug_level, options.dsaport, options.dsa_rs_device );
220  }
221 
222 
223  //---------------------
224  // print requested info:
225  if ( options.controllerinfo )
226  {
227  cout << "Controller Info:\n";
228  cout << ts->GetControllerInfo();
229  }
230 
231  if ( options.sensorinfo )
232  {
233  cout << "Sensor Info:\n";
234  cout << ts->GetSensorInfo();
235  }
236 
237  for ( i=0; i<6; i++ )
238  {
239  if ( options.matrixinfo[i] < 0 )
240  continue;
241 
242  cout << "Matrix Info " << options.matrixinfo[i] << ":\n";
243  cout << ts->GetMatrixInfo( options.matrixinfo[i] );
244 
245  if ( ts->GetControllerInfo().sw_version >= 268 )
246  {
247  cDSA::sSensitivityInfo sensitivity_info;
248 
249  sensitivity_info = ts->GetMatrixSensitivity(i);
250  cout << " sensitivity = " << sensitivity_info.cur_sens << "\n";
251  cout << " factory_sensitivity = " << sensitivity_info.fact_sens << "\n";
252  cout << " threshold = " << ts->GetMatrixThreshold(i) << "\n";
253  }
254  }
255  //---------------------
256 
257  if ( options.showdsasettings )
258  {
259  if ( ts->GetControllerInfo().sw_version < 268 )
260  {
261  cerr << "To be able to read the sensitivity/threshold settings you must update\n";
262  cerr << "the firmware of the DSACON32m (tactile sensor controller in the SDH)\n";
263  cerr << "to at least release R268.\n";
264  }
265  else
266  {
267  cDSA::sSensitivityInfo sensitivity_info;
268  char const* descr[] = { " proximal", " distal" };
269  for ( i=0; i<6; i++ )
270  {
271  sensitivity_info = ts->GetMatrixSensitivity(i);
272  cout << "Sensor " << i << " (finger " << i/2+1 << descr[i%2] << ")\n";
273  cout << " sensitivity = " << sensitivity_info.cur_sens << "\n";
274  cout << " factory_sensitivity = " << sensitivity_info.fact_sens << "\n";
275  cout << " threshold = " << ts->GetMatrixThreshold(i) << "\n";
276  }
277  }
278  }
279 
280 
281  //---------------------
282  // Set sensitivities if requested
283  for ( i=0; i<6; i++ )
284  {
285  if ( options.sensitivity[i] < 0.0 )
286  continue;
287  ts->SetMatrixSensitivity(i, options.sensitivity[i], false, options.reset_to_default, options.persistent );
288  }
289 
290  // Set thresholds if requested
291  for ( i=0; i<6; i++ )
292  {
293  if ( options.threshold[i] > 4095 )
294  continue;
295  ts->SetMatrixThreshold(i, options.threshold[i], false, options.reset_to_default, options.persistent );
296  }
297  //---------------------
298 
299 
300  //---------------------
301  // Read and show actual tactile sensor data ("full frame")
302  if ( options.fullframe )
303  {
304  bool do_single_frames = options.framerate < 30;
305 
306  if ( do_single_frames )
307  {
308  // Make remote tactile sensor controller stop sending data automatically as fast as possible (prepare for DSA pull-mode):
309  cdbg << "Starting DSA pull-mode, framerate=0 do_rle=" << options.do_RLE << " do_data_acquisition=false" << "\n";
310  ts->SetFramerate( 0, options.do_RLE, false );
311  }
312  else
313  {
314  // Make remote tactile sensor controller send data automatically as fast as possible (DSA push-mode):
315  cdbg << "Starting DSA push-mode, framerate=1 do_rle=" << options.do_RLE << " do_data_acquisition=true" << "\n";
316  ts->SetFramerate( 1, options.do_RLE );
317  }
318 
319 
320  //-----------
321  // start periodic or one time processing of full frames if requested:
322  double period_s = 0.0;
323  if ( options.framerate > 0)
324  period_s = 1.0 / double(options.framerate);
325  double remaining_s;
326  int nb_errors = 0;
327  int nb_frames = 0;
328  cSimpleTime start;
329  cSimpleTime now;
330  cSimpleTime last;
331  int nb_last = 0;
332  do
333  {
334  //-----------
335  try
336  {
337  if ( do_single_frames )
338  {
339  start.StoreNow();
340  ts->SetFramerateRetries( 0, options.do_RLE, true, 3 );
341  }
342  ts->UpdateFrame();
343  now.StoreNow();
344  nb_frames++;
345  cout << *ts;
346  if ( options.framerate > 0 )
347  cout << "Actual framerate=" << ((nb_frames-nb_last)/last.Elapsed( now )) << "Hz nb_frames=" << nb_frames << " nb_errors=" << nb_errors << " (" << ((100.0*nb_errors)/nb_frames) << "%)\n";
348 
349  if ( last.Elapsed( now ) > 3.0 )
350  {
351  last = now;
352  nb_last = nb_frames;
353  }
354  cout.flush();
355  }
356  catch ( cDSAException* e)
357  {
358  nb_errors++;
359  cerr << "Caught and ignored cDSAException: " << e->what() << " nb_errors=" << nb_errors << "\n";
360  delete e;
361  }
362  //-----------
363 
364  //-----------
365  if ( do_single_frames )
366  {
367  remaining_s = period_s - (start.Elapsed());
368  if ( remaining_s > 0.0 )
369  SleepSec(remaining_s);
370  }
371  } while ( options.framerate > 0 );
372  //---------------------
373  }
374  //---------------------
375 
376  ts->Close();
377  }
378  catch ( cSDHLibraryException* e )
379  {
380  cerr << "\ndemo-dsa main(): Caught exception from SDHLibrary: " << e->what() << ". Giving up!\n";
381  delete e;
382  }
383  catch (...)
384  {
385  cerr << "\ncaught unknown exception, giving up\n";
386  }
387  delete ts;
388 }
389 //----------------------------------------------------------------------
390 
391 
392 //======================================================================
393 /*
394  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
395  (e.g. to explicitely set C++ mode for *.h header files)
396 
397  Local Variables:
398  mode:C++
399  mode:ELSE
400  End:
401 */
402 //======================================================================
int main(int argc, char **argv)
Definition: demo-dsa.cpp:179
int Parse(int argc, char **argv, char const *helptext, char const *progname, char const *version, char const *libname, char const *librelease)
Definition: sdhoptions.cpp:464
std::string tcp_adr
Definition: sdhoptions.h:124
This file contains the interface to class #SDH::cSDH, the end user class to access the SDH from a PC...
#define SDH_ASSERT_TYPESIZES()
macro to assert that the defined typedefs have the expected sizes
Definition: basisdef.h:73
void StoreNow(void)
Store current time internally.
Definition: simpletime.h:103
A class to print colored debug messages.
Definition: dbg.h:113
sMatrixInfo const & GetMatrixInfo(int m) const
Return a reference to the sMatrixInfo of matrix m read from the remote DSACON32m controller.
Definition: dsa.h:485
#define NULL
Definition: getopt1.c:56
Derived exception class for low-level DSA related exceptions.
Definition: dsa.h:88
double Elapsed(void) const
Return time in seconds elapsed between the time stored in the object and now.
Definition: simpletime.h:115
bool dsa_use_tcp
Definition: sdhoptions.h:127
bool reset_to_default
Definition: sdhoptions.h:119
std::ostream * debuglog
Definition: sdhoptions.h:86
void Close(void)
Set the framerate of the remote DSACON32m controller to 0 and close connection to it...
Definition: dsa.cpp:739
int debug_level
Definition: sdhoptions.h:85
Implementation of a class to parse common SDH related command line options.
bool controllerinfo
Definition: sdhoptions.h:114
char const * usage
Definition: demo-dsa.cpp:169
void SleepSec(double t)
Definition: util.cpp:155
static char const * GetLibraryRelease(void)
Definition: sdh.cpp:626
double sensitivity[6]
Definition: sdhoptions.h:117
void SetFramerate(UInt16 framerate, bool do_RLE=true, bool do_data_acquisition=true)
Definition: dsa.cpp:766
void SetMatrixSensitivity(int matrix_no, double sensitivity, bool do_all_matrices=false, bool do_reset=false, bool do_persistent=false)
Definition: dsa.cpp:836
int dsa_tcp_port
Definition: sdhoptions.h:128
cDBG cdbg(false,"red")
SDH::cDSA is the end user interface class to access the DSACON32m, the tactile sensor controller of t...
Definition: dsa.h:111
Base class for exceptions in the SDHLibrary-CPP.
Definition: sdhexception.h:132
bool persistent
Definition: sdhoptions.h:120
Interface of auxilliary utility functions for SDHLibrary-CPP.
virtual const char * what() const
bool fullframe
Definition: sdhoptions.h:112
int matrixinfo[6]
Definition: sdhoptions.h:115
This file contains interface to #SDH::cDSA, a class to communicate with the tactile sensors of the SD...
char const * __version__
Definition: demo-dsa.cpp:166
#define USING_NAMESPACE_SDH
bool sensorinfo
Definition: sdhoptions.h:113
UInt16 sw_version
Definition: dsa.h:160
Structure to hold info about the sensitivity settings of one sensor patch.
Definition: dsa.h:204
This file contains settings to make the SDHLibrary compile on differen systems:
char dsa_rs_device[MAX_DEV_LENGTH]
Definition: sdhoptions.h:108
Very simple class to measure elapsed time.
Definition: simpletime.h:84
char const * __url__
Definition: demo-dsa.cpp:165
char const * __author__
Definition: demo-dsa.cpp:164
static char const * GetLibraryName(void)
Definition: sdh.cpp:633
char const * __help__
Definition: demo-dsa.cpp:83
bool showdsasettings
Definition: sdhoptions.h:121
void SetMatrixThreshold(int matrix_no, UInt16 threshold, bool do_all_matrices=false, bool do_reset=false, bool do_persistent=false)
Definition: dsa.cpp:902
This file contains some basic definitions (defines, macros, datatypes)
class for command line option parsing holding option parsing results
Definition: sdhoptions.h:78
unsigned int threshold[6]
Definition: sdhoptions.h:118
sSensorInfo const & GetSensorInfo(void) const
Return a reference to the sSensorInfo read from the remote DSACON32m controller.
Definition: dsa.h:478
USING_NAMESPACE_SDH NAMESPACE_SDH_START std::ostream * g_sdh_debug_log
Definition: sdhbase.cpp:55
sTactileSensorFrame const & UpdateFrame()
read the tactile sensor frame from remote DSACON32m and return a reference to it. A command to query ...
Definition: dsa.h:500
sControllerInfo const & GetControllerInfo(void) const
Return a reference to the sControllerInfo read from the remote DSACON32m controller.
Definition: dsa.h:471
UInt16 GetMatrixThreshold(int matrix_no)
Definition: dsa.cpp:934
sSensitivityInfo GetMatrixSensitivity(int matrix_no)
Definition: dsa.cpp:879
char const * __copyright__
Definition: demo-dsa.cpp:167
void SetFramerateRetries(UInt16 framerate, bool do_RLE=true, bool do_data_acquisition=true, unsigned int retries=0, bool ignore_exceptions=false)
Definition: dsa.cpp:810
double timeout
Definition: sdhoptions.h:90


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Sun Aug 18 2019 03:42:20