Statistics
| Branch: | Tag: | Revision:

humotion / src / server / motion_generator.cpp @ 01fafd4d

History | View | Annotate | Download (5.22 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 <boost/date_time/posix_time/posix_time.hpp>
29
#include "humotion/server/motion_generator.h"
30 8c6c1163 Simon Schulz
31 0c8d22a5 sschulz
using humotion::server::MotionGenerator;
32 6d13138a sschulz
using humotion::server::Config;
33 8c6c1163 Simon Schulz
34
//! constructor
35 6d13138a sschulz
MotionGenerator::MotionGenerator(JointInterface *j, Config *cfg) {
36
    config = cfg;
37 ea068cf1 sschulz
    joint_interface_ = j;
38
    last_mouth_target_update_ = boost::posix_time::ptime(boost::posix_time::min_date_time);
39
    last_gaze_target_update_  = boost::posix_time::ptime(boost::posix_time::min_date_time);
40 8c6c1163 Simon Schulz
}
41
42
43
//! destructor
44 0c8d22a5 sschulz
MotionGenerator::~MotionGenerator() {
45 8c6c1163 Simon Schulz
}
46
47 730467d3 Simon Schulz
//! fetch the latest position and velocity and return the timestamp of that dataset
48
//! \param joint id
49
//! \param pointer to position variable
50
//! \param pointer to veolocity variable
51
//! \return Timestamp of this dataset
52
humotion::Timestamp MotionGenerator::get_timestamped_state(int joint_id,
53
                                                           float *position, float *velocity) {
54 ea068cf1 sschulz
    humotion::Timestamp stamp = joint_interface_->get_ts_position(joint_id).get_last_timestamp();
55
    *position = joint_interface_->get_ts_position(joint_id).get_interpolated_value(stamp);
56
    *velocity = joint_interface_->get_ts_speed(joint_id).get_interpolated_value(stamp);
57 730467d3 Simon Schulz
    return stamp;
58
}
59
60 0d0f5ca1 Simon Schulz
//! fetch the latest (=current) speed of a joint
61
//! \param joint_id
62
//! \return float value of joint speed
63 0c8d22a5 sschulz
float MotionGenerator::get_current_speed(int joint_id) {
64 ea068cf1 sschulz
    return joint_interface_->get_ts_speed(joint_id).get_newest_value();
65 0d0f5ca1 Simon Schulz
}
66
67
//! fetch the latest (=current) position of a joint
68 8c6c1163 Simon Schulz
//! \param joint_id
69
//! \return float value of joint position
70 0c8d22a5 sschulz
float MotionGenerator::get_current_position(int joint_id) {
71 21444915 Simon Schulz
    /*Timestamp tsl = joint_interface->get_ts_position(joint_id).get_last_timestamp();
72
    Timestamp now = Timestamp::now();
73
    Timestamp diff=now-tsl;
74
    printf("TIME DIFF %fs %fns\n",diff.sec, diff.nsec);*/
75
76 ea068cf1 sschulz
    return joint_interface_->get_ts_position(joint_id).get_newest_value();
77 8c6c1163 Simon Schulz
}
78
79
//! update gaze target:
80
//! \param GazeState with target values for the overall gaze
81 0c8d22a5 sschulz
void MotionGenerator::set_gaze_target(GazeState new_gaze_target) {
82
    // store value for next iteration
83 ea068cf1 sschulz
    requested_gaze_state_        = new_gaze_target;
84 8c6c1163 Simon Schulz
85 0c8d22a5 sschulz
    // keep track if the gaze targets are comming in regulary
86 ea068cf1 sschulz
    last_gaze_target_update_ = boost::get_system_time();
87 8c6c1163 Simon Schulz
}
88
89
//! update mouth state:
90
//! \param MouthState with target values for the mouth joints
91 0c8d22a5 sschulz
void MotionGenerator::set_mouth_target(MouthState s) {
92
    // store value
93 ea068cf1 sschulz
    requested_mouth_target_ = s;
94 8c6c1163 Simon Schulz
95 0c8d22a5 sschulz
    // keep track if the mouth targets are comming in regulary
96 ea068cf1 sschulz
    last_mouth_target_update_ = boost::get_system_time();
97 8c6c1163 Simon Schulz
}
98
99
100
//! was there incoming gaze data the last second?
101
//! \return true if there was data incoming in the last second, false otherwise
102 0c8d22a5 sschulz
bool MotionGenerator::gaze_target_input_active() {
103 ea068cf1 sschulz
    if (last_gaze_target_update_  + boost::posix_time::milliseconds(1000)
104 0c8d22a5 sschulz
            > boost::get_system_time() ) {
105
        // incoming data -> if gaze is disabled, enable it!
106 ea068cf1 sschulz
        joint_interface_->enable_gaze_joints();
107 8c6c1163 Simon Schulz
        return true;
108
    }
109 0c8d22a5 sschulz
110
    // else: no incoming data, disable!
111 ea068cf1 sschulz
    joint_interface_->disable_gaze_joints();
112 0c8d22a5 sschulz
    return false;
113 8c6c1163 Simon Schulz
}
114
115
//! was there incoming mouth data the last second?
116
//! \return true if there was data incoming in the last second, false otherwise
117 0c8d22a5 sschulz
bool MotionGenerator::mouth_target_input_active() {
118 ea068cf1 sschulz
    if (last_mouth_target_update_ + boost::posix_time::milliseconds(1000)
119 0c8d22a5 sschulz
            > boost::get_system_time() ) {
120
        // incoming data -> if mouth is disabled, enable it!
121 ea068cf1 sschulz
        joint_interface_->enable_mouth_joints();
122 8c6c1163 Simon Schulz
        return true;
123
    }
124 0c8d22a5 sschulz
125
    // else: no incoming data, disable!
126 ea068cf1 sschulz
    joint_interface_->disable_mouth_joints();
127 0c8d22a5 sschulz
    return false;
128 8c6c1163 Simon Schulz
}
129
130
//! limit target to min/max bounds:
131 0c8d22a5 sschulz
float MotionGenerator::limit_target(int joint_id, float val) {
132 8c6c1163 Simon Schulz
    assert(joint_id < JointInterface::JOINT_ID_ENUM_SIZE);
133
134 0c8d22a5 sschulz
    // fetch min/max for joint:
135 ea068cf1 sschulz
    float min = joint_interface_->get_joint_min(joint_id);
136
    float max = joint_interface_->get_joint_max(joint_id);
137 8c6c1163 Simon Schulz
138 0c8d22a5 sschulz
    if (max < min) {
139
        printf("> ERROR: how can min (%4.2f) be bigger than max (%4.2f)?? EXITING NOW\n", min, max);
140
        printf("> HINT : did you initialize the joints' min/max positions properly?\n");
141 8c6c1163 Simon Schulz
        exit(0);
142
    }
143
144
    val = fmin(val, max);
145
    val = fmax(val, min);
146
147
    return val;
148
}