humotion / examples / yarp_icub / src / icub_data_receiver.cpp @ ad7b6f18
History | View | Annotate | Download (7.999 KB)
1 | 8c6c1163 | Simon Schulz | #include "icub_data_receiver.h" |
---|---|---|---|
2 | 35b3ca25 | Simon Schulz | #include <humotion/server/joint_interface.h> |
3 | 8c6c1163 | Simon Schulz | #include <yarp/os/Property.h> |
4 | ad7b6f18 | Simon Schulz | #include <boost/format.hpp> |
5 | |||
6 | 35b3ca25 | Simon Schulz | using std::cout;
|
7 | 1f748ce7 | Simon Schulz | using std::cerr;
|
8 | 35b3ca25 | Simon Schulz | using std::string; |
9 | using humotion::server::JointInterface;
|
||
10 | using yarp::dev::IEncodersTimed;
|
||
11 | using yarp::sig::Vector;
|
||
12 | |||
13 | ea29304b | Simon Schulz | #define ICUB_DATA_RECEIVER_USE_ENCODERSPEED 0 |
14 | ad7b6f18 | Simon Schulz | #define ICUB_DATA_RECEIVER_DUMP_DATA 0 |
15 | ea29304b | Simon Schulz | |
16 | ad7b6f18 | Simon Schulz | //! constructor
|
17 | //! \param period_ms for the yarp rate thread
|
||
18 | //! \param icub_jointinterface
|
||
19 | iCubDataReceiver::iCubDataReceiver(int period_ms, iCubJointInterface *icub_jointinterface)
|
||
20 | : yarp::os::RateThread(period_ms) { |
||
21 | 35b3ca25 | Simon Schulz | |
22 | // store pointer to icub jointinterface
|
||
23 | icub_jointinterface_ = icub_jointinterface; |
||
24 | |||
25 | ea29304b | Simon Schulz | //fetch iencs view from yarp
|
26 | 35b3ca25 | Simon Schulz | yarp::dev::PolyDriver *poly_driver = icub_jointinterface->get_yarp_polydriver(); |
27 | ea29304b | Simon Schulz | bool success = poly_driver->view(iencs_);
|
28 | 35b3ca25 | Simon Schulz | if (!success) {
|
29 | 1f748ce7 | Simon Schulz | cerr << "ERROR: polydriver failed to init iencs view\n";
|
30 | 35b3ca25 | Simon Schulz | exit(EXIT_FAILURE); |
31 | } |
||
32 | |||
33 | ea29304b | Simon Schulz | // resize data storage vectors to match the number of axes
|
34 | 8c6c1163 | Simon Schulz | int joints;
|
35 | ea29304b | Simon Schulz | iencs_->getAxes(&joints); |
36 | positions_.resize(joints); |
||
37 | velocities_.resize(joints); |
||
38 | timestamps_.resize(joints); |
||
39 | 8c6c1163 | Simon Schulz | } |
40 | |||
41 | ad7b6f18 | Simon Schulz | //! yarp rate thread initializer
|
42 | 35b3ca25 | Simon Schulz | bool iCubDataReceiver::threadInit() {
|
43 | return true; |
||
44 | 8c6c1163 | Simon Schulz | } |
45 | |||
46 | ad7b6f18 | Simon Schulz | //! yarp thread release function
|
47 | 35b3ca25 | Simon Schulz | void iCubDataReceiver::threadRelease() {
|
48 | } |
||
49 | 7adf90be | Simon Schulz | |
50 | ad7b6f18 | Simon Schulz | //! manully calculate joint velocities (instead of using joint encoder speeds)
|
51 | //! \param positions vector with current encoder position
|
||
52 | //! \param timestamps vector with the associated timestamps
|
||
53 | //! \return velocities as vector
|
||
54 | 1f748ce7 | Simon Schulz | Vector iCubDataReceiver::calculate_velocities(Vector positions, Vector timestamps) { |
55 | Vector velocities; |
||
56 | ea29304b | Simon Schulz | velocities.resize(positions.size()); |
57 | |||
58 | if (previous_positions_.size() == 0){ |
||
59 | // first run, no valid old position available, return zero velocities
|
||
60 | // by setting all all elements to zero
|
||
61 | velocities = 0.0; |
||
62 | }else{
|
||
63 | // calculate speed based on positions:
|
||
64 | for(int i=0; i<positions.size(); i++){ |
||
65 | float diff = positions[i] - previous_positions_[i];
|
||
66 | float timediff = timestamps[i] - previous_timestamps_[i];
|
||
67 | // calc speed:
|
||
68 | velocities[i] = diff / timediff; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | previous_positions_ = positions; |
||
73 | previous_timestamps_ = timestamps; |
||
74 | |||
75 | return velocities;
|
||
76 | } |
||
77 | |||
78 | ad7b6f18 | Simon Schulz | //! main loop routine, called by yarp rate thread
|
79 | 35b3ca25 | Simon Schulz | void iCubDataReceiver::run() {
|
80 | ea29304b | Simon Schulz | float velocity;
|
81 | |||
82 | 8c6c1163 | Simon Schulz | //grab pos+vel data:
|
83 | ea29304b | Simon Schulz | iencs_->getEncodersTimed(positions_.data(), timestamps_.data()); |
84 | 8c6c1163 | Simon Schulz | |
85 | ea29304b | Simon Schulz | #if ICUB_DATA_RECEIVER_USE_ENCODERSPEED
|
86 | // fetch data from icub. NOTE: make sure to enable the vel broadcast in the ini file!
|
||
87 | iencs_->getEncoderSpeeds(velocities_.data()); |
||
88 | #else
|
||
89 | // manually calculate the speed based on old position:
|
||
90 | velocities_ = calculate_velocities(positions_, timestamps_); |
||
91 | #endif
|
||
92 | |||
93 | // publish data to humotion
|
||
94 | for(int i=0; i<positions_.size(); i++){ |
||
95 | // store position values
|
||
96 | store_incoming_position(i, positions_[i], timestamps_[i]); |
||
97 | // store velocity
|
||
98 | store_incoming_velocity(i, velocities_[i], timestamps_[i]); |
||
99 | 8c6c1163 | Simon Schulz | } |
100 | 4dbb1a71 | sschulz | |
101 | 35b3ca25 | Simon Schulz | //small hack to tell humotion to update the lid angle
|
102 | 8c6c1163 | Simon Schulz | //fixme: use real id
|
103 | ea29304b | Simon Schulz | store_incoming_position(100, 0.0, timestamps_[0]); |
104 | ad7b6f18 | Simon Schulz | |
105 | #if ICUB_DATA_RECEIVER_DUMP_DATA
|
||
106 | dump_incoming_data(); |
||
107 | #endif
|
||
108 | 35b3ca25 | Simon Schulz | } |
109 | |||
110 | ad7b6f18 | Simon Schulz | //! store incoming position for a given icub joint
|
111 | //! \param icub _id icub joint id
|
||
112 | //! \param position
|
||
113 | //! \param timestamp
|
||
114 | void iCubDataReceiver::store_incoming_position(int icub_id, double position, double timestamp) { |
||
115 | cout << "iCubDataReceiver::store_incoming_position(icub=" << icub_id << ", " << position << ")\n"; |
||
116 | 6c028e11 | Simon Schulz | |
117 | 35b3ca25 | Simon Schulz | // store joint position in humotion backend
|
118 | if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) ||
|
||
119 | (icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) { |
||
120 | // the icub handles eyes differently
|
||
121 | // instead of using seperate left/right pan the icub uses
|
||
122 | // a combined pan angle and vergence. therfore we have to convert this here:
|
||
123 | if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) {
|
||
124 | ad7b6f18 | Simon Schulz | target_eye_pan_ = position; |
125 | 35b3ca25 | Simon Schulz | } else {
|
126 | ad7b6f18 | Simon Schulz | target_eye_vergence_ = -position; |
127 | 35b3ca25 | Simon Schulz | } |
128 | |||
129 | float left = target_eye_pan_ + target_eye_vergence_/2.0; |
||
130 | float right = target_eye_pan_ - target_eye_vergence_/2.0; |
||
131 | |||
132 | icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_LEFT_LR, |
||
133 | left, timestamp); |
||
134 | icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_RIGHT_LR, |
||
135 | right, timestamp); |
||
136 | } else if (icub_id == 100) { |
||
137 | //HACK
|
||
138 | //icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER,
|
||
139 | // lid_angle, timestamp);
|
||
140 | } else {
|
||
141 | 6c028e11 | Simon Schulz | if (icub_id == iCubJointInterface::ID_NECK_PAN) {
|
142 | // icub uses an inverted neck pan specification
|
||
143 | ad7b6f18 | Simon Schulz | position = -position; |
144 | 6c028e11 | Simon Schulz | } |
145 | |||
146 | 35b3ca25 | Simon Schulz | // known configured mapping between joint ids
|
147 | int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id);
|
||
148 | ad7b6f18 | Simon Schulz | icub_jointinterface_->store_incoming_position(humotion_id, position, timestamp); |
149 | 35b3ca25 | Simon Schulz | } |
150 | 8c6c1163 | Simon Schulz | } |
151 | ea29304b | Simon Schulz | |
152 | ad7b6f18 | Simon Schulz | //! store incoming velocity for a given icub joint
|
153 | //! \param icub_id icub joint id
|
||
154 | //! \param velocity
|
||
155 | //! \param timestamp
|
||
156 | ea29304b | Simon Schulz | void iCubDataReceiver::store_incoming_velocity(int icub_id, double velocity, double timestamp) { |
157 | cout << "iCubDataReceiver::store_incoming_velocity(icub=" << icub_id << ", " << velocity << ")\n"; |
||
158 | |||
159 | // store joint position in humotion backend
|
||
160 | if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) ||
|
||
161 | (icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) { |
||
162 | // the icub handles eyes differently
|
||
163 | // instead of using seperate left/right pan the icub uses
|
||
164 | // a combined pan angle and vergence. therfore we have to convert this here:
|
||
165 | if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) {
|
||
166 | target_eye_pan_velocity_ = velocity; |
||
167 | } else {
|
||
168 | target_eye_vergence_velocity_ = -velocity; |
||
169 | } |
||
170 | |||
171 | float left = target_eye_pan_velocity_ + target_eye_vergence_velocity_/2.0; |
||
172 | float right = target_eye_pan_velocity_ - target_eye_vergence_velocity_/2.0; |
||
173 | |||
174 | icub_jointinterface_->store_incoming_velocity(JointInterface::ID_EYES_LEFT_LR, |
||
175 | left, timestamp); |
||
176 | icub_jointinterface_->store_incoming_velocity(JointInterface::ID_EYES_RIGHT_LR, |
||
177 | right, timestamp); |
||
178 | } else if (icub_id == 100) { |
||
179 | //HACK
|
||
180 | //icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER,
|
||
181 | // lid_angle, timestamp);
|
||
182 | } else {
|
||
183 | if (icub_id == iCubJointInterface::ID_NECK_PAN) {
|
||
184 | // icub uses an inverted neck pan specification
|
||
185 | velocity = -velocity; |
||
186 | } |
||
187 | |||
188 | // known configured mapping between joint ids
|
||
189 | int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id);
|
||
190 | icub_jointinterface_->store_incoming_velocity(humotion_id, velocity, timestamp); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | ad7b6f18 | Simon Schulz | //! helper for debugging purposes, feed this data into gnuplot for visual inspection
|
195 | void iCubDataReceiver::dump_incoming_data() {
|
||
196 | // use gnuplot for viz:
|
||
197 | // ./icub_humotion_server --robot icub | grep "INCOMING" |tee log
|
||
198 | // @gnuplot: plot "log" using 0:1 w l t "p neck tilt", "log" using 0:2 w l t "v neck tilt", \
|
||
199 | // "log" using 0:5 w l t "p neck pan", "log" using 0:6 w l t "v neck pan", \
|
||
200 | // "log" using 0:7 w l t "p eyes ud", "log" using 0:8 w l t "v eyes ud", \
|
||
201 | // "log" using 0:9 w l t "p eyes vergence", "log" using 0:10 w l t "v eyes vergence"
|
||
202 | cout << "\n";
|
||
203 | // publish data to humotion
|
||
204 | for(int i=0; i<positions_.size(); i++){ |
||
205 | cout << positions_[i] << " ";
|
||
206 | cout << velocities_[i] << " ";
|
||
207 | } |
||
208 | cout << " #INCOMING_DATA_DUMP\n";
|
||
209 | } |
||
210 |