humotion / src / server / joint_interface.cpp @ 0c8d22a5
History | View | Annotate | Download (8.223 KB)
| 1 |
/*
|
|---|---|
| 2 |
* This file is part of humotion
|
| 3 |
*
|
| 4 |
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
| 5 |
* http://opensource.cit-ec.de/projects/humotion
|
| 6 |
*
|
| 7 |
* This file may be licensed under the terms of the
|
| 8 |
* GNU Lesser General Public License Version 3 (the ``LGPL''),
|
| 9 |
* or (at your option) any later version.
|
| 10 |
*
|
| 11 |
* Software distributed under the License is distributed
|
| 12 |
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
| 13 |
* express or implied. See the LGPL for the specific language
|
| 14 |
* governing rights and limitations.
|
| 15 |
*
|
| 16 |
* You should have received a copy of the LGPL along with this
|
| 17 |
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
|
| 18 |
* or write to the Free Software Foundation, Inc.,
|
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 20 |
*
|
| 21 |
* The development of this software was supported by the
|
| 22 |
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
| 23 |
* The Excellence Cluster EXC 277 is a grant of the Deutsche
|
| 24 |
* Forschungsgemeinschaft (DFG) in the context of the German
|
| 25 |
* Excellence Initiative.
|
| 26 |
*/
|
| 27 |
|
| 28 |
#include "humotion/server/joint_interface.h" |
| 29 |
#include "humotion/server/controller.h" |
| 30 |
|
| 31 |
using boost::mutex;
|
| 32 |
using humotion::server::JointInterface;
|
| 33 |
|
| 34 |
//! constructor
|
| 35 |
JointInterface::JointInterface() {
|
| 36 |
framerate = 50.0; |
| 37 |
mouth_enabled = false;
|
| 38 |
gaze_enabled = false;
|
| 39 |
} |
| 40 |
|
| 41 |
//! destructor
|
| 42 |
JointInterface::~JointInterface() {
|
| 43 |
} |
| 44 |
|
| 45 |
//! set joint target position
|
| 46 |
//! \param joint_id of joint
|
| 47 |
//! \param float position
|
| 48 |
void JointInterface::set_target(int joint_id, float position, float velocity) { |
| 49 |
assert(joint_id < JOINT_ID_ENUM_SIZE); |
| 50 |
|
| 51 |
// update current value
|
| 52 |
joint_target_position_[joint_id] = position; |
| 53 |
joint_target_velocity_[joint_id] = velocity; |
| 54 |
} |
| 55 |
|
| 56 |
//! fetch target position
|
| 57 |
//! \param joint_id of joint
|
| 58 |
float JointInterface::get_target_position(int joint_id) { |
| 59 |
assert(joint_id < JOINT_ID_ENUM_SIZE); |
| 60 |
return joint_target_position_[joint_id];
|
| 61 |
} |
| 62 |
|
| 63 |
//! fetch target velocity
|
| 64 |
//! \param joint_id of joint
|
| 65 |
float JointInterface::get_target_velocity(int joint_id) { |
| 66 |
assert(joint_id < JOINT_ID_ENUM_SIZE); |
| 67 |
return joint_target_velocity_[joint_id];
|
| 68 |
} |
| 69 |
|
| 70 |
//! incoming position data
|
| 71 |
//! \param joint name
|
| 72 |
//! \param position
|
| 73 |
//! \param timestamp when the position was measured
|
| 74 |
void JointInterface::store_incoming_position(int joint_id, float position, Timestamp timestamp) { |
| 75 |
// lock the tsd_list for this access. by doing this we assure
|
| 76 |
// that no other thread accessing this element can diturb the
|
| 77 |
// following atomic instructions:
|
| 78 |
mutex::scoped_lock sl(joint_ts_position_map_access_mutex); |
| 79 |
|
| 80 |
// printf("> humotion: incoming joint position for joint id 0x%02X "
|
| 81 |
// "= %4.2f (ts=%.2f)\n",joint_id,position,timestamp.to_seconds());
|
| 82 |
joint_ts_position_map[joint_id].insert(timestamp, position); |
| 83 |
|
| 84 |
incoming_position_count++; |
| 85 |
} |
| 86 |
|
| 87 |
//! return incoming position data count & clear this counter
|
| 88 |
//! this can be used as a keep alive status check
|
| 89 |
//! \return number of incoming joint positions since the last call
|
| 90 |
unsigned int JointInterface::get_and_clear_incoming_position_count() { |
| 91 |
unsigned int i = incoming_position_count; |
| 92 |
incoming_position_count = 0;
|
| 93 |
return i;
|
| 94 |
} |
| 95 |
|
| 96 |
//! incoming speed data
|
| 97 |
//! \param joint name
|
| 98 |
//! \param speed
|
| 99 |
//! \param timestamp when the position was measured
|
| 100 |
void JointInterface::store_incoming_velocity(int joint_id, float velocity, Timestamp timestamp) { |
| 101 |
// lock the tsd_list for this access. by doing this we assure
|
| 102 |
// that no other thread accessing this element can disturb the
|
| 103 |
// following atomic instructions:
|
| 104 |
mutex::scoped_lock scoped_lock(joint_ts_speed_map_access_mutex); |
| 105 |
|
| 106 |
// printf("> humotion: incoming joint velocity for joint id 0x%02X = %4.2f "
|
| 107 |
// "(ts=%.2f)\n",joint_id,velocity,timestamp.to_seconds());
|
| 108 |
joint_ts_speed_map[joint_id].insert(timestamp, velocity); |
| 109 |
} |
| 110 |
|
| 111 |
//! return the timestamped float for the given joints position
|
| 112 |
humotion::TimestampedList JointInterface::get_ts_position(int joint_id) {
|
| 113 |
// lock the tsd_list for this access. by doing this we assure
|
| 114 |
// that no other thread accessing this element can disturb the
|
| 115 |
// following atomic instructions
|
| 116 |
mutex::scoped_lock sl(joint_ts_position_map_access_mutex); |
| 117 |
|
| 118 |
// search map
|
| 119 |
joint_tsl_map_t::iterator it = joint_ts_position_map.find(joint_id); |
| 120 |
|
| 121 |
if (it == joint_ts_position_map.end()) {
|
| 122 |
printf("> humotion: no ts_position for joint id 0x%02X found\n", joint_id);
|
| 123 |
return TimestampedList();
|
| 124 |
} |
| 125 |
|
| 126 |
// ok fine, we found the requested value
|
| 127 |
return it->second;
|
| 128 |
} |
| 129 |
|
| 130 |
//! return the timestamped float for the given joints speed
|
| 131 |
humotion::TimestampedList JointInterface::get_ts_speed(int joint_id) {
|
| 132 |
// lock the tsd_list for this access. by doing this we assure
|
| 133 |
// that no other thread accessing this element can diturb the
|
| 134 |
// following atomic instructions
|
| 135 |
mutex::scoped_lock sl(joint_ts_speed_map_access_mutex); |
| 136 |
|
| 137 |
// search map
|
| 138 |
joint_tsl_map_t::iterator it = joint_ts_speed_map.find(joint_id); |
| 139 |
|
| 140 |
if (it == joint_ts_speed_map.end()) {
|
| 141 |
printf("> humotion: no ts_speed for joint id 0x%02X found\n", joint_id);
|
| 142 |
return humotion::TimestampedList();
|
| 143 |
} |
| 144 |
|
| 145 |
// ok fine, we found our value
|
| 146 |
return it->second;
|
| 147 |
} |
| 148 |
|
| 149 |
//! set framerate
|
| 150 |
void JointInterface::set_framerate(float f) { |
| 151 |
framerate = f; |
| 152 |
} |
| 153 |
|
| 154 |
//! enable all mouth joints
|
| 155 |
void JointInterface::enable_mouth_joints() {
|
| 156 |
// already enabled? skip this
|
| 157 |
if (mouth_enabled) {
|
| 158 |
return;
|
| 159 |
} |
| 160 |
|
| 161 |
printf("> humotion: ENABLING MOUTH JOINTS\n");
|
| 162 |
enable_joint(ID_LIP_LEFT_UPPER); |
| 163 |
enable_joint(ID_LIP_LEFT_LOWER); |
| 164 |
enable_joint(ID_LIP_CENTER_UPPER); |
| 165 |
enable_joint(ID_LIP_CENTER_LOWER); |
| 166 |
enable_joint(ID_LIP_RIGHT_UPPER); |
| 167 |
enable_joint(ID_LIP_RIGHT_LOWER); |
| 168 |
mouth_enabled = true;
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
//! disable all mouth joints
|
| 173 |
void JointInterface::disable_mouth_joints() {
|
| 174 |
// already disabled? skip this
|
| 175 |
if (!mouth_enabled) {
|
| 176 |
return;
|
| 177 |
} |
| 178 |
|
| 179 |
printf("> humotion: DISABLING MOUTH JOINTS\n");
|
| 180 |
disable_joint(ID_LIP_LEFT_UPPER); |
| 181 |
disable_joint(ID_LIP_LEFT_LOWER); |
| 182 |
disable_joint(ID_LIP_CENTER_UPPER); |
| 183 |
disable_joint(ID_LIP_CENTER_LOWER); |
| 184 |
disable_joint(ID_LIP_RIGHT_UPPER); |
| 185 |
disable_joint(ID_LIP_RIGHT_LOWER); |
| 186 |
mouth_enabled = false;
|
| 187 |
} |
| 188 |
|
| 189 |
//! enable all gaze joints
|
| 190 |
void JointInterface::enable_gaze_joints() {
|
| 191 |
// already enabled? skip this
|
| 192 |
if (gaze_enabled) {
|
| 193 |
return;
|
| 194 |
} |
| 195 |
|
| 196 |
printf("> humotion: ENABLING GAZE JOINTS\n");
|
| 197 |
enable_joint(ID_EYES_LEFT_LR); |
| 198 |
enable_joint(ID_EYES_RIGHT_LR); |
| 199 |
enable_joint(ID_EYES_BOTH_UD); |
| 200 |
|
| 201 |
enable_joint(ID_EYES_LEFT_LID_UPPER); |
| 202 |
enable_joint(ID_EYES_LEFT_LID_LOWER); |
| 203 |
enable_joint(ID_EYES_RIGHT_LID_UPPER); |
| 204 |
enable_joint(ID_EYES_RIGHT_LID_LOWER); |
| 205 |
|
| 206 |
enable_joint(ID_EYES_LEFT_BROW); |
| 207 |
enable_joint(ID_EYES_RIGHT_BROW); |
| 208 |
|
| 209 |
enable_joint(ID_NECK_PAN); |
| 210 |
enable_joint(ID_NECK_TILT); |
| 211 |
enable_joint(ID_NECK_ROLL); |
| 212 |
|
| 213 |
gaze_enabled = true;
|
| 214 |
} |
| 215 |
|
| 216 |
//! disable all gaze joints
|
| 217 |
void JointInterface::disable_gaze_joints() {
|
| 218 |
// already disabled? skip this
|
| 219 |
if (!gaze_enabled) {
|
| 220 |
return;
|
| 221 |
} |
| 222 |
|
| 223 |
printf("> humotion: DISABLING GAZE JOINTS\n");
|
| 224 |
disable_joint(ID_EYES_LEFT_LR); |
| 225 |
disable_joint(ID_EYES_RIGHT_LR); |
| 226 |
disable_joint(ID_EYES_BOTH_UD); |
| 227 |
|
| 228 |
disable_joint(ID_EYES_LEFT_LID_UPPER); |
| 229 |
disable_joint(ID_EYES_LEFT_LID_LOWER); |
| 230 |
disable_joint(ID_EYES_RIGHT_LID_UPPER); |
| 231 |
disable_joint(ID_EYES_RIGHT_LID_LOWER); |
| 232 |
|
| 233 |
disable_joint(ID_EYES_LEFT_BROW); |
| 234 |
disable_joint(ID_EYES_RIGHT_BROW); |
| 235 |
|
| 236 |
disable_joint(ID_NECK_PAN); |
| 237 |
disable_joint(ID_NECK_TILT); |
| 238 |
disable_joint(ID_NECK_ROLL); |
| 239 |
|
| 240 |
gaze_enabled = false;
|
| 241 |
} |
| 242 |
|
| 243 |
//! fetch maximum allowed joint position
|
| 244 |
//! \return max position
|
| 245 |
float JointInterface::get_joint_max(int joint_id) { |
| 246 |
assert((joint_id > 0) && (joint_id < JOINT_ID_ENUM_SIZE));
|
| 247 |
return joint_max[joint_id];
|
| 248 |
} |
| 249 |
|
| 250 |
//! fetch minimum allowed joint position
|
| 251 |
//! \return min position
|
| 252 |
float JointInterface::get_joint_min(int joint_id) { |
| 253 |
assert((joint_id > 0) && (joint_id < JOINT_ID_ENUM_SIZE));
|
| 254 |
return joint_min[joint_id];
|
| 255 |
} |
| 256 |
|
| 257 |
//! check if joint position map is empty
|
| 258 |
//! \return true if empty
|
| 259 |
bool JointInterface::get_joint_position_map_empty() {
|
| 260 |
return (joint_ts_position_map.empty());
|
| 261 |
} |
| 262 |
|
| 263 |
//! call the virtual store function with given position and velocities
|
| 264 |
void JointInterface::publish_target(int joint_id) { |
| 265 |
publish_target(joint_id, joint_target_position_[joint_id], joint_target_velocity_[joint_id]); |
| 266 |
} |