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