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