demo-velocity-acceleration.cpp
Go to the documentation of this file.
1 //======================================================================
39 //======================================================================
40 
41 #include <iostream>
42 #include <vector>
43 
44 
45 // Include the cSDH interface
46 #include "sdh/sdh.h"
47 #include "sdh/util.h"
49 #include "sdh/basisdef.h"
50 #include "sdhoptions.h"
51 
53 
60 char const* __help__ =
62  "Make the SDH move one finger in \"velocity with acceleration ramp\" control mode.\n(C++ demo application using the SDHLibrary-CPP library.)\n"
63  "\n"
64  " - Make SDH connected via Ethernet move.\n"
65  " The SDH has IP-Address 192.168.1.42 and is attached to TCP port 23.\n"
66  " (Requires at least SDH-firmware v0.0.3.1)\n"
67  " > demo-velocity-acceleration --tcp=192.168.1.42:23\n"
68  " \n"
69  "- Example usage:\n"
70  " - Make SDH connected to port 2 = COM3 move:\n"
71  " > demo-velocity-acceleration -p 2\n"
72  " \n"
73  " - Make SDH connected to USB to RS232 converter 0 move:\n"
74  " > demo-velocity-acceleration --sdh_rs_device=/dev/ttyUSB0 \n"
75  " \n"
76  " - Get the version info of both the joint controllers and the tactile \n"
77  " sensor firmware from an SDH connected via Ethernet.\n"
78  " The joint controllers and the tactile sensors have a common IP-Address,\n"
79  " here 192.168.1.42. The SDH controller is attached to the \n"
80  " default TCP port 23 and the tactile sensors to the default TCP port 13000.\n"
81  " (Requires at least SDH-firmware v0.0.3.2)\n"
82  " > demo-velocity-acceleration --tcp=192.168.1.42 --dsa_tcp -v\n"
83  " \n"
84  " - Get the version info of an SDH connected to port 2 = COM3 \n"
85  " > demo-velocity-acceleration --port=2 -v\n";
86 
87 char const* __author__ = "Dirk Osswald: dirk.osswald@de.schunk.com";
88 char const* __url__ = "http://www.schunk.com";
89 char const* __version__ = "$Id: demo-velocity-acceleration.cpp 10351 2013-06-18 16:28:14Z Osswald2 $";
90 char const* __copyright__ = "Copyright (c) 2007 SCHUNK GmbH & Co. KG";
91 
92 // end of doxygen name group sdhlibrary_cpp_demo_velocity_acceleration_cpp_vars
93 // @}
94 
95 char const* usage =
96  "usage: demo-velocity-acceleration [options]\n";
97 
98 
99 int main( int argc, char** argv )
100 {
102 
103  //---------------------
104  // handle command line options: set defaults first then overwrite by parsing actual command line
105  cSDHOptions options;
106 
107  options.Parse( argc, argv, __help__, "demo-velocity-acceleration", __version__, cSDH::GetLibraryName(), cSDH::GetLibraryRelease() );
108  //
109  //---------------------
110 
111  //---------------------
112  // initialize debug message printing:
113  cDBG cdbg( options.debug_level > 0, "red", options.debuglog );
114  g_sdh_debug_log = options.debuglog;
115 
116  cdbg << "Debug messages of " << argv[0] << " are printed like this.\n";
117 
118  // reduce debug level for subsystems
119  options.debug_level-=1;
120  //---------------------
121 
122  // Pack the rest of this demo into a try block so that we can recover in case of an error:
123  try
124  {
125  // Create an instance "hand" of the class cSDH:
126  cSDH hand( options.use_radians, options.use_fahrenheit, options.debug_level );
127  cdbg << "Successfully created cSDH instance\n";
128 
129  // Open configured communication to the SDH device
130  options.OpenCommunication( hand );
131  cdbg << "Successfully opened communication to SDH\n";
132 
133  //##############
134  // Preparations: Move the hand to a pose that is adequate for this demo:
135  std::cout << "Preparation:\n";
136  std::cout << " Moving to start position with \"pose\" controller type:\n\n";
137 
138  // Switch to "pose" controller mode first, then move to "home"
139  hand.SetController( hand.eCT_POSE );
140  hand.SetAxisTargetVelocity( hand.All, 40.0 );
141  hand.SetAxisTargetAngle( hand.All, 0.0 );
142  hand.MoveHand();
143  //
144  //##############
145 
146 
147  //##############
148  // Do some movements with "velocity with acceleration ramp" controller type,
149  // move with different velocities and accelerations.
150  std::cout << "Moving in \"velocity with acceleration ramp\" controller type:\n";
151  std::cout << " Now move back and forth with increasing acceleration\n";
152  std::cout << " and target velocities of alternating sign:\n";
153 
154  // Now switch to "velocity control with acceleration ramp" controller mode.
156 
157  // Use one axis only:
158  int axis_index = 2;
159 
160  // In this controller mode we must switch the power on explicitly:
161  // (OK, here the power is switched on already since we used hand.MoveHand() before.)
162  hand.SetAxisEnable( axis_index, true );
163 
164  double accelerations[] = { 10.0, 20.0, 40.0, 80.0, 160.0, 0.0 };
165  double velocity = 40.0;
166  int i = 0;
167  do
168  {
169  // set desired acceleration (must be done first!):
170  hand.SetAxisTargetAcceleration( axis_index, accelerations[i] );
171 
172  for ( double sign = -1.0; sign <= 1.0; sign += 2.0 ) // generate an alternating sign: -1.0 / +1.0
173  {
174  std::cout << "Setting target acceleration,velocity= " << std::setw(7) << accelerations[i] << " deg/(s*s) " << std::setw(7) << sign * velocity << " deg/s\n";
175 
176  // set the desired target velocity. This will make the axis move!
177  hand.SetAxisTargetVelocity( axis_index, sign * velocity );
178 
179  // keep current velocity (and acceleration) as long as the axis |angle| < 10 deg,
180  // when 10 deg are exceeded then invert the velocity sign (and probably use next acceleration)
181  bool position_reached = false;
182  while ( !position_reached )
183  {
184  // print out some debug data while moving:
185  cdbg << " Actual angle: " << std::setw(7) << hand.GetAxisActualAngle( axis_index ) << " deg";
186  cdbg << ", actual velocity: " << std::setw(7) << hand.GetAxisActualVelocity(axis_index) << " deg/s";
187  cdbg << ", reference velocity: " << std::setw(7) << hand.GetAxisReferenceVelocity(axis_index) << " deg/s\n";
188  if ( sign > 0.0 )
189  position_reached = (hand.GetAxisActualAngle(axis_index) >= 10.0);
190  else
191  position_reached = (hand.GetAxisActualAngle(axis_index) <= -10.0);
192  SleepSec(0.05);
193  }
194  }
195  i++;
196  } while ( accelerations[i] != 0.0);
197  //
198  //##############
199 
200 
201  //##############
202  // Stop movement:
203 
204  std::cout << "Setting target acceleration,velocity= " << std::setw(7) << 100.0 << " deg/(s*s) " << std::setw(7) << 0.0 << " deg/s (for stopping)\n";
205 
206  // set a default acceleration:
207  hand.SetAxisTargetAcceleration( axis_index, 100.0 );
208 
209  // set the desired target velocity to 0.0. This will make the axis slow down until stop.
210  hand.SetAxisTargetVelocity( axis_index, 0.0 );
211 
212  // wait until the joint has stopped to give the SDH time for slowing down:
213  // Solution one:
214  // Simply wait for the time needed to reach velocity 0.0
215  // (v = a * t hence t= v/a)
216  //SleepSec( hand.GetAxisReferenceVelocity( axis_index ) / 100.0 );
217 
218  // Solution two:
219  // Wait until SDH reports "IDLE":
220  hand.WaitAxis( axis_index, 5.0 );
221 
222  //
223  //##############
224 
225 
226  // Finally close connection to SDH again, this switches the axis controllers off
227  hand.Close();
228  }
229  catch (cSDHLibraryException* e)
230  {
231  std::cerr << "demo-velocity-acceleration main(): An exception was caught: " << e->what() << "\n";
232  delete e;
233  }
234 }
235 //----------------------------------------------------------------------
236 
237 
238 //======================================================================
239 /*
240  Here are some settings for the emacs/xemacs editor (and can be safely ignored)
241  (e.g. to explicitely set C++ mode for *.h header files)
242 
243  Local Variables:
244  mode:C++
245  mode:ELSE
246  End:
247 */
248 //======================================================================]
A meta-value that means "access all possible values".
Definition: sdhbase.h:103
#SDH::cSDH is the end user interface class to control a SDH (SCHUNK Dexterous Hand).
Definition: sdh.h:172
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
char const * usage
This file contains the interface to class #SDH::cSDH, the end user class to access the SDH from a PC...
bool use_fahrenheit
Definition: sdhoptions.h:104
#define SDH_ASSERT_TYPESIZES()
macro to assert that the defined typedefs have the expected sizes
Definition: basisdef.h:73
A class to print colored debug messages.
Definition: dbg.h:113
char const * __help__
void WaitAxis(std::vector< int > const &axes, double timeout=-1.0)
Definition: sdh.cpp:1158
bool use_radians
Definition: sdhoptions.h:103
void SetAxisEnable(std::vector< int > const &axes, std::vector< double > const &states)
Definition: sdh.cpp:1055
velocity controller with acceleration ramp, velocities and accelerations of axes are controlled indep...
Definition: sdhbase.h:181
std::ostream * debuglog
Definition: sdhoptions.h:86
int debug_level
Definition: sdhoptions.h:85
Implementation of a class to parse common SDH related command line options.
void SleepSec(double t)
Definition: util.cpp:155
static char const * GetLibraryRelease(void)
Definition: sdh.cpp:626
coordinated position controller (position per axis => "pose controller"), all axes start and stop mov...
Definition: sdhbase.h:179
void SetAxisTargetAcceleration(std::vector< int >const &axes, std::vector< double >const &accelerations)
Definition: sdh.cpp:1457
char const * __url__
cDBG cdbg(false,"red")
void OpenCommunication(NS_SDH cSDH &hand)
Definition: sdhoptions.cpp:865
Base class for exceptions in the SDHLibrary-CPP.
Definition: sdhexception.h:132
char const * __author__
void SetController(cSDHBase::eControllerType controller)
Definition: sdh.cpp:946
Interface of auxilliary utility functions for SDHLibrary-CPP.
virtual const char * what() const
void Close(bool leave_enabled=false)
Definition: sdh.cpp:888
void SetAxisTargetVelocity(std::vector< int > const &axes, std::vector< double > const &velocities)
Definition: sdh.cpp:1289
char const * __copyright__
void SetAxisTargetAngle(std::vector< int > const &axes, std::vector< double > const &angles)
Definition: sdh.cpp:1209
#define USING_NAMESPACE_SDH
int main(int argc, char **argv)
This file contains settings to make the SDHLibrary compile on differen systems:
std::vector< double > GetAxisActualAngle(std::vector< int > const &axes)
Definition: sdh.cpp:1270
static char const * GetLibraryName(void)
Definition: sdh.cpp:633
double MoveHand(bool sequ=true)
Definition: sdh.cpp:2039
This file contains some basic definitions (defines, macros, datatypes)
class for command line option parsing holding option parsing results
Definition: sdhoptions.h:78
std::vector< double > GetAxisReferenceVelocity(std::vector< int >const &axes)
Definition: sdh.cpp:1438
std::vector< double > GetAxisActualVelocity(std::vector< int >const &axes)
Definition: sdh.cpp:1419
USING_NAMESPACE_SDH NAMESPACE_SDH_START std::ostream * g_sdh_debug_log
Definition: sdhbase.cpp:55
char const * __version__


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