humotion / src / server / mouth_motion_generator.cpp @ ea068cf1
History | View | Annotate | Download (5.817 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/mouth_motion_generator.h" |
29 |
|
30 |
using humotion::server::MouthMotionGenerator;
|
31 |
|
32 |
// minimum mouth opening
|
33 |
const float MouthMotionGenerator::MOUTH_MIN_OPENING = 9.0; // mm |
34 |
|
35 |
//! constructor
|
36 |
MouthMotionGenerator::MouthMotionGenerator(JointInterface *j) : MotionGenerator(j) { |
37 |
} |
38 |
|
39 |
//! destructor
|
40 |
MouthMotionGenerator::~MouthMotionGenerator() { |
41 |
} |
42 |
|
43 |
//! calculate joint targets
|
44 |
void MouthMotionGenerator::calculate_targets() {
|
45 |
// printf("> humotion: calculating mouth targets\n");
|
46 |
update_mouth_target(JointInterface::ID_LIP_LEFT_UPPER, JointInterface::ID_LIP_LEFT_LOWER); |
47 |
update_mouth_target(JointInterface::ID_LIP_CENTER_UPPER, JointInterface::ID_LIP_CENTER_LOWER); |
48 |
update_mouth_target(JointInterface::ID_LIP_RIGHT_UPPER, JointInterface::ID_LIP_RIGHT_LOWER); |
49 |
} |
50 |
|
51 |
//! calculate mouth target angles for a given combination of upper/lower joints
|
52 |
//! \param int upper joint id
|
53 |
//! \param int lower joint id
|
54 |
void MouthMotionGenerator::update_mouth_target(int upper_id, int lower_id) { |
55 |
// fetch min/max for joints
|
56 |
float min_upper = joint_interface_->get_joint_min(upper_id);
|
57 |
float max_lower = joint_interface_->get_joint_max(lower_id);
|
58 |
float min_opening = MOUTH_MIN_OPENING;
|
59 |
|
60 |
// fetch position & opening for joint, parameter is only used to
|
61 |
// determine LEFT/CENTER/RIGHT. upper/lower plays no role here
|
62 |
float position = mouthstate_to_position(requested_mouth_target_, upper_id);
|
63 |
float opening = min_opening + mouthstate_to_opening(requested_mouth_target_, upper_id);
|
64 |
|
65 |
// check opening larger than minimum
|
66 |
if (opening < min_opening) {
|
67 |
// oops, not safe to move to this opening, abort move!
|
68 |
// printf("> invalid opening (%f)\n", opening);
|
69 |
opening = min_opening; |
70 |
} |
71 |
|
72 |
float unsafe_target_upper = position - opening/2.0; |
73 |
float unsafe_target_lower = position + opening/2.0; |
74 |
|
75 |
// now check if this would exceed the allowed limits for that joint
|
76 |
float distance_min_upper = unsafe_target_upper - min_upper;
|
77 |
if (distance_min_upper < 0) { |
78 |
// oops, we will get to close to the safe border, abort move!
|
79 |
// printf("> collision on TOP [u=%4.2f l=%4.2f]\n",unsafe_target_upper,unsafe_target_lower);
|
80 |
unsafe_target_upper = min_upper; |
81 |
unsafe_target_lower = unsafe_target_upper + opening; |
82 |
} |
83 |
|
84 |
float distance_max_lower = max_lower - unsafe_target_lower;
|
85 |
if (distance_max_lower < 0) { |
86 |
// oops, we will get to close to the safe border, abort move!
|
87 |
// printf("> collision on BOT[u=%4.2f l=%4.2f]\n",unsafe_target_upper,unsafe_target_lower);
|
88 |
unsafe_target_lower = max_lower; |
89 |
unsafe_target_upper = unsafe_target_lower - opening; |
90 |
} |
91 |
|
92 |
// tell the joint about the new values
|
93 |
joint_interface_->set_target(upper_id, unsafe_target_upper, 0.0); |
94 |
joint_interface_->set_target(lower_id, unsafe_target_lower, 0.0); |
95 |
} |
96 |
|
97 |
|
98 |
//! extract opening from mouth state for given joint enum id
|
99 |
//! \param MouthState m
|
100 |
//! \param int enum with joint id
|
101 |
float MouthMotionGenerator::mouthstate_to_opening(MouthState m, int e) { |
102 |
switch (e) {
|
103 |
default: printf("> get_opening(0x%02X): invalid joint enum!\n", e); exit(EXIT_FAILURE); |
104 |
case(JointInterface::ID_LIP_LEFT_UPPER):
|
105 |
case(JointInterface::ID_LIP_LEFT_LOWER):
|
106 |
return m.opening_left;
|
107 |
case(JointInterface::ID_LIP_CENTER_UPPER):
|
108 |
case(JointInterface::ID_LIP_CENTER_LOWER):
|
109 |
return m.opening_center;
|
110 |
case(JointInterface::ID_LIP_RIGHT_UPPER):
|
111 |
case(JointInterface::ID_LIP_RIGHT_LOWER):
|
112 |
return m.opening_right;
|
113 |
} |
114 |
} |
115 |
|
116 |
//! extract position from mouth state for given joint enum id
|
117 |
//! \param MouthState m
|
118 |
//! \param int enum with joint id
|
119 |
float MouthMotionGenerator::mouthstate_to_position(MouthState m, int e) { |
120 |
switch (e) {
|
121 |
default: printf("> get_position(0x%02X): invalid joint enum!\n", e); exit(EXIT_FAILURE); |
122 |
case(JointInterface::ID_LIP_LEFT_UPPER):
|
123 |
case(JointInterface::ID_LIP_LEFT_LOWER):
|
124 |
return m.position_left;
|
125 |
case(JointInterface::ID_LIP_CENTER_UPPER):
|
126 |
case(JointInterface::ID_LIP_CENTER_LOWER):
|
127 |
return m.position_center;
|
128 |
case(JointInterface::ID_LIP_RIGHT_UPPER):
|
129 |
case(JointInterface::ID_LIP_RIGHT_LOWER):
|
130 |
return m.position_right;
|
131 |
} |
132 |
} |
133 |
|
134 |
//! publish targets to motor boards:
|
135 |
void MouthMotionGenerator::publish_targets() {
|
136 |
// publish values if there is an active gaze input within the last timerange
|
137 |
if (mouth_target_input_active()) {
|
138 |
joint_interface_->publish_target(JointInterface::ID_LIP_LEFT_UPPER); |
139 |
joint_interface_->publish_target(JointInterface::ID_LIP_LEFT_LOWER); |
140 |
joint_interface_->publish_target(JointInterface::ID_LIP_CENTER_UPPER); |
141 |
joint_interface_->publish_target(JointInterface::ID_LIP_CENTER_LOWER); |
142 |
joint_interface_->publish_target(JointInterface::ID_LIP_RIGHT_UPPER); |
143 |
joint_interface_->publish_target(JointInterface::ID_LIP_RIGHT_LOWER); |
144 |
} |
145 |
} |
146 |
|