YARP
Yet Another Robot Platform
ControlBoardWrapperInteractionMode.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 
11 
13 {
14  size_t off;
15  try {
16  off = device.lut.at(j).offset;
17  } catch (...) {
18  yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
19  return false;
20  }
21  size_t subIndex = device.lut[j].deviceEntry;
22 
23  SubDevice* s = device.getSubdevice(subIndex);
24  if (!s) {
25  return false;
26  }
27 
28  if (s->iInteract) {
29  return s->iInteract->getInteractionMode(static_cast<int>(off + s->base), mode);
30  }
31  return false;
32 }
33 
35 {
36  bool ret = true;
37 
38  rpcDataMutex.lock();
39  //Reset subdev_jointsVectorLen vector
40  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
41 
42  // Create a map of joints for each subDevice
43  size_t subIndex = 0;
44  for (int j = 0; j < n_joints; j++) {
45  subIndex = device.lut[joints[j]].deviceEntry;
46  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
47  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
48  rpcData.subdev_jointsVectorLen[subIndex]++;
49  }
50 
51  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
52  if (rpcData.subdevices_p[subIndex]->iInteract) {
55  rpcData.jointNumbers[subIndex],
56  reinterpret_cast<yarp::dev::InteractionModeEnum*>(rpcData.modes[subIndex]));
57  } else {
58  ret = false;
59  }
60  }
61 
62  if (ret) {
63  // ReMix values by user expectations
64  for (size_t i = 0; i < rpcData.deviceNum; i++) {
65  rpcData.subdev_jointsVectorLen[i] = 0; // reset tmp index
66  }
67 
68  // fill the output vector
69  for (int j = 0; j < n_joints; j++) {
70  subIndex = device.lut[joints[j]].deviceEntry;
71  modes[j] = static_cast<yarp::dev::InteractionModeEnum>(rpcData.modes[subIndex][rpcData.subdev_jointsVectorLen[subIndex]]);
72  rpcData.subdev_jointsVectorLen[subIndex]++;
73  }
74  } else {
75  for (int j = 0; j < n_joints; j++) {
76  modes[j] = VOCAB_IM_UNKNOWN;
77  }
78  }
79  rpcDataMutex.unlock();
80  return ret;
81 }
82 
84 {
85 
87  bool ret = true;
88  for (size_t d = 0; d < device.subdevices.size(); d++) {
90  if (!p) {
91  ret = false;
92  break;
93  }
94 
95  if ((p->iInteract) && (ret = p->iInteract->getInteractionModes(imodes))) {
96  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
97  modes[juser] = imodes[jdevice];
98  }
99  } else {
100  printError("getInteractionModes", p->id, ret);
101  ret = false;
102  break;
103  }
104  }
105 
106  delete[] imodes;
107  return ret;
108 }
109 
111 {
112  size_t off;
113  try {
114  off = device.lut.at(j).offset;
115  } catch (...) {
116  yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
117  return false;
118  }
119  size_t subIndex = device.lut[j].deviceEntry;
120 
121  SubDevice* s = device.getSubdevice(subIndex);
122  if (!s) {
123  return false;
124  }
125 
126  if (s->iInteract) {
127  return s->iInteract->setInteractionMode(static_cast<int>(off + s->base), mode);
128  }
129  return false;
130 }
131 
133 {
134  bool ret = true;
135 
136  rpcDataMutex.lock();
137  //Reset subdev_jointsVectorLen vector
138  memset(rpcData.subdev_jointsVectorLen, 0x00, sizeof(int) * rpcData.deviceNum);
139 
140  // Create a map of joints for each subDevice
141  size_t subIndex = 0;
142  for (int j = 0; j < n_joints; j++) {
143  subIndex = device.lut[joints[j]].deviceEntry;
144  rpcData.jointNumbers[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] =
145  static_cast<int>(device.lut[joints[j]].offset + rpcData.subdevices_p[subIndex]->base);
146  rpcData.modes[subIndex][rpcData.subdev_jointsVectorLen[subIndex]] = static_cast<int>(modes[j]);
147  rpcData.subdev_jointsVectorLen[subIndex]++;
148  }
149 
150  for (subIndex = 0; subIndex < rpcData.deviceNum; subIndex++) {
151  if (rpcData.subdevices_p[subIndex]->iInteract) {
154  rpcData.jointNumbers[subIndex],
155  reinterpret_cast<yarp::dev::InteractionModeEnum*>(rpcData.modes[subIndex]));
156  } else {
157  ret = false;
158  }
159  }
160  rpcDataMutex.unlock();
161  return ret;
162 }
163 
165 {
166  bool ret = true;
167 
168  for (size_t j = 0; j < controlledJoints; j++) {
169  size_t off;
170  try {
171  off = device.lut.at(j).offset;
172  } catch (...) {
173  yCError(CONTROLBOARD, "Joint number %zu out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
174  return false;
175  }
176  size_t subIndex = device.lut[j].deviceEntry;
177 
178  SubDevice* p = device.getSubdevice(subIndex);
179  if (!p) {
180  return false;
181  }
182 
183  if (p->iInteract) {
184  ret = ret && p->iInteract->setInteractionMode(static_cast<int>(off + p->base), modes[j]);
185  } else {
186  ret = false;
187  }
188  }
189  return ret;
190 }
const yarp::os::LogComponent & CONTROLBOARD()
bool ret
void printError(const std::string &func_name, const std::string &info, bool result)
bool setInteractionMode(int j, yarp::dev::InteractionModeEnum mode) override
Set the interaction mode of the robot, values can be stiff or compliant.
bool setInteractionModes(int n_joints, int *joints, yarp::dev::InteractionModeEnum *modes) override
Set the interaction mode of the robot for a set of joints, values can be stiff or compliant.
bool getInteractionMode(int j, yarp::dev::InteractionModeEnum *mode) override
Get the current interaction mode of the robot, values can be stiff or compliant.
bool getInteractionModes(int n_joints, int *joints, yarp::dev::InteractionModeEnum *modes) override
Get the current interaction mode of the robot for a set of joints, values can be stiff or compliant.
int * subdev_jointsVectorLen
SubDevice ** subdevices_p
size_t base
Definition: SubDevice.h:55
std::string id
Definition: SubDevice.h:54
yarp::dev::IInteractionMode * iInteract
Definition: SubDevice.h:81
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 getInteractionMode(int axis, yarp::dev::InteractionModeEnum *mode)=0
Get the current interaction mode of the robot, values can be stiff or compliant.
virtual bool getInteractionModes(int n_joints, int *joints, yarp::dev::InteractionModeEnum *modes)=0
Get the current interaction mode of the robot for a set of joints, values can be stiff or compliant.
virtual bool setInteractionModes(int n_joints, int *joints, yarp::dev::InteractionModeEnum *modes)=0
Set the interaction mode of the robot for a set of joints, values can be stiff or compliant.
virtual bool setInteractionMode(int axis, yarp::dev::InteractionModeEnum mode)=0
Set the interaction mode of the robot, values can be stiff or compliant.
#define yCError(component,...)
Definition: LogComponent.h:154