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