humotion / examples / yarp_icub / src / icub_data_receiver.cpp @ 6c028e11
History | View | Annotate | Download (3.38 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 | 35b3ca25 | Simon Schulz | //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:
|
||
29 | 8c6c1163 | Simon Schulz | int joints;
|
30 | 35b3ca25 | Simon Schulz | yarp_iencs_->getAxes(&joints); |
31 | yarp_positions_.resize(joints); |
||
32 | yarp_timestamps_.resize(joints); |
||
33 | 8c6c1163 | Simon Schulz | } |
34 | |||
35 | 35b3ca25 | Simon Schulz | bool iCubDataReceiver::threadInit() {
|
36 | return true; |
||
37 | 8c6c1163 | Simon Schulz | } |
38 | |||
39 | 35b3ca25 | Simon Schulz | void iCubDataReceiver::threadRelease() {
|
40 | } |
||
41 | 7adf90be | Simon Schulz | |
42 | 35b3ca25 | Simon Schulz | void iCubDataReceiver::run() {
|
43 | 8c6c1163 | Simon Schulz | //grab pos+vel data:
|
44 | 35b3ca25 | Simon Schulz | yarp_iencs_->getEncodersTimed(yarp_positions_.data(), yarp_timestamps_.data()); |
45 | //iencs->getEncoderSpeeds(velocities.data());
|
||
46 | 8c6c1163 | Simon Schulz | |
47 | //publish data to humotion
|
||
48 | 35b3ca25 | Simon Schulz | for(int i=0; i<yarp_positions_.size(); i++){ |
49 | store_incoming_position(i, yarp_positions_[i], yarp_timestamps_[i]); |
||
50 | 8c6c1163 | Simon Schulz | } |
51 | 4dbb1a71 | sschulz | |
52 | printf("\n");
|
||
53 | 8c6c1163 | Simon Schulz | |
54 | 35b3ca25 | Simon Schulz | //small hack to tell humotion to update the lid angle
|
55 | 8c6c1163 | Simon Schulz | //fixme: use real id
|
56 | 35b3ca25 | Simon Schulz | store_incoming_position(100, 0.0, yarp_timestamps_[0]); |
57 | } |
||
58 | |||
59 | void iCubDataReceiver::store_incoming_position(int icub_id, double value, double timestamp) { |
||
60 | 6c028e11 | Simon Schulz | cout << "iCubDataReceiver::store_incoming_position(icubid=" << icub_id << ", " << value << "..)\n"; |
61 | |||
62 | 35b3ca25 | Simon Schulz | // store joint position in humotion backend
|
63 | if ((icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) ||
|
||
64 | (icub_id == iCubJointInterface::ICUB_ID_EYES_VERGENCE)) { |
||
65 | // the icub handles eyes differently
|
||
66 | // instead of using seperate left/right pan the icub uses
|
||
67 | // a combined pan angle and vergence. therfore we have to convert this here:
|
||
68 | if (icub_id == iCubJointInterface::ICUB_ID_EYES_PAN) {
|
||
69 | target_eye_pan_ = value; |
||
70 | } else {
|
||
71 | 6c028e11 | Simon Schulz | target_eye_vergence_ = -value; |
72 | 35b3ca25 | Simon Schulz | } |
73 | |||
74 | float left = target_eye_pan_ + target_eye_vergence_/2.0; |
||
75 | float right = target_eye_pan_ - target_eye_vergence_/2.0; |
||
76 | |||
77 | icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_LEFT_LR, |
||
78 | left, timestamp); |
||
79 | icub_jointinterface_->store_incoming_position(JointInterface::ID_EYES_RIGHT_LR, |
||
80 | right, timestamp); |
||
81 | } else if (icub_id == 100) { |
||
82 | //HACK
|
||
83 | //icub_jointinterface->store_incoming_position(ID_EYES_RIGHT_LID_UPPER,
|
||
84 | // lid_angle, timestamp);
|
||
85 | } else {
|
||
86 | 6c028e11 | Simon Schulz | if (icub_id == iCubJointInterface::ID_NECK_PAN) {
|
87 | // icub uses an inverted neck pan specification
|
||
88 | value = -value; |
||
89 | } |
||
90 | |||
91 | 35b3ca25 | Simon Schulz | // known configured mapping between joint ids
|
92 | int humotion_id = icub_jointinterface_->convert_icub_jointid_to_humotion(icub_id);
|
||
93 | icub_jointinterface_->store_incoming_position(humotion_id, value, timestamp); |
||
94 | } |
||
95 | 8c6c1163 | Simon Schulz | } |