ocra-recipes
Doxygen documentation for the ocra-recipes repository
RobotState.cpp
Go to the documentation of this file.
2 
3 using namespace ocra_recipes;
4 
5 
7 {
8 }
9 
10 RobotState::RobotState(const int numberOfDoF)
11 : nDoF(numberOfDoF)
12 {
13  q.resize(nDoF);
14  qd.resize(nDoF);
15 }
16 
18 {
19 }
20 
21 std::ostream& operator<<(std::ostream &out, const RobotState& state)
22 {
23  std::cout << "q \t | \t qd" << std::endl;
24  for(auto i=0; i<state.q.size(); ++i)
25  {
26  out << state.q(i) << "\t | \t" << state.qd(i) << std::endl;
27  }
28 
29  out << "x" << " " << "y" << " " << "z" << " " << "qx" << " " << "qy" << " " << "qz" << " " << "qw" << std::endl;
30  out << state.H_root.x() << " ";
31  out << state.H_root.y() << " ";
32  out << state.H_root.z() << " ";
33  out << state.H_root.qx() << " ";
34  out << state.H_root.qy() << " ";
35  out << state.H_root.qz() << " ";
36  out << state.H_root.qw() << std::endl;
37 
38  out << "rx" << " " << "ry" << " " << "rz" << " " << "rx" << " " << "ry" << " " << "rz" << std::endl;
39  // out << state.T_root.rx() << " ";
40  // out << state.T_root.ry() << " ";
41  // out << state.T_root.rz() << " ";
42  // out << state.T_root.vx() << " ";
43  // out << state.T_root.vy() << " ";
44  // out << state.T_root.vz() << std::endl;
45  out << state.T_root << std::endl;
46 
47  return out;
48 }
49 
50 bool RobotState::write(yarp::os::ConnectionWriter& connection)
51 {
52  // Two lists will be transmitted.
53  connection.appendInt(BOTTLE_TAG_LIST);
54  connection.appendInt(2);
55  // The first list will contain just an integer (DOF))
56  connection.appendInt(BOTTLE_TAG_INT);
57  connection.appendInt(q.size());
58  // The second list will contain the state of the robot
59  connection.appendInt(BOTTLE_TAG_LIST + BOTTLE_TAG_DOUBLE);
60  connection.appendInt(q.size() + qd.size() + 13);
61  for(auto i=0; i<q.size(); ++i)
62  {
63  connection.appendDouble(q(i));
64  connection.appendDouble(qd(i));
65  }
66  connection.appendDouble(H_root.x());
67  connection.appendDouble(H_root.y());
68  connection.appendDouble(H_root.z());
69  connection.appendDouble(H_root.qx());
70  connection.appendDouble(H_root.qy());
71  connection.appendDouble(H_root.qz());
72  connection.appendDouble(H_root.qw());
73 
74  connection.appendDouble(T_root.rx());
75  connection.appendDouble(T_root.ry());
76  connection.appendDouble(T_root.rz());
77  connection.appendDouble(T_root.vx());
78  connection.appendDouble(T_root.vy());
79  connection.appendDouble(T_root.vz());
80 
81  // If someone connects in text mode, show something readable
82  connection.convertTextMode();
83 
84  return !connection.isError();
85 }
86 
87 bool RobotState::read(yarp::os::ConnectionReader& connection)
88 {
89  // Auto-convert text mode interaction
90  connection.convertTextMode();
91 
92  if ( connection.expectInt() != BOTTLE_TAG_LIST || connection.expectInt() != 2) {
93  OCRA_ERROR("Received malformed data. Expected two lists with the following structure: (DOF)(ROBOT STATE)");
94  return false;
95  }
96 
97  // Reading DOF
98  if ( connection.expectInt() != BOTTLE_TAG_INT ) {
99  OCRA_ERROR("Received malformed data. Expected one integer for DOF)");
100  return false;
101  }
102  this->nDoF = connection.expectInt();
103  this->q.resize(nDoF);
104  this->qd.resize(nDoF);
105 
106  if ( connection.expectInt() != BOTTLE_TAG_LIST + BOTTLE_TAG_DOUBLE || connection.expectInt()!= q.size() + qd.size() + 13 ) {
107  OCRA_ERROR("Received a list with less data than expected");
108  return false;
109  }
110 
111  for(auto i=0; i<this->nDoF; ++i)
112  {
113  this->q(i) = connection.expectDouble();
114  this->qd(i) = connection.expectDouble();
115  }
116  this->H_root.x() = connection.expectDouble();
117  this->H_root.y() = connection.expectDouble();
118  this->H_root.z() = connection.expectDouble();
119  this->H_root.qx() = connection.expectDouble();
120  this->H_root.qy() = connection.expectDouble();
121  this->H_root.qz() = connection.expectDouble();
122  this->H_root.qw() = connection.expectDouble();
123 
124  this->T_root.rx() = connection.expectDouble();
125  this->T_root.ry() = connection.expectDouble();
126  this->T_root.rz() = connection.expectDouble();
127  this->T_root.vx() = connection.expectDouble();
128  this->T_root.vy() = connection.expectDouble();
129  this->T_root.vz() = connection.expectDouble();
130 
131  return !connection.isError();
132 }
133 
134 
135 /**************************************************************************************************
136  StateListener Class
137 **************************************************************************************************/
139 {
140 }
141 
143 {
144 }
145 
146 StateListener::StateListener(std::shared_ptr<ocra::Model> modelPtr)
147 : model(modelPtr)
148 {
149  //do nothing
150 }
151 
152 bool StateListener::read(yarp::os::ConnectionReader& connection)
153 {
154 
155  RobotState state;
156 
157  if (!state.read(connection)){
158  OCRA_ERROR("Couldn't read state: " << state << std::endl);
159  return false;
160  }
161  else{
162  model->setState(state.H_root, state.q, state.T_root, state.qd);
163  return true;
164  }
165 }
Eigen::VectorXd qd
Definition: RobotState.h:42
Eigen::Displacementd H_root
Definition: RobotState.h:43
#define OCRA_ERROR(msg)
Definition: ErrorsHelper.h:32
bool read(yarp::os::ConnectionReader &connection)
Definition: RobotState.cpp:152
std::ostream & operator<<(std::ostream &out, const RobotState &state)
Definition: RobotState.cpp:21
virtual bool write(yarp::os::ConnectionWriter &connection)
Definition: RobotState.cpp:50
Eigen::VectorXd q
Definition: RobotState.h:41
Eigen::Twistd T_root
Definition: RobotState.h:44
A portable class for sending robot state information over yarp.
Definition: RobotState.h:26
virtual bool read(yarp::os::ConnectionReader &connection)
Definition: RobotState.cpp:87