YARP
Yet Another Robot Platform
StreamingMessagesParser.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
8 
9 #include <yarp/os/LogStream.h>
10 
13 #include <iostream>
14 
15 using namespace yarp::os;
16 using namespace yarp::dev;
17 using namespace yarp::dev::impl;
18 using namespace yarp::sig;
19 
20 
22 {
23  stream_nJoints = 0;
24  x->view(stream_IPosCtrl);
25  x->view(stream_IPosDirect);
26  x->view(stream_IVel);
27  x->view(stream_ITorque);
28  x->view(stream_IPWM);
29  x->view(stream_ICurrent);
30 }
31 
33 {
34  stream_nJoints = 0;
35  stream_IPosCtrl = nullptr;
36  stream_IPosDirect = nullptr;
37  stream_IVel = nullptr;
38  stream_ITorque = nullptr;
39  stream_IPWM = nullptr;
40  stream_ICurrent = nullptr;
41 }
42 
44 {
45  stream_nJoints = 0;
46  if (stream_IPosCtrl) {
47  stream_IPosCtrl->getAxes(&stream_nJoints);
48  }
49 
50  return true;
51 }
52 
53 // streaming port callback
55 {
56  Bottle& b = v.head;
57  Vector& cmdVector = v.body;
58 
59  //Use the following only for debug, since it can heavily slow down the system
60  yCTrace(CONTROLBOARD, "Received command %s, %s\n", b.toString().c_str(), cmdVector.toString().c_str());
61 
62  // some consistency checks
63  if (static_cast<int>(cmdVector.size()) > stream_nJoints) {
64  std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
65  yCError(CONTROLBOARD, "Received command vector with number of elements bigger than axis controlled by this wrapper (cmd: %s requested jnts: %d received jnts: %d)\n", str.c_str(), stream_nJoints, (int)cmdVector.size());
66  return;
67  }
68  if (cmdVector.data() == nullptr) {
69  yCError(CONTROLBOARD, "Received null command vector");
70  return;
71  }
72 
73  switch (b.get(0).asVocab32()) {
74  // manage commands with interface name as first
76  switch (b.get(1).asVocab32()) {
78  if (stream_IPWM) {
79  bool ok = stream_IPWM->setRefDutyCycle(b.get(2).asInt32(), cmdVector[0]);
80  if (!ok) {
81  yCError(CONTROLBOARD, "Errors while trying to command an pwm message");
82  }
83  } else {
84  yCError(CONTROLBOARD, "PWM interface not valid");
85  }
86  } break;
88  if (stream_IPWM) {
89  bool ok = stream_IPWM->setRefDutyCycles(cmdVector.data());
90  if (!ok) {
91  yCError(CONTROLBOARD, "Errors while trying to command an pwm message");
92  }
93  } else {
94  yCError(CONTROLBOARD, "PWM interface not valid");
95  }
96  } break;
97  }
98  } break;
99 
101  switch (b.get(1).asVocab32()) {
102  case VOCAB_CURRENT_REF: {
103  if (stream_ICurrent) {
104  bool ok = stream_ICurrent->setRefCurrent(b.get(2).asInt32(), cmdVector[0]);
105  if (!ok) {
106  yCError(CONTROLBOARD, "Errors while trying to command a streaming current message on single joint\n");
107  }
108  }
109  } break;
110  case VOCAB_CURRENT_REFS: {
111  if (stream_ICurrent) {
112  bool ok = stream_ICurrent->setRefCurrents(cmdVector.data());
113  if (!ok) {
114  yCError(CONTROLBOARD, "Errors while trying to command a streaming current message on all joints\n");
115  }
116  }
117  } break;
119  if (stream_ICurrent) {
120  int n_joints = b.get(2).asInt32();
121  Bottle* jlut = b.get(3).asList();
122  if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
123  yCError(CONTROLBOARD, "Received VOCAB_CURRENT_REF_GROUP size of joints vector or currents vector does not match the selected joint number\n");
124  }
125 
126  int* joint_list = new int[n_joints];
127  for (int i = 0; i < n_joints; i++) {
128  joint_list[i] = jlut->get(i).asInt32();
129  }
130 
131 
132  bool ok = stream_ICurrent->setRefCurrents(n_joints, joint_list, cmdVector.data());
133  if (!ok) {
134  yCError(CONTROLBOARD, "Error while trying to command a streaming current message on joint group\n");
135  }
136 
137  delete[] joint_list;
138  }
139  } break;
140  default:
141  {
142  std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
143  yCError(CONTROLBOARD, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
144  } break;
145  }
146  } break;
147 
148  // fallback to commands without interface name
149  case VOCAB_POSITION_MODE: {
150  yCError(CONTROLBOARD, "Received VOCAB_POSITION_MODE this is an send invalid message on streaming port");
151  break;
152  }
153 
154  case VOCAB_POSITION_MOVES: {
155  if (stream_IPosCtrl) {
156  bool ok = stream_IPosCtrl->positionMove(cmdVector.data());
157  if (!ok) {
158  yCError(CONTROLBOARD, "Errors while trying to start a position move");
159  }
160  }
161 
162  } break;
163 
164  case VOCAB_VELOCITY_MODE: {
165  yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_MODE this is an send invalid message on streaming port");
166  break;
167  }
168 
169  case VOCAB_VELOCITY_MOVE: {
170  stream_IVel->velocityMove(b.get(1).asInt32(), cmdVector[0]);
171  } break;
172 
173  case VOCAB_VELOCITY_MOVES: {
174  if (stream_IVel) {
175  bool ok = stream_IVel->velocityMove(cmdVector.data());
176  if (!ok) {
177  yCError(CONTROLBOARD, "Errors while trying to start a velocity move");
178  }
179  }
180  } break;
181 
182  case VOCAB_POSITION_DIRECT: {
183  if (stream_IPosDirect) {
184  bool ok = stream_IPosDirect->setPosition(b.get(1).asInt32(), cmdVector[0]); // cmdVector.data());
185  if (!ok) {
186  yCError(CONTROLBOARD, "Errors while trying to command an streaming position direct message on joint %d\n", b.get(1).asInt32());
187  }
188  }
189  } break;
190 
191  case VOCAB_TORQUES_DIRECT: {
192  if (stream_ITorque) {
193  bool ok = stream_ITorque->setRefTorque(b.get(1).asInt32(), cmdVector[0]);
194  if (!ok) {
195  yCError(CONTROLBOARD, "Errors while trying to command a streaming torque direct message on single joint\n");
196  }
197  }
198  } break;
199 
200  case VOCAB_TORQUES_DIRECTS: {
201  if (stream_ITorque) {
202  bool ok = stream_ITorque->setRefTorques(cmdVector.data());
203  if (!ok) {
204  yCError(CONTROLBOARD, "Errors while trying to command a streaming torque direct message on all joints\n");
205  }
206  }
207  } break;
208 
210  if (stream_ITorque) {
211  int n_joints = b.get(1).asInt32();
212  Bottle* jlut = b.get(2).asList();
213  if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
214  yCError(CONTROLBOARD, "Received VOCAB_TORQUES_DIRECT_GROUP size of joints vector or torques vector does not match the selected joint number\n");
215  }
216 
217  int* joint_list = new int[n_joints];
218  for (int i = 0; i < n_joints; i++) {
219  joint_list[i] = jlut->get(i).asInt32();
220  }
221 
222 
223  bool ok = stream_ITorque->setRefTorques(n_joints, joint_list, cmdVector.data());
224  if (!ok) {
225  yCError(CONTROLBOARD, "Error while trying to command a streaming toruqe direct message on joint group\n");
226  }
227 
228  delete[] joint_list;
229  }
230  } break;
231 
233  if (stream_IPosDirect) {
234  int n_joints = b.get(1).asInt32();
235  Bottle* jlut = b.get(2).asList();
236  if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
237  yCError(CONTROLBOARD, "Received VOCAB_POSITION_DIRECT_GROUP size of joints vector or positions vector does not match the selected joint number\n");
238  }
239 
240  int* joint_list = new int[n_joints];
241  for (int i = 0; i < n_joints; i++) {
242  joint_list[i] = jlut->get(i).asInt32();
243  }
244 
245 
246  bool ok = stream_IPosDirect->setPositions(n_joints, joint_list, cmdVector.data());
247  if (!ok) {
248  yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on joint group\n");
249  }
250 
251  delete[] joint_list;
252  }
253  } break;
254 
255  case VOCAB_POSITION_DIRECTS: {
256  if (stream_IPosDirect) {
257  bool ok = stream_IPosDirect->setPositions(cmdVector.data());
258  if (!ok) {
259  yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on all joints\n");
260  }
261  }
262  } break;
263 
265  if (stream_IVel) {
266  int n_joints = b.get(1).asInt32();
267  Bottle* jlut = b.get(2).asList();
268  if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
269  yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_MOVE_GROUP size of joints vector or positions vector does not match the selected joint number\n");
270  }
271 
272  int* joint_list = new int[n_joints];
273  for (int i = 0; i < n_joints; i++) {
274  joint_list[i] = jlut->get(i).asInt32();
275  }
276 
277  bool ok = stream_IVel->velocityMove(n_joints, joint_list, cmdVector.data());
278  if (!ok) {
279  yCError(CONTROLBOARD, "Error while trying to command a velocity move on joint group\n");
280  }
281 
282  delete[] joint_list;
283  }
284  } break;
285 
286  default:
287  {
288  std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
289  yCError(CONTROLBOARD, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
290  } break;
291  }
292 }
const yarp::os::LogComponent & CONTROLBOARD()
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MOVE
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MODE
constexpr yarp::conf::vocab32_t VOCAB_POSITION_MODE
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MOVES
constexpr yarp::conf::vocab32_t VOCAB_POSITION_MOVES
constexpr yarp::conf::vocab32_t VOCAB_CURRENT_REF_GROUP
constexpr yarp::conf::vocab32_t VOCAB_CURRENT_REFS
constexpr yarp::conf::vocab32_t VOCAB_CURRENTCONTROL_INTERFACE
constexpr yarp::conf::vocab32_t VOCAB_CURRENT_REF
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_INTERFACE
Definition: IPWMControl.h:141
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWMS
Definition: IPWMControl.h:144
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWM
Definition: IPWMControl.h:143
constexpr yarp::conf::vocab32_t VOCAB_POSITION_DIRECTS
constexpr yarp::conf::vocab32_t VOCAB_POSITION_DIRECT
constexpr yarp::conf::vocab32_t VOCAB_POSITION_DIRECT_GROUP
constexpr yarp::conf::vocab32_t VOCAB_TORQUES_DIRECTS
constexpr yarp::conf::vocab32_t VOCAB_TORQUES_DIRECT_GROUP
constexpr yarp::conf::vocab32_t VOCAB_TORQUES_DIRECT
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MOVE_GROUP
void init(yarp::dev::DeviceDriver *x)
Initialization.
void onRead(CommandMessage &v) override
Callback function.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
Group a pair of objects to be sent and received together.
Definition: PortablePair.h:48
BODY body
An object of the second type (BODY).
Definition: PortablePair.h:58
HEAD head
An object of the first type (HEAD).
Definition: PortablePair.h:53
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:240
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition: Vector.h:359
size_t size() const
Definition: Vector.h:323
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:207
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCTrace(component,...)
Definition: LogComponent.h:85
An interface for the device drivers.
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:33
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22