Statistics
| Branch: | Tag: | Revision:

humotion / src / server / mouth_motion_generator.cpp @ dbf66483

History | View | Annotate | Download (5.87 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 6d13138a sschulz
using humotion::server::Config;
32 8c6c1163 Simon Schulz
33 0c8d22a5 sschulz
// minimum mouth opening
34 5fba4e15 sschulz
const float MouthMotionGenerator::MOUTH_MIN_OPENING = 7.0;  // mm
35 8c6c1163 Simon Schulz
36
//! constructor
37 6d13138a sschulz
MouthMotionGenerator::MouthMotionGenerator(JointInterface *j, Config *cfg) :
38
    MotionGenerator(j, cfg) {
39 8c6c1163 Simon Schulz
}
40
41
//! destructor
42 0c8d22a5 sschulz
MouthMotionGenerator::~MouthMotionGenerator() {
43 8c6c1163 Simon Schulz
}
44
45
//! calculate joint targets
46 0c8d22a5 sschulz
void MouthMotionGenerator::calculate_targets() {
47
    // printf("> humotion: calculating mouth targets\n");
48 8c6c1163 Simon Schulz
    update_mouth_target(JointInterface::ID_LIP_LEFT_UPPER,   JointInterface::ID_LIP_LEFT_LOWER);
49
    update_mouth_target(JointInterface::ID_LIP_CENTER_UPPER, JointInterface::ID_LIP_CENTER_LOWER);
50
    update_mouth_target(JointInterface::ID_LIP_RIGHT_UPPER,  JointInterface::ID_LIP_RIGHT_LOWER);
51
}
52
53
//! calculate mouth target angles for a given combination of upper/lower joints
54
//! \param int upper joint id
55
//! \param int lower joint id
56 0c8d22a5 sschulz
void MouthMotionGenerator::update_mouth_target(int upper_id, int lower_id) {
57
    // fetch min/max for joints
58 ea068cf1 sschulz
    float min_upper = joint_interface_->get_joint_min(upper_id);
59
    float max_lower = joint_interface_->get_joint_max(lower_id);
60 8c6c1163 Simon Schulz
    float min_opening = MOUTH_MIN_OPENING;
61
62 0c8d22a5 sschulz
    // fetch position & opening for joint, parameter is only used to
63
    // determine LEFT/CENTER/RIGHT. upper/lower plays no role here
64 ea068cf1 sschulz
    float position = mouthstate_to_position(requested_mouth_target_, upper_id);
65
    float opening  = min_opening + mouthstate_to_opening(requested_mouth_target_, upper_id);
66 8c6c1163 Simon Schulz
67 0c8d22a5 sschulz
    // 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 8c6c1163 Simon Schulz
        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 0c8d22a5 sschulz
    // now check if this would exceed the allowed limits for that joint
78 8c6c1163 Simon Schulz
    float distance_min_upper = unsafe_target_upper - min_upper;
79 0c8d22a5 sschulz
    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 8c6c1163 Simon Schulz
        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 0c8d22a5 sschulz
    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 8c6c1163 Simon Schulz
        unsafe_target_lower = max_lower;
91
        unsafe_target_upper = unsafe_target_lower - opening;
92
    }
93
94 0c8d22a5 sschulz
    // tell the joint about the new values
95 ea068cf1 sschulz
    joint_interface_->set_target(upper_id, unsafe_target_upper, 0.0);
96
    joint_interface_->set_target(lower_id, unsafe_target_lower, 0.0);
97 8c6c1163 Simon Schulz
}
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 0c8d22a5 sschulz
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(EXIT_FAILURE);
106 7ed40bef Simon Schulz
    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 8c6c1163 Simon Schulz
    }
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 0c8d22a5 sschulz
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(EXIT_FAILURE);
124 7ed40bef Simon Schulz
    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 8c6c1163 Simon Schulz
    }
134
}
135
136
//! publish targets to motor boards:
137 0c8d22a5 sschulz
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 ea068cf1 sschulz
        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 8c6c1163 Simon Schulz
    }
147
}