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 
18 using namespace yarp::os;
19 using namespace yarp::dev;
20 using namespace yarp::sig;
21 
22 
23 bool 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 
51  subDev_joint_encoders.resize(axes);
52  jointEncodersTimes.resize(axes);
53  subDev_motor_encoders.resize(axes);
54  motorEncodersTimes.resize(axes);
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 
84 bool 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()) {
105  subdevice->view(pid);
106  subdevice->view(pos);
107  subdevice->view(posDir);
108  subdevice->view(vel);
109  subdevice->view(amp);
110  subdevice->view(lim);
111  subdevice->view(calib);
112  subdevice->view(info);
113  subdevice->view(iTimed);
114  subdevice->view(iTorque);
115  subdevice->view(iImpedance);
116  subdevice->view(iMode);
117  subdevice->view(iJntEnc);
118  subdevice->view(iMotEnc);
119  subdevice->view(iInteract);
120  subdevice->view(imotor);
121  subdevice->view(iVar);
122  subdevice->view(iCurr);
123  subdevice->view(iPWM);
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
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
void detach()
Definition: SubDevice.cpp:60
bool attach(yarp::dev::PolyDriver *d, const std::string &id)
Definition: SubDevice.cpp:84
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
A container for a device driver.
Definition: PolyDriver.h:24
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCDebug(component,...)
Definition: LogComponent.h:109
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22