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
15using namespace yarp::os;
16using namespace yarp::dev;
17using namespace yarp::dev::impl;
18using namespace yarp::sig;
19
20
22{
26 x->view(stream_IVel);
28 x->view(stream_IPWM);
30}
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}
42
44{
46 if (stream_IPosCtrl) {
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
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
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
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
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
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
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:139
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWMS
Definition: IPWMControl.h:142
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_REF_PWM
Definition: IPWMControl.h:141
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
Interface implemented by all device drivers.
Definition: DeviceDriver.h:30
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
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 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
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:47
BODY body
An object of the second type (BODY).
Definition: PortablePair.h:57
HEAD head
An object of the first type (HEAD).
Definition: PortablePair.h:52
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:357
size_t size() const
Definition: Vector.h:321
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:205
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
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.