Fawkes API  Fawkes Development Version
goto_thread.cpp
1 
2 /***************************************************************************
3  * goto_thread.cpp - Katana goto one-time thread
4  *
5  * Created: Wed Jun 10 11:45:31 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  * 2011-2014 Bahram Maleki-Fard
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL file in the doc directory.
22  */
23 
24 #include "goto_thread.h"
25 #include "controller.h"
26 #include "exception.h"
27 
28 #include <interfaces/KatanaInterface.h>
29 
30 #include <cstdlib>
31 #include <unistd.h>
32 
33 /** @class KatanaGotoThread "goto_thread.h"
34  * Katana linear goto thread.
35  * This thread moves the arm into a specified position.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param katana katana controller base class
41  * @param logger logger
42  * @param poll_interval_ms interval in ms between two checks if the
43  * final position has been reached
44  */
46  fawkes::Logger *logger,
47  unsigned int poll_interval_ms)
48  : KatanaMotionThread("KatanaGotoThread", katana, logger)
49 {
50  __poll_interval_usec = poll_interval_ms * 1000;
51 }
52 
53 
54 /** Set target position.
55  * @param x X coordinate relative to base
56  * @param y Y coordinate relative to base
57  * @param z Z coordinate relative to base
58  * @param phi Phi Euler angle of tool
59  * @param theta Theta Euler angle of tool
60  * @param psi Psi Euler angle of tool
61  */
62 void
63 KatanaGotoThread::set_target(float x, float y, float z,
64  float phi, float theta, float psi)
65 {
66  __x = x;
67  __y = y;
68  __z = z;
69  __phi = phi;
70  __theta = theta;
71  __psi = psi;
72 }
73 
74 void
76 {
77  try {
78  // non-blocking call
79  _katana->move_to(__x, __y, __z, __phi, __theta, __psi);
81  _logger->log_warn("KatanaGotoThread", "Initiating goto failed (no solution, ignoring): %s", e.what());
82  _finished = true;
84  return;
85  } catch (fawkes::Exception &e) {
86  _logger->log_warn("KatanaGotoThread", "Initiating goto failed (ignoring): %s", e.what());
87  _finished = true;
89  return;
90  }
91 
92  // check if final
93  bool final = false;
94  short num_errors = 0;
95  while ( !final ) {
96  usleep(__poll_interval_usec);
97  try {
100  } catch (fawkes::Exception &e) {
101  if (++num_errors <= 10) {
102  _logger->log_warn("KatanaMotorControlThread", "Reading sensor/motor data failed, retrying");
103  continue;
104  } else {
105  _logger->log_warn("KatanaMotorControlThread", "Receiving sensor/motor data failed too often, aborting");
107  break;
108  }
109  }
110 
111  try {
112  final = _katana->final();
114  _logger->log_warn("KatanaMotorControlTrhead", e.what());
116  break;
117  }
118  }
119 
120  _logger->log_debug(name(), "Position (%f,%f,%f, %f,%f,%f) reached",
121  __x, __y, __z, __phi, __theta, __psi);
122 
123  _finished = true;
124 }
static const uint32_t ERROR_NO_SOLUTION
ERROR_NO_SOLUTION constant.
virtual void read_motor_data()=0
Read motor data of currently active joints from device into controller libray.
fawkes::RefPtr< fawkes::KatanaController > _katana
Katana object for interaction with the arm.
Definition: motion_thread.h:48
fawkes::Logger * _logger
Logger.
Definition: motion_thread.h:52
Katana motion thread base class.
Definition: motion_thread.h:35
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
No joint configuration for desired target found.
Definition: exception.h:34
virtual void set_target(float x, float y, float z, float phi, float theta, float psi)
Set target position.
Definition: goto_thread.cpp:63
virtual void read_sensor_data()=0
Read all sensor data from device into controller libray.
static const uint32_t ERROR_COMMUNICATION
ERROR_COMMUNICATION constant.
virtual void once()
Execute an action exactly once.
Definition: goto_thread.cpp:75
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void move_to(float x, float y, float z, float phi, float theta, float psi, bool blocking=false)=0
Move endeffctor to given coordinates.
const char * name() const
Get name of thread.
Definition: thread.h:95
static const uint32_t ERROR_MOTOR_CRASHED
ERROR_MOTOR_CRASHED constant.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
At least one motor crashed.
Definition: exception.h:45
bool _finished
Set to true when motion is finished, to false on reset.
Definition: motion_thread.h:50
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
unsigned int _error_code
Set to the desired error code on error.
Definition: motion_thread.h:54
KatanaGotoThread(fawkes::RefPtr< fawkes::KatanaController > katana, fawkes::Logger *logger, unsigned int poll_interval_ms)
Constructor.
Definition: goto_thread.cpp:45
virtual bool final()=0
Check if movement is final.
static const uint32_t ERROR_CMD_START_FAILED
ERROR_CMD_START_FAILED constant.
Interface for logging.
Definition: logger.h:34