Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_data_receiver.cpp @ 1f748ce7

History | View | Annotate | Download (6.36 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
using std::cout;
6
using std::cerr;
7
using std::string;
8
using humotion::server::JointInterface;
9
using yarp::dev::IEncodersTimed;
10
using yarp::sig::Vector;
11

    
12
#define ICUB_DATA_RECEIVER_USE_ENCODERSPEED 0
13

    
14
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
    //fetch iencs view from yarp
21
    yarp::dev::PolyDriver *poly_driver = icub_jointinterface->get_yarp_polydriver();
22
    bool success = poly_driver->view(iencs_);
23
    if (!success) {
24
        cerr << "ERROR: polydriver failed to init iencs view\n";
25
        exit(EXIT_FAILURE);
26
    }
27

    
28
    // resize data storage vectors to match the number of axes
29
    int joints;
30
    iencs_->getAxes(&joints);
31
    positions_.resize(joints);
32
    velocities_.resize(joints);
33
    timestamps_.resize(joints);
34
}
35

    
36
bool iCubDataReceiver::threadInit() {
37
    return true;
38
}
39

    
40
void iCubDataReceiver::threadRelease() {
41
}
42

    
43
Vector iCubDataReceiver::calculate_velocities(Vector positions, Vector timestamps) {
44
    Vector velocities;
45
    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
void iCubDataReceiver::run() {
68
    float velocity;
69

    
70
    //grab pos+vel data:
71
    iencs_->getEncodersTimed(positions_.data(), timestamps_.data());
72

    
73
    #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
    }
88
    
89
    //small hack to tell humotion to update the lid angle
90
    //fixme: use real id
91
    store_incoming_position(100, 0.0, timestamps_[0]);
92
}
93

    
94
void iCubDataReceiver::store_incoming_position(int icub_id, double value, double timestamp) {
95
    cout << "iCubDataReceiver::store_incoming_position(icub=" << icub_id << ", " << value << ")\n";
96

    
97
    // 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
            target_eye_vergence_ = -value;
107
        }
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
        if (icub_id == iCubJointInterface::ID_NECK_PAN) {
122
            // icub uses an inverted neck pan specification
123
            value = -value;
124
        }
125

    
126
        // 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
}
131

    
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

    
170