Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_data_receiver.cpp @ 372eec67

History | View | Annotate | Download (7.999 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
    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
    }
100
    
101
    //small hack to tell humotion to update the lid angle
102
    //fixme: use real id
103
    store_incoming_position(100, 0.0, timestamps_[0]);
104

    
105
    #if ICUB_DATA_RECEIVER_DUMP_DATA
106
    dump_incoming_data();
107
    #endif
108
}
109

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

    
117
    // 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
            target_eye_pan_ = position;
125
        } else {
126
            target_eye_vergence_ = -position;
127
        }
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
        if (icub_id == iCubJointInterface::ID_NECK_PAN) {
142
            // icub uses an inverted neck pan specification
143
            position = -position;
144
        }
145

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

    
152
//! store incoming velocity for a given icub joint
153
//! \param icub_id icub joint id
154
//! \param velocity
155
//! \param timestamp
156
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
//! 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

    
211