YARP
Yet Another Robot Platform
ControlBoardWrapperCurrentControl.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 
14 bool ControlBoardWrapperCurrentControl::getCurrentRange(int j, double* min, double* max)
15 {
16  size_t off;
17  try {
18  off = device.lut.at(j).offset;
19  } catch (...) {
20  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
21  return false;
22  }
23  size_t subIndex = device.lut[j].deviceEntry;
24 
25  SubDevice* p = device.getSubdevice(subIndex);
26  if (!p) {
27  return false;
28  }
29 
30  if (p->iCurr) {
31  return p->iCurr->getCurrentRange(static_cast<int>(off + p->base), min, max);
32  }
33 
34  return false;
35 }
36 
38 {
39  auto* c_min = new double[device.maxNumOfJointsInDevices];
40  auto* c_max = new double[device.maxNumOfJointsInDevices];
41  bool ret = true;
42  for (size_t d = 0; d < device.subdevices.size(); d++) {
44  if (!p) {
45  ret = false;
46  break;
47  }
48 
49  if ((p->iCurr) && (ret = p->iCurr->getCurrentRanges(c_min, c_max))) {
50  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
51  min[juser] = c_min[jdevice];
52  max[juser] = c_max[jdevice];
53  }
54  } else {
55  printError("getCurrentRanges", p->id, ret);
56  ret = false;
57  break;
58  }
59  }
60 
61  delete[] c_min;
62  delete[] c_max;
63  return ret;
64 }
65 
67 {
68  bool ret = true;
69 
70  for (size_t l = 0; l < controlledJoints; l++) {
71  int off = device.lut[l].offset;
72  size_t subIndex = device.lut[l].deviceEntry;
73 
74  SubDevice* p = device.getSubdevice(subIndex);
75  if (!p) {
76  return false;
77  }
78 
79  if (p->iCurr) {
80  ret = ret && p->iCurr->setRefCurrent(static_cast<int>(off + p->base), t[l]);
81  } else {
82  ret = false;
83  }
84  }
85  return ret;
86 }
87 
89 {
90  size_t off;
91  try {
92  off = device.lut.at(j).offset;
93  } catch (...) {
94  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
95  return false;
96  }
97  size_t subIndex = device.lut[j].deviceEntry;
98 
99  SubDevice* p = device.getSubdevice(subIndex);
100  if (!p) {
101  return false;
102  }
103 
104  if (p->iCurr) {
105  return p->iCurr->setRefCurrent(static_cast<int>(off + p->base), t);
106  }
107  return false;
108 }
109 
110 bool ControlBoardWrapperCurrentControl::setRefCurrents(const int n_joint, const int* joints, const double* t)
111 {
112  bool ret = true;
113 
114  rpcDataMutex.lock();
115  //Reset subdev_jointsVectorLen vector
116  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
117 
118  // Create a map of joints for each subDevice
119  size_t subIndex = 0;
120  for (int j = 0; j < n_joint; j++) {
121  subIndex = device.lut[joints[j]].deviceEntry;
122  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
123  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
124  rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = t[j];
125  rpcData.subdev_jointsVectorLen[subIndex]++;
126  }
127 
128  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
129  if (rpcData.subdevices_p[subIndex]->iCurr) {
131  } else {
132  ret = false;
133  }
134  }
135  rpcDataMutex.unlock();
136  return ret;
137 }
138 
140 {
141  auto* references = new double[device.maxNumOfJointsInDevices];
142  bool ret = true;
143  for (size_t d = 0; d < device.subdevices.size(); d++) {
144  SubDevice* p = device.getSubdevice(d);
145  if (!p) {
146  ret = false;
147  break;
148  }
149 
150  if ((p->iCurr) && (ret = p->iCurr->getRefCurrents(references))) {
151  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
152  t[juser] = references[jdevice];
153  }
154  } else {
155  printError("getRefCurrents", p->id, ret);
156  ret = false;
157  break;
158  }
159  }
160 
161  delete[] references;
162  return ret;
163 }
164 
166 {
167  size_t off;
168  try {
169  off = device.lut.at(j).offset;
170  } catch (...) {
171  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
172  return false;
173  }
174  size_t subIndex = device.lut[j].deviceEntry;
175 
176  SubDevice* p = device.getSubdevice(subIndex);
177  if (!p) {
178  return false;
179  }
180 
181  if (p->iCurr) {
182  return p->iCurr->getRefCurrent(static_cast<int>(off + p->base), t);
183  }
184 
185  return false;
186 }
ControlBoardWrapperCommon::rpcData
MultiJointData rpcData
Definition: ControlBoardWrapperCommon.h:28
yarp::dev::ICurrentControl::getRefCurrent
virtual bool getRefCurrent(int m, double *curr)=0
Get the reference value of the current for a single motor.
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
ControlBoardWrapperCurrentControl::setRefCurrents
bool setRefCurrents(const double *t) override
Set the reference value of the currents for all motors.
Definition: ControlBoardWrapperCurrentControl.cpp:66
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::dev::ICurrentControl::setRefCurrents
virtual bool setRefCurrents(const double *currs)=0
Set the reference value of the currents for all motors.
MultiJointData::subdev_jointsVectorLen
int * subdev_jointsVectorLen
Definition: MultiJointData.h:20
ControlBoardWrapperCurrentControl::setRefCurrent
bool setRefCurrent(int j, double t) override
Set the reference value of the current for a single motor.
Definition: ControlBoardWrapperCurrentControl.cpp:88
SubDevice::iCurr
yarp::dev::ICurrentControl * iCurr
Definition: SubDevice.h:88
MultiJointData::subdevices_p
SubDevice ** subdevices_p
Definition: MultiJointData.h:24
CONTROLBOARDWRAPPER
const yarp::os::LogComponent & CONTROLBOARDWRAPPER()
Definition: ControlBoardWrapperLogComponent.cpp:11
ControlBoardWrapperCurrentControl.h
ControlBoardWrapperCurrentControl::getRefCurrent
bool getRefCurrent(int j, double *t) override
Get the reference value of the current for a single motor.
Definition: ControlBoardWrapperCurrentControl.cpp:165
ControlBoardWrapperCommon::partName
std::string partName
Definition: ControlBoardWrapperCommon.h:24
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
WrappedDevice::subdevices
SubDeviceVector subdevices
Definition: SubDevice.h:125
yarp::dev::ICurrentControl::getRefCurrents
virtual bool getRefCurrents(double *currs)=0
Get the reference value of the currents for all motors.
ControlBoardWrapperCurrentControl::getCurrentRange
bool getCurrentRange(int j, double *min, double *max) override
Get the full scale of the current measurement for a given motor (e.g.
Definition: ControlBoardWrapperCurrentControl.cpp:14
ControlBoardWrapperCurrentControl::getCurrentRanges
bool getCurrentRanges(double *min, double *max) override
Get the full scale of the current measurements for all motors motor (e.g.
Definition: ControlBoardWrapperCurrentControl.cpp:37
ControlBoardWrapperCommon::device
WrappedDevice device
Definition: ControlBoardWrapperCommon.h:22
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::dev::ICurrentControl::setRefCurrent
virtual bool setRefCurrent(int m, double curr)=0
Set the reference value of the current for a single motor.
SubDevice::base
size_t base
Definition: SubDevice.h:58
WrappedDevice::lut
std::vector< DevicesLutEntry > lut
Definition: SubDevice.h:126
yarp::dev::ICurrentControl::getCurrentRange
virtual bool getCurrentRange(int m, double *min, double *max)=0
Get the full scale of the current measurement for a given motor (e.g.
MultiJointData::deviceNum
size_t deviceNum
Definition: MultiJointData.h:17
yarp::dev::ICurrentControl::getCurrentRanges
virtual bool getCurrentRanges(double *min, double *max)=0
Get the full scale of the current measurements for all motors motor (e.g.
SubDevice::wbase
size_t wbase
Definition: SubDevice.h:60
ControlBoardWrapperCurrentControl::getRefCurrents
bool getRefCurrents(double *t) override
Get the reference value of the currents for all motors.
Definition: ControlBoardWrapperCurrentControl.cpp:139
SubDevice
Definition: SubDevice.h:55
ControlBoardWrapperCommon::rpcDataMutex
std::mutex rpcDataMutex
Definition: ControlBoardWrapperCommon.h:27
WrappedDevice::maxNumOfJointsInDevices
size_t maxNumOfJointsInDevices
Definition: SubDevice.h:127
ControlBoardWrapperCommon::printError
void printError(const std::string &func_name, const std::string &info, bool result)
Definition: ControlBoardWrapperCommon.h:72