YARP
Yet Another Robot Platform
ImplementPWMControl.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 
8 #include <yarp/os/LogStream.h>
10 #include <iostream>
11 
12 using namespace yarp::dev;
13 using namespace yarp::os;
14 
15 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
16 
19  helper(nullptr),
20  raw(r),
21  doubleBuffManager(nullptr)
22 {;}
23 
24 bool ImplementPWMControl::initialize(int size, const int *amap, const double* dutyToPWM)
25 {
26  if (helper != nullptr) {
27  return false;
28  }
29 
30  helper = (void *)(new ControlBoardHelper(size, amap, nullptr, nullptr, nullptr, nullptr, nullptr, dutyToPWM));
31  yAssert(helper != nullptr);
32 
33  doubleBuffManager = new yarp::dev::impl::FixedSizeBuffersManager<double> (size);
34  yAssert (doubleBuffManager != nullptr);
35 
36  return true;
37 }
38 
40 {
41  uninitialize();
42 }
43 
45 {
46  if (helper != nullptr)
47  {
48  delete castToMapper(helper);
49  helper = nullptr;
50  }
51 
52  if(doubleBuffManager)
53  {
54  delete doubleBuffManager;
55  doubleBuffManager=nullptr;
56  }
57 
58  return true;
59 }
60 
62 {
63  return raw->getNumberOfMotorsRaw(axes);
64 }
65 
66 bool ImplementPWMControl::setRefDutyCycle(int j, double duty)
67 {
69  int k;
70  double pwm;
71  castToMapper(helper)->dutycycle2PWM(duty, j, pwm, k);
72  return raw->setRefDutyCycleRaw(k, pwm);
73 }
74 
75 bool ImplementPWMControl::setRefDutyCycles(const double *duty)
76 {
77  yarp::dev::impl::Buffer<double> buffValues = doubleBuffManager->getBuffer();
78  castToMapper(helper)->dutycycle2PWM(duty, buffValues.getData());
79  bool ret = raw->setRefDutyCyclesRaw( buffValues.getData());
80  doubleBuffManager->releaseBuffer(buffValues);
81  return ret;
82 }
83 
85 {
87  double pwm;
88  int k = castToMapper(helper)->toHw(j);
89  bool ret = raw->getRefDutyCycleRaw(k, &pwm);
90  *v = castToMapper(helper)->PWM2dutycycle(pwm, k);
91  return ret;
92 }
93 
95 {
96  yarp::dev::impl::Buffer<double> buffValues = doubleBuffManager->getBuffer();
97  bool ret = raw->getRefDutyCyclesRaw(buffValues.getData());
98  castToMapper(helper)->PWM2dutycycle(buffValues.getData(), duty);
99  doubleBuffManager->releaseBuffer(buffValues);
100  return ret;
101 }
102 
103 bool ImplementPWMControl::getDutyCycle(int j, double *duty)
104 {
106  double pwm;
107  int k = castToMapper(helper)->toHw(j);
108  bool ret = raw->getDutyCycleRaw(k, &pwm);
109  *duty = castToMapper(helper)->PWM2dutycycle(pwm, k);
110  return ret;
111 }
112 
114 {
115  yarp::dev::impl::Buffer<double> buffValues = doubleBuffManager->getBuffer();
116  bool ret = raw->getDutyCyclesRaw(buffValues.getData());
117  castToMapper(helper)->PWM2dutycycle(buffValues.getData(), duty);
118  doubleBuffManager->releaseBuffer(buffValues);
119  return ret;
120 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:294
void dutycycle2PWM(double dutycycle, int j, double &pwm, int &k)
void PWM2dutycycle(const double *pwm, double *dutycycle)
Interface for controlling an axis, by sending directly a PWM reference signal to a motor.
Definition: IPWMControl.h:85
virtual bool getDutyCycleRaw(int m, double *val)=0
Gets the current dutycycle of the output of the amplifier (i.e.
virtual bool setRefDutyCycleRaw(int m, double ref)=0
Sets the reference dutycycle of a single motor.
virtual bool getDutyCyclesRaw(double *vals)=0
Gets the current dutycycle of the output of the amplifier (i.e.
virtual bool getNumberOfMotorsRaw(int *number)=0
Retrieves the number of controlled motors from the current physical interface.
virtual bool setRefDutyCyclesRaw(const double *refs)=0
Sets the reference dutycycle for all motors.
virtual bool getRefDutyCyclesRaw(double *refs)=0
Gets the last reference sent using the setRefDutyCyclesRaw function.
virtual bool getRefDutyCycleRaw(int m, double *ref)=0
Gets the last reference sent using the setRefDutyCycleRaw function.
bool initialize(int k, const int *amap, const double *dutyToPWM)
bool getDutyCycle(int j, double *v) override
Gets the current dutycycle of the output of the amplifier (i.e.
bool getDutyCycles(double *v) override
Gets the current dutycycle of the output of the amplifier (i.e.
bool getNumberOfMotors(int *ax) override
Retrieves the number of controlled motors from the current physical interface.
bool getRefDutyCycle(int j, double *v) override
Gets the last reference sent using the setRefDutyCycle function.
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.
bool setRefDutyCycle(int j, double v) override
Sets the reference dutycycle to a single motor.
Buffer contains info about a buffer of type T and it is used to exchange information with yarp::dev::...
T * getData()
Return the data pointer.
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
An interface for the device drivers.
An interface to the operating system, including Port based communication.