Statistics
| Branch: | Tag: | Revision:

humotion / examples / yarp_icub / src / icub_data_receiver.cpp @ ea29304b

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