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