Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.085 KB)

1
#include "icub_data_receiver.h"
2
#include <humotion/server/joint_interface.h>
3
#include <yarp/os/Property.h>
4
#include <boost/format.hpp>
5

    
6
using std::cout;
7
using std::cerr;
8
using std::string;
9
using humotion::server::JointInterface;
10
using yarp::dev::IEncodersTimed;
11
using yarp::sig::Vector;
12

    
13
#define ICUB_DATA_RECEIVER_USE_ENCODERSPEED 0
14
#define ICUB_DATA_RECEIVER_DUMP_DATA 0
15

    
16
//! 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

    
22
    // store pointer to icub jointinterface
23
    icub_jointinterface_ = icub_jointinterface;
24

    
25
    //fetch iencs view from yarp
26
    yarp::dev::PolyDriver *poly_driver = icub_jointinterface->get_yarp_polydriver();
27
    bool success = poly_driver->view(iencs_);
28
    if (!success) {
29
        cerr << "ERROR: polydriver failed to init iencs view\n";
30
        exit(EXIT_FAILURE);
31
    }
32

    
33
    // resize data storage vectors to match the number of axes
34
    int joints;
35
    iencs_->getAxes(&joints);
36
    positions_.resize(joints);
37
    velocities_.resize(joints);
38
    timestamps_.resize(joints);
39
}
40

    
41
//! yarp rate thread initializer
42
bool iCubDataReceiver::threadInit() {
43
    return true;
44
}
45

    
46
//! yarp thread release function
47
void iCubDataReceiver::threadRelease() {
48
}
49

    
50
//! 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
Vector iCubDataReceiver::calculate_velocities(Vector positions, Vector timestamps) {
55
    Vector velocities;
56
    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
//! main loop routine, called by yarp rate thread
79
void iCubDataReceiver::run() {
80
    float velocity;
81

    
82
    //grab pos+vel data:
83
    iencs_->getEncodersTimed(positions_.data(), timestamps_.data());
84

    
85
    #if ICUB_DATA_RECEIVER_USE_ENCODERSPEED
86
    // fetch data from icub. NOTE: make sure to enable the vel broadcast in the ini file!
87
    if (! iencs_->getEncoderSpeeds(velocities_.data())){
88
        cout << "Failed to fetch encoder speeds...\n";
89
        return;
90
    }
91
    #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
    }
103
    
104
    //small hack to tell humotion to update the lid angle
105
    //fixme: use real id
106
    store_incoming_position(100, 0.0, timestamps_[0]);
107

    
108
    #if ICUB_DATA_RECEIVER_DUMP_DATA
109
    dump_incoming_data();
110
    #endif
111
}
112

    
113
//! 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
    //cout << "iCubDataReceiver::store_incoming_position(icub=" << icub_id << ", " << position << ")\n";
119

    
120
    // 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
            target_eye_pan_ = position;
128
        } else {
129
            target_eye_vergence_ = -position;
130
        }
131

    
132
        float right = target_eye_pan_ + target_eye_vergence_/2.0;
133
        float left  = target_eye_pan_ - target_eye_vergence_/2.0;
134

    
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
        if (icub_id == iCubJointInterface::ID_NECK_PAN) {
145
            // icub uses an inverted neck pan specification
146
            position = -position;
147
        }
148

    
149
        // known configured mapping between joint ids
150
        int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id);
151
        icub_jointinterface_->store_incoming_position(humotion_id, position, timestamp);
152
    }
153
}
154

    
155
//! store incoming velocity for a given icub joint
156
//! \param icub_id icub joint id
157
//! \param velocity
158
//! \param timestamp
159
void iCubDataReceiver::store_incoming_velocity(int icub_id, double velocity, double timestamp) {
160
    //cout << "iCubDataReceiver::store_incoming_velocity(icub=" << icub_id << ", " << velocity << ")\n";
161

    
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
        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

    
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
//! 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

    
214