Statistics
| Branch: | Tag: | Revision:

humotion / src / server / motion_generator.cpp @ 45091351

History | View | Annotate | Download (5.931 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 <boost/date_time/posix_time/posix_time.hpp>
29

    
30
#include <utility>
31

    
32
#include "humotion/server/motion_generator.h"
33

    
34
using humotion::server::MotionGenerator;
35
using humotion::server::Config;
36
using humotion::server::debug_data_t;
37

    
38
//! constructor
39
MotionGenerator::MotionGenerator(JointInterface *j, Config *cfg) {
40
    config = cfg;
41
    joint_interface_ = j;
42
    last_mouth_target_update_ = boost::posix_time::ptime(boost::posix_time::min_date_time);
43
    last_gaze_target_update_  = boost::posix_time::ptime(boost::posix_time::min_date_time);
44
}
45

    
46

    
47
//! destructor
48
MotionGenerator::~MotionGenerator() {
49
}
50

    
51
//! return all accumulated debug data
52
debug_data_t MotionGenerator::get_debug_data() {
53
    return debug_data_;
54
}
55

    
56
//! store debug data
57
void MotionGenerator::store_debug_data(std::string name, float value) {
58
    debug_data_[name] = value;
59
}
60

    
61
//! fetch the latest position and velocity and return the timestamp of that dataset
62
//! \param joint id
63
//! \param pointer to position variable
64
//! \param pointer to velocity variable
65
//! \return Timestamp of this dataset
66
humotion::Timestamp MotionGenerator::get_timestamped_state(int joint_id,
67
                                                           float *position, float *velocity) {
68
    humotion::Timestamp stamp_p = joint_interface_->get_ts_position(joint_id).get_last_timestamp();
69
    humotion::Timestamp stamp_v = joint_interface_->get_ts_speed(joint_id).get_last_timestamp();
70
    humotion::Timestamp stamp;
71

    
72
    if (stamp_v < stamp_p) {
73
        // right now there is no velocity with that timestamp yet, therefore use the velocity ts
74
        // for both:
75
        stamp = stamp_v;
76
    } else {
77
        // both are available at the position ts, use that one
78
        stamp = stamp_p;
79
    }
80

    
81
    // fetch data
82
    *position = joint_interface_->get_ts_position(joint_id).get_interpolated_value(stamp);
83
    *velocity = joint_interface_->get_ts_speed(joint_id).get_interpolated_value(stamp);
84
    return stamp;
85
}
86

    
87
//! fetch the latest (=current) speed of a joint
88
//! \param joint_id
89
//! \return float value of joint speed
90
float MotionGenerator::get_current_speed(int joint_id) {
91
    return joint_interface_->get_ts_speed(joint_id).get_newest_value();
92
}
93

    
94
//! fetch the latest (=current) position of a joint
95
//! \param joint_id
96
//! \return float value of joint position
97
float MotionGenerator::get_current_position(int joint_id) {
98
    /*Timestamp tsl = joint_interface->get_ts_position(joint_id).get_last_timestamp();
99
    Timestamp now = Timestamp::now();
100
    Timestamp diff=now-tsl;
101
    printf("TIME DIFF %fs %fns\n",diff.sec, diff.nsec);*/
102

    
103
    return joint_interface_->get_ts_position(joint_id).get_newest_value();
104
}
105

    
106
//! update gaze target:
107
//! \param GazeState with target values for the overall gaze
108
void MotionGenerator::set_gaze_target(GazeState new_gaze_target) {
109
    // store value for next iteration
110
    requested_gaze_state_        = new_gaze_target;
111

    
112
    // keep track if the gaze targets are comming in regulary
113
    last_gaze_target_update_ = boost::get_system_time();
114
}
115

    
116
//! update mouth state:
117
//! \param MouthState with target values for the mouth joints
118
void MotionGenerator::set_mouth_target(MouthState s) {
119
    // store value
120
    requested_mouth_target_ = s;
121

    
122
    // keep track if the mouth targets are comming in regulary
123
    last_mouth_target_update_ = boost::get_system_time();
124
}
125

    
126

    
127
//! was there incoming gaze data the last second?
128
//! \return true if there was data incoming in the last second, false otherwise
129
bool MotionGenerator::gaze_target_input_active() {
130
    if (last_gaze_target_update_  + boost::posix_time::milliseconds(1000)
131
            > boost::get_system_time() ) {
132
        // incoming data -> if gaze is disabled, enable it!
133
        joint_interface_->enable_gaze_joints();
134
        return true;
135
    }
136

    
137
    // else: no incoming data, disable!
138
    joint_interface_->disable_gaze_joints();
139
    return false;
140
}
141

    
142
//! was there incoming mouth data the last second?
143
//! \return true if there was data incoming in the last second, false otherwise
144
bool MotionGenerator::mouth_target_input_active() {
145
    if (last_mouth_target_update_ + boost::posix_time::milliseconds(1000)
146
            > boost::get_system_time() ) {
147
        // incoming data -> if mouth is disabled, enable it!
148
        joint_interface_->enable_mouth_joints();
149
        return true;
150
    }
151

    
152
    // else: no incoming data, disable!
153
    joint_interface_->disable_mouth_joints();
154
    return false;
155
}
156

    
157
//! limit target to min/max bounds:
158
float MotionGenerator::limit_target(int joint_id, float val) {
159
    assert(joint_id < JointInterface::JOINT_ID_ENUM_SIZE);
160

    
161
    // fetch min/max for joint:
162
    float min = joint_interface_->get_joint_min(joint_id);
163
    float max = joint_interface_->get_joint_max(joint_id);
164

    
165
    if (max < min) {
166
        printf("> ERROR: how can min (%4.2f) be bigger than max (%4.2f)?? EXITING NOW\n", min, max);
167
        printf("> HINT : did you initialize the joints' min/max positions properly?\n");
168
        exit(0);
169
    }
170

    
171
    val = fmin(val, max);
172
    val = fmax(val, min);
173

    
174
    return val;
175
}
176