Fawkes API  Fawkes Development Version
gripper_thread.cpp
1 
2 /***************************************************************************
3  * gripper_thread.cpp - Katana gripper one-time thread
4  *
5  * Created: Thu Jun 11 11:59:38 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "gripper_thread.h"
24 #include "controller.h"
25 #include "exception.h"
26 
27 
28 /** @class KatanaGripperThread "gripper_thread.h"
29  * Katana gripper thread.
30  * This thread opens or closes the gripper when started.
31  * @author Tim Niemueller
32  */
33 
34 /** Constructor.
35  * @param katana katana controller base class
36  * @param logger logger
37  * @param poll_interval_ms interval in ms between two checks if the
38  * final position has been reached
39  */
41  fawkes::Logger *logger,
42  unsigned int poll_interval_ms)
43  : KatanaMotionThread("KatanaGripperThread", katana, logger)
44 {
45  __mode = OPEN_GRIPPER;
46  __poll_interval_usec = poll_interval_ms * 1000;
47 }
48 
49 
50 /** Set mode.
51  * @param mode open, either open or close
52  */
53 void
55 {
56  __mode = mode;
57 }
58 
59 
60 void
62 {
63  try {
64  // non-blocking call
65  if (__mode == CLOSE_GRIPPER) {
66  _katana->gripper_close(/* wait */ false);
67  } else {
68  _katana->gripper_open(/* wait */ false);
69  }
70 
71  } catch (fawkes::Exception &e) {
72  _logger->log_warn("KatanaGripperThread", "Starting gripper motion failed (ignoring): %s", e.what());
73  _finished = true;
75  return;
76  }
77 
78  // check if final
79  bool final = false;
80  short num_errors = 0;
81  while ( !final ) {
82  usleep(__poll_interval_usec);
83  try {
86  } catch (fawkes::Exception &e) {
87  if (++num_errors <= 10) {
88  _logger->log_warn("KatanaMotorControlThread", "Reading sensor/motor data failed, retrying");
89  continue;
90  } else {
91  _logger->log_warn("KatanaMotorControlThread", "Receiving sensor/motor data failed too often, aborting");
93  break;
94  }
95  }
96 
97  try {
98  final = _katana->final();
100  _logger->log_warn("KatanaMotorControlTrhead", e.what());
102  break;
103  }
104  }
105 
106  _logger->log_debug("KatanaGripperThread", "Gripper motion finished");
107 
108  _finished = true;
109 }
virtual void read_motor_data()=0
Read motor data of currently active joints from device into controller libray.
virtual void gripper_close(bool blocking=false)=0
Close Gripper.
KatanaGripperThread(fawkes::RefPtr< fawkes::KatanaController > katana, fawkes::Logger *logger, unsigned int poll_interval_ms)
Constructor.
fawkes::RefPtr< fawkes::KatanaController > _katana
Katana object for interaction with the arm.
Definition: motion_thread.h:50
fawkes::Logger * _logger
Logger.
Definition: motion_thread.h:54
Katana motion thread base class.
Definition: motion_thread.h:37
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 const char * what() const
Get primary string.
Definition: exception.cpp:661
Base class for exceptions in Fawkes.
Definition: exception.h:36
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.
void set_mode(gripper_mode_t mode)
Set mode.
gripper_mode_t
Gripper execution mode.
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:52
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:56
virtual void once()
Execute an action exactly once.
virtual void gripper_open(bool blocking=false)=0
Open Gripper.
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