YARP
Yet Another Robot Platform
SubDevice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include "SubDevice.h"
11 #include "ControlBoardWrapper.h"
13 #include "RPCMessagesParser.h"
15 #include <iostream>
16 #include <yarp/os/Log.h>
17 #include <yarp/os/LogStream.h>
18 
19 using namespace yarp::os;
20 using namespace yarp::dev;
21 using namespace yarp::sig;
22 using namespace std;
23 
24 
26  base(-1),
27  top(-1),
28  axes(0),
29  configuredF(false),
30  parent(nullptr),
31  subdevice(nullptr),
32  pid(nullptr),
33  pos(nullptr),
34  vel(nullptr),
35  iJntEnc(nullptr),
36  iMotEnc(nullptr),
37  amp(nullptr),
38  lim(nullptr),
39  calib(nullptr),
40  iTimed(nullptr),
41  iTorque(nullptr),
42  iImpedance(nullptr),
43  iMode(nullptr),
44  info(nullptr),
45  posDir(nullptr),
46  iInteract(nullptr),
47  imotor(nullptr),
48  iVar(nullptr),
49  iPWM(nullptr),
50  iCurr(nullptr),
51  _subDevVerbose(false),
52  attachedF(false)
53 {}
54 
55 bool SubDevice::configure(int wb, int wt, int b, int t, int n, const std::string &key, ControlBoardWrapper *_parent)
56 {
57  parent = _parent;
58  configuredF=false;
59 
60  wbase = wb;
61  wtop = wt;
62  base=b;
63  top=t;
64  axes=n;
65  id=key;
66 
67  if (top<base)
68  {
69  yCError(CONTROLBOARDWRAPPER) << "Check configuration file top<base.";
70  return false;
71  }
72 
73  if ((top-base+1)!=axes)
74  {
75  yCError(CONTROLBOARDWRAPPER) << "Check configuration file, number of axes and top/base parameters do not match";
76  return false;
77  }
78 
79  if (axes<=0)
80  {
81  yCError(CONTROLBOARDWRAPPER) << "Check number of axes";
82  return false;
83  }
84 
89 
90  configuredF=true;
91  return true;
92 }
93 
95 {
96  subdevice=nullptr;
97 
98  pid=nullptr;
99  pos=nullptr;
100  posDir=nullptr;
101  vel=nullptr;
102  amp = nullptr;
103  iJntEnc=nullptr;
104  iMotEnc=nullptr;
105  lim=nullptr;
106  calib=nullptr;
107  info=nullptr;
108  iTorque=nullptr;
109  iImpedance=nullptr;
110  iMode=nullptr;
111  iTimed=nullptr;
112  iInteract=nullptr;
113  iVar = nullptr;
114  configuredF=false;
115  attachedF=false;
116 }
117 
118 bool SubDevice::attach(yarp::dev::PolyDriver *d, const std::string &k)
119 {
120  std::string parentName;
121  if(parent) {
122  parentName = parent->getId();
123  } else {
124  parentName = "";
125  }
126 
127  if (id!=k) {
128  yCError(CONTROLBOARDWRAPPER, "Part <%s>: Wrong or unknown device %s. Cannot attach to it.", parentName.c_str(), k.c_str());
129  return false;
130  }
131 
132  //configure first
133  if (!configuredF) {
134  yCError(CONTROLBOARDWRAPPER, "Part <%s>: You need to call configure before you can attach any device", parentName.c_str());
135  return false;
136  }
137 
138  if (d==nullptr) {
139  yCError(CONTROLBOARDWRAPPER, "Part <%s>: Invalid device (null pointer)", parentName.c_str());
140  return false;
141  }
142 
143  subdevice=d;
144 
145  if (subdevice->isValid()) {
146  subdevice->view(pid);
147  subdevice->view(pos);
149  subdevice->view(vel);
150  subdevice->view(amp);
151  subdevice->view(lim);
152  subdevice->view(calib);
153  subdevice->view(info);
157  subdevice->view(iMode);
162  subdevice->view(iVar);
163  subdevice->view(iCurr);
164  subdevice->view(iPWM);
165  } else {
166  yCError(CONTROLBOARDWRAPPER, "Part <%s>: Invalid device %s (isValid() returned false).", parentName.c_str(), k.c_str());
167  return false;
168  }
169 
170  if ( ((iMode==nullptr)) && (_subDevVerbose )) {
171  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iMode not valid interface.", parentName.c_str());
172  }
173 
174  if ((iTorque==nullptr) && (_subDevVerbose)) {
175  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iTorque not valid interface.", parentName.c_str());
176  }
177 
178  if ((iCurr == nullptr) && (_subDevVerbose)) {
179  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iCurr not valid interface.", parentName.c_str());
180  }
181 
182  if ((iPWM == nullptr) && (_subDevVerbose)) {
183  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iPWM not valid interface.", parentName.c_str());
184  }
185 
186  if ((iImpedance==nullptr) && (_subDevVerbose)) {
187  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iImpedance not valid interface.", parentName.c_str());
188  }
189 
190  if ((iInteract==nullptr) && (_subDevVerbose)) {
191  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iInteractionMode not valid interface.", parentName.c_str());
192  }
193 
194  if ((iMotEnc==nullptr) && (_subDevVerbose)) {
195  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iMotorEncoder not valid interface.", parentName.c_str());
196  }
197 
198  if ((imotor==nullptr) && (_subDevVerbose)) {
199  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iMotor not valid interface.", parentName.c_str());
200  }
201 
202  if ((iVar == nullptr) && (_subDevVerbose)) {
203  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iRemoteVariable not valid interface.", parentName.c_str());
204  }
205 
206  if ((info == nullptr) && (_subDevVerbose)) {
207  yCWarning(CONTROLBOARDWRAPPER, "Part <%s>: iAxisInfo not valid interface.", parentName.c_str());
208  }
209 
210  int deviceJoints=0;
211 
212  // checking minimum set of intefaces required
213  if( ! (pos) )
214  {
215  yCError(CONTROLBOARDWRAPPER, "Neither IPositionControl nor IPositionControl2 interface was not found in subdevice. Quitting");
216  return false;
217  }
218 
219  if( ! (vel) )
220  {
221  yCError(CONTROLBOARDWRAPPER, "Neither IVelocityControl nor IVelocityControl2 interface was not found in subdevice. Quitting");
222  return false;
223  }
224 
225  if(!iJntEnc)
226  {
227  yCError(CONTROLBOARDWRAPPER, "Part <%s>: IEncoderTimed interface was not found in subdevice.", parentName.c_str());
228  return false;
229  }
230 
231  if (pos!=nullptr)
232  {
233  if (!pos->getAxes(&deviceJoints))
234  {
235  yCError(CONTROLBOARDWRAPPER) << "Failed to get axes number for subdevice " << k.c_str();
236  return false;
237  }
238  if(deviceJoints <= 0)
239  {
240  yCError(CONTROLBOARDWRAPPER, "Part <%s>: attached device has an invalid number of joints (%d)", parentName.c_str(), deviceJoints);
241  return false;
242  }
243  }
244  else
245  {
246  if (!pos->getAxes(&deviceJoints))
247  {
248  yCError(CONTROLBOARDWRAPPER, "Part <%s>: failed to get axes number for subdevice %s.", parentName.c_str(), k.c_str());
249  return false;
250  }
251  if(deviceJoints <=0)
252  {
253  yCError(CONTROLBOARDWRAPPER, "Part <%s>: attached device has an invalid number of joints (%d)", parentName.c_str(), deviceJoints);
254  return false;
255  }
256  }
257 
258  if (deviceJoints<axes)
259  {
260  yCError(CONTROLBOARDWRAPPER, "Part <%s>: check device configuration, number of joints of attached device '%d' less \
261  than the one specified during configuration '%d' for %s.", parentName.c_str(), deviceJoints, axes, k.c_str());
262  return false;
263  }
264 
265  int subdevAxes;
266  if(!pos || !pos->getAxes(&subdevAxes))
267  {
268  yCError(CONTROLBOARDWRAPPER) << "Device <" << parentName << "> attached to subdevice " << k.c_str() << " but it was not ready yet. \n" \
269  << "Please check the device has been correctly created and all required initialization actions has been performed.";
270  return false;
271  }
272 
273  totalAxis = deviceJoints;
274  attachedF=true;
275  return true;
276 }
const yarp::os::LogComponent & CONTROLBOARDWRAPPER()
float t
yarp::dev::IControlLimits * lim
Definition: SubDevice.h:77
int axes
Definition: SubDevice.h:63
yarp::dev::IImpedanceControl * iImpedance
Definition: SubDevice.h:81
bool configure(int wbase, int wtop, int base, int top, int axes, const std::string &id, ControlBoardWrapper *_parent)
Definition: SubDevice.cpp:55
yarp::dev::IMotorEncoders * iMotEnc
Definition: SubDevice.h:75
yarp::dev::IControlMode * iMode
Definition: SubDevice.h:82
yarp::dev::IEncodersTimed * iJntEnc
Definition: SubDevice.h:74
yarp::dev::IVelocityControl * vel
Definition: SubDevice.h:73
yarp::sig::Vector subDev_motor_encoders
Definition: SubDevice.h:93
yarp::sig::Vector subDev_joint_encoders
Definition: SubDevice.h:91
yarp::sig::Vector motorEncodersTimes
Definition: SubDevice.h:94
yarp::dev::PolyDriver * subdevice
Definition: SubDevice.h:70
yarp::dev::IAmplifierControl * amp
Definition: SubDevice.h:76
yarp::dev::IAxisInfo * info
Definition: SubDevice.h:83
yarp::dev::IMotor * imotor
Definition: SubDevice.h:86
yarp::dev::IInteractionMode * iInteract
Definition: SubDevice.h:85
yarp::sig::Vector jointEncodersTimes
Definition: SubDevice.h:92
yarp::dev::IPositionControl * pos
Definition: SubDevice.h:72
yarp::dev::ITorqueControl * iTorque
Definition: SubDevice.h:80
void detach()
Definition: SubDevice.cpp:94
yarp::dev::IPidControl * pid
Definition: SubDevice.h:71
yarp::dev::IPreciselyTimed * iTimed
Definition: SubDevice.h:79
int base
Definition: SubDevice.h:59
bool attach(yarp::dev::PolyDriver *d, const std::string &id)
Definition: SubDevice.cpp:118
ControlBoardWrapper * parent
Definition: SubDevice.h:68
int top
Definition: SubDevice.h:60
int wbase
Definition: SubDevice.h:61
yarp::dev::IRemoteVariables * iVar
Definition: SubDevice.h:87
yarp::dev::IPWMControl * iPWM
Definition: SubDevice.h:88
yarp::dev::IPositionDirect * posDir
Definition: SubDevice.h:84
int totalAxis
Definition: SubDevice.h:64
int wtop
Definition: SubDevice.h:62
yarp::dev::IControlCalibration * calib
Definition: SubDevice.h:78
bool configuredF
Definition: SubDevice.h:66
yarp::dev::ICurrentControl * iCurr
Definition: SubDevice.h:89
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
A container for a device driver.
Definition: PolyDriver.h:27
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:199
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
#define yCError(component,...)
Definition: LogComponent.h:157
#define yCWarning(component,...)
Definition: LogComponent.h:146
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25