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 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:297
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:88
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.