Revision 35b3ca25 examples/yarp_icub/src/icub_data_receiver.cpp
examples/yarp_icub/src/icub_data_receiver.cpp | ||
---|---|---|
1 | 1 |
#include "icub_data_receiver.h" |
2 |
#include <humotion/server/joint_interface.h> |
|
2 | 3 |
#include <yarp/os/Property.h> |
3 |
using namespace yarp::dev; |
|
4 |
using namespace yarp::sig; |
|
5 |
using namespace yarp::os; |
|
6 |
using namespace std; |
|
7 |
|
|
8 |
iCubDataReceiver::iCubDataReceiver(int period, IEncodersTimed *_iencs, iCubJointInterface *_icub_jointinterface):RateThread(period){ |
|
9 |
iencs = _iencs; |
|
10 |
icub_jointinterface = _icub_jointinterface; |
|
4 |
//using namespace yarp::dev; |
|
5 |
//using namespace yarp::sig; |
|
6 |
//using namespace yarp::os; |
|
7 |
using std::cout; |
|
8 |
using std::string; |
|
9 |
|
|
10 |
using humotion::server::JointInterface; |
|
11 |
using yarp::dev::IEncodersTimed; |
|
12 |
using yarp::sig::Vector; |
|
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 yarp iencs view: |
|
21 |
yarp::dev::PolyDriver *poly_driver = icub_jointinterface->get_yarp_polydriver(); |
|
22 |
bool success = poly_driver->view(yarp_iencs_); |
|
23 |
if (!success) { |
|
24 |
cout << "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: |
|
11 | 29 |
int joints; |
12 |
iencs->getAxes(&joints); |
|
13 |
positions.resize(joints); |
|
14 |
velocities.resize(joints); |
|
15 |
timestamps.resize(joints); |
|
30 |
yarp_iencs_->getAxes(&joints); |
|
31 |
yarp_positions_.resize(joints); |
|
32 |
yarp_timestamps_.resize(joints); |
|
16 | 33 |
} |
17 | 34 |
|
18 |
bool iCubDataReceiver::threadInit(){ return true; } |
|
19 |
void iCubDataReceiver::threadRelease(){ } |
|
20 |
|
|
21 |
double iCubDataReceiver::get_timestamp_ms(){ |
|
22 |
struct timespec spec; |
|
23 |
clock_gettime(CLOCK_REALTIME, &spec); |
|
24 |
return spec.tv_sec+spec.tv_nsec/1000000000.0; |
|
35 |
bool iCubDataReceiver::threadInit() { |
|
36 |
return true; |
|
25 | 37 |
} |
26 | 38 |
|
27 |
void iCubDataReceiver::run(){ |
|
39 |
void iCubDataReceiver::threadRelease() { |
|
40 |
} |
|
28 | 41 |
|
42 |
void iCubDataReceiver::run() { |
|
29 | 43 |
//grab pos+vel data: |
30 |
iencs->getEncodersTimed(positions.data(), timestamps.data()); |
|
31 |
iencs->getEncoderSpeeds(velocities.data()); |
|
32 |
|
|
33 |
//double timestamp = get_timestamp_ms(); |
|
44 |
yarp_iencs_->getEncodersTimed(yarp_positions_.data(), yarp_timestamps_.data()); |
|
45 |
//iencs->getEncoderSpeeds(velocities.data()); |
|
34 | 46 |
|
35 | 47 |
//publish data to humotion |
36 |
for(int i=0; i<positions.size(); i++){ |
|
37 |
icub_jointinterface->fetch_position(i, positions[i], timestamps[i]); |
|
38 |
icub_jointinterface->fetch_speed(i, velocities[i], timestamps[i]); |
|
39 |
if (i==iCubJointInterface::ICUB_ID_NECK_PAN){ |
|
40 |
printf("\nMMM p=%f v=%f\n",positions[i],velocities[i]); |
|
41 |
} |
|
48 |
for(int i=0; i<yarp_positions_.size(); i++){ |
|
49 |
store_incoming_position(i, yarp_positions_[i], yarp_timestamps_[i]); |
|
42 | 50 |
} |
43 | 51 |
|
44 |
//printf("\n%f %f %f TIME\n", get_timestamp_ms(), timestamps[2], positions[2]); |
|
45 |
//printf("TIMEDIFF %f\n", get_timestamp_ms() - timestamps[2]); |
|
46 |
|
|
47 |
//tell humotion to update lid angle (hack) |
|
48 |
icub_jointinterface->fetch_position(100, 0.0, get_timestamp_ms()); |
|
52 |
//small hack to tell humotion to update the lid angle |
|
49 | 53 |
//fixme: use real id |
54 |
store_incoming_position(100, 0.0, yarp_timestamps_[0]); |
|
55 |
} |
|
56 |
|
|
57 |
void iCubDataReceiver::store_incoming_position(int icub_id, double value, double timestamp) { |
|
58 |
// store joint position in humotion backend |
|
59 |
if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) || |
|
60 |
(icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) { |
|
61 |
// the icub handles eyes differently |
|
62 |
// instead of using seperate left/right pan the icub uses |
|
63 |
// a combined pan angle and vergence. therfore we have to convert this here: |
|
64 |
if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) { |
|
65 |
target_eye_pan_ = value; |
|
66 |
} else { |
|
67 |
target_eye_vergence_ = value; |
|
68 |
} |
|
69 |
|
|
70 |
float left = target_eye_pan_ + target_eye_vergence_/2.0; |
|
71 |
float right = target_eye_pan_ - target_eye_vergence_/2.0; |
|
72 |
|
|
73 |
icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_LEFT_LR, |
|
74 |
left, timestamp); |
|
75 |
icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_RIGHT_LR, |
|
76 |
right, timestamp); |
|
77 |
} else if (icub_id == 100) { |
|
78 |
//HACK |
|
79 |
//icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER, |
|
80 |
// lid_angle, timestamp); |
|
81 |
} else { |
|
82 |
// known configured mapping between joint ids |
|
83 |
int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id); |
|
84 |
icub_jointinterface_->store_incoming_position(humotion_id, value, timestamp); |
|
85 |
} |
|
50 | 86 |
} |
Also available in: Unified diff