YARP
Yet Another Robot Platform
ControlBoardWrapperPositionDirect.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  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->posDir) {
28  return p->posDir->setPosition(static_cast<int>(off + p->base), ref);
29  }
30 
31  return false;
32 }
33 
34 
35 bool ControlBoardWrapperPositionDirect::setPositions(const int n_joints, const int* joints, const double* dpos)
36 {
37  bool ret = true;
38 
39  rpcDataMutex.lock();
40  //Reset subdev_jointsVectorLen vector
41  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
42 
43  // Create a map of joints for each subDevice
44  size_t subIndex = 0;
45  for (int j = 0; j < n_joints; j++) {
46  subIndex = device.lut[joints[j]].deviceEntry;
47  int offset = device.lut[joints[j]].offset;
48  int base = rpcData.subdevices_p[subIndex]->base;
49  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = offset + base;
50  rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = dpos[j];
51  rpcData.subdev_jointsVectorLen[subIndex]++;
52  }
53 
54  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
55  if (rpcData.subdevices_p[subIndex]->posDir) // Position Direct
56  {
58  } else {
59  ret = false;
60  }
61  }
62  rpcDataMutex.unlock();
63  return ret;
64 }
65 
66 
68 {
69  bool ret = true;
70 
71  for (size_t l = 0; l < controlledJoints; l++) {
72  int off = device.lut[l].offset;
73  size_t subIndex = device.lut[l].deviceEntry;
74 
75  SubDevice* p = device.getSubdevice(subIndex);
76  if (!p) {
77  return false;
78  }
79 
80  if (p->posDir) {
81  ret = p->posDir->setPosition(static_cast<int>(off + p->base), refs[l]) && ret;
82  } else {
83  ret = false;
84  }
85  }
86  return ret;
87 }
88 
89 
91 {
92  size_t off;
93  try {
94  off = device.lut.at(j).offset;
95  } catch (...) {
96  yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
97  return false;
98  }
99  size_t subIndex = device.lut[j].deviceEntry;
100 
101  SubDevice* p = device.getSubdevice(subIndex);
102 
103  if (!p) {
104  return false;
105  }
106 
107  if (p->posDir) {
108  bool ret = p->posDir->getRefPosition(static_cast<int>(off + p->base), ref);
109  return ret;
110  }
111  *ref = 0;
112  return false;
113 }
114 
115 
117 {
118  auto* references = new double[device.maxNumOfJointsInDevices];
119  bool ret = true;
120  for (size_t d = 0; d < device.subdevices.size(); d++) {
121  SubDevice* p = device.getSubdevice(d);
122  if (!p) {
123  ret = false;
124  break;
125  }
126 
127  if ((p->posDir) && (ret = p->posDir->getRefPositions(references))) {
128  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
129  spds[juser] = references[jdevice];
130  }
131  } else {
132  printError("getRefPositions", p->id, ret);
133  ret = false;
134  break;
135  }
136  }
137 
138  delete[] references;
139  return ret;
140 }
141 
142 
143 bool ControlBoardWrapperPositionDirect::getRefPositions(const int n_joints, const int* joints, double* targets)
144 {
145  bool ret = true;
146 
147  rpcDataMutex.lock();
148  //Reset subdev_jointsVectorLen vector
149  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
150 
151  // Create a map of joints for each subDevice
152  size_t subIndex = 0;
153  for (int j = 0; j < n_joints; j++) {
154  subIndex = device.lut[joints[j]].deviceEntry;
155  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
156  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
157  rpcData.subdev_jointsVectorLen[subIndex]++;
158  }
159 
160  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
161  if (rpcData.subdevices_p[subIndex]->posDir) {
163  }
164  }
165 
166  if (ret) {
167  // ReMix values by user expectations
168  for (size_t i = 0; i < rpcData.deviceNum; i++) {
169  rpcData.subdev_jointsVectorLen[i] = 0; // reset tmp index
170  }
171 
172  // fill the output vector
173  for (int j = 0; j < n_joints; j++) {
174  subIndex = device.lut[joints[j]].deviceEntry;
175  targets[j] = rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]];
176  rpcData.subdev_jointsVectorLen[subIndex]++;
177  }
178  } else {
179  for (int j = 0; j < n_joints; j++) {
180  targets[j] = 0;
181  }
182  }
183  rpcDataMutex.unlock();
184  return ret;
185 }
const yarp::os::LogComponent & CONTROLBOARD()
bool ret
void printError(const std::string &func_name, const std::string &info, bool result)
bool getRefPositions(double *refs) override
Get the last position reference for all axes.
bool setPositions(const int n_joints, const int *joints, const double *dpos) override
Set new reference point for all axes.
bool setPosition(int j, double ref) override
Set new position for a single axis.
bool getRefPosition(const int joint, double *ref) override
Get the last position reference for the specified axis.
int * subdev_jointsVectorLen
SubDevice ** subdevices_p
double ** values
size_t base
Definition: SubDevice.h:55
std::string id
Definition: SubDevice.h:54
yarp::dev::IPositionDirect * posDir
Definition: SubDevice.h:80
size_t wbase
Definition: SubDevice.h:57
std::vector< DevicesLutEntry > lut
Definition: SubDevice.h:122
SubDevice * getSubdevice(size_t i)
Definition: SubDevice.h:125
size_t maxNumOfJointsInDevices
Definition: SubDevice.h:123
SubDeviceVector subdevices
Definition: SubDevice.h:121
virtual bool getRefPositions(double *refs)
Get the last position reference for all axes.
virtual bool setPositions(const int n_joint, const int *joints, const double *refs)=0
Set new reference point for all axes.
virtual bool setPosition(int j, double ref)=0
Set new position for a single axis.
virtual bool getRefPosition(const int joint, double *ref)
Get the last position reference for the specified axis.
#define yCError(component,...)
Definition: LogComponent.h:154