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 
12 #include <yarp/os/Log.h>
13 #include <yarp/os/LogStream.h>
14 
15 #include "ControlBoardWrapper.h"
17 #include "RPCMessagesParser.h"
19 #include <iostream>
20 
21 using namespace yarp::os;
22 using namespace yarp::dev;
23 using namespace yarp::sig;
24 using namespace std;
25 
26 
27 bool SubDevice::configure(size_t wb, size_t wt, size_t b, size_t t, size_t n, const std::string& key, ControlBoardWrapper* _parent)
28 {
29  parent = _parent;
30  configuredF = false;
31 
32  wbase = wb;
33  wtop = wt;
34  base = b;
35  top = t;
36  axes = n;
37  id = key;
38 
39  if (top < base) {
40  yCError(CONTROLBOARDWRAPPER) << "Check configuration file top<base.";
41  return false;
42  }
43 
44  if ((top - base + 1) != axes) {
45  yCError(CONTROLBOARDWRAPPER) << "Check configuration file, number of axes and top/base parameters do not match";
46  return false;
47  }
48 
49  if (axes <= 0) {
50  yCError(CONTROLBOARDWRAPPER) << "Check number of axes";
51  return false;
52  }
53 
54  subDev_joint_encoders.resize(axes);
55  jointEncodersTimes.resize(axes);
56  subDev_motor_encoders.resize(axes);
57  motorEncodersTimes.resize(axes);
58 
59  configuredF = true;
60  return true;
61 }
62 
64 {
65  subdevice = nullptr;
66 
67  pid = nullptr;
68  pos = nullptr;
69  posDir = nullptr;
70  vel = nullptr;
71  amp = nullptr;
72  iJntEnc = nullptr;
73  iMotEnc = nullptr;
74  lim = nullptr;
75  calib = nullptr;
76  info = nullptr;
77  iTorque = nullptr;
78  iImpedance = nullptr;
79  iMode = nullptr;
80  iTimed = nullptr;
81  iInteract = nullptr;
82  iVar = nullptr;
83  configuredF = false;
84  attachedF = false;
85 }
86 
87 bool SubDevice::attach(yarp::dev::PolyDriver* d, const std::string& k)
88 {
89  std::string parentName;
90  if (parent) {
91  parentName = parent->getId();
92  } else {
93  parentName = "";
94  }
95 
96  if (id != k) {
97  yCError(CONTROLBOARDWRAPPER, "Part <%s>: Wrong or unknown device %s. Cannot attach to it.", parentName.c_str(), k.c_str());
98  return false;
99  }
100 
101  //configure first
102  if (!configuredF) {
103  yCError(CONTROLBOARDWRAPPER, "Part <%s>: You need to call configure before you can attach any device", parentName.c_str());
104  return false;
105  }
106 
107  if (d == nullptr) {
108  yCError(CONTROLBOARDWRAPPER, "Part <%s>: Invalid device (null pointer)", parentName.c_str());
109  return false;
110  }
111 
112  subdevice = d;
113 
114  if (subdevice->isValid()) {
115  subdevice->view(pid);
116  subdevice->view(pos);
117  subdevice->view(posDir);
118  subdevice->view(vel);
119  subdevice->view(amp);
120  subdevice->view(lim);
121  subdevice->view(calib);
122  subdevice->view(info);
123  subdevice->view(iTimed);
124  subdevice->view(iTorque);
125  subdevice->view(iImpedance);
126  subdevice->view(iMode);
127  subdevice->view(iJntEnc);
128  subdevice->view(iMotEnc);
129  subdevice->view(iInteract);
130  subdevice->view(imotor);
131  subdevice->view(iVar);
132  subdevice->view(iCurr);
133  subdevice->view(iPWM);
134  } else {
135  yCError(CONTROLBOARDWRAPPER, "Part <%s>: Invalid device %s (isValid() returned false).", parentName.c_str(), k.c_str());
136  return false;
137  }
138 
139  if (!iMode) {
140  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iMode not valid interface.", parentName.c_str());
141  }
142 
143  if (!iTorque) {
144  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iTorque not valid interface.", parentName.c_str());
145  }
146 
147  if (!iCurr) {
148  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iCurr not valid interface.", parentName.c_str());
149  }
150 
151  if (!iPWM) {
152  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iPWM not valid interface.", parentName.c_str());
153  }
154 
155  if (!iImpedance) {
156  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iImpedance not valid interface.", parentName.c_str());
157  }
158 
159  if (!iInteract) {
160  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iInteractionMode not valid interface.", parentName.c_str());
161  }
162 
163  if (!iMotEnc) {
164  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iMotorEncoder not valid interface.", parentName.c_str());
165  }
166 
167  if (!imotor) {
168  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iMotor not valid interface.", parentName.c_str());
169  }
170 
171  if (!iVar) {
172  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iRemoteVariable not valid interface.", parentName.c_str());
173  }
174 
175  if (!info) {
176  yCDebug(CONTROLBOARDWRAPPER, "Part <%s>: iAxisInfo not valid interface.", parentName.c_str());
177  }
178 
179  size_t deviceJoints = 0;
180 
181  // checking minimum set of intefaces required
182  if (!pos) {
183  yCError(CONTROLBOARDWRAPPER, "Part <%s>: IPositionControl interface was not found in subdevice. Quitting", parentName.c_str());
184  return false;
185  }
186 
187  if (!vel) {
188  yCError(CONTROLBOARDWRAPPER, "Part <%s>: IVelocityControl nor IVelocityControl2 interface was not found in subdevice. Quitting", parentName.c_str());
189  return false;
190  }
191 
192  if (!iJntEnc) {
193  yCError(CONTROLBOARDWRAPPER, "Part <%s>: IEncoderTimed interface was not found in subdevice.", parentName.c_str());
194  return false;
195  }
196 
197  if (pos != nullptr) {
198  int tmp_axes;
199  if (!pos->getAxes(&tmp_axes)) {
200  yCError(CONTROLBOARDWRAPPER) << "Failed to get axes number for subdevice " << k.c_str();
201  return false;
202  }
203  if (tmp_axes <= 0) {
204  yCError(CONTROLBOARDWRAPPER, "Part <%s>: attached device has an invalid number of joints (%d)", parentName.c_str(), tmp_axes);
205  return false;
206  }
207  deviceJoints = static_cast<size_t>(tmp_axes);
208  } else {
209  int tmp_axes;
210  if (!pos->getAxes(&tmp_axes)) {
211  yCError(CONTROLBOARDWRAPPER, "Part <%s>: failed to get axes number for subdevice %s.", parentName.c_str(), k.c_str());
212  return false;
213  }
214  if (tmp_axes <= 0) {
215  yCError(CONTROLBOARDWRAPPER, "Part <%s>: attached device has an invalid number of joints (%d)", parentName.c_str(), tmp_axes);
216  return false;
217  }
218  deviceJoints = static_cast<size_t>(tmp_axes);
219  }
220 
221  if (deviceJoints < axes) {
222  yCError(CONTROLBOARDWRAPPER, "Part <%s>: check device configuration, number of joints of attached device '%zu' less \
223  than the one specified during configuration '%zu' for %s.",
224  parentName.c_str(),
225  deviceJoints,
226  axes,
227  k.c_str());
228  return false;
229  }
230 
231  int subdevAxes;
232  if (!pos || !pos->getAxes(&subdevAxes)) {
233  yCError(CONTROLBOARDWRAPPER) << "Device <" << parentName << "> attached to subdevice " << k.c_str() << " but it was not ready yet. \n"
234  << "Please check the device has been correctly created and all required initialization actions has been performed.";
235  return false;
236  }
237 
238  totalAxis = deviceJoints;
239  attachedF = true;
240  return true;
241 }
LogStream.h
SubDevice.h
ControlBoardWrapperLogComponent.h
yarp::sig
Signal processing.
Definition: Image.h:25
t
float t
Definition: FfmpegWriter.cpp:74
ControlBoardWrapper.h
yarp::dev::DeviceDriver::view
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
Log.h
ControlBoardWrapper
controlboardwrapper2: An updated version of the controlBoard network wrapper.
Definition: ControlBoardWrapper.h:204
CONTROLBOARDWRAPPER
const yarp::os::LogComponent & CONTROLBOARDWRAPPER()
Definition: ControlBoardWrapperLogComponent.cpp:11
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
SubDevice::attach
bool attach(yarp::dev::PolyDriver *d, const std::string &id)
Definition: SubDevice.cpp:87
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
SubDevice::detach
void detach()
Definition: SubDevice.cpp:63
RPCMessagesParser.h
SubDevice::configure
bool configure(size_t wbase, size_t wtop, size_t base, size_t top, size_t axes, const std::string &id, ControlBoardWrapper *_parent)
Definition: SubDevice.cpp:27
StreamingMessagesParser.h