Statistics
| Branch: | Tag: | Revision:

humotion / src / server / joint_interface.cpp @ f7954788

History | View | Annotate | Download (8.246 KB)

1 8c6c1163 Simon Schulz
/*
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 0c8d22a5 sschulz
#include "humotion/server/joint_interface.h"
29
#include "humotion/server/controller.h"
30 8c6c1163 Simon Schulz
31 0c8d22a5 sschulz
using boost::mutex;
32
using humotion::server::JointInterface;
33 8c6c1163 Simon Schulz
34
//! constructor
35 0c8d22a5 sschulz
JointInterface::JointInterface() {
36 8c6c1163 Simon Schulz
    framerate = 50.0;
37 ea068cf1 sschulz
    mouth_enabled_ = false;
38
    gaze_enabled_  = false;
39 8c6c1163 Simon Schulz
}
40
41
//! destructor
42 0c8d22a5 sschulz
JointInterface::~JointInterface() {
43 8c6c1163 Simon Schulz
}
44
45
//! set joint target position
46
//! \param joint_id of joint
47
//! \param float position
48 0c8d22a5 sschulz
void JointInterface::set_target(int joint_id, float position, float velocity) {
49 8c6c1163 Simon Schulz
    assert(joint_id < JOINT_ID_ENUM_SIZE);
50
51 0c8d22a5 sschulz
    // update current value
52 35b3ca25 Simon Schulz
    joint_target_position_[joint_id] = position;
53
    joint_target_velocity_[joint_id] = velocity;
54 8c6c1163 Simon Schulz
}
55
56
//! fetch target position
57
//! \param joint_id of joint
58 0c8d22a5 sschulz
float JointInterface::get_target_position(int joint_id) {
59 8c6c1163 Simon Schulz
    assert(joint_id < JOINT_ID_ENUM_SIZE);
60 35b3ca25 Simon Schulz
    return joint_target_position_[joint_id];
61
}
62
63
//! fetch target velocity
64
//! \param joint_id of joint
65 0c8d22a5 sschulz
float JointInterface::get_target_velocity(int joint_id) {
66 35b3ca25 Simon Schulz
    assert(joint_id < JOINT_ID_ENUM_SIZE);
67
    return joint_target_velocity_[joint_id];
68 8c6c1163 Simon Schulz
}
69
70
//! incoming position data
71
//! \param joint name
72
//! \param position
73
//! \param timestamp when the position was measured
74 0c8d22a5 sschulz
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 ea068cf1 sschulz
    mutex::scoped_lock sl(joint_ts_position_map_access_mutex_);
79 0c8d22a5 sschulz
80
    // printf("> humotion: incoming joint position for joint id 0x%02X "
81
    // "= %4.2f (ts=%.2f)\n",joint_id,position,timestamp.to_seconds());
82 ea068cf1 sschulz
    joint_ts_position_map_[joint_id].insert(timestamp, position);
83 8c6c1163 Simon Schulz
84 ea068cf1 sschulz
    incoming_position_count_++;
85 8c6c1163 Simon Schulz
}
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 0c8d22a5 sschulz
unsigned int JointInterface::get_and_clear_incoming_position_count() {
91 ea068cf1 sschulz
    unsigned int i = incoming_position_count_;
92
    incoming_position_count_ = 0;
93 8c6c1163 Simon Schulz
    return i;
94
}
95
96
//! incoming speed data
97
//! \param joint name
98
//! \param speed
99
//! \param timestamp when the position was measured
100 0c8d22a5 sschulz
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 ea068cf1 sschulz
    mutex::scoped_lock scoped_lock(joint_ts_speed_map_access_mutex_);
105 8c6c1163 Simon Schulz
106 0c8d22a5 sschulz
    // printf("> humotion: incoming joint velocity for joint id 0x%02X = %4.2f "
107
    // "(ts=%.2f)\n",joint_id,velocity,timestamp.to_seconds());
108 ea068cf1 sschulz
    joint_ts_speed_map_[joint_id].insert(timestamp, velocity);
109 8c6c1163 Simon Schulz
}
110
111
//! return the timestamped float for the given joints position
112 0c8d22a5 sschulz
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 ea068cf1 sschulz
    mutex::scoped_lock sl(joint_ts_position_map_access_mutex_);
117 8c6c1163 Simon Schulz
118 0c8d22a5 sschulz
    // search map
119 ea068cf1 sschulz
    joint_tsl_map_t::iterator it = joint_ts_position_map_.find(joint_id);
120 8c6c1163 Simon Schulz
121 ea068cf1 sschulz
    if (it == joint_ts_position_map_.end()) {
122 8c6c1163 Simon Schulz
        printf("> humotion: no ts_position for joint id 0x%02X found\n", joint_id);
123
        return TimestampedList();
124
    }
125
126 0c8d22a5 sschulz
    // ok fine, we found the requested value
127 8c6c1163 Simon Schulz
    return it->second;
128
}
129
130
//! return the timestamped float for the given joints speed
131 0c8d22a5 sschulz
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 ea068cf1 sschulz
    mutex::scoped_lock sl(joint_ts_speed_map_access_mutex_);
136 8c6c1163 Simon Schulz
137 0c8d22a5 sschulz
    // search map
138 ea068cf1 sschulz
    joint_tsl_map_t::iterator it = joint_ts_speed_map_.find(joint_id);
139 8c6c1163 Simon Schulz
140 ea068cf1 sschulz
    if (it == joint_ts_speed_map_.end()) {
141 8c6c1163 Simon Schulz
        printf("> humotion: no ts_speed for joint id 0x%02X found\n", joint_id);
142 0c8d22a5 sschulz
        return humotion::TimestampedList();
143 8c6c1163 Simon Schulz
    }
144
145 0c8d22a5 sschulz
    // ok fine, we found our value
146 8c6c1163 Simon Schulz
    return it->second;
147
}
148
149
//! set framerate
150 0c8d22a5 sschulz
void JointInterface::set_framerate(float f) {
151 8c6c1163 Simon Schulz
    framerate = f;
152
}
153
154
//! enable all mouth joints
155 0c8d22a5 sschulz
void JointInterface::enable_mouth_joints() {
156
    // already enabled? skip this
157 ea068cf1 sschulz
    if (mouth_enabled_) {
158 8c6c1163 Simon Schulz
        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 ea068cf1 sschulz
    mouth_enabled_ = true;
169 8c6c1163 Simon Schulz
}
170
171
172
//! disable all mouth joints
173 0c8d22a5 sschulz
void JointInterface::disable_mouth_joints() {
174
    // already disabled? skip this
175 ea068cf1 sschulz
    if (!mouth_enabled_) {
176 8c6c1163 Simon Schulz
        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 ea068cf1 sschulz
    mouth_enabled_ = false;
187 8c6c1163 Simon Schulz
}
188
189
//! enable all gaze joints
190 0c8d22a5 sschulz
void JointInterface::enable_gaze_joints() {
191
    // already enabled? skip this
192 ea068cf1 sschulz
    if (gaze_enabled_) {
193 8c6c1163 Simon Schulz
        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 ea068cf1 sschulz
    gaze_enabled_ = true;
214 8c6c1163 Simon Schulz
}
215
216
//! disable all gaze joints
217 0c8d22a5 sschulz
void JointInterface::disable_gaze_joints() {
218
    // already disabled? skip this
219 ea068cf1 sschulz
    if (!gaze_enabled_) {
220 8c6c1163 Simon Schulz
        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 ea068cf1 sschulz
    gaze_enabled_ = false;
241 8c6c1163 Simon Schulz
}
242
243 35b3ca25 Simon Schulz
//! fetch maximum allowed joint position
244 8c6c1163 Simon Schulz
//! \return max position
245 0c8d22a5 sschulz
float JointInterface::get_joint_max(int joint_id) {
246 8c6c1163 Simon Schulz
    assert((joint_id > 0) && (joint_id < JOINT_ID_ENUM_SIZE));
247
    return joint_max[joint_id];
248
}
249
250 35b3ca25 Simon Schulz
//! fetch minimum allowed joint position
251 8c6c1163 Simon Schulz
//! \return min position
252 0c8d22a5 sschulz
float JointInterface::get_joint_min(int joint_id) {
253 8c6c1163 Simon Schulz
    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 0c8d22a5 sschulz
bool JointInterface::get_joint_position_map_empty() {
260 ea068cf1 sschulz
    return (joint_ts_position_map_.empty());
261 8c6c1163 Simon Schulz
}
262 35b3ca25 Simon Schulz
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
}