Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_data_receiver.cpp @ 708960ff

History | View | Annotate | Download (8.085 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 18e9b892 Simon Schulz
    if (! iencs_->getEncoderSpeeds(velocities_.data())){
88
        cout << "Failed to fetch encoder speeds...\n";
89
        return;
90
    }
91 ea29304b Simon Schulz
    #else
92
    // manually calculate the speed based on old position:
93
    velocities_ = calculate_velocities(positions_, timestamps_);
94
    #endif
95
96
    // publish data to humotion
97
    for(int i=0; i<positions_.size(); i++){
98
        // store position values
99
        store_incoming_position(i, positions_[i], timestamps_[i]);
100
        // store velocity
101
        store_incoming_velocity(i, velocities_[i], timestamps_[i]);
102 8c6c1163 Simon Schulz
    }
103 4dbb1a71 sschulz
    
104 35b3ca25 Simon Schulz
    //small hack to tell humotion to update the lid angle
105 8c6c1163 Simon Schulz
    //fixme: use real id
106 ea29304b Simon Schulz
    store_incoming_position(100, 0.0, timestamps_[0]);
107 ad7b6f18 Simon Schulz
108
    #if ICUB_DATA_RECEIVER_DUMP_DATA
109
    dump_incoming_data();
110
    #endif
111 35b3ca25 Simon Schulz
}
112
113 ad7b6f18 Simon Schulz
//! store incoming position for a given icub joint
114
//! \param icub _id icub joint id
115
//! \param position
116
//! \param timestamp
117
void iCubDataReceiver::store_incoming_position(int icub_id, double position, double timestamp) {
118 708960ff Simon Schulz
    //cout << "iCubDataReceiver::store_incoming_position(icub=" << icub_id << ", " << position << ")\n";
119 6c028e11 Simon Schulz
120 35b3ca25 Simon Schulz
    // store joint position in humotion backend
121
    if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) ||
122
            (icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) {
123
        // the icub handles eyes differently
124
        // instead of using seperate left/right pan the icub uses
125
        // a combined pan angle and vergence. therfore we have to convert this here:
126
        if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) {
127 ad7b6f18 Simon Schulz
            target_eye_pan_ = position;
128 35b3ca25 Simon Schulz
        } else {
129 ad7b6f18 Simon Schulz
            target_eye_vergence_ = -position;
130 35b3ca25 Simon Schulz
        }
131
132 18e9b892 Simon Schulz
        float right = target_eye_pan_ + target_eye_vergence_/2.0;
133
        float left  = target_eye_pan_ - target_eye_vergence_/2.0;
134 35b3ca25 Simon Schulz
135
        icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_LEFT_LR,
136
                                                     left, timestamp);
137
        icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_RIGHT_LR,
138
                                                     right, timestamp);
139
    } else if (icub_id == 100) {
140
        //HACK
141
        //icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER,
142
        //                                             lid_angle, timestamp);
143
    } else {
144 6c028e11 Simon Schulz
        if (icub_id == iCubJointInterface::ID_NECK_PAN) {
145
            // icub uses an inverted neck pan specification
146 ad7b6f18 Simon Schulz
            position = -position;
147 6c028e11 Simon Schulz
        }
148
149 35b3ca25 Simon Schulz
        // known configured mapping between joint ids
150
        int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id);
151 ad7b6f18 Simon Schulz
        icub_jointinterface_->store_incoming_position(humotion_id, position, timestamp);
152 35b3ca25 Simon Schulz
    }
153 8c6c1163 Simon Schulz
}
154 ea29304b Simon Schulz
155 ad7b6f18 Simon Schulz
//! store incoming velocity for a given icub joint
156
//! \param icub_id icub joint id
157
//! \param velocity
158
//! \param timestamp
159 ea29304b Simon Schulz
void iCubDataReceiver::store_incoming_velocity(int icub_id, double velocity, double timestamp) {
160 708960ff Simon Schulz
    //cout << "iCubDataReceiver::store_incoming_velocity(icub=" << icub_id << ", " << velocity << ")\n";
161 ea29304b Simon Schulz
162
    // store joint position in humotion backend
163
    if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) ||
164
            (icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) {
165
        // the icub handles eyes differently
166
        // instead of using seperate left/right pan the icub uses
167
        // a combined pan angle and vergence. therfore we have to convert this here:
168
        if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) {
169
            target_eye_pan_velocity_ = velocity;
170
        } else {
171
            target_eye_vergence_velocity_ = -velocity;
172
        }
173
174 18e9b892 Simon Schulz
        float right = target_eye_pan_velocity_ + target_eye_vergence_velocity_/2.0;
175
        float left  = target_eye_pan_velocity_ - target_eye_vergence_velocity_/2.0;
176 ea29304b Simon Schulz
177
        icub_jointinterface_->store_incoming_velocity(JointInterface::ID_EYES_LEFT_LR,
178
                                                     left, timestamp);
179
        icub_jointinterface_->store_incoming_velocity(JointInterface::ID_EYES_RIGHT_LR,
180
                                                     right, timestamp);
181
    } else if (icub_id == 100) {
182
        //HACK
183
        //icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER,
184
        //                                             lid_angle, timestamp);
185
    } else {
186
        if (icub_id == iCubJointInterface::ID_NECK_PAN) {
187
            // icub uses an inverted neck pan specification
188
            velocity = -velocity;
189
        }
190
191
        // known configured mapping between joint ids
192
        int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id);
193
        icub_jointinterface_->store_incoming_velocity(humotion_id, velocity, timestamp);
194
    }
195
}
196
197 ad7b6f18 Simon Schulz
//! helper for debugging purposes, feed this data into gnuplot for visual inspection
198
void iCubDataReceiver::dump_incoming_data() {
199
    // use gnuplot for viz:
200
    // ./icub_humotion_server --robot icub | grep "INCOMING" |tee  log
201
    // @gnuplot: plot "log" using 0:1 w l t "p neck tilt", "log" using 0:2 w l t "v neck tilt", \
202
    //                "log" using 0:5 w l t "p neck pan", "log" using 0:6 w l t "v neck pan", \
203
    //                "log" using 0:7 w l t "p eyes ud", "log" using 0:8 w l t "v eyes ud", \
204
    //                "log" using 0:9 w l t "p eyes vergence", "log" using 0:10 w l t "v eyes vergence"
205
    cout << "\n";
206
            // publish data to humotion
207
    for(int i=0; i<positions_.size(); i++){
208
        cout << positions_[i] << " ";
209
        cout << velocities_[i] << " ";
210
    }
211
    cout << " #INCOMING_DATA_DUMP\n";
212
}
213