YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
12#include <iostream>
13
14using namespace yarp::os;
15using namespace yarp::dev;
16using namespace yarp::dev::impl;
17using namespace yarp::sig;
18
19
32
34{
36 stream_IPosCtrl = nullptr;
37 stream_IPosDirect = nullptr;
38 stream_IVel = nullptr;
39 stream_IVelDirect = nullptr;
40 stream_ITorque = nullptr;
41 stream_IPWM = nullptr;
42 stream_ICurrent = nullptr;
43 stream_IAxis = nullptr;
44}
45
47{
50 if (stream_IVel) { stream_IVel->getAxes(&stream_nJoints); return true; }
51 if (stream_ITorque) { stream_ITorque->getAxes(&stream_nJoints); return true; }
52 if (stream_IAxis) { stream_IAxis->getAxes(&stream_nJoints); return true; }
53
54 yCError(CONTROLBOARD, "Unable to get number of joints");
55 return false;
56}
57
58// streaming port callback
60{
61 Bottle& b = v.head;
63
64 //Use the following only for debug, since it can heavily slow down the system
65 yCTrace(CONTROLBOARD, "Received command %s, %s\n", b.toString().c_str(), cmdVector.toString().c_str());
66
67 // some consistency checks
68 if (static_cast<int>(cmdVector.size()) > stream_nJoints) {
69 std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
70 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());
71 return;
72 }
73 if (cmdVector.data() == nullptr) {
74 yCError(CONTROLBOARD, "Received null command vector");
75 return;
76 }
77
78 switch (b.get(0).asVocab32()) {
79 // manage commands with interface name as first
81 switch (b.get(1).asVocab32()) {
83 if (stream_IPWM) {
84 bool ok = stream_IPWM->setRefDutyCycle(b.get(2).asInt32(), cmdVector[0]);
85 if (!ok) {
86 yCError(CONTROLBOARD, "Errors while trying to command an pwm message");
87 }
88 } else {
89 yCError(CONTROLBOARD, "PWM interface not valid");
90 }
91 } break;
93 if (stream_IPWM) {
94 bool ok = stream_IPWM->setRefDutyCycles(cmdVector.data());
95 if (!ok) {
96 yCError(CONTROLBOARD, "Errors while trying to command an pwm message");
97 }
98 } else {
99 yCError(CONTROLBOARD, "PWM interface not valid");
100 }
101 } break;
102 }
103 } break;
104
106 switch (b.get(1).asVocab32()) {
107 case VOCAB_CURRENT_REF: {
108 if (stream_ICurrent) {
109 bool ok = stream_ICurrent->setRefCurrent(b.get(2).asInt32(), cmdVector[0]);
110 if (!ok) {
111 yCError(CONTROLBOARD, "Errors while trying to command a streaming current message on single joint\n");
112 }
113 }
114 } break;
115 case VOCAB_CURRENT_REFS: {
116 if (stream_ICurrent) {
117 bool ok = stream_ICurrent->setRefCurrents(cmdVector.data());
118 if (!ok) {
119 yCError(CONTROLBOARD, "Errors while trying to command a streaming current message on all joints\n");
120 }
121 }
122 } break;
124 if (stream_ICurrent) {
125 int n_joints = b.get(2).asInt32();
126 Bottle* jlut = b.get(3).asList();
127 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
128 yCError(CONTROLBOARD, "Received VOCAB_CURRENT_REF_GROUP size of joints vector or currents vector does not match the selected joint number\n");
129 }
130
131 int* joint_list = new int[n_joints];
132 for (int i = 0; i < n_joints; i++) {
133 joint_list[i] = jlut->get(i).asInt32();
134 }
135
136
137 bool ok = stream_ICurrent->setRefCurrents(n_joints, joint_list, cmdVector.data());
138 if (!ok) {
139 yCError(CONTROLBOARD, "Error while trying to command a streaming current message on joint group\n");
140 }
141
142 delete[] joint_list;
143 }
144 } break;
145 default:
146 {
147 std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
148 yCError(CONTROLBOARD, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
149 } break;
150 }
151 } break;
152
153 // fallback to commands without interface name
154 case VOCAB_POSITION_MODE: {
155 yCError(CONTROLBOARD, "Received VOCAB_POSITION_MODE this is an send invalid message on streaming port");
156 break;
157 }
158
160 if (stream_IPosCtrl) {
161 bool ok = stream_IPosCtrl->positionMove(cmdVector.data());
162 if (!ok) {
163 yCError(CONTROLBOARD, "Errors while trying to start a position move");
164 }
165 }
166
167 } break;
168
169 case VOCAB_VELOCITY_MODE: {
170 yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_MODE this is an send invalid message on streaming port");
171 break;
172 }
173
174 case VOCAB_VELOCITY_MOVE: {
175 if (stream_IVel) {
176 bool ok = stream_IVel->velocityMove(b.get(1).asInt32(), cmdVector[0]);
177 if (!ok) {
178 yCError(CONTROLBOARD, "Errors while trying to start a velocity move");
179 }
180 }
181 } break;
182
184 if (stream_IVel) {
185 bool ok = stream_IVel->velocityMove(cmdVector.data());
186 if (!ok) {
187 yCError(CONTROLBOARD, "Errors while trying to start a velocity move");
188 }
189 }
190 } break;
191
193 if (stream_IPosDirect) {
194 bool ok = stream_IPosDirect->setPosition(b.get(1).asInt32(), cmdVector[0]); // cmdVector.data());
195 if (!ok) {
196 yCError(CONTROLBOARD, "Errors while trying to command an streaming position direct message on joint %d\n", b.get(1).asInt32());
197 }
198 }
199 } break;
200
202 if (stream_ITorque) {
203 bool ok = stream_ITorque->setRefTorque(b.get(1).asInt32(), cmdVector[0]);
204 if (!ok) {
205 yCError(CONTROLBOARD, "Errors while trying to command a streaming torque direct message on single joint\n");
206 }
207 }
208 } break;
209
211 if (stream_ITorque) {
212 bool ok = stream_ITorque->setRefTorques(cmdVector.data());
213 if (!ok) {
214 yCError(CONTROLBOARD, "Errors while trying to command a streaming torque direct message on all joints\n");
215 }
216 }
217 } break;
218
220 if (stream_ITorque) {
221 int n_joints = b.get(1).asInt32();
222 Bottle* jlut = b.get(2).asList();
223 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
224 yCError(CONTROLBOARD, "Received VOCAB_TORQUES_DIRECT_GROUP size of joints vector or torques vector does not match the selected joint number\n");
225 }
226
227 int* joint_list = new int[n_joints];
228 for (int i = 0; i < n_joints; i++) {
229 joint_list[i] = jlut->get(i).asInt32();
230 }
231
232
233 bool ok = stream_ITorque->setRefTorques(n_joints, joint_list, cmdVector.data());
234 if (!ok) {
235 yCError(CONTROLBOARD, "Error while trying to command a streaming toruqe direct message on joint group\n");
236 }
237
238 delete[] joint_list;
239 }
240 } break;
241
243 if (stream_IPosDirect) {
244 int n_joints = b.get(1).asInt32();
245 Bottle* jlut = b.get(2).asList();
246 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
247 yCError(CONTROLBOARD, "Received VOCAB_POSITION_DIRECT_GROUP size of joints vector or positions vector does not match the selected joint number\n");
248 }
249
250 int* joint_list = new int[n_joints];
251 for (int i = 0; i < n_joints; i++) {
252 joint_list[i] = jlut->get(i).asInt32();
253 }
254
255
256 bool ok = stream_IPosDirect->setPositions(n_joints, joint_list, cmdVector.data());
257 if (!ok) {
258 yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on joint group\n");
259 }
260
261 delete[] joint_list;
262 }
263 } break;
264
266 if (stream_IPosDirect) {
267 bool ok = stream_IPosDirect->setPositions(cmdVector.data());
268 if (!ok) {
269 yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on all joints\n");
270 }
271 }
272 } break;
273
275 if (stream_IVelDirect) {
276 bool ok = stream_IVelDirect->setDesiredVelocity(b.get(1).asInt32(), cmdVector[0]); // cmdVector.data());
277 if (!ok) {
278 yCError(CONTROLBOARD, "Errors while trying to command an streaming position direct message on joint %d\n", b.get(1).asInt32());
279 }
280 }
281 } break;
282
284 if (stream_IVelDirect) {
285 int n_joints = b.get(1).asInt32();
286 Bottle* jlut = b.get(2).asList();
287 if ((static_cast<int>(jlut->size()) != n_joints) &&
288 (static_cast<int>(cmdVector.size()) != n_joints)) {
289 yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_DIRECT_SET_GROUP size of joints vector or positions vector does not match the selected joint number\n");
290 }
291
292 std::vector<int> joint_list(n_joints);
293 for (int i = 0; i < n_joints; i++) {
294 joint_list[i] = jlut->get(i).asInt32();
295 }
296 std::vector<double> vel_list(n_joints);
297 for (int i = 0; i < n_joints; i++) {
298 vel_list[i] = cmdVector[i];
299 }
300
302 if (!ok) {
303 yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on joint group\n");
304 }
305
306 }
307 } break;
308
310 if (stream_IVelDirect) {
311
312 int n_joints = cmdVector.size();
313 std::vector<double> vel_list(n_joints);
314 for (int i = 0; i < n_joints; i++) {
315 vel_list[i] = cmdVector[i];
316 }
317
319 if (!ok) {
320 yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on all joints\n");
321 }
322 }
323 } break;
324
326 if (stream_IVel) {
327 int n_joints = b.get(1).asInt32();
328 Bottle* jlut = b.get(2).asList();
329 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
330 yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_MOVE_GROUP size of joints vector or positions vector does not match the selected joint number\n");
331 }
332
333 int* joint_list = new int[n_joints];
334 for (int i = 0; i < n_joints; i++) {
335 joint_list[i] = jlut->get(i).asInt32();
336 }
337
338 bool ok = stream_IVel->velocityMove(n_joints, joint_list, cmdVector.data());
339 if (!ok) {
340 yCError(CONTROLBOARD, "Error while trying to command a velocity move on joint group\n");
341 }
342
343 delete[] joint_list;
344 }
345 } break;
346
347 default:
348 {
349 std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
350 yCError(CONTROLBOARD, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
351 } break;
352 }
353}
const yarp::os::LogComponent & CONTROLBOARD()
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MODE
constexpr yarp::conf::vocab32_t VOCAB_POSITION_MODE
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
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWMS
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWM
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
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MOVE_GROUP
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_MOVES
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_DIRECT_SET_ONE
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_DIRECT_SET_GROUP
constexpr yarp::conf::vocab32_t VOCAB_VELOCITY_DIRECT_SET_ALL
yarp::dev::IPositionDirect * stream_IPosDirect
void init(yarp::dev::DeviceDriver *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::IVelocityDirect * stream_IVelDirect
yarp::dev::ITorqueControl * stream_ITorque
yarp::dev::IPositionControl * stream_IPosCtrl
yarp::dev::IAxisInfo * stream_IAxis
Interface implemented by all device drivers.
bool view(T *&x)
Get an interface to the device driver.
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
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.
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.
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.
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.
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.
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
virtual bool getAxes(int *axes)=0
Get the number of controlled axes.
virtual bool velocityMove(int j, double sp)=0
Start motion at a given speed, single joint.
virtual yarp::dev::ReturnValue setDesiredVelocity(int jnt, double vel)=0
Set the velocity of single joint.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:217
A mini-server for performing network communication in the background.
Group a pair of objects to be sent and received together.
BODY body
An object of the second type (BODY).
HEAD head
An object of the first type (HEAD).
virtual yarp::conf::vocab32_t asVocab32() const
Get 32 bit vocabulary identifier as an integer.
Definition Value.cpp:234
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:210
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:252
#define yCError(component,...)
#define yCTrace(component,...)
For streams capable of holding different kinds of content, check what they actually have.
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition Vocab32.cpp:33
An interface to the operating system, including Port based communication.