Revision ea29304b examples/yarp_icub/src/icub_data_receiver.cpp
examples/yarp_icub/src/icub_data_receiver.cpp | ||
---|---|---|
1 | 1 |
#include "icub_data_receiver.h" |
2 | 2 |
#include <humotion/server/joint_interface.h> |
3 | 3 |
#include <yarp/os/Property.h> |
4 |
#include <boost/format.hpp> |
|
4 | 5 |
//using namespace yarp::dev; |
5 | 6 |
//using namespace yarp::sig; |
6 | 7 |
//using namespace yarp::os; |
... | ... | |
11 | 12 |
using yarp::dev::IEncodersTimed; |
12 | 13 |
using yarp::sig::Vector; |
13 | 14 |
|
15 |
#define ICUB_DATA_RECEIVER_USE_ENCODERSPEED 0 |
|
16 |
|
|
14 | 17 |
iCubDataReceiver::iCubDataReceiver(int period, iCubJointInterface *icub_jointinterface) |
15 | 18 |
: yarp::os::RateThread(period) { |
16 | 19 |
|
17 | 20 |
// store pointer to icub jointinterface |
18 | 21 |
icub_jointinterface_ = icub_jointinterface; |
19 | 22 |
|
20 |
//fetch yarp iencs view:
|
|
23 |
//fetch iencs view from yarp
|
|
21 | 24 |
yarp::dev::PolyDriver *poly_driver = icub_jointinterface->get_yarp_polydriver(); |
22 |
bool success = poly_driver->view(yarp_iencs_);
|
|
25 |
bool success = poly_driver->view(iencs_); |
|
23 | 26 |
if (!success) { |
24 | 27 |
cout << "ERROR: polydriver failed to init iencs view\n"; |
25 | 28 |
exit(EXIT_FAILURE); |
26 | 29 |
} |
27 | 30 |
|
28 |
// resize data storage vectors to match the number of axes:
|
|
31 |
// resize data storage vectors to match the number of axes |
|
29 | 32 |
int joints; |
30 |
yarp_iencs_->getAxes(&joints); |
|
31 |
yarp_positions_.resize(joints); |
|
32 |
yarp_timestamps_.resize(joints); |
|
33 |
iencs_->getAxes(&joints); |
|
34 |
positions_.resize(joints); |
|
35 |
velocities_.resize(joints); |
|
36 |
timestamps_.resize(joints); |
|
33 | 37 |
} |
34 | 38 |
|
35 | 39 |
bool iCubDataReceiver::threadInit() { |
... | ... | |
39 | 43 |
void iCubDataReceiver::threadRelease() { |
40 | 44 |
} |
41 | 45 |
|
46 |
yarp::sig::Vector iCubDataReceiver::calculate_velocities(yarp::sig::Vector positions, |
|
47 |
yarp::sig::Vector timestamps) { |
|
48 |
yarp::sig::Vector velocities; |
|
49 |
velocities.resize(positions.size()); |
|
50 |
|
|
51 |
cout << "\n"; |
|
52 |
if (previous_positions_.size() == 0){ |
|
53 |
// first run, no valid old position available, return zero velocities |
|
54 |
// by setting all all elements to zero |
|
55 |
velocities = 0.0; |
|
56 |
}else{ |
|
57 |
// calculate speed based on positions: |
|
58 |
for(int i=0; i<positions.size(); i++){ |
|
59 |
float diff = positions[i] - previous_positions_[i]; |
|
60 |
float timediff = timestamps[i] - previous_timestamps_[i]; |
|
61 |
// calc speed: |
|
62 |
velocities[i] = diff / timediff; |
|
63 |
cout << " [" << i << "]=" << boost::format("%6.3f") % velocities[i]; |
|
64 |
} |
|
65 |
} |
|
66 |
cout << " VEL\n"; |
|
67 |
|
|
68 |
previous_positions_ = positions; |
|
69 |
previous_timestamps_ = timestamps; |
|
70 |
|
|
71 |
return velocities; |
|
72 |
} |
|
73 |
|
|
42 | 74 |
void iCubDataReceiver::run() { |
75 |
float velocity; |
|
76 |
|
|
43 | 77 |
//grab pos+vel data: |
44 |
yarp_iencs_->getEncodersTimed(yarp_positions_.data(), yarp_timestamps_.data()); |
|
45 |
//iencs->getEncoderSpeeds(velocities.data()); |
|
78 |
iencs_->getEncodersTimed(positions_.data(), timestamps_.data()); |
|
46 | 79 |
|
47 |
//publish data to humotion |
|
48 |
for(int i=0; i<yarp_positions_.size(); i++){ |
|
49 |
store_incoming_position(i, yarp_positions_[i], yarp_timestamps_[i]); |
|
80 |
#if ICUB_DATA_RECEIVER_USE_ENCODERSPEED |
|
81 |
// fetch data from icub. NOTE: make sure to enable the vel broadcast in the ini file! |
|
82 |
iencs_->getEncoderSpeeds(velocities_.data()); |
|
83 |
#else |
|
84 |
// manually calculate the speed based on old position: |
|
85 |
velocities_ = calculate_velocities(positions_, timestamps_); |
|
86 |
#endif |
|
87 |
|
|
88 |
// publish data to humotion |
|
89 |
for(int i=0; i<positions_.size(); i++){ |
|
90 |
// store position values |
|
91 |
store_incoming_position(i, positions_[i], timestamps_[i]); |
|
92 |
// store velocity |
|
93 |
store_incoming_velocity(i, velocities_[i], timestamps_[i]); |
|
50 | 94 |
} |
51 | 95 |
|
52 |
printf("\n"); |
|
53 |
|
|
54 | 96 |
//small hack to tell humotion to update the lid angle |
55 | 97 |
//fixme: use real id |
56 |
store_incoming_position(100, 0.0, yarp_timestamps_[0]);
|
|
98 |
store_incoming_position(100, 0.0, timestamps_[0]); |
|
57 | 99 |
} |
58 | 100 |
|
59 | 101 |
void iCubDataReceiver::store_incoming_position(int icub_id, double value, double timestamp) { |
60 |
cout << "iCubDataReceiver::store_incoming_position(icubid=" << icub_id << ", " << value << "..)\n";
|
|
102 |
cout << "iCubDataReceiver::store_incoming_position(icub=" << icub_id << ", " << value << ")\n";
|
|
61 | 103 |
|
62 | 104 |
// store joint position in humotion backend |
63 | 105 |
if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) || |
... | ... | |
93 | 135 |
icub_jointinterface_->store_incoming_position(humotion_id, value, timestamp); |
94 | 136 |
} |
95 | 137 |
} |
138 |
|
|
139 |
void iCubDataReceiver::store_incoming_velocity(int icub_id, double velocity, double timestamp) { |
|
140 |
cout << "iCubDataReceiver::store_incoming_velocity(icub=" << icub_id << ", " << velocity << ")\n"; |
|
141 |
|
|
142 |
// store joint position in humotion backend |
|
143 |
if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) || |
|
144 |
(icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) { |
|
145 |
// the icub handles eyes differently |
|
146 |
// instead of using seperate left/right pan the icub uses |
|
147 |
// a combined pan angle and vergence. therfore we have to convert this here: |
|
148 |
if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) { |
|
149 |
target_eye_pan_velocity_ = velocity; |
|
150 |
} else { |
|
151 |
target_eye_vergence_velocity_ = -velocity; |
|
152 |
} |
|
153 |
|
|
154 |
float left = target_eye_pan_velocity_ + target_eye_vergence_velocity_/2.0; |
|
155 |
float right = target_eye_pan_velocity_ - target_eye_vergence_velocity_/2.0; |
|
156 |
|
|
157 |
icub_jointinterface_->store_incoming_velocity(JointInterface::ID_EYES_LEFT_LR, |
|
158 |
left, timestamp); |
|
159 |
icub_jointinterface_->store_incoming_velocity(JointInterface::ID_EYES_RIGHT_LR, |
|
160 |
right, timestamp); |
|
161 |
} else if (icub_id == 100) { |
|
162 |
//HACK |
|
163 |
//icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER, |
|
164 |
// lid_angle, timestamp); |
|
165 |
} else { |
|
166 |
if (icub_id == iCubJointInterface::ID_NECK_PAN) { |
|
167 |
// icub uses an inverted neck pan specification |
|
168 |
velocity = -velocity; |
|
169 |
} |
|
170 |
|
|
171 |
// known configured mapping between joint ids |
|
172 |
int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id); |
|
173 |
icub_jointinterface_->store_incoming_velocity(humotion_id, velocity, timestamp); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
|
Also available in: Unified diff