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