YARP
Yet Another Robot Platform
SubDevice.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
7#include "SubDevice.h"
8
9#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12#include "ControlBoardWrapper.h"
14#include "RPCMessagesParser.h"
16#include <iostream>
17
18using namespace yarp::os;
19using namespace yarp::dev;
20using namespace yarp::sig;
21
22
23bool SubDevice::configure(size_t wb, size_t wt, size_t b, size_t t, size_t n, const std::string& key, const std::string& _parentName)
24{
25 parentName = _parentName;
26
27 configuredF = false;
28
29 wbase = wb;
30 wtop = wt;
31 base = b;
32 top = t;
33 axes = n;
34 id = key;
35
36 if (top < base) {
37 yCError(CONTROLBOARD) << "Check configuration file top<base.";
38 return false;
39 }
40
41 if ((top - base + 1) != axes) {
42 yCError(CONTROLBOARD) << "Check configuration file, number of axes and top/base parameters do not match";
43 return false;
44 }
45
46 if (axes <= 0) {
47 yCError(CONTROLBOARD) << "Check number of axes";
48 return false;
49 }
50
55
56 configuredF = true;
57 return true;
58}
59
61{
62 subdevice = nullptr;
63
64 pid = nullptr;
65 pos = nullptr;
66 posDir = nullptr;
67 vel = nullptr;
68 amp = nullptr;
69 iJntEnc = nullptr;
70 iMotEnc = nullptr;
71 lim = nullptr;
72 calib = nullptr;
73 info = nullptr;
74 iTorque = nullptr;
75 iImpedance = nullptr;
76 iMode = nullptr;
77 iTimed = nullptr;
78 iInteract = nullptr;
79 iVar = nullptr;
80 configuredF = false;
81 attachedF = false;
82}
83
84bool SubDevice::attach(yarp::dev::PolyDriver* d, const std::string& k)
85{
86 if (id != k) {
87 yCError(CONTROLBOARD, "Part <%s>: Wrong or unknown device %s. Cannot attach to it.", parentName.c_str(), k.c_str());
88 return false;
89 }
90
91 //configure first
92 if (!configuredF) {
93 yCError(CONTROLBOARD, "Part <%s>: You need to call configure before you can attach any device", parentName.c_str());
94 return false;
95 }
96
97 if (d == nullptr) {
98 yCError(CONTROLBOARD, "Part <%s>: Invalid device (null pointer)", parentName.c_str());
99 return false;
100 }
101
102 subdevice = d;
103
104 if (subdevice->isValid()) {
124 } else {
125 yCError(CONTROLBOARD, "Part <%s>: Invalid device %s (isValid() returned false).", parentName.c_str(), k.c_str());
126 return false;
127 }
128
129 if (!iMode) {
130 yCDebug(CONTROLBOARD, "Part <%s>: iMode not valid interface.", parentName.c_str());
131 }
132
133 if (!iTorque) {
134 yCDebug(CONTROLBOARD, "Part <%s>: iTorque not valid interface.", parentName.c_str());
135 }
136
137 if (!iCurr) {
138 yCDebug(CONTROLBOARD, "Part <%s>: iCurr not valid interface.", parentName.c_str());
139 }
140
141 if (!iPWM) {
142 yCDebug(CONTROLBOARD, "Part <%s>: iPWM not valid interface.", parentName.c_str());
143 }
144
145 if (!iImpedance) {
146 yCDebug(CONTROLBOARD, "Part <%s>: iImpedance not valid interface.", parentName.c_str());
147 }
148
149 if (!iInteract) {
150 yCDebug(CONTROLBOARD, "Part <%s>: iInteractionMode not valid interface.", parentName.c_str());
151 }
152
153 if (!iMotEnc) {
154 yCDebug(CONTROLBOARD, "Part <%s>: iMotorEncoder not valid interface.", parentName.c_str());
155 }
156
157 if (!imotor) {
158 yCDebug(CONTROLBOARD, "Part <%s>: iMotor not valid interface.", parentName.c_str());
159 }
160
161 if (!iVar) {
162 yCDebug(CONTROLBOARD, "Part <%s>: iRemoteVariable not valid interface.", parentName.c_str());
163 }
164
165 if (!info) {
166 yCDebug(CONTROLBOARD, "Part <%s>: iAxisInfo not valid interface.", parentName.c_str());
167 }
168
169 size_t deviceJoints = 0;
170
171 // checking minimum set of intefaces required
172 if (!pos) {
173 yCError(CONTROLBOARD, "Part <%s>: IPositionControl interface was not found in subdevice. Quitting", parentName.c_str());
174 return false;
175 }
176
177 if (!vel) {
178 yCError(CONTROLBOARD, "Part <%s>: IVelocityControl interface was not found in subdevice. Quitting", parentName.c_str());
179 return false;
180 }
181
182 if (!iJntEnc) {
183 yCError(CONTROLBOARD, "Part <%s>: IEncoderTimed interface was not found in subdevice.", parentName.c_str());
184 return false;
185 }
186
187 if (pos != nullptr) {
188 int tmp_axes;
189 if (!pos->getAxes(&tmp_axes)) {
190 yCError(CONTROLBOARD) << "Failed to get axes number for subdevice " << k.c_str();
191 return false;
192 }
193 if (tmp_axes <= 0) {
194 yCError(CONTROLBOARD, "Part <%s>: attached device has an invalid number of joints (%d)", parentName.c_str(), tmp_axes);
195 return false;
196 }
197 deviceJoints = static_cast<size_t>(tmp_axes);
198 } else {
199 int tmp_axes;
200 if (!pos->getAxes(&tmp_axes)) {
201 yCError(CONTROLBOARD, "Part <%s>: failed to get axes number for subdevice %s.", parentName.c_str(), k.c_str());
202 return false;
203 }
204 if (tmp_axes <= 0) {
205 yCError(CONTROLBOARD, "Part <%s>: attached device has an invalid number of joints (%d)", parentName.c_str(), tmp_axes);
206 return false;
207 }
208 deviceJoints = static_cast<size_t>(tmp_axes);
209 }
210
211 if (deviceJoints < axes) {
212 yCError(CONTROLBOARD, "Part <%s>: check device configuration, number of joints of attached device '%zu' less \
213 than the one specified during configuration '%zu' for %s.",
214 parentName.c_str(),
215 deviceJoints,
216 axes,
217 k.c_str());
218 return false;
219 }
220
221 int subdevAxes;
222 if (!pos || !pos->getAxes(&subdevAxes)) {
223 yCError(CONTROLBOARD) << "Device <" << parentName << "> attached to subdevice " << k.c_str() << " but it was not ready yet. \n"
224 << "Please check the device has been correctly created and all required initialization actions has been performed.";
225 return false;
226 }
227
228 totalAxis = deviceJoints;
229 attachedF = true;
230 return true;
231}
const yarp::os::LogComponent & CONTROLBOARD()
float t
yarp::dev::IControlLimits * lim
Definition: SubDevice.h:73
yarp::dev::IImpedanceControl * iImpedance
Definition: SubDevice.h:77
size_t totalAxis
Definition: SubDevice.h:60
yarp::dev::IMotorEncoders * iMotEnc
Definition: SubDevice.h:71
size_t base
Definition: SubDevice.h:55
bool configure(size_t wbase, size_t wtop, size_t base, size_t top, size_t axes, const std::string &id, const std::string &_parentName)
Definition: SubDevice.cpp:23
size_t wtop
Definition: SubDevice.h:58
yarp::dev::IControlMode * iMode
Definition: SubDevice.h:78
yarp::dev::IEncodersTimed * iJntEnc
Definition: SubDevice.h:70
yarp::dev::IVelocityControl * vel
Definition: SubDevice.h:69
yarp::sig::Vector subDev_motor_encoders
Definition: SubDevice.h:89
yarp::sig::Vector subDev_joint_encoders
Definition: SubDevice.h:87
yarp::sig::Vector motorEncodersTimes
Definition: SubDevice.h:90
yarp::dev::PolyDriver * subdevice
Definition: SubDevice.h:66
yarp::dev::IAmplifierControl * amp
Definition: SubDevice.h:72
yarp::dev::IAxisInfo * info
Definition: SubDevice.h:79
yarp::dev::IMotor * imotor
Definition: SubDevice.h:82
yarp::dev::IInteractionMode * iInteract
Definition: SubDevice.h:81
yarp::sig::Vector jointEncodersTimes
Definition: SubDevice.h:88
yarp::dev::IPositionControl * pos
Definition: SubDevice.h:68
yarp::dev::ITorqueControl * iTorque
Definition: SubDevice.h:76
std::string parentName
Definition: SubDevice.h:64
void detach()
Definition: SubDevice.cpp:60
yarp::dev::IPidControl * pid
Definition: SubDevice.h:67
yarp::dev::IPreciselyTimed * iTimed
Definition: SubDevice.h:75
bool attach(yarp::dev::PolyDriver *d, const std::string &id)
Definition: SubDevice.cpp:84
yarp::dev::IRemoteVariables * iVar
Definition: SubDevice.h:83
yarp::dev::IPWMControl * iPWM
Definition: SubDevice.h:84
yarp::dev::IPositionDirect * posDir
Definition: SubDevice.h:80
size_t top
Definition: SubDevice.h:56
size_t axes
Definition: SubDevice.h:59
size_t wbase
Definition: SubDevice.h:57
yarp::dev::IControlCalibration * calib
Definition: SubDevice.h:74
bool configuredF
Definition: SubDevice.h:62
yarp::dev::ICurrentControl * iCurr
Definition: SubDevice.h:85
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
A container for a device driver.
Definition: PolyDriver.h:23
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:196
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:220
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCDebug(component,...)
Definition: LogComponent.h:128
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.