Revision ea29304b

View differences:

examples/yarp_icub/include/icub_data_receiver.h
26 26
    void run();
27 27
private:
28 28
    void store_incoming_position(int icub_id, double value, double timestamp);
29
    void store_incoming_velocity(int icub_id, double velocity, double timestamp);
30
    yarp::sig::Vector calculate_velocities(yarp::sig::Vector positions,
31
                              yarp::sig::Vector timestamps);
29 32

  
30 33
    float target_eye_pan_;
31 34
    float target_eye_vergence_;
32 35

  
33
    yarp::sig::Vector yarp_positions_;
34
    yarp::sig::Vector yarp_commands_;
35
    yarp::sig::Vector yarp_timestamps_;
36
    float target_eye_pan_velocity_;
37
    float target_eye_vergence_velocity_;
38

  
39
    yarp::sig::Vector positions_;
40
    yarp::sig::Vector timestamps_;
41
    yarp::sig::Vector velocities_;
42
    yarp::sig::Vector commands_;
43

  
44
    yarp::sig::Vector previous_positions_;
45
    yarp::sig::Vector previous_timestamps_;
36 46

  
37 47
    iCubJointInterface *icub_jointinterface_;
38
    yarp::dev::IEncodersTimed *yarp_iencs_;
48
    yarp::dev::IEncodersTimed *iencs_;
39 49
};
40 50

  
examples/yarp_icub/include/icub_jointinterface.h
25 25
    iCubJointInterface(std::string scope);
26 26
    ~iCubJointInterface();
27 27

  
28
    void store_incoming_position(int humotion_id, double position, double timestamp);
28
    //void store_incoming_position(int humotion_id, double position, double timestamp);
29
    //void store_incoming_velocity(int humotion_id, double position, double timestamp);
29 30
    void run();
30 31

  
31 32
    int convert_icub_jointid_to_humotion(int id);
examples/yarp_icub/src/icub_data_receiver.cpp
1 1
#include "icub_data_receiver.h"
2 2
#include <humotion/server/joint_interface.h>
3 3
#include <yarp/os/Property.h>
4
#include <boost/format.hpp>
4 5
//using namespace yarp::dev;
5 6
//using namespace yarp::sig;
6 7
//using namespace yarp::os;
......
11 12
using yarp::dev::IEncodersTimed;
12 13
using yarp::sig::Vector;
13 14

  
15
#define ICUB_DATA_RECEIVER_USE_ENCODERSPEED 0
16

  
14 17
iCubDataReceiver::iCubDataReceiver(int period, iCubJointInterface *icub_jointinterface)
15 18
    : yarp::os::RateThread(period) {
16 19

  
17 20
    // store pointer to icub jointinterface
18 21
    icub_jointinterface_ = icub_jointinterface;
19 22

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

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

  
35 39
bool iCubDataReceiver::threadInit() {
......
39 43
void iCubDataReceiver::threadRelease() {
40 44
}
41 45

  
46
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

  
42 74
void iCubDataReceiver::run() {
75
    float velocity;
76

  
43 77
    //grab pos+vel data:
44
    yarp_iencs_->getEncodersTimed(yarp_positions_.data(), yarp_timestamps_.data());
45
    //iencs->getEncoderSpeeds(velocities.data());
78
    iencs_->getEncodersTimed(positions_.data(), timestamps_.data());
46 79

  
47
    //publish data to humotion
48
    for(int i=0; i<yarp_positions_.size(); i++){
49
        store_incoming_position(i, yarp_positions_[i], yarp_timestamps_[i]);
80
    #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]);
50 94
    }
51 95
    
52
    printf("\n");
53

  
54 96
    //small hack to tell humotion to update the lid angle
55 97
    //fixme: use real id
56
    store_incoming_position(100, 0.0, yarp_timestamps_[0]);
98
    store_incoming_position(100, 0.0, timestamps_[0]);
57 99
}
58 100

  
59 101
void iCubDataReceiver::store_incoming_position(int icub_id, double value, double timestamp) {
60
    cout << "iCubDataReceiver::store_incoming_position(icubid=" << icub_id << ", " << value << "..)\n";
102
    cout << "iCubDataReceiver::store_incoming_position(icub=" << icub_id << ", " << value << ")\n";
61 103

  
62 104
    // store joint position in humotion backend
63 105
    if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) ||
......
93 135
        icub_jointinterface_->store_incoming_position(humotion_id, value, timestamp);
94 136
    }
95 137
}
138

  
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

  
177

  
examples/yarp_icub/src/icub_jointinterface.cpp
320 320
}
321 321

  
322 322

  
323
//! set the current position of a joint
324
//! \param id of joint
325
//! \param float value of position
326
//! \param double timestamp
327
void iCubJointInterface::store_incoming_position(int humotion_id, double position, double timestamp){
328
    cout << "iCubJointInterface::store_incoming_position(humotionid=" << humotion_id <<
329
            ", " << position << ", ...)\n";
330

  
331
    JointInterface::store_incoming_position(humotion_id, position, timestamp);
332
}
333
/*
334
    //store joint based on id:
335
    switch(id){
336
        default:
337
            //printf("> ERROR: unhandled joint id %d\n",id);
338
            return;
339

  
340
        case(100):
341
            //JointInterface::store_incoming_position(ID_EYES_RIGHT_LID_UPPER, lid_angle, timestamp);
342
            break;
343

  
344
        case(ICUB_ID_NECK_PAN):
345
            //PAN is inverted!
346
            JointInterface::store_incoming_position(ID_NECK_PAN, value, timestamp);
347
            break;
348

  
349
        case(ICUB_ID_NECK_TILT):
350
            JointInterface::store_incoming_position(ID_NECK_TILT, value, timestamp);
351
            break;
352

  
353
        case(ICUB_ID_NECK_ROLL):
354
            JointInterface::store_incoming_position(ID_NECK_ROLL, value, timestamp);
355
            break;
356

  
357
        case(ICUB_ID_EYES_BOTH_UD):
358
            JointInterface::store_incoming_position(ID_EYES_BOTH_UD, value, timestamp);
359
            break;
360

  
361
        //icub handles eyes differently, we have to set pan angle + vergence
362
        case(ICUB_ID_EYES_PAN): {//pan
363
            last_pos_eye_pan = value;
364
            float left  = last_pos_eye_pan + last_pos_eye_vergence/2.0;
365
            float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;
366

  
367
            //printf("> eye: pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_pos_eye_pan, last_pos_eye_vergence, left, right);
368
            JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);
369
            JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);
370
            break;
371
        }
372

  
373
        case(ICUB_ID_EYES_VERGENCE): { //vergence
374
            last_pos_eye_vergence = value;
375
            float left  = last_pos_eye_pan + last_pos_eye_vergence/2.0;
376
            float right = last_pos_eye_pan - last_pos_eye_vergence/2.0;
377

  
378
            //printf("> eye: pan=%3.2f vergence=%3.2f --> L=%3.2f R=%3.2f\n", last_pos_eye_pan, last_pos_eye_vergence, left, right);
379
            JointInterface::store_incoming_position(ID_EYES_LEFT_LR, left, timestamp);
380
            JointInterface::store_incoming_position(ID_EYES_RIGHT_LR, right, timestamp);
381
            break;
382
        }
383
    }
384

  
385

  
386
}*/
387

  
388 323
void iCubJointInterface::set_joint_enable_state(int e, bool enable) {
389 324
    int icub_jointid = -1;
390 325

  
include/humotion/server/joint_interface.h
105 105

  
106 106
    bool get_joint_position_map_empty();
107 107

  
108
    void store_incoming_position(int joint_id, float position, Timestamp timestamp);
109
    void store_incoming_velocity(int joint_id, float velocity, Timestamp timestamp);
110

  
108 111
protected:
109 112

  
110
    void store_incoming_position(int joint_id, float position, Timestamp timestamp);
111
    void store_incoming_speed(int joint_id, float speed, Timestamp timestamp);
112 113

  
113 114
    virtual void enable_joint(int id) = 0;
114 115
    virtual void disable_joint(int id) = 0;
src/server/joint_interface.cpp
80 80
    //following atomic instructions:
81 81
    mutex::scoped_lock scoped_lock(joint_ts_position_map_access_mutex);
82 82

  
83
    //printf("> humotion: incoming joint position for joint id 0x%02X = %4.2f (ts=%.2f)\n",joint_id,position,timestamp.to_seconds());
83
    printf("> humotion: incoming joint position for joint id 0x%02X = %4.2f (ts=%.2f)\n",joint_id,position,timestamp.to_seconds());
84 84
    joint_ts_position_map[joint_id].insert(timestamp, position);
85 85

  
86 86
    incoming_position_count++;
......
99 99
//! \param joint name
100 100
//! \param speed
101 101
//! \param timestamp when the position was measured
102
void JointInterface::store_incoming_speed(int joint_id, float speed, Timestamp timestamp){
102
void JointInterface::store_incoming_velocity(int joint_id, float velocity, Timestamp timestamp){
103 103
    //lock the tsd_list for this access. by doing this we assure
104 104
    //that no other thread accessing this element can diturb the
105 105
    //following atomic instructions:
106 106
    mutex::scoped_lock scoped_lock(joint_ts_speed_map_access_mutex);
107 107

  
108
    //printf("> humotion: incoming joint speed for joint id 0x%02X = %4.2f (ts=%.2f)\n",joint_id,speed,timestamp.to_seconds());
109
    joint_ts_speed_map[joint_id].insert(timestamp, speed);
108
    //printf("> humotion: incoming joint velocity for joint id 0x%02X = %4.2f (ts=%.2f)\n",joint_id,velocity,timestamp.to_seconds());
109
    joint_ts_speed_map[joint_id].insert(timestamp, velocity);
110 110
}
111 111

  
112 112
//! return the timestamped float for the given joints position

Also available in: Unified diff