humotion / src / server / motion_generator.cpp @ 8d293778
History | View | Annotate | Download (5.931 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 | 2aa96942 | sschulz | |
30 | #include <utility> |
||
31 | |||
32 | 0c8d22a5 | sschulz | #include "humotion/server/motion_generator.h" |
33 | 8c6c1163 | Simon Schulz | |
34 | 0c8d22a5 | sschulz | using humotion::server::MotionGenerator;
|
35 | 6d13138a | sschulz | using humotion::server::Config;
|
36 | 2aa96942 | sschulz | using humotion::server::debug_data_t;
|
37 | 8c6c1163 | Simon Schulz | |
38 | //! constructor
|
||
39 | 6d13138a | sschulz | MotionGenerator::MotionGenerator(JointInterface *j, Config *cfg) { |
40 | config = cfg; |
||
41 | ea068cf1 | sschulz | 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 | 8c6c1163 | Simon Schulz | } |
45 | |||
46 | |||
47 | //! destructor
|
||
48 | 0c8d22a5 | sschulz | MotionGenerator::~MotionGenerator() { |
49 | 8c6c1163 | Simon Schulz | } |
50 | |||
51 | 2aa96942 | sschulz | //! return all accumulated debug data
|
52 | debug_data_t MotionGenerator::get_debug_data() { |
||
53 | 4b77b008 | Florian Lier | return debug_data_;
|
54 | 2aa96942 | sschulz | } |
55 | |||
56 | //! store debug data
|
||
57 | void MotionGenerator::store_debug_data(std::string name, float value) { |
||
58 | 4b77b008 | Florian Lier | debug_data_[name] = value; |
59 | 2aa96942 | sschulz | } |
60 | |||
61 | 730467d3 | Simon Schulz | //! fetch the latest position and velocity and return the timestamp of that dataset
|
62 | //! \param joint id
|
||
63 | //! \param pointer to position variable
|
||
64 | 2aa96942 | sschulz | //! \param pointer to velocity variable
|
65 | 730467d3 | Simon Schulz | //! \return Timestamp of this dataset
|
66 | humotion::Timestamp MotionGenerator::get_timestamped_state(int joint_id,
|
||
67 | float *position, float *velocity) { |
||
68 | 2aa96942 | sschulz | 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 | ea068cf1 | sschulz | *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 | 730467d3 | Simon Schulz | return stamp;
|
85 | } |
||
86 | |||
87 | 0d0f5ca1 | Simon Schulz | //! fetch the latest (=current) speed of a joint
|
88 | //! \param joint_id
|
||
89 | //! \return float value of joint speed
|
||
90 | 0c8d22a5 | sschulz | float MotionGenerator::get_current_speed(int joint_id) { |
91 | ea068cf1 | sschulz | return joint_interface_->get_ts_speed(joint_id).get_newest_value();
|
92 | 0d0f5ca1 | Simon Schulz | } |
93 | |||
94 | //! fetch the latest (=current) position of a joint
|
||
95 | 8c6c1163 | Simon Schulz | //! \param joint_id
|
96 | //! \return float value of joint position
|
||
97 | 0c8d22a5 | sschulz | float MotionGenerator::get_current_position(int joint_id) { |
98 | 21444915 | Simon Schulz | /*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 | ea068cf1 | sschulz | return joint_interface_->get_ts_position(joint_id).get_newest_value();
|
104 | 8c6c1163 | Simon Schulz | } |
105 | |||
106 | //! update gaze target:
|
||
107 | //! \param GazeState with target values for the overall gaze
|
||
108 | 0c8d22a5 | sschulz | void MotionGenerator::set_gaze_target(GazeState new_gaze_target) {
|
109 | // store value for next iteration
|
||
110 | ea068cf1 | sschulz | requested_gaze_state_ = new_gaze_target; |
111 | 8c6c1163 | Simon Schulz | |
112 | 0c8d22a5 | sschulz | // keep track if the gaze targets are comming in regulary
|
113 | ea068cf1 | sschulz | last_gaze_target_update_ = boost::get_system_time(); |
114 | 8c6c1163 | Simon Schulz | } |
115 | |||
116 | //! update mouth state:
|
||
117 | //! \param MouthState with target values for the mouth joints
|
||
118 | 0c8d22a5 | sschulz | void MotionGenerator::set_mouth_target(MouthState s) {
|
119 | // store value
|
||
120 | ea068cf1 | sschulz | requested_mouth_target_ = s; |
121 | 8c6c1163 | Simon Schulz | |
122 | 0c8d22a5 | sschulz | // keep track if the mouth targets are comming in regulary
|
123 | ea068cf1 | sschulz | last_mouth_target_update_ = boost::get_system_time(); |
124 | 8c6c1163 | Simon Schulz | } |
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 | 0c8d22a5 | sschulz | bool MotionGenerator::gaze_target_input_active() {
|
130 | ea068cf1 | sschulz | if (last_gaze_target_update_ + boost::posix_time::milliseconds(1000) |
131 | 0c8d22a5 | sschulz | > boost::get_system_time() ) { |
132 | // incoming data -> if gaze is disabled, enable it!
|
||
133 | ea068cf1 | sschulz | joint_interface_->enable_gaze_joints(); |
134 | 8c6c1163 | Simon Schulz | return true; |
135 | } |
||
136 | 0c8d22a5 | sschulz | |
137 | // else: no incoming data, disable!
|
||
138 | ea068cf1 | sschulz | joint_interface_->disable_gaze_joints(); |
139 | 0c8d22a5 | sschulz | return false; |
140 | 8c6c1163 | Simon Schulz | } |
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 | 0c8d22a5 | sschulz | bool MotionGenerator::mouth_target_input_active() {
|
145 | ea068cf1 | sschulz | if (last_mouth_target_update_ + boost::posix_time::milliseconds(1000) |
146 | 0c8d22a5 | sschulz | > boost::get_system_time() ) { |
147 | // incoming data -> if mouth is disabled, enable it!
|
||
148 | ea068cf1 | sschulz | joint_interface_->enable_mouth_joints(); |
149 | 8c6c1163 | Simon Schulz | return true; |
150 | } |
||
151 | 0c8d22a5 | sschulz | |
152 | // else: no incoming data, disable!
|
||
153 | ea068cf1 | sschulz | joint_interface_->disable_mouth_joints(); |
154 | 0c8d22a5 | sschulz | return false; |
155 | 8c6c1163 | Simon Schulz | } |
156 | |||
157 | //! limit target to min/max bounds:
|
||
158 | 0c8d22a5 | sschulz | float MotionGenerator::limit_target(int joint_id, float val) { |
159 | 8c6c1163 | Simon Schulz | assert(joint_id < JointInterface::JOINT_ID_ENUM_SIZE); |
160 | |||
161 | 0c8d22a5 | sschulz | // fetch min/max for joint:
|
162 | ea068cf1 | sschulz | float min = joint_interface_->get_joint_min(joint_id);
|
163 | float max = joint_interface_->get_joint_max(joint_id);
|
||
164 | 8c6c1163 | Simon Schulz | |
165 | 0c8d22a5 | sschulz | 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 | 8c6c1163 | Simon Schulz | exit(0);
|
169 | } |
||
170 | |||
171 | val = fmin(val, max); |
||
172 | val = fmax(val, min); |
||
173 | |||
174 | return val;
|
||
175 | } |