YARP
Yet Another Robot Platform
ControlBoardWrapperTorqueControl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
12 
13 
15 {
16  auto* references = new double[device.maxNumOfJointsInDevices];
17  bool ret = true;
18  for (size_t d = 0; d < device.subdevices.size(); d++) {
20  if (!p) {
21  ret = false;
22  break;
23  }
24 
25  if ((p->iTorque) && (ret = p->iTorque->getRefTorques(references))) {
26  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
27  refs[juser] = references[jdevice];
28  }
29  } else {
30  printError("getRefTorques", p->id, ret);
31  ret = false;
32  break;
33  }
34  }
35 
36  delete[] references;
37  return ret;
38 }
39 
41 {
42 
43  size_t off;
44  try {
45  off = device.lut.at(j).offset;
46  } catch (...) {
47  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
48  return false;
49  }
50  size_t subIndex = device.lut[j].deviceEntry;
51 
52  SubDevice* p = device.getSubdevice(subIndex);
53  if (!p) {
54  return false;
55  }
56 
57  if (p->iTorque) {
58  return p->iTorque->getRefTorque(static_cast<int>(off + p->base), t);
59  }
60  return false;
61 }
62 
64 {
65  bool ret = true;
66 
67  for (size_t l = 0; l < controlledJoints; l++) {
68  int off = device.lut[l].offset;
69  size_t subIndex = device.lut[l].deviceEntry;
70 
71  SubDevice* p = device.getSubdevice(subIndex);
72  if (!p) {
73  return false;
74  }
75 
76  if (p->iTorque) {
77  ret = ret && p->iTorque->setRefTorque(static_cast<int>(off + p->base), t[l]);
78  } else {
79  ret = false;
80  }
81  }
82  return ret;
83 }
84 
86 {
87  size_t off;
88  try {
89  off = device.lut.at(j).offset;
90  } catch (...) {
91  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
92  return false;
93  }
94  size_t subIndex = device.lut[j].deviceEntry;
95 
96  SubDevice* p = device.getSubdevice(subIndex);
97  if (!p) {
98  return false;
99  }
100 
101  if (p->iTorque) {
102  return p->iTorque->setRefTorque(static_cast<int>(off + p->base), t);
103  }
104  return false;
105 }
106 
107 bool ControlBoardWrapperTorqueControl::setRefTorques(const int n_joints, const int* joints, const double* t)
108 {
109  bool ret = true;
110 
111  rpcDataMutex.lock();
112  //Reset subdev_jointsVectorLen vector
113  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
114 
115  // Create a map of joints for each subDevice
116  size_t subIndex = 0;
117  for (int j = 0; j < n_joints; j++) {
118  subIndex = device.lut[joints[j]].deviceEntry;
119  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
120  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
121  rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = t[j];
122  rpcData.subdev_jointsVectorLen[subIndex]++;
123  }
124 
125  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
126  if (rpcData.subdevices_p[subIndex]->iTorque) {
128  } else {
129  ret = false;
130  }
131  }
132  rpcDataMutex.unlock();
133  return ret;
134 }
135 
137 {
138  size_t off;
139  try {
140  off = device.lut.at(j).offset;
141  } catch (...) {
142  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
143  return false;
144  }
145  size_t subIndex = device.lut[j].deviceEntry;
146 
147  SubDevice* p = device.getSubdevice(subIndex);
148  if (!p) {
149  return false;
150  }
151 
152  if (p->iTorque) {
153  return p->iTorque->getMotorTorqueParams(static_cast<int>(off + p->base), params);
154  }
155  return false;
156 }
157 
159 {
160  size_t off;
161  try {
162  off = device.lut.at(j).offset;
163  } catch (...) {
164  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
165  return false;
166  }
167  size_t subIndex = device.lut[j].deviceEntry;
168 
169  SubDevice* p = device.getSubdevice(subIndex);
170  if (!p) {
171  return false;
172  }
173 
174  if (p->iTorque) {
175  return p->iTorque->setMotorTorqueParams(static_cast<int>(off + p->base), params);
176  }
177  return false;
178 }
179 
181 {
182  size_t off;
183  try {
184  off = device.lut.at(j).offset;
185  } catch (...) {
186  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
187  return false;
188  }
189  size_t subIndex = device.lut[j].deviceEntry;
190 
191  SubDevice* p = device.getSubdevice(subIndex);
192  if (!p) {
193  return false;
194  }
195 
196  if (p->iTorque) {
197  return p->iTorque->getTorque(static_cast<int>(off + p->base), t);
198  }
199 
200  return false;
201 }
202 
204 {
205  auto* trqs = new double[device.maxNumOfJointsInDevices];
206  bool ret = true;
207  for (size_t d = 0; d < device.subdevices.size(); d++) {
208  SubDevice* p = device.getSubdevice(d);
209  if (!p) {
210  ret = false;
211  break;
212  }
213 
214  if ((p->iTorque) && (ret = p->iTorque->getTorques(trqs))) {
215  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
216  t[juser] = trqs[jdevice];
217  }
218  } else {
219  printError("getTorques", p->id, ret);
220  ret = false;
221  break;
222  }
223  }
224 
225  delete[] trqs;
226  return ret;
227 }
228 
229 bool ControlBoardWrapperTorqueControl::getTorqueRange(int j, double* min, double* max)
230 {
231  size_t off;
232  try {
233  off = device.lut.at(j).offset;
234  } catch (...) {
235  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
236  return false;
237  }
238  size_t subIndex = device.lut[j].deviceEntry;
239 
240  SubDevice* p = device.getSubdevice(subIndex);
241  if (!p) {
242  return false;
243  }
244 
245  if (p->iTorque) {
246  return p->iTorque->getTorqueRange(static_cast<int>(off + p->base), min, max);
247  }
248 
249  return false;
250 }
251 
253 {
254  auto* t_min = new double[device.maxNumOfJointsInDevices];
255  auto* t_max = new double[device.maxNumOfJointsInDevices];
256  bool ret = true;
257  for (size_t d = 0; d < device.subdevices.size(); d++) {
258  SubDevice* p = device.getSubdevice(d);
259  if (!p) {
260  ret = false;
261  break;
262  }
263 
264  if ((p->iTorque) && (ret = p->iTorque->getTorqueRanges(t_min, t_max))) {
265  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
266  min[juser] = t_min[jdevice];
267  max[juser] = t_max[jdevice];
268  }
269  } else {
270  printError("getTorqueRanges", p->id, ret);
271  ret = false;
272  break;
273  }
274  }
275 
276  delete[] t_min;
277  delete[] t_max;
278  return ret;
279 }
ControlBoardWrapperCommon::rpcData
MultiJointData rpcData
Definition: ControlBoardWrapperCommon.h:28
ControlBoardWrapperLogComponent.h
MultiJointData::values
double ** values
Definition: MultiJointData.h:23
t
float t
Definition: FfmpegWriter.cpp:74
MultiJointData::jointNumbers
int ** jointNumbers
Definition: MultiJointData.h:21
yarp::dev::MotorTorqueParameters
Definition: ITorqueControl.h:24
ControlBoardWrapperTorqueControl::getTorqueRange
bool getTorqueRange(int j, double *min, double *max) override
Get the full scale of the torque sensor of a given joint.
Definition: ControlBoardWrapperTorqueControl.cpp:229
ControlBoardWrapperTorqueControl::setRefTorques
bool setRefTorques(const double *t) override
Set the reference value of the torque for all joints.
Definition: ControlBoardWrapperTorqueControl.cpp:63
yarp::dev::ITorqueControl::getTorque
virtual bool getTorque(int j, double *t)=0
Get the value of the torque on a given joint (this is the feedback if you have a torque sensor).
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
ControlBoardWrapperTorqueControl::getRefTorques
bool getRefTorques(double *refs) override
Get the reference value of the torque for all joints.
Definition: ControlBoardWrapperTorqueControl.cpp:14
MultiJointData::subdev_jointsVectorLen
int * subdev_jointsVectorLen
Definition: MultiJointData.h:20
ControlBoardWrapperTorqueControl::getMotorTorqueParams
bool getMotorTorqueParams(int j, yarp::dev::MotorTorqueParameters *params) override
Get a subset of motor parameters (bemf, ktau etc) useful for torque control.
Definition: ControlBoardWrapperTorqueControl.cpp:136
yarp::dev::ITorqueControl::getTorqueRanges
virtual bool getTorqueRanges(double *min, double *max)=0
Get the full scale of the torque sensors of all joints.
yarp::dev::ITorqueControl::getRefTorques
virtual bool getRefTorques(double *t)=0
Get the reference value of the torque for all joints.
MultiJointData::subdevices_p
SubDevice ** subdevices_p
Definition: MultiJointData.h:24
CONTROLBOARDWRAPPER
const yarp::os::LogComponent & CONTROLBOARDWRAPPER()
Definition: ControlBoardWrapperLogComponent.cpp:11
yarp::dev::ITorqueControl::setRefTorques
virtual bool setRefTorques(const double *t)=0
Set the reference value of the torque for all joints.
ControlBoardWrapperCommon::partName
std::string partName
Definition: ControlBoardWrapperCommon.h:24
ControlBoardWrapperTorqueControl::getRefTorque
bool getRefTorque(int j, double *t) override
Get the reference value of the torque for a given joint.
Definition: ControlBoardWrapperTorqueControl.cpp:40
ControlBoardWrapperCommon::controlledJoints
size_t controlledJoints
Definition: ControlBoardWrapperCommon.h:23
SubDevice::id
std::string id
Definition: SubDevice.h:57
WrappedDevice::getSubdevice
SubDevice * getSubdevice(size_t i)
Definition: SubDevice.h:129
yarp::dev::ITorqueControl::setMotorTorqueParams
virtual bool setMotorTorqueParams(int j, const yarp::dev::MotorTorqueParameters params)
Set a subset of motor parameters (bemf, ktau etc) useful for torque control.
Definition: ITorqueControl.h:100
WrappedDevice::subdevices
SubDeviceVector subdevices
Definition: SubDevice.h:125
yarp::dev::ITorqueControl::getTorques
virtual bool getTorques(double *t)=0
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
ControlBoardWrapperTorqueControl.h
ControlBoardWrapperTorqueControl::getTorques
bool getTorques(double *t) override
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
Definition: ControlBoardWrapperTorqueControl.cpp:203
ControlBoardWrapperCommon::device
WrappedDevice device
Definition: ControlBoardWrapperCommon.h:22
SubDevice::iTorque
yarp::dev::ITorqueControl * iTorque
Definition: SubDevice.h:79
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
ControlBoardWrapperTorqueControl::setMotorTorqueParams
bool setMotorTorqueParams(int j, const yarp::dev::MotorTorqueParameters params) override
Set a subset of motor parameters (bemf, ktau etc) useful for torque control.
Definition: ControlBoardWrapperTorqueControl.cpp:158
ControlBoardWrapperTorqueControl::getTorqueRanges
bool getTorqueRanges(double *min, double *max) override
Get the full scale of the torque sensors of all joints.
Definition: ControlBoardWrapperTorqueControl.cpp:252
SubDevice::base
size_t base
Definition: SubDevice.h:58
WrappedDevice::lut
std::vector< DevicesLutEntry > lut
Definition: SubDevice.h:126
ControlBoardWrapperTorqueControl::getTorque
bool getTorque(int j, double *t) override
Get the value of the torque on a given joint (this is the feedback if you have a torque sensor).
Definition: ControlBoardWrapperTorqueControl.cpp:180
MultiJointData::deviceNum
size_t deviceNum
Definition: MultiJointData.h:17
SubDevice::wbase
size_t wbase
Definition: SubDevice.h:60
yarp::dev::ITorqueControl::setRefTorque
virtual bool setRefTorque(int j, double t)=0
Set the reference value of the torque for a given joint.
SubDevice
Definition: SubDevice.h:55
ControlBoardWrapperCommon::rpcDataMutex
std::mutex rpcDataMutex
Definition: ControlBoardWrapperCommon.h:27
WrappedDevice::maxNumOfJointsInDevices
size_t maxNumOfJointsInDevices
Definition: SubDevice.h:127
ControlBoardWrapperTorqueControl::setRefTorque
bool setRefTorque(int j, double t) override
Set the reference value of the torque for a given joint.
Definition: ControlBoardWrapperTorqueControl.cpp:85
yarp::dev::ITorqueControl::getRefTorque
virtual bool getRefTorque(int j, double *t)=0
Get the reference value of the torque for a given joint.
yarp::dev::ITorqueControl::getMotorTorqueParams
virtual bool getMotorTorqueParams(int j, yarp::dev::MotorTorqueParameters *params)
Get a subset of motor parameters (bemf, ktau etc) useful for torque control.
Definition: ITorqueControl.h:93
ControlBoardWrapperCommon::printError
void printError(const std::string &func_name, const std::string &info, bool result)
Definition: ControlBoardWrapperCommon.h:72
yarp::dev::ITorqueControl::getTorqueRange
virtual bool getTorqueRange(int j, double *min, double *max)=0
Get the full scale of the torque sensor of a given joint.