Statistics
| Branch: | Tag: | Revision:

humotion / src / server / mouth_motion_generator.cpp @ b6e7118f

History | View | Annotate | Download (5.817 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/mouth_motion_generator.h"
29 8c6c1163 Simon Schulz
30 0c8d22a5 sschulz
using humotion::server::MouthMotionGenerator;
31 8c6c1163 Simon Schulz
32 0c8d22a5 sschulz
// minimum mouth opening
33
const float MouthMotionGenerator::MOUTH_MIN_OPENING = 9.0;  // mm
34 8c6c1163 Simon Schulz
35
//! constructor
36 0c8d22a5 sschulz
MouthMotionGenerator::MouthMotionGenerator(JointInterface *j) : MotionGenerator(j) {
37 8c6c1163 Simon Schulz
}
38
39
//! destructor
40 0c8d22a5 sschulz
MouthMotionGenerator::~MouthMotionGenerator() {
41 8c6c1163 Simon Schulz
}
42
43
//! calculate joint targets
44 0c8d22a5 sschulz
void MouthMotionGenerator::calculate_targets() {
45
    // printf("> humotion: calculating mouth targets\n");
46 8c6c1163 Simon Schulz
    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 0c8d22a5 sschulz
void MouthMotionGenerator::update_mouth_target(int upper_id, int lower_id) {
55
    // fetch min/max for joints
56 ea068cf1 sschulz
    float min_upper = joint_interface_->get_joint_min(upper_id);
57
    float max_lower = joint_interface_->get_joint_max(lower_id);
58 8c6c1163 Simon Schulz
    float min_opening = MOUTH_MIN_OPENING;
59
60 0c8d22a5 sschulz
    // fetch position & opening for joint, parameter is only used to
61
    // determine LEFT/CENTER/RIGHT. upper/lower plays no role here
62 ea068cf1 sschulz
    float position = mouthstate_to_position(requested_mouth_target_, upper_id);
63
    float opening  = min_opening + mouthstate_to_opening(requested_mouth_target_, upper_id);
64 8c6c1163 Simon Schulz
65 0c8d22a5 sschulz
    // 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 8c6c1163 Simon Schulz
        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 0c8d22a5 sschulz
    // now check if this would exceed the allowed limits for that joint
76 8c6c1163 Simon Schulz
    float distance_min_upper = unsafe_target_upper - min_upper;
77 0c8d22a5 sschulz
    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 8c6c1163 Simon Schulz
        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 0c8d22a5 sschulz
    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 8c6c1163 Simon Schulz
        unsafe_target_lower = max_lower;
89
        unsafe_target_upper = unsafe_target_lower - opening;
90
    }
91
92 0c8d22a5 sschulz
    // tell the joint about the new values
93 ea068cf1 sschulz
    joint_interface_->set_target(upper_id, unsafe_target_upper, 0.0);
94
    joint_interface_->set_target(lower_id, unsafe_target_lower, 0.0);
95 8c6c1163 Simon Schulz
}
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 0c8d22a5 sschulz
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 7ed40bef Simon Schulz
    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 8c6c1163 Simon Schulz
    }
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 0c8d22a5 sschulz
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 7ed40bef Simon Schulz
    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 8c6c1163 Simon Schulz
    }
132
}
133
134
//! publish targets to motor boards:
135 0c8d22a5 sschulz
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 ea068cf1 sschulz
        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 8c6c1163 Simon Schulz
    }
145
}