demo-dsa-simple.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 <fstream>
50 #include <string>
51 #include <vector>
52 
53 using namespace std;
54 
55 //----------------------------------------------------------------------
56 // Project Includes - include with ""
57 //---------------------------------------------------------------------
58 
59 #include "sdh/sdh.h"
60 #include "sdh/dsa.h"
61 #include "sdh/basisdef.h"
62 #include "sdh/util.h"
63 
64 //----------------------------------------------------------------------
65 // Defines, enums, unions, structs,
66 //----------------------------------------------------------------------
67 
69 
70 
71 //----------------------------------------------------------------------
72 // Global variables
73 //----------------------------------------------------------------------
74 
83 char const* __help__ =
85  "Simple demo to test cDSA class of SDHLibrary-cpp for tactile sensor data\n"
86  "reading only.\n"
87  "\n"
88  "This program does not use command line options. Instead all communication\n"
89  "parameters are fixed here in the source code\n"
90  ;
91 
92 char const* __author__ = "Dirk Osswald: dirk.osswald@de.schunk.com";
93 char const* __url__ = "http://www.schunk.com";
94 char const* __version__ = "$Id: demo-dsa-simple.cpp 12284 2014-09-30 08:28:44Z Osswald2 $";
95 char const* __copyright__ = "Copyright (c) 2014 SCHUNK GmbH & Co. KG";
96 
97 char const* usage =
98  "usage: demo-dsa-simple\n"
99  ;
100 
101 // end of doxygen name group sdhlibrary_cpp_demo_dsa_simple_cpp_vars
102 // @}
103 //----------------------------------------------------------------------
104 
105 
106 
107 int main( int argc, char **argv )
108 {
110 
112  //string tcp_adr = "192.168.1.42"; // TODO: adjust this to your needs!
113  string tcp_adr = "192.168.100.200"; // TODO: adjust this to your needs!
115 
116  int debug_level = 5;
117  int dsa_tcp_port = 13000;
118  double timeout = -1.0; // no timeout
119  int framerate = 5; // max framerate for data reading ( < 30 => pull mode; 30 => push mode)
120  bool do_RLE = true;
121 
122  //---------------------
123  // initialize debug message printing:
124  //ios_base::openmode mode = ios_base::app;
125  ostream* debuglog = NULL;
126  debuglog = new ofstream( "demo-dsa-simple.log", ios_base::app );//mode );
127  cDBG cdbg( debug_level>0, "red", debuglog );
128  g_sdh_debug_log = debuglog;
129 
130  cdbg << "Debug messages of " << argv[0] << " are printed like this.\n";
131 
132  //---------------------
133 
134  //---------------------
135  // start actual processing:
136  cDSA* ts = NULL;
137  try
138  {
139  // Create and opent cDSA object to communicate with tactile sensors
140  cdbg << "debug_level=" << debug_level << " tcp_adr=" << tcp_adr << " dsa_tcp_port=" << dsa_tcp_port << "\n";
141  ts = new cDSA( debug_level, tcp_adr.c_str(), dsa_tcp_port, timeout );
142 
143  //---------------------
144  // Read and show actual tactile sensor data ("full frame")
145  bool do_single_frames = framerate < 30;
146 
147  if ( do_single_frames )
148  {
149  // Make remote tactile sensor controller stop sending data automatically as fast as possible (prepare for DSA pull-mode):
150  cdbg << "Starting DSA pull-mode, framerate=0 do_rle=" << do_RLE << " do_data_acquisition=false" << "\n";
151  ts->SetFramerate( 0, do_RLE, false );
152  }
153  else
154  {
155  // Make remote tactile sensor controller send data automatically as fast as possible (DSA push-mode):
156  cdbg << "Starting DSA push-mode, framerate=1 do_rle=" << do_RLE << " do_data_acquisition=true" << "\n";
157  ts->SetFramerate( 1, do_RLE );
158  }
159 
160 
161  //-----------
162  // start periodic or one time processing of full frames if requested:
163  double period_s = 0.0;
164  if ( framerate > 0)
165  period_s = 1.0 / double(framerate);
166  double remaining_s;
167  int nb_errors = 0;
168  int nb_frames = 0;
169  cSimpleTime start;
170  cSimpleTime now;
171  cSimpleTime last;
172  int nb_last = 0;
173  do
174  {
175  //-----------
176  try
177  {
178  if ( do_single_frames )
179  {
180  start.StoreNow();
181  ts->SetFramerateRetries( 0, do_RLE, true, 3 );
182  }
183 
184 
185  /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
186  * Here the tactile sensor data is read from the DSACON32m tactile sensor controller:
187  */
188  ts->UpdateFrame();
189  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
190 
191 
192  now.StoreNow();
193  nb_frames++;
194 
195 
196  /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
197  * Here the tactile sensor data is put to screen:
198  */
199  cout << *ts;
200  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
201 
202 
203  if ( framerate > 0 )
204  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";
205 
206  if ( last.Elapsed( now ) > 3.0 )
207  {
208  last = now;
209  nb_last = nb_frames;
210  }
211  cout.flush();
212  }
213  catch ( cDSAException* e)
214  {
215  nb_errors++;
216  cerr << "Caught and ignored cDSAException: " << e->what() << " nb_errors=" << nb_errors << "\n";
217  cdbg << "Caught and ignored cDSAException: " << e->what() << " nb_errors=" << nb_errors << "\n";
218  delete e;
219  }
220  //-----------
221 
222  //-----------
223  if ( do_single_frames )
224  {
225  remaining_s = period_s - (start.Elapsed());
226  if ( remaining_s > 0.0 )
227  SleepSec(remaining_s);
228  }
229  } while ( true );
230  //---------------------
231  }
232  catch ( cSDHLibraryException* e )
233  {
234  cerr << "\ndemo-dsa-simple main(): Caught exception from SDHLibrary: " << e->what() << ". Giving up!\n";
235  cdbg << "\ndemo-dsa-simple main(): Caught exception from SDHLibrary: " << e->what() << ". Giving up!\n";
236  delete e;
237  }
238  catch (...)
239  {
240  cerr << "\ncaught unknown exception, giving up\n";
241  cdbg << "\ncaught unknown exception, giving up\n";
242  }
243  delete ts;
244 }
245 //----------------------------------------------------------------------
246 
247 
248 //======================================================================
249 /*
250  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
251  (e.g. to explicitely set C++ mode for *.h header files)
252 
253  Local Variables:
254  mode:C++
255  mode:ELSE
256  End:
257 */
258 //======================================================================
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
char const * __url__
#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
void SleepSec(double t)
Definition: util.cpp:155
void SetFramerate(UInt16 framerate, bool do_RLE=true, bool do_data_acquisition=true)
Definition: dsa.cpp:766
char const * usage
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
Interface of auxilliary utility functions for SDHLibrary-CPP.
virtual const char * what() const
This file contains interface to #SDH::cDSA, a class to communicate with the tactile sensors of the SD...
char const * __version__
#define USING_NAMESPACE_SDH
This file contains settings to make the SDHLibrary compile on differen systems:
Very simple class to measure elapsed time.
Definition: simpletime.h:84
This file contains some basic definitions (defines, macros, datatypes)
char const * __author__
USING_NAMESPACE_SDH NAMESPACE_SDH_START std::ostream * g_sdh_debug_log
Definition: sdhbase.cpp:55
char const * __help__
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
int main(int argc, char **argv)
char const * __copyright__
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


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