Statistics
| Branch: | Tag: | Revision:

humotion / src / server / joint_interface.cpp @ b0f4bf4a

History | View | Annotate | Download (7.776 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
#include "server/joint_interface.h"
29
#include "server/controller.h"
30
31
using namespace std;
32
using namespace humotion;
33
using namespace humotion::server;
34
using namespace boost;
35
36
37
//! constructor
38
JointInterface::JointInterface(){
39
    framerate = 50.0;
40
    mouth_enabled = false;
41
    gaze_enabled  = false;
42 b0f4bf4a Simon Schulz
    for(int i=0; i<JOINT_ID_ENUM_SIZE; i++){
43
        joint_min[i] = 0.0;
44
        joint_max[i] = 0.0;
45
        joint_target[i] = 0.0;
46
    }
47 8c6c1163 Simon Schulz
}
48
49
//! destructor
50
JointInterface::~JointInterface(){
51
}
52
53
//! set joint target position
54
//! \param joint_id of joint
55
//! \param float position
56
void JointInterface::set_target_position(int joint_id, float position){
57
    assert(joint_id < JOINT_ID_ENUM_SIZE);
58
59
    //update current value
60
    joint_target[joint_id] = position;
61
}
62
63
//! fetch target position
64
//! \param joint_id of joint
65
float JointInterface::get_target_position(int joint_id){
66
    assert(joint_id < JOINT_ID_ENUM_SIZE);
67
    return joint_target[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, double 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 scoped_lock(joint_ts_position_map_access_mutex);
79
80
    //printf("> humotion: incoming joint position for joint id 0x%02X = %4.2f (ts=%.2f)\n",joint_id,position,timestamp);
81
    joint_ts_position_map[joint_id].insert(timestamp, position);
82
83
    incoming_position_count++;
84
}
85
86
//! return incoming position data count & clear this counter
87
//! this can be used as a keep alive status check
88
//! \return number of incoming joint positions since the last call
89
unsigned int JointInterface::get_and_clear_incoming_position_count(){
90
    unsigned int i = incoming_position_count;
91
    incoming_position_count = 0;
92
    return i;
93
}
94
95
//! incoming speed data
96
//! \param joint name
97
//! \param speed
98
//! \param timestamp when the position was measured
99
void JointInterface::store_incoming_speed(int joint_id, float speed, double timestamp){
100
    //lock the tsd_list for this access. by doing this we assure
101
    //that no other thread accessing this element can diturb the
102
    //following atomic instructions:
103
    mutex::scoped_lock scoped_lock(joint_ts_speed_map_access_mutex);
104
105
    //printf("> humotion: incoming joint speed for joint id 0x%02X = %4.2f (ts=%.2f)\n",joint_id,speed,timestamp);
106
    joint_ts_speed_map[joint_id].insert(timestamp, speed);
107
}
108
109
//! return the timestamped float for the given joints position
110
TimestampedList JointInterface::get_ts_position(int joint_id){
111
    //lock the tsd_list for this access. by doing this we assure
112
    //that no other thread accessing this element can diturb the
113
    //following atomic instructions:
114
    mutex::scoped_lock scoped_lock(joint_ts_position_map_access_mutex);
115
116
    //search map:
117
    joint_tsl_map_t::iterator it = joint_ts_position_map.find(joint_id);
118
119
    if (it == joint_ts_position_map.end()){
120
        printf("> humotion: no ts_position for joint id 0x%02X found\n", joint_id);
121
        return TimestampedList();
122
    }
123
124
    //ok fine, we found our value:
125
    return it->second;
126
}
127
128
//! return the timestamped float for the given joints speed
129
TimestampedList JointInterface::get_ts_speed(int joint_id){
130
    //lock the tsd_list for this access. by doing this we assure
131
    //that no other thread accessing this element can diturb the
132
    //following atomic instructions:
133
    mutex::scoped_lock scoped_lock(joint_ts_speed_map_access_mutex);
134
135
    //search map:
136
    joint_tsl_map_t::iterator it = joint_ts_speed_map.find(joint_id);
137
138
    if (it == joint_ts_speed_map.end()){
139
        printf("> humotion: no ts_speed for joint id 0x%02X found\n", joint_id);
140
        return TimestampedList();
141
    }
142
143
    //ok fine, we found our value:
144
    return it->second;
145
}
146
147
//! set framerate
148
void JointInterface::set_framerate(float f){
149
    framerate = f;
150
}
151
152
//! enable all mouth joints
153
void JointInterface::enable_mouth_joints(){
154
    //already enabled? skip this
155
    if (mouth_enabled){
156
        return;
157
    }
158
159
    printf("> humotion: ENABLING MOUTH JOINTS\n");
160
    enable_joint(ID_LIP_LEFT_UPPER);
161
    enable_joint(ID_LIP_LEFT_LOWER);
162
    enable_joint(ID_LIP_CENTER_UPPER);
163
    enable_joint(ID_LIP_CENTER_LOWER);
164
    enable_joint(ID_LIP_RIGHT_UPPER);
165
    enable_joint(ID_LIP_RIGHT_LOWER);
166
    mouth_enabled = true;
167
}
168
169
170
//! disable all mouth joints
171
void JointInterface::disable_mouth_joints(){
172
    //already disabled? skip this
173
    if (!mouth_enabled){
174
        return;
175
    }
176
177
    printf("> humotion: DISABLING MOUTH JOINTS\n");
178
    disable_joint(ID_LIP_LEFT_UPPER);
179
    disable_joint(ID_LIP_LEFT_LOWER);
180
    disable_joint(ID_LIP_CENTER_UPPER);
181
    disable_joint(ID_LIP_CENTER_LOWER);
182
    disable_joint(ID_LIP_RIGHT_UPPER);
183
    disable_joint(ID_LIP_RIGHT_LOWER);
184
    mouth_enabled = false;
185
}
186
187
//! enable all gaze joints
188
void JointInterface::enable_gaze_joints(){
189
    //already enabled? skip this
190
    if (gaze_enabled){
191
        return;
192
    }
193
194
    printf("> humotion: ENABLING GAZE JOINTS\n");
195
    enable_joint(ID_EYES_LEFT_LR);
196
    enable_joint(ID_EYES_RIGHT_LR);
197
    enable_joint(ID_EYES_BOTH_UD);
198
199
    enable_joint(ID_EYES_LEFT_LID_UPPER);
200
    enable_joint(ID_EYES_LEFT_LID_LOWER);
201
    enable_joint(ID_EYES_RIGHT_LID_UPPER);
202
    enable_joint(ID_EYES_RIGHT_LID_LOWER);
203
204
    enable_joint(ID_EYES_LEFT_BROW);
205
    enable_joint(ID_EYES_RIGHT_BROW);
206
207
    enable_joint(ID_NECK_PAN);
208
    enable_joint(ID_NECK_TILT);
209
    enable_joint(ID_NECK_ROLL);
210
211
    gaze_enabled = true;
212
}
213
214
//! disable all gaze joints
215
void JointInterface::disable_gaze_joints(){
216
    //already disabled? skip this
217
    if (!gaze_enabled){
218
        return;
219
    }
220
221
    printf("> humotion: DISABLING GAZE JOINTS\n");
222
    disable_joint(ID_EYES_LEFT_LR);
223
    disable_joint(ID_EYES_RIGHT_LR);
224
    disable_joint(ID_EYES_BOTH_UD);
225
226
    disable_joint(ID_EYES_LEFT_LID_UPPER);
227
    disable_joint(ID_EYES_LEFT_LID_LOWER);
228
    disable_joint(ID_EYES_RIGHT_LID_UPPER);
229
    disable_joint(ID_EYES_RIGHT_LID_LOWER);
230
231
    disable_joint(ID_EYES_LEFT_BROW);
232
    disable_joint(ID_EYES_RIGHT_BROW);
233
234
    disable_joint(ID_NECK_PAN);
235
    disable_joint(ID_NECK_TILT);
236
    disable_joint(ID_NECK_ROLL);
237
238
    gaze_enabled = false;
239
}
240
241
//! fetch maximum allowable joint position
242
//! \return max position
243
float JointInterface::get_joint_max(int joint_id){
244
    assert((joint_id > 0) && (joint_id < JOINT_ID_ENUM_SIZE));
245
    return joint_max[joint_id];
246
}
247
248
//! fetch minimum allowable joint position
249
//! \return min position
250
float JointInterface::get_joint_min(int joint_id){
251
    assert((joint_id > 0) && (joint_id < JOINT_ID_ENUM_SIZE));
252
    return joint_min[joint_id];
253
}
254
255
//! check if joint position map is empty
256
//! \return true if empty
257
bool JointInterface::get_joint_position_map_empty(){
258
    return (joint_ts_position_map.empty());
259
}