humotion / src / server / motion_generator.cpp @ 25400c71
History | View | Annotate | Download (5.22 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 |
#include "humotion/server/motion_generator.h" |
30 |
|
31 |
using humotion::server::MotionGenerator;
|
32 |
using humotion::server::Config;
|
33 |
|
34 |
//! constructor
|
35 |
MotionGenerator::MotionGenerator(JointInterface *j, Config *cfg) { |
36 |
config = cfg; |
37 |
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 |
} |
41 |
|
42 |
|
43 |
//! destructor
|
44 |
MotionGenerator::~MotionGenerator() { |
45 |
} |
46 |
|
47 |
//! 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 |
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 |
return stamp;
|
58 |
} |
59 |
|
60 |
//! fetch the latest (=current) speed of a joint
|
61 |
//! \param joint_id
|
62 |
//! \return float value of joint speed
|
63 |
float MotionGenerator::get_current_speed(int joint_id) { |
64 |
return joint_interface_->get_ts_speed(joint_id).get_newest_value();
|
65 |
} |
66 |
|
67 |
//! fetch the latest (=current) position of a joint
|
68 |
//! \param joint_id
|
69 |
//! \return float value of joint position
|
70 |
float MotionGenerator::get_current_position(int joint_id) { |
71 |
/*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 |
return joint_interface_->get_ts_position(joint_id).get_newest_value();
|
77 |
} |
78 |
|
79 |
//! update gaze target:
|
80 |
//! \param GazeState with target values for the overall gaze
|
81 |
void MotionGenerator::set_gaze_target(GazeState new_gaze_target) {
|
82 |
// store value for next iteration
|
83 |
requested_gaze_state_ = new_gaze_target; |
84 |
|
85 |
// keep track if the gaze targets are comming in regulary
|
86 |
last_gaze_target_update_ = boost::get_system_time(); |
87 |
} |
88 |
|
89 |
//! update mouth state:
|
90 |
//! \param MouthState with target values for the mouth joints
|
91 |
void MotionGenerator::set_mouth_target(MouthState s) {
|
92 |
// store value
|
93 |
requested_mouth_target_ = s; |
94 |
|
95 |
// keep track if the mouth targets are comming in regulary
|
96 |
last_mouth_target_update_ = boost::get_system_time(); |
97 |
} |
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 |
bool MotionGenerator::gaze_target_input_active() {
|
103 |
if (last_gaze_target_update_ + boost::posix_time::milliseconds(1000) |
104 |
> boost::get_system_time() ) { |
105 |
// incoming data -> if gaze is disabled, enable it!
|
106 |
joint_interface_->enable_gaze_joints(); |
107 |
return true; |
108 |
} |
109 |
|
110 |
// else: no incoming data, disable!
|
111 |
joint_interface_->disable_gaze_joints(); |
112 |
return false; |
113 |
} |
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 |
bool MotionGenerator::mouth_target_input_active() {
|
118 |
if (last_mouth_target_update_ + boost::posix_time::milliseconds(1000) |
119 |
> boost::get_system_time() ) { |
120 |
// incoming data -> if mouth is disabled, enable it!
|
121 |
joint_interface_->enable_mouth_joints(); |
122 |
return true; |
123 |
} |
124 |
|
125 |
// else: no incoming data, disable!
|
126 |
joint_interface_->disable_mouth_joints(); |
127 |
return false; |
128 |
} |
129 |
|
130 |
//! limit target to min/max bounds:
|
131 |
float MotionGenerator::limit_target(int joint_id, float val) { |
132 |
assert(joint_id < JointInterface::JOINT_ID_ENUM_SIZE); |
133 |
|
134 |
// fetch min/max for joint:
|
135 |
float min = joint_interface_->get_joint_min(joint_id);
|
136 |
float max = joint_interface_->get_joint_max(joint_id);
|
137 |
|
138 |
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 |
exit(0);
|
142 |
} |
143 |
|
144 |
val = fmin(val, max); |
145 |
val = fmax(val, min); |
146 |
|
147 |
return val;
|
148 |
} |
149 |
|