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 #include "ControlBoardWrapper.h"
13 #include <iostream>
14 #include <yarp/os/LogStream.h>
15 
16 using namespace yarp::os;
17 using namespace yarp::dev;
18 using namespace yarp::dev::impl;
19 using namespace yarp::sig;
20 using namespace std;
21 
22 
24  stream_IPosCtrl(nullptr),
25  stream_IPosDirect(nullptr),
26  stream_IVel(nullptr),
27  stream_ITorque(nullptr),
28  stream_IPWM(nullptr),
29  stream_ICurrent(nullptr),
30  stream_nJoints(0)
31 {
32 }
33 
35  stream_nJoints = 0;
36  stream_IPosCtrl = dynamic_cast<yarp::dev::IPositionControl *> (x);
37  stream_IPosDirect = dynamic_cast<yarp::dev::IPositionDirect *> (x);
38  stream_IVel = dynamic_cast<yarp::dev::IVelocityControl *> (x);
39  stream_ITorque=dynamic_cast<yarp::dev::ITorqueControl *> (x);
40  stream_IPWM = dynamic_cast<yarp::dev::IPWMControl *> (x);
41  stream_ICurrent = dynamic_cast<yarp::dev::ICurrentControl *> (x);
42 }
43 
44 
46 {
48  if (stream_IPosCtrl)
50 
51  return true;
52 }
53 
54 // streaming port callback
56 {
57  Bottle& b = v.head;
58  Vector& cmdVector = v.body;
59 
60  //Use the following only for debug, since it can heavily slow down the system
61  yCTrace(CONTROLBOARDWRAPPER, "Received command %s, %s\n", b.toString().c_str(), cmdVector.toString().c_str());
62 
63  // some consistency checks
64  if ((int)cmdVector.size() > stream_nJoints)
65  {
66  std::string str = yarp::os::Vocab::decode(b.get(0).asVocab());
67  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());
68  return;
69  }
70  if (cmdVector.data()==nullptr)
71  {
72  yCError(CONTROLBOARDWRAPPER, "Received null command vector");
73  return;
74  }
75 
76  switch (b.get(0).asVocab())
77  {
78  // manage commands with interface name as first
80  {
81  switch (b.get(1).asVocab())
82  {
84  {
85  if (stream_IPWM)
86  {
87  bool ok = stream_IPWM->setRefDutyCycle(b.get(2).asInt32(), cmdVector[0]);
88  if (!ok)
89  yCError(CONTROLBOARDWRAPPER, "Errors while trying to command an pwm message");
90  }
91  else
92  yCError(CONTROLBOARDWRAPPER, "PWM interface not valid");
93  }
94  break;
96  {
97  if (stream_IPWM)
98  {
99  bool ok = stream_IPWM->setRefDutyCycles(cmdVector.data());
100  if (!ok)
101  yCError(CONTROLBOARDWRAPPER, "Errors while trying to command an pwm message");
102  }
103  else
104  yCError(CONTROLBOARDWRAPPER, "PWM interface not valid");
105  }
106  break;
107  }
108  }
109  break;
110 
112  {
113  switch (b.get(1).asVocab())
114  {
115  case VOCAB_CURRENT_REF:
116  {
117  if (stream_ICurrent)
118  {
119  bool ok = stream_ICurrent->setRefCurrent(b.get(2).asInt32(), cmdVector[0]);
120  if (!ok)
121  {
122  yCError(CONTROLBOARDWRAPPER, "Errors while trying to command a streaming current message on single joint\n");
123  }
124  }
125  }
126  break;
127  case VOCAB_CURRENT_REFS:
128  {
129  if (stream_ICurrent)
130  {
131  bool ok = stream_ICurrent->setRefCurrents(cmdVector.data());
132  if (!ok)
133  {
134  yCError(CONTROLBOARDWRAPPER, "Errors while trying to command a streaming current message on all joints\n");
135  }
136  }
137  }
138  break;
140  {
141  if (stream_ICurrent)
142  {
143  int n_joints = b.get(2).asInt32();
144  Bottle *jlut = b.get(3).asList();
145  if (((int)jlut->size() != n_joints) && ((int)cmdVector.size() != n_joints))
146  {
147  yCError(CONTROLBOARDWRAPPER, "Received VOCAB_CURRENT_REF_GROUP size of joints vector or currents vector does not match the selected joint number\n");
148  }
149 
150  int *joint_list = new int[n_joints];
151  for (int i = 0; i < n_joints; i++)
152  joint_list[i] = jlut->get(i).asInt32();
153 
154 
155  bool ok = stream_ICurrent->setRefCurrents(n_joints, joint_list, cmdVector.data());
156  if (!ok)
157  {
158  yCError(CONTROLBOARDWRAPPER, "Error while trying to command a streaming current message on joint group\n");
159  }
160 
161  delete[] joint_list;
162  }
163  }
164  break;
165  default:
166  {
167  std::string str = yarp::os::Vocab::decode(b.get(0).asVocab());
168  yCError(CONTROLBOARDWRAPPER, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
169  }
170  break;
171  }
172  }
173  break;
174 
175  // fallback to commands without interface name
176  case VOCAB_POSITION_MODE:
177  {
178  yCError(CONTROLBOARDWRAPPER, "Received VOCAB_POSITION_MODE this is an send invalid message on streaming port");
179  break;
180  }
182  {
183  if (stream_IPosCtrl)
184  {
185  bool ok = stream_IPosCtrl->positionMove(cmdVector.data());
186  if (!ok)
187  yCError(CONTROLBOARDWRAPPER, "Errors while trying to start a position move");
188  }
189 
190  }
191  break;
192 
193  case VOCAB_VELOCITY_MODE:
194  {
195  yCError(CONTROLBOARDWRAPPER, "Received VOCAB_VELOCITY_MODE this is an send invalid message on streaming port");
196  break;
197  }
198 
199  case VOCAB_VELOCITY_MOVE:
200  {
201  stream_IVel->velocityMove(b.get(1).asInt32(), cmdVector[0]);
202  }
203  break;
204 
206  {
207  if (stream_IVel)
208  {
209  bool ok = stream_IVel->velocityMove(cmdVector.data());
210  if (!ok)
211  yCError(CONTROLBOARDWRAPPER, "Errors while trying to start a velocity move");
212  }
213  }
214  break;
215 
217  {
219  {
220  bool ok = stream_IPosDirect->setPosition(b.get(1).asInt32(), cmdVector[0]); // cmdVector.data());
221  if (!ok)
222  { yCError(CONTROLBOARDWRAPPER, "Errors while trying to command an streaming position direct message on joint %d\n", b.get(1).asInt32() ); }
223  }
224  }
225  break;
226 
228  {
229  if (stream_ITorque)
230  {
231  bool ok = stream_ITorque->setRefTorque(b.get(1).asInt32(), cmdVector[0]);
232  if (!ok)
233  { yCError(CONTROLBOARDWRAPPER, "Errors while trying to command a streaming torque direct message on single joint\n"); }
234  }
235  }
236  break;
237 
239  {
240  if (stream_ITorque)
241  {
242  bool ok = stream_ITorque->setRefTorques(cmdVector.data());
243  if (!ok)
244  { yCError(CONTROLBOARDWRAPPER, "Errors while trying to command a streaming torque direct message on all joints\n"); }
245  }
246  }
247  break;
248 
250  {
251  if (stream_ITorque)
252  {
253  int n_joints = b.get(1).asInt32();
254  Bottle *jlut = b.get(2).asList();
255  if( ((int)jlut->size() != n_joints) && ((int)cmdVector.size() != n_joints) )
256  {
257  yCError(CONTROLBOARDWRAPPER, "Received VOCAB_TORQUES_DIRECT_GROUP size of joints vector or torques vector does not match the selected joint number\n" );
258  }
259 
260  int *joint_list = new int[n_joints];
261  for (int i = 0; i < n_joints; i++)
262  joint_list[i] = jlut->get(i).asInt32();
263 
264 
265  bool ok = stream_ITorque->setRefTorques(n_joints, joint_list, cmdVector.data());
266  if (!ok)
267  { yCError(CONTROLBOARDWRAPPER, "Error while trying to command a streaming toruqe direct message on joint group\n" ); }
268 
269  delete[] joint_list;
270  }
271  }
272  break;
273 
275  {
277  {
278  int n_joints = b.get(1).asInt32();
279  Bottle *jlut = b.get(2).asList();
280  if( ((int)jlut->size() != n_joints) && ((int)cmdVector.size() != n_joints) )
281  {
282  yCError(CONTROLBOARDWRAPPER, "Received VOCAB_POSITION_DIRECT_GROUP size of joints vector or positions vector does not match the selected joint number\n" );
283  }
284 
285  int *joint_list = new int[n_joints];
286  for (int i = 0; i < n_joints; i++)
287  joint_list[i] = jlut->get(i).asInt32();
288 
289 
290  bool ok = stream_IPosDirect->setPositions(n_joints, joint_list, cmdVector.data());
291  if (!ok)
292  { yCError(CONTROLBOARDWRAPPER, "Error while trying to command a streaming position direct message on joint group\n" ); }
293 
294  delete[] joint_list;
295  }
296  }
297  break;
298 
300  {
302  {
303  bool ok = stream_IPosDirect->setPositions(cmdVector.data());
304  if (!ok)
305  { yCError(CONTROLBOARDWRAPPER, "Error while trying to command a streaming position direct message on all joints\n" ); }
306  }
307  }
308  break;
310  {
311  if(stream_IVel)
312  {
313  int n_joints = b.get(1).asInt32();
314  Bottle *jlut = b.get(2).asList();
315  if( ((int)jlut->size() != n_joints) && ((int)cmdVector.size() != n_joints) )
316  yCError(CONTROLBOARDWRAPPER, "Received VOCAB_VELOCITY_MOVE_GROUP size of joints vector or positions vector does not match the selected joint number\n" );
317 
318  int *joint_list = new int[n_joints];
319  for (int i = 0; i < n_joints; i++)
320  joint_list[i] = jlut->get(i).asInt32();
321 
322  bool ok = stream_IVel->velocityMove(n_joints, joint_list, cmdVector.data());
323  if (!ok)
324  { yCError(CONTROLBOARDWRAPPER, "Error while trying to command a velocity move on joint group\n" ); }
325 
326  delete[] joint_list;
327  }
328  }
329  break;
330 
331  default:
332  {
333  std::string str = yarp::os::Vocab::decode(b.get(0).asVocab());
334  yCError(CONTROLBOARDWRAPPER, "Unrecognized message while receiving on command port (%s)\n",str.c_str());
335  }
336  break;
337  }
338 }
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
const yarp::os::LogComponent & CONTROLBOARDWRAPPER()
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:144
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWMS
Definition: IPWMControl.h:147
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWM
Definition: IPWMControl.h:146
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
yarp::dev::IPositionDirect * stream_IPosDirect
void init(ControlBoardWrapper *x)
Initialization.
yarp::dev::IVelocityControl * stream_IVel
yarp::dev::ICurrentControl * stream_ICurrent
void onRead(CommandMessage &v) override
Callback function.
yarp::dev::IPWMControl * stream_IPWM
yarp::dev::ITorqueControl * stream_ITorque
yarp::dev::IPositionControl * stream_IPosCtrl
Interface for control boards implementing current control.
virtual bool setRefCurrent(int m, double curr)=0
Set the reference value of the current for a single motor.
virtual bool setRefCurrents(const double *currs)=0
Set the reference value of the currents for all motors.
Interface for controlling an axis, by sending directly a PWM reference signal to a motor.
Definition: IPWMControl.h:28
virtual bool setRefDutyCycle(int m, double ref)=0
Sets the reference dutycycle to a single motor.
virtual bool setRefDutyCycles(const double *refs)=0
Sets the reference dutycycle for all the motors.
Interface for a generic control board device implementing position control.
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
virtual bool positionMove(int j, double ref)=0
Set new reference point for a single axis.
Interface for a generic control board device implementing position control.
virtual bool setPositions(const int n_joint, const int *joints, const double *refs)=0
Set new reference point for all axes.
virtual bool setPosition(int j, double ref)=0
Set new position for a single axis.
Interface for control boards implementing torque control.
virtual bool setRefTorque(int j, double t)=0
Set the reference value of the torque for a given joint.
virtual bool setRefTorques(const double *t)=0
Set the reference value of the torque for all joints.
Interface for control boards implementing velocity control.
virtual bool velocityMove(int j, double sp)=0
Start motion at a given speed, single joint.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
Group a pair of objects to be sent and received together.
Definition: PortablePair.h:51
BODY body
An object of the second type (BODY).
Definition: PortablePair.h:61
HEAD head
An object of the first type (HEAD).
Definition: PortablePair.h:56
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
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
size_t size() const
Definition: Vector.h:355
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:239
#define yCError(component,...)
Definition: LogComponent.h:157
#define yCTrace(component,...)
Definition: LogComponent.h:88
An interface for the device drivers.
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25