Fawkes API  Fawkes Development Version
motor_control_thread.cpp
1 
2 /***************************************************************************
3  * motor_control_thread.cpp - Katana direct motor encoder/value control thread
4  *
5  * Created: Sun Mar 13 14:44:24 2011
6  * Copyright 2011-2014 Bahram Maleki-Fard
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 "motor_control_thread.h"
24 #include "controller.h"
25 #include "exception.h"
26 
27 #include <interfaces/KatanaInterface.h>
28 
29 #include <cstdlib>
30 #include <unistd.h>
31 
32 /** @class KatanaMotorControlThread "goto_thread.h"
33  * Katana motor control thread.
34  * This thread moves a single motor to/by a given value in encoders or angle.
35  * @author Bahram Maleki-Fard
36  */
37 
38 /** Constructor.
39  * @param katana katana controller base class
40  * @param logger logger
41  * @param poll_interval_ms interval in ms between two checks if the
42  * final position has been reached
43  */
45  fawkes::Logger *logger,
46  unsigned int poll_interval_ms)
47  : KatanaMotionThread("KatanaMotorControlThread", katana, logger)
48 {
49  __poll_interval_usec = poll_interval_ms * 1000;
50 }
51 
52 
53 /** Set target encoder value
54  * @param nr number of motor
55  * @param value encoder value
56  * @param inc is value incremental? deault:false -> absolute
57  */
58 void
59 KatanaMotorControlThread::set_encoder(unsigned int nr, int value, bool inc)
60 {
61  __nr = nr;
62  __encoder = value;
63 
64  __is_encoder = true;
65  __is_inc = inc;
66 }
67 
68 /** Set target angle value
69  * @param nr number of motor
70  * @param value angle value
71  * @param inc is value incremental? deault:false -> absolute
72  */
73 void
74 KatanaMotorControlThread::set_angle(unsigned int nr, float value, bool inc)
75 {
76  __nr = nr;
77  __angle = value;
78 
79  __is_encoder = false;
80  __is_inc = inc;
81 }
82 
83 
84 void
86 {
87  try {
88  // non-blocking call to KNI
89  if( __is_encoder ) {
90  if( __is_inc )
91 
92  _katana->move_motor_by(__nr, __encoder);
93  else
94  _katana->move_motor_to(__nr, __encoder);
95  } else {
96  if( __is_inc )
97  _katana->move_motor_by(__nr, __angle);
98  else
99  _katana->move_motor_to(__nr, __angle);
100  }
101  } catch (fawkes::KatanaOutOfRangeException &e) {
102  _logger->log_warn("KatanaMotorControlThread", "Motor %u out of range. Ex%s", __nr, e.what());
103  _finished = true;
105  return;
106  } catch (fawkes::Exception &e) {
107  _logger->log_warn("KatanaMotorControlThread", "Moving motor %u failed (ignoring): %s", __nr, e.what());
108  _finished = true;
110  return;
111  }
112 
113  // check if final
114  bool final = false;
115  short num_errors = 0;
116  while ( !final ) {
117  usleep(__poll_interval_usec);
118  try {
121  } catch (fawkes::Exception &e) {
122  if (++num_errors <= 10) {
123  _logger->log_warn("KatanaMotorControlThread", "Reading sensor/motor data failed, retrying");
124  continue;
125  } else {
126  _logger->log_warn("KatanaMotorControlThread", "Receiving sensor/motor data failed too often, aborting");
128  break;
129  }
130  }
131 
132  try {
133  final = _katana->final();
135  _logger->log_warn("KatanaMotorControlTrhead", e.what());
137  break;
138  }
139  }
140 
141  _logger->log_debug(name(), "Successfully moved motor %u", __nr);
142 
143  _finished = true;
144 }
virtual void read_motor_data()=0
Read motor data of currently active joints from device into controller libray.
virtual void move_motor_to(unsigned short id, int enc, bool blocking=false)=0
Move single joint/motor to encoder value.
virtual void once()
Execute an action exactly once.
At least one motor is out of range.
Definition: exception.h:40
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
virtual void read_sensor_data()=0
Read all sensor data from device into controller libray.
static const uint32_t ERROR_UNSPECIFIC
ERROR_UNSPECIFIC constant.
static const uint32_t ERROR_COMMUNICATION
ERROR_COMMUNICATION constant.
KatanaMotorControlThread(fawkes::RefPtr< fawkes::KatanaController > katana, fawkes::Logger *logger, unsigned int poll_interval_ms)
Constructor.
Base class for exceptions in Fawkes.
Definition: exception.h:36
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.
virtual void move_motor_by(unsigned short id, int enc, bool blocking=false)=0
Move single joint/motor by encoder value (i.e.
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
virtual void set_angle(unsigned int nr, float value, bool inc=false)
Set target angle value.
virtual void set_encoder(unsigned int nr, int value, bool inc=false)
Set target encoder value.
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