YARP
Yet Another Robot Platform
ControlBoardWrapperControlMode.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->iMode) {
31  return p->iMode->getControlMode(static_cast<int>(off + p->base), mode);
32  }
33  return false;
34 }
35 
36 
38 {
39  int* all_mode = new int[device.maxNumOfJointsInDevices];
40  bool ret = true;
41  for (size_t d = 0; d < device.subdevices.size(); d++) {
43  if (!p) {
44  ret = false;
45  break;
46  }
47 
48  if ((p->iMode) && (ret = p->iMode->getControlModes(all_mode))) {
49  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
50  modes[juser] = all_mode[jdevice];
51  }
52  } else {
53  printError("getControlModes", p->id, ret);
54  ret = false;
55  break;
56  }
57  }
58 
59  delete[] all_mode;
60  return ret;
61 }
62 
63 
64 bool ControlBoardWrapperControlMode::getControlModes(const int n_joint, const int* joints, int* modes)
65 {
66  bool ret = true;
67 
68  for (int l = 0; l < n_joint; l++) {
69  int off = device.lut[joints[l]].offset;
70  size_t subIndex = device.lut[joints[l]].deviceEntry;
71 
72  SubDevice* p = device.getSubdevice(subIndex);
73  if (!p) {
74  return false;
75  }
76 
77  if (p->iMode) {
78  ret = ret && p->iMode->getControlMode(static_cast<int>(off + p->base), &modes[l]);
79  } else {
80  ret = false;
81  }
82  }
83  return ret;
84 }
85 
86 
87 bool ControlBoardWrapperControlMode::setControlMode(const int j, const int mode)
88 {
89  bool ret = true;
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->iMode) {
105  ret = p->iMode->setControlMode(static_cast<int>(off + p->base), mode);
106  }
107  return ret;
108 }
109 
110 
111 bool ControlBoardWrapperControlMode::setControlModes(const int n_joints, const int* joints, int* modes)
112 {
113  bool ret = true;
114 
115  rpcDataMutex.lock();
116  //Reset subdev_jointsVectorLen vector
117  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
118 
119  // Create a map of joints for each subDevice
120  size_t subIndex = 0;
121  for (int j = 0; j < n_joints; j++) {
122  subIndex = device.lut[joints[j]].deviceEntry;
123  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
124  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
125  rpcData.modes[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = modes[j];
126  rpcData.subdev_jointsVectorLen[subIndex]++;
127  }
128 
129  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
130  if (rpcData.subdevices_p[subIndex]->iMode) {
132  } else {
133  ret = false;
134  }
135  }
136  rpcDataMutex.unlock();
137  return ret;
138 }
139 
140 
142 {
143  bool ret = true;
144  int j_wrap = 0; // index of the wrapper joint
145 
146  int nDev = device.subdevices.size();
147  for (int subDev_idx = 0; subDev_idx < nDev; subDev_idx++) {
148  size_t subIndex = device.lut[j_wrap].deviceEntry;
149  SubDevice* p = device.getSubdevice(subIndex);
150  if (!p) {
151  return false;
152  }
153 
154  int wrapped_joints = static_cast<int>((p->top - p->base) + 1);
155  int* joints = new int[wrapped_joints];
156 
157  if (p->iMode) {
158  // versione comandi su subset di giunti
159  for (int j_dev = 0; j_dev < wrapped_joints; j_dev++) {
160  joints[j_dev] = static_cast<int>(p->base + j_dev); // for all joints is equivalent to add offset term
161  }
162 
163  ret = ret && p->iMode->setControlModes(wrapped_joints, joints, &modes[j_wrap]);
164  j_wrap += wrapped_joints;
165  }
166 
167  if (joints != nullptr) {
168  delete[] joints;
169  joints = nullptr;
170  }
171  }
172 
173  return ret;
174 }
ControlBoardWrapperCommon::rpcData
MultiJointData rpcData
Definition: ControlBoardWrapperCommon.h:28
ControlBoardWrapperLogComponent.h
ControlBoardWrapperControlMode::setControlModes
bool setControlModes(const int n_joints, const int *joints, int *modes) override
Set the current control mode for a subset of axes.
Definition: ControlBoardWrapperControlMode.cpp:111
ControlBoardWrapperControlMode::getControlMode
bool getControlMode(int j, int *mode) override
Get the current control mode.
Definition: ControlBoardWrapperControlMode.cpp:14
yarp::dev::IControlMode::getControlMode
virtual bool getControlMode(int j, int *mode)=0
Get the current control mode.
MultiJointData::jointNumbers
int ** jointNumbers
Definition: MultiJointData.h:21
yarp::dev::IControlMode::setControlModes
virtual bool setControlModes(const int n_joint, const int *joints, int *modes)=0
Set the current control mode for a subset of axes.
yarp::dev::IControlMode::getControlModes
virtual bool getControlModes(int *modes)=0
Get the current control mode (multiple joints).
SubDevice::iMode
yarp::dev::IControlMode * iMode
Definition: SubDevice.h:81
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
MultiJointData::subdevices_p
SubDevice ** subdevices_p
Definition: MultiJointData.h:24
ControlBoardWrapperControlMode::setControlMode
bool setControlMode(const int j, const int mode) override
Set the current control mode.
Definition: ControlBoardWrapperControlMode.cpp:87
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
ControlBoardWrapperControlMode.h
ControlBoardWrapperCommon::device
WrappedDevice device
Definition: ControlBoardWrapperCommon.h:22
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::dev::IControlMode::setControlMode
virtual bool setControlMode(const int j, const int mode)=0
Set the current control mode.
SubDevice::base
size_t base
Definition: SubDevice.h:58
WrappedDevice::lut
std::vector< DevicesLutEntry > lut
Definition: SubDevice.h:126
ControlBoardWrapperControlMode::getControlModes
bool getControlModes(int *modes) override
Get the current control mode (multiple joints).
Definition: ControlBoardWrapperControlMode.cpp:37
MultiJointData::modes
int ** modes
Definition: MultiJointData.h:22
MultiJointData::deviceNum
size_t deviceNum
Definition: MultiJointData.h:17
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