YARP
Yet Another Robot Platform
ControlBoardWrapperPWMControl.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->iPWM) {
28  return p->iPWM->setRefDutyCycle(static_cast<int>(off + p->base), v);
29  }
30  return false;
31 }
32 
34 {
35  bool ret = true;
36 
37  for (size_t l = 0; l < controlledJoints; l++) {
38  int off = device.lut[l].offset;
39  size_t subIndex = device.lut[l].deviceEntry;
40 
41  SubDevice* p = device.getSubdevice(subIndex);
42  if (!p) {
43  return false;
44  }
45 
46  if (p->iPWM) {
47  ret = ret && p->iPWM->setRefDutyCycle(static_cast<int>(off + p->base), v[l]);
48  } else {
49  ret = false;
50  }
51  }
52  return ret;
53 }
54 
56 {
57  size_t off;
58  try {
59  off = device.lut.at(j).offset;
60  } catch (...) {
61  yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
62  return false;
63  }
64  size_t subIndex = device.lut[j].deviceEntry;
65 
66  SubDevice* p = device.getSubdevice(subIndex);
67  if (!p) {
68  return false;
69  }
70 
71  if (p->iPWM) {
72  return p->iPWM->getRefDutyCycle(static_cast<int>(off + p->base), v);
73  }
74  return false;
75 }
76 
78 {
79  auto* references = new double[device.maxNumOfJointsInDevices];
80  bool ret = true;
81  for (size_t d = 0; d < device.subdevices.size(); d++) {
83  if (!p) {
84  ret = false;
85  break;
86  }
87 
88  if ((p->iPWM) && (ret = p->iPWM->getRefDutyCycles(references))) {
89  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
90  v[juser] = references[jdevice];
91  }
92  } else {
93  printError("getRefDutyCycles", p->id, ret);
94  ret = false;
95  break;
96  }
97  }
98 
99  delete[] references;
100  return ret;
101 }
102 
104 {
105  size_t off;
106  try {
107  off = device.lut.at(j).offset;
108  } catch (...) {
109  yCError(CONTROLBOARD, "Joint number %d out of bound [0-%zu] for part %s", j, controlledJoints, partName.c_str());
110  return false;
111  }
112  size_t subIndex = device.lut[j].deviceEntry;
113 
114  SubDevice* p = device.getSubdevice(subIndex);
115  if (!p) {
116  return false;
117  }
118 
119  if (p->iPWM) {
120  return p->iPWM->getDutyCycle(static_cast<int>(off + p->base), v);
121  }
122  return false;
123 }
124 
126 {
127  auto* dutyCicles = new double[device.maxNumOfJointsInDevices];
128  bool ret = true;
129  for (size_t d = 0; d < device.subdevices.size(); d++) {
130  SubDevice* p = device.getSubdevice(d);
131  if (!p) {
132  ret = false;
133  break;
134  }
135 
136  if ((p->iPWM) && (ret = p->iPWM->getDutyCycles(dutyCicles))) {
137  for (size_t juser = p->wbase, jdevice = p->base; juser <= p->wtop; juser++, jdevice++) {
138  v[juser] = dutyCicles[jdevice];
139  }
140  } else {
141  printError("getDutyCycles", p->id, ret);
142  ret = false;
143  break;
144  }
145  }
146 
147  delete[] dutyCicles;
148  return ret;
149 }
const yarp::os::LogComponent & CONTROLBOARD()
bool ret
void printError(const std::string &func_name, const std::string &info, bool result)
bool getRefDutyCycle(int j, double *v) override
Gets the last reference sent using the setRefDutyCycle function.
bool getDutyCycles(double *v) override
Gets the current dutycycle of the output of the amplifier (i.e.
bool getDutyCycle(int j, double *v) override
Gets the current dutycycle of the output of the amplifier (i.e.
bool setRefDutyCycle(int j, double v) override
Sets the reference dutycycle to a single motor.
bool getRefDutyCycles(double *v) override
Gets the last reference sent using the setRefDutyCycles function.
bool setRefDutyCycles(const double *v) override
Sets the reference dutycycle for all the motors.
size_t base
Definition: SubDevice.h:55
std::string id
Definition: SubDevice.h:54
yarp::dev::IPWMControl * iPWM
Definition: SubDevice.h:84
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 getDutyCycle(int m, double *val)=0
Gets the current dutycycle of the output of the amplifier (i.e.
virtual bool getRefDutyCycles(double *refs)=0
Gets the last reference sent using the setRefDutyCycles function.
virtual bool setRefDutyCycle(int m, double ref)=0
Sets the reference dutycycle to a single motor.
virtual bool getRefDutyCycle(int m, double *ref)=0
Gets the last reference sent using the setRefDutyCycle function.
virtual bool getDutyCycles(double *vals)=0
Gets the current dutycycle of the output of the amplifier (i.e.
#define yCError(component,...)
Definition: LogComponent.h:154