Statistics
| Branch: | Tag: | Revision:

humotion / src / server / motion_generator.cpp @ 497d9d24

History | View | Annotate | Download (4.361 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/motion_generator.h"
29
#include "boost/date_time/posix_time/posix_time.hpp"
30

    
31
using namespace std;
32
using namespace humotion;
33
using namespace humotion::server;
34
using namespace boost;
35

    
36
//! constructor
37
MotionGenerator::MotionGenerator(JointInterface *j){
38
    joint_interface = j;
39
    last_mouth_target_update = posix_time::ptime(posix_time::min_date_time);
40
    last_gaze_target_update  = posix_time::ptime(posix_time::min_date_time);
41
}
42

    
43

    
44
//! destructor
45
MotionGenerator::~MotionGenerator(){
46
}
47

    
48
//! fetch the latest (=current) speed of a joint
49
//! \param joint_id
50
//! \return float value of joint speed
51
float MotionGenerator::get_current_speed(int joint_id){
52
    return joint_interface->get_ts_speed(joint_id).get_newest_value();
53
}
54

    
55
//! fetch the latest (=current) position of a joint
56
//! \param joint_id
57
//! \return float value of joint position
58
float MotionGenerator::get_current_position(int joint_id){
59
    /*Timestamp tsl = joint_interface->get_ts_position(joint_id).get_last_timestamp();
60
    Timestamp now = Timestamp::now();
61
    Timestamp diff=now-tsl;
62
    printf("TIME DIFF %fs %fns\n",diff.sec, diff.nsec);*/
63

    
64
    return joint_interface->get_ts_position(joint_id).get_newest_value();
65
}
66

    
67
//! update gaze target:
68
//! \param GazeState with target values for the overall gaze
69
void MotionGenerator::set_gaze_target(GazeState new_gaze_target){
70
    //store value for next iteration
71
    requested_gaze_state        = new_gaze_target;
72

    
73
    //keep track if the gaze targets are comming in regulary
74
    last_gaze_target_update = get_system_time();
75
}
76

    
77
//! update mouth state:
78
//! \param MouthState with target values for the mouth joints
79
void MotionGenerator::set_mouth_target(MouthState s){
80
    //store value
81
    requested_mouth_target = s;
82

    
83
    //keep track if the mouth targets are comming in regulary
84
    last_mouth_target_update = get_system_time();
85
}
86

    
87

    
88
//! was there incoming gaze data the last second?
89
//! \return true if there was data incoming in the last second, false otherwise
90
bool MotionGenerator::gaze_target_input_active(){
91
    if (last_gaze_target_update  + posix_time::milliseconds(1000) > get_system_time() ){
92
        //incoming data -> if gaze is disabled, enable it!
93
        joint_interface->enable_gaze_joints();
94
        return true;
95
    }else{
96
        //no incoming data, disable!
97
        joint_interface->disable_gaze_joints();
98
        return false;
99
    }
100
}
101

    
102
//! was there incoming mouth data the last second?
103
//! \return true if there was data incoming in the last second, false otherwise
104
bool MotionGenerator::mouth_target_input_active(){
105
    if (last_mouth_target_update + posix_time::milliseconds(1000) > get_system_time() ){
106
        //incoming data -> if mouth is disabled, enable it!
107
        joint_interface->enable_mouth_joints();
108
        return true;
109
    }else{
110
        //no incoming data, disable!
111
        joint_interface->disable_mouth_joints();
112
        return false;
113
    }
114
}
115

    
116
//! limit target to min/max bounds:
117
float MotionGenerator::limit_target(int joint_id, float val){
118
    assert(joint_id < JointInterface::JOINT_ID_ENUM_SIZE);
119

    
120
    //fetch min/max for joint:
121
    float min = joint_interface->get_joint_min(joint_id);
122
    float max = joint_interface->get_joint_max(joint_id);
123

    
124
    if (max < min){
125
        printf("> ERROR: how can min (%4.2f) be bigger than max (%4.2f)?? FIX THIS! EXITING NOW\n",min,max);
126
        exit(0);
127
    }
128

    
129
    val = fmin(val, max);
130
    val = fmax(val, min);
131

    
132
    return val;
133
}
134