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
31
33{
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 stream_IAxis = nullptr;
42}
43
45{
48 if (stream_IVel) { stream_IVel->getAxes(&stream_nJoints); return true; }
49 if (stream_ITorque) { stream_ITorque->getAxes(&stream_nJoints); return true; }
50 if (stream_IAxis) { stream_IAxis->getAxes(&stream_nJoints); return true; }
51
52 yCError(CONTROLBOARD, "Unable to get number of joints");
53 return false;
54}
55
56// streaming port callback
58{
59 Bottle& b = v.head;
61
62 //Use the following only for debug, since it can heavily slow down the system
63 yCTrace(CONTROLBOARD, "Received command %s, %s\n", b.toString().c_str(), cmdVector.toString().c_str());
64
65 // some consistency checks
66 if (static_cast<int>(cmdVector.size()) > stream_nJoints) {
67 std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
68 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());
69 return;
70 }
71 if (cmdVector.data() == nullptr) {
72 yCError(CONTROLBOARD, "Received null command vector");
73 return;
74 }
75
76 switch (b.get(0).asVocab32()) {
77 // manage commands with interface name as first
79 switch (b.get(1).asVocab32()) {
81 if (stream_IPWM) {
82 bool ok = stream_IPWM->setRefDutyCycle(b.get(2).asInt32(), cmdVector[0]);
83 if (!ok) {
84 yCError(CONTROLBOARD, "Errors while trying to command an pwm message");
85 }
86 } else {
87 yCError(CONTROLBOARD, "PWM interface not valid");
88 }
89 } break;
91 if (stream_IPWM) {
92 bool ok = stream_IPWM->setRefDutyCycles(cmdVector.data());
93 if (!ok) {
94 yCError(CONTROLBOARD, "Errors while trying to command an pwm message");
95 }
96 } else {
97 yCError(CONTROLBOARD, "PWM interface not valid");
98 }
99 } break;
100 }
101 } break;
102
104 switch (b.get(1).asVocab32()) {
105 case VOCAB_CURRENT_REF: {
106 if (stream_ICurrent) {
107 bool ok = stream_ICurrent->setRefCurrent(b.get(2).asInt32(), cmdVector[0]);
108 if (!ok) {
109 yCError(CONTROLBOARD, "Errors while trying to command a streaming current message on single joint\n");
110 }
111 }
112 } break;
113 case VOCAB_CURRENT_REFS: {
114 if (stream_ICurrent) {
115 bool ok = stream_ICurrent->setRefCurrents(cmdVector.data());
116 if (!ok) {
117 yCError(CONTROLBOARD, "Errors while trying to command a streaming current message on all joints\n");
118 }
119 }
120 } break;
122 if (stream_ICurrent) {
123 int n_joints = b.get(2).asInt32();
124 Bottle* jlut = b.get(3).asList();
125 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
126 yCError(CONTROLBOARD, "Received VOCAB_CURRENT_REF_GROUP size of joints vector or currents vector does not match the selected joint number\n");
127 }
128
129 int* joint_list = new int[n_joints];
130 for (int i = 0; i < n_joints; i++) {
131 joint_list[i] = jlut->get(i).asInt32();
132 }
133
134
135 bool ok = stream_ICurrent->setRefCurrents(n_joints, joint_list, cmdVector.data());
136 if (!ok) {
137 yCError(CONTROLBOARD, "Error while trying to command a streaming current message on joint group\n");
138 }
139
140 delete[] joint_list;
141 }
142 } break;
143 default:
144 {
145 std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
146 yCError(CONTROLBOARD, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
147 } break;
148 }
149 } break;
150
151 // fallback to commands without interface name
152 case VOCAB_POSITION_MODE: {
153 yCError(CONTROLBOARD, "Received VOCAB_POSITION_MODE this is an send invalid message on streaming port");
154 break;
155 }
156
158 if (stream_IPosCtrl) {
159 bool ok = stream_IPosCtrl->positionMove(cmdVector.data());
160 if (!ok) {
161 yCError(CONTROLBOARD, "Errors while trying to start a position move");
162 }
163 }
164
165 } break;
166
167 case VOCAB_VELOCITY_MODE: {
168 yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_MODE this is an send invalid message on streaming port");
169 break;
170 }
171
172 case VOCAB_VELOCITY_MOVE: {
173 if (stream_IVel) {
174 bool ok = stream_IVel->velocityMove(b.get(1).asInt32(), cmdVector[0]);
175 if (!ok) {
176 yCError(CONTROLBOARD, "Errors while trying to start a velocity move");
177 }
178 }
179 } break;
180
182 if (stream_IVel) {
183 bool ok = stream_IVel->velocityMove(cmdVector.data());
184 if (!ok) {
185 yCError(CONTROLBOARD, "Errors while trying to start a velocity move");
186 }
187 }
188 } break;
189
191 if (stream_IPosDirect) {
192 bool ok = stream_IPosDirect->setPosition(b.get(1).asInt32(), cmdVector[0]); // cmdVector.data());
193 if (!ok) {
194 yCError(CONTROLBOARD, "Errors while trying to command an streaming position direct message on joint %d\n", b.get(1).asInt32());
195 }
196 }
197 } break;
198
200 if (stream_ITorque) {
201 bool ok = stream_ITorque->setRefTorque(b.get(1).asInt32(), cmdVector[0]);
202 if (!ok) {
203 yCError(CONTROLBOARD, "Errors while trying to command a streaming torque direct message on single joint\n");
204 }
205 }
206 } break;
207
209 if (stream_ITorque) {
210 bool ok = stream_ITorque->setRefTorques(cmdVector.data());
211 if (!ok) {
212 yCError(CONTROLBOARD, "Errors while trying to command a streaming torque direct message on all joints\n");
213 }
214 }
215 } break;
216
218 if (stream_ITorque) {
219 int n_joints = b.get(1).asInt32();
220 Bottle* jlut = b.get(2).asList();
221 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
222 yCError(CONTROLBOARD, "Received VOCAB_TORQUES_DIRECT_GROUP size of joints vector or torques vector does not match the selected joint number\n");
223 }
224
225 int* joint_list = new int[n_joints];
226 for (int i = 0; i < n_joints; i++) {
227 joint_list[i] = jlut->get(i).asInt32();
228 }
229
230
231 bool ok = stream_ITorque->setRefTorques(n_joints, joint_list, cmdVector.data());
232 if (!ok) {
233 yCError(CONTROLBOARD, "Error while trying to command a streaming toruqe direct message on joint group\n");
234 }
235
236 delete[] joint_list;
237 }
238 } break;
239
241 if (stream_IPosDirect) {
242 int n_joints = b.get(1).asInt32();
243 Bottle* jlut = b.get(2).asList();
244 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
245 yCError(CONTROLBOARD, "Received VOCAB_POSITION_DIRECT_GROUP size of joints vector or positions vector does not match the selected joint number\n");
246 }
247
248 int* joint_list = new int[n_joints];
249 for (int i = 0; i < n_joints; i++) {
250 joint_list[i] = jlut->get(i).asInt32();
251 }
252
253
254 bool ok = stream_IPosDirect->setPositions(n_joints, joint_list, cmdVector.data());
255 if (!ok) {
256 yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on joint group\n");
257 }
258
259 delete[] joint_list;
260 }
261 } break;
262
264 if (stream_IPosDirect) {
265 bool ok = stream_IPosDirect->setPositions(cmdVector.data());
266 if (!ok) {
267 yCError(CONTROLBOARD, "Error while trying to command a streaming position direct message on all joints\n");
268 }
269 }
270 } break;
271
273 if (stream_IVel) {
274 int n_joints = b.get(1).asInt32();
275 Bottle* jlut = b.get(2).asList();
276 if ((static_cast<int>(jlut->size()) != n_joints) && (static_cast<int>(cmdVector.size()) != n_joints)) {
277 yCError(CONTROLBOARD, "Received VOCAB_VELOCITY_MOVE_GROUP size of joints vector or positions vector does not match the selected joint number\n");
278 }
279
280 int* joint_list = new int[n_joints];
281 for (int i = 0; i < n_joints; i++) {
282 joint_list[i] = jlut->get(i).asInt32();
283 }
284
285 bool ok = stream_IVel->velocityMove(n_joints, joint_list, cmdVector.data());
286 if (!ok) {
287 yCError(CONTROLBOARD, "Error while trying to command a velocity move on joint group\n");
288 }
289
290 delete[] joint_list;
291 }
292 } break;
293
294 default:
295 {
296 std::string str = yarp::os::Vocab32::decode(b.get(0).asVocab32());
297 yCError(CONTROLBOARD, "Unrecognized message while receiving on command port (%s)\n", str.c_str());
298 } break;
299 }
300}
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
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_GROUP
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::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.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
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
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 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
#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 Vocab.cpp:33
An interface to the operating system, including Port based communication.