Statistics
| Branch: | Tag: | Revision:

humotion / src / server / mouth_motion_generator.cpp @ 58c9f56c

History | View | Annotate | Download (5.994 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/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 6c893fc3 Simon Schulz
        //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
    //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 3218ba65 Simon Schulz
        //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
    if (distance_max_lower < 0){
88
        //oops, we will get to close to the safe border, abort move!
89 3218ba65 Simon Schulz
        //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
    //tell the joint about the new values:
95 21444915 Simon Schulz
    joint_interface->set_target_position(upper_id, unsafe_target_upper, 0.0);
96
    joint_interface->set_target_position(lower_id, unsafe_target_lower, 0.0);
97 8c6c1163 Simon Schulz
98
    //printf("> update_mouth_target(u=%d, l=%d) -> upper = %4.2f, lower = %4.2f\n",upper_id, lower_id,unsafe_target_upper,unsafe_target_lower);
99
}
100
101
102
//! extract opening from mouth state for given joint enum id
103
//! \param MouthState m
104
//! \param int enum with joint id
105
float MouthMotionGenerator::mouthstate_to_opening(MouthState m, int e){
106
    switch (e){
107 7ed40bef Simon Schulz
    default: printf("> get_opening(0x%02X): invalid joint enum!\n",e); exit(0);
108
    case(JointInterface::ID_LIP_LEFT_UPPER):
109
    case(JointInterface::ID_LIP_LEFT_LOWER):
110
        return m.opening_left;
111
    case(JointInterface::ID_LIP_CENTER_UPPER):
112
    case(JointInterface::ID_LIP_CENTER_LOWER):
113
        return m.opening_center;
114
    case(JointInterface::ID_LIP_RIGHT_UPPER):
115
    case(JointInterface::ID_LIP_RIGHT_LOWER):
116
        return m.opening_right;
117 8c6c1163 Simon Schulz
    }
118
}
119
120
//! extract position from mouth state for given joint enum id
121
//! \param MouthState m
122
//! \param int enum with joint id
123
float MouthMotionGenerator::mouthstate_to_position(MouthState m, int e){
124
    switch (e){
125 7ed40bef Simon Schulz
    default: printf("> get_position(0x%02X): invalid joint enum!\n",e); exit(0);
126
    case(JointInterface::ID_LIP_LEFT_UPPER):
127
    case(JointInterface::ID_LIP_LEFT_LOWER):
128
        return m.position_left;
129
    case(JointInterface::ID_LIP_CENTER_UPPER):
130
    case(JointInterface::ID_LIP_CENTER_LOWER):
131
        return m.position_center;
132
    case(JointInterface::ID_LIP_RIGHT_UPPER):
133
    case(JointInterface::ID_LIP_RIGHT_LOWER):
134
        return m.position_right;
135 8c6c1163 Simon Schulz
    }
136
}
137
138
//! publish targets to motor boards:
139
void MouthMotionGenerator::publish_targets(){
140
    //publish values if there is an active gaze input within the last timerange
141
    if (mouth_target_input_active()){
142
        joint_interface->publish_target_position(JointInterface::ID_LIP_LEFT_UPPER);
143
        joint_interface->publish_target_position(JointInterface::ID_LIP_LEFT_LOWER);
144
        joint_interface->publish_target_position(JointInterface::ID_LIP_CENTER_UPPER);
145
        joint_interface->publish_target_position(JointInterface::ID_LIP_CENTER_LOWER);
146
        joint_interface->publish_target_position(JointInterface::ID_LIP_RIGHT_UPPER);
147
        joint_interface->publish_target_position(JointInterface::ID_LIP_RIGHT_LOWER);
148
    }
149
}