YARP
Yet Another Robot Platform
ControlBoardWrapperCurrentControl.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
11bool ControlBoardWrapperCurrentControl::getCurrentRange(int j, double* min, double* max)
12{
13 size_t off;
14 try {
15 off = device.lut.at(j).offset;
16 } catch (...) {
17 yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
18 return false;
19 }
20 size_t subIndex = device.lut[j].deviceEntry;
21
22 SubDevice* p = device.getSubdevice(subIndex);
23 if (!p) {
24 return false;
25 }
26
27 if (p->iCurr) {
28 return p->iCurr->getCurrentRange(static_cast<int>(off + p->base), min, max);
29 }
30
31 return false;
32}
33
35{
36 auto* c_min = new double[device.maxNumOfJointsInDevices];
37 auto* c_max = new double[device.maxNumOfJointsInDevices];
38 bool ret = true;
39 for (size_t d = 0; d < device.subdevices.size(); d++) {
41 if (!p) {
42 ret = false;
43 break;
44 }
45
46 if ((p->iCurr) && (ret = p->iCurr->getCurrentRanges(c_min, c_max))) {
47 for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
48 min[juser] = c_min[jdevice];
49 max[juser] = c_max[jdevice];
50 }
51 } else {
52 printError("getCurrentRanges", p->id, ret);
53 ret = false;
54 break;
55 }
56 }
57
58 delete[] c_min;
59 delete[] c_max;
60 return ret;
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->iCurr) {
77 ret = ret && p->iCurr->setRefCurrent(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(CONTROLBOARD, "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->iCurr) {
102 return p->iCurr->setRefCurrent(static_cast<int>(off + p->base), t);
103 }
104 return false;
105}
106
107bool ControlBoardWrapperCurrentControl::setRefCurrents(const int n_joint, 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_joint; j++) {
118 subIndex = device.lut[joints[j]].deviceEntry;
120 static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
121 rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = t[j];
123 }
124
125 for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
126 if (rpcData.subdevices_p[subIndex]->iCurr) {
128 } else {
129 ret = false;
130 }
131 }
132 rpcDataMutex.unlock();
133 return ret;
134}
135
137{
138 auto* references = new double[device.maxNumOfJointsInDevices];
139 bool ret = true;
140 for (size_t d = 0; d < device.subdevices.size(); d++) {
142 if (!p) {
143 ret = false;
144 break;
145 }
146
147 if ((p->iCurr) && (ret = p->iCurr->getRefCurrents(references))) {
148 for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
149 t[juser] = references[jdevice];
150 }
151 } else {
152 printError("getRefCurrents", p->id, ret);
153 ret = false;
154 break;
155 }
156 }
157
158 delete[] references;
159 return ret;
160}
161
163{
164 size_t off;
165 try {
166 off = device.lut.at(j).offset;
167 } catch (...) {
168 yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
169 return false;
170 }
171 size_t subIndex = device.lut[j].deviceEntry;
172
173 SubDevice* p = device.getSubdevice(subIndex);
174 if (!p) {
175 return false;
176 }
177
178 if (p->iCurr) {
179 return p->iCurr->getRefCurrent(static_cast<int>(off + p->base), t);
180 }
181
182 return false;
183}
const yarp::os::LogComponent & CONTROLBOARD()
float t
bool ret
void printError(const std::string &func_name, const std::string &info, bool result)
bool getCurrentRanges(double *min, double *max) override
Get the full scale of the current measurements for all motors motor (e.g.
bool setRefCurrent(int j, double t) override
Set the reference value of the current for a single motor.
bool getRefCurrent(int j, double *t) override
Get the reference value of the current for a single motor.
bool getCurrentRange(int j, double *min, double *max) override
Get the full scale of the current measurement for a given motor (e.g.
bool setRefCurrents(const double *t) override
Set the reference value of the currents for all motors.
bool getRefCurrents(double *t) override
Get the reference value of the currents for all motors.
int * subdev_jointsVectorLen
SubDevice ** subdevices_p
double ** values
size_t base
Definition: SubDevice.h:55
std::string id
Definition: SubDevice.h:54
size_t wbase
Definition: SubDevice.h:57
yarp::dev::ICurrentControl * iCurr
Definition: SubDevice.h:85
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 setRefCurrent(int m, double curr)=0
Set the reference value of the current for a single motor.
virtual bool getRefCurrents(double *currs)=0
Get the reference value of the currents for all motors.
virtual bool setRefCurrents(const double *currs)=0
Set the reference value of the currents for all motors.
virtual bool getCurrentRange(int m, double *min, double *max)=0
Get the full scale of the current measurement for a given motor (e.g.
virtual bool getRefCurrent(int m, double *curr)=0
Get the reference value of the current for a single motor.
virtual bool getCurrentRanges(double *min, double *max)=0
Get the full scale of the current measurements for all motors motor (e.g.
#define yCError(component,...)
Definition: LogComponent.h:213