Statistics
| Branch: | Tag: | Revision:

humotion / src / server / motion_generator.cpp @ 0c8d22a5

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