YARP
Yet Another Robot Platform
ControlBoardWrapperVelocityControl.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 
11 #include <yarp/os/LogStream.h>
12 
14 
16 {
17  size_t off;
18  try {
19  off = device.lut.at(j).offset;
20  } catch (...) {
21  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
22  return false;
23  }
24  size_t subIndex = device.lut[j].deviceEntry;
25 
26  SubDevice* p = device.getSubdevice(subIndex);
27 
28  if (!p) {
29  return false;
30  }
31 
32  if (p->vel) {
33  return p->vel->velocityMove(static_cast<int>(off + p->base), v);
34  }
35  return false;
36 }
37 
39 {
40  bool ret = true;
41  int j_wrap = 0; // index of the wrapper joint
42 
43  for (size_t subDev_idx = 0; subDev_idx < device.subdevices.size(); subDev_idx++) {
44  SubDevice* p = device.getSubdevice(subDev_idx);
45 
46  if (!p) {
47  return false;
48  }
49 
50  int wrapped_joints = static_cast<int>((p->top - p->base) + 1);
51  int* joints = new int[wrapped_joints];
52 
53  if (p->vel) {
54  // verione comandi su subset di giunti
55  for (int j_dev = 0; j_dev < wrapped_joints; j_dev++) {
56  joints[j_dev] = static_cast<int>(p->base + j_dev);
57  }
58 
59  ret = ret && p->vel->velocityMove(wrapped_joints, joints, &v[j_wrap]);
60  j_wrap += wrapped_joints;
61  } else {
62  ret = false;
63  }
64 
65  if (joints != nullptr) {
66  delete[] joints;
67  joints = nullptr;
68  }
69  }
70 
71  return ret;
72 }
73 
74 bool ControlBoardWrapperVelocityControl::velocityMove(const int n_joints, const int* joints, const double* spds)
75 {
76  bool ret = true;
77 
78  rpcDataMutex.lock();
79  //Reset subdev_jointsVectorLen vector
80  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
81 
82  // Create a map of joints for each subDevice
83  size_t subIndex = 0;
84  for (int j = 0; j < n_joints; j++) {
85  subIndex = device.lut[joints[j]].deviceEntry;
86  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
87  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
88  rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = spds[j];
89  rpcData.subdev_jointsVectorLen[subIndex]++;
90  }
91 
92  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
93  if (rpcData.subdevices_p[subIndex]->vel) {
94  ret = ret && rpcData.subdevices_p[subIndex]->vel->velocityMove(rpcData.subdev_jointsVectorLen[subIndex], rpcData.jointNumbers[subIndex], rpcData.values[subIndex]);
95  } else {
96  ret = false;
97  }
98  }
99  rpcDataMutex.unlock();
100  return ret;
101 }
102 
104 {
106 
107  size_t off;
108  try {
109  off = device.lut.at(j).offset;
110  } catch (...) {
111  yCError(CONTROLBOARDWRAPPER, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
112  return false;
113  }
114  size_t subIndex = device.lut[j].deviceEntry;
115 
116  SubDevice* p = device.getSubdevice(subIndex);
117 
118  if (!p) {
119  return false;
120  }
121 
122  if (p->vel) {
123  bool ret = p->vel->getRefVelocity(static_cast<int>(off + p->base), vel);
124  return ret;
125  }
126  *vel = 0;
127  return false;
128 }
129 
130 
132 {
133  auto* references = new double[device.maxNumOfJointsInDevices];
134  bool ret = true;
135  for (size_t d = 0; d < device.subdevices.size(); d++) {
136  SubDevice* p = device.getSubdevice(d);
137  if (!p) {
138  ret = false;
139  break;
140  }
141 
142  if ((p->vel) && (ret = p->vel->getRefVelocities(references))) {
143  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
144  vels[juser] = references[jdevice];
145  }
146  } else {
147  printError("getRefVelocities", p->id, ret);
148  ret = false;
149  break;
150  }
151  }
152 
153  delete[] references;
154  return ret;
155 }
156 
157 bool ControlBoardWrapperVelocityControl::getRefVelocities(const int n_joints, const int* joints, double* vels)
158 {
160 
161  bool ret = true;
162 
163  rpcDataMutex.lock();
164  //Reset subdev_jointsVectorLen vector
165  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
166 
167  // Create a map of joints for each subDevice
168  size_t subIndex = 0;
169  for (int j = 0; j < n_joints; j++) {
170  subIndex = device.lut[joints[j]].deviceEntry;
171  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
172  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
173  rpcData.subdev_jointsVectorLen[subIndex]++;
174  }
175 
176  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
177  if (rpcData.subdevices_p[subIndex]->vel) {
179  }
180  }
181 
182  if (ret) {
183  // ReMix values by user expectations
184  for (size_t i = 0; i < rpcData.deviceNum; i++) {
185  rpcData.subdev_jointsVectorLen[i] = 0; // reset tmp index
186  }
187 
188  // fill the output vector
189  for (int j = 0; j < n_joints; j++) {
190  subIndex = device.lut[joints[j]].deviceEntry;
191  vels[j] = rpcData.values[subIndex][rpcData.subdev_jointsVectorLen[subIndex]];
192  rpcData.subdev_jointsVectorLen[subIndex]++;
193  }
194  } else {
195  for (int j = 0; j < n_joints; j++) {
196  vels[j] = 0;
197  }
198  }
199  rpcDataMutex.unlock();
200  return ret;
201 }
ControlBoardWrapperCommon::rpcData
MultiJointData rpcData
Definition: ControlBoardWrapperCommon.h:28
SubDevice::vel
yarp::dev::IVelocityControl * vel
Definition: SubDevice.h:72
LogStream.h
ControlBoardWrapperVelocityControl.h
yarp::dev::IVelocityControl::velocityMove
virtual bool velocityMove(int j, double sp)=0
Start motion at a given speed, single joint.
ControlBoardWrapperLogComponent.h
MultiJointData::values
double ** values
Definition: MultiJointData.h:23
MultiJointData::jointNumbers
int ** jointNumbers
Definition: MultiJointData.h:21
yarp::dev::IVelocityControl::getRefVelocities
virtual bool getRefVelocities(double *vels)
Get the last reference speed set by velocityMove for all joints.
Definition: IVelocityControl.h:255
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
SubDevice::top
size_t top
Definition: SubDevice.h:59
MultiJointData::subdev_jointsVectorLen
int * subdev_jointsVectorLen
Definition: MultiJointData.h:20
yarp::dev::IVelocityControl::getRefVelocity
virtual bool getRefVelocity(const int joint, double *vel)
Get the last reference speed set by velocityMove for single joint.
Definition: IVelocityControl.h:249
ControlBoardWrapperVelocityControl::velocityMove
bool velocityMove(int j, double v) override
Start motion at a given speed, single joint.
Definition: ControlBoardWrapperVelocityControl.cpp:15
MultiJointData::subdevices_p
SubDevice ** subdevices_p
Definition: MultiJointData.h:24
CONTROLBOARDWRAPPER
const yarp::os::LogComponent & CONTROLBOARDWRAPPER()
Definition: ControlBoardWrapperLogComponent.cpp:11
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
ControlBoardWrapperCommon::device
WrappedDevice device
Definition: ControlBoardWrapperCommon.h:22
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
ControlBoardWrapperVelocityControl::getRefVelocity
bool getRefVelocity(const int joint, double *vel) override
Get the last reference speed set by velocityMove for single joint.
Definition: ControlBoardWrapperVelocityControl.cpp:103
ControlBoardWrapperVelocityControl::getRefVelocities
bool getRefVelocities(double *vels) override
Get the last reference speed set by velocityMove for all joints.
Definition: ControlBoardWrapperVelocityControl.cpp:131
SubDevice::base
size_t base
Definition: SubDevice.h:58
WrappedDevice::lut
std::vector< DevicesLutEntry > lut
Definition: SubDevice.h:126
MultiJointData::deviceNum
size_t deviceNum
Definition: MultiJointData.h:17
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
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
ControlBoardWrapperCommon::printError
void printError(const std::string &func_name, const std::string &info, bool result)
Definition: ControlBoardWrapperCommon.h:72