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