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