YARP
Yet Another Robot Platform
ImplementPositionDirect.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 
9 #include <cstdio>
10 
13 #include <yarp/os/Log.h>
15 
16 using namespace yarp::dev;
17 using namespace yarp::os;
18 
19 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
20 
22  iPDirect(y),
23  helper(nullptr),
24  intBuffManager(nullptr),
25  doubleBuffManager(nullptr)
26 {;}
27 
28 
30 {
31  uninitialize();
32 }
33 
34 bool ImplementPositionDirect::initialize(int size, const int *amap, const double *enc, const double *zos)
35 {
36  if(helper != nullptr)
37  return false;
38 
39  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos));
40  yAssert(helper != nullptr);
41 
43  yAssert (intBuffManager != nullptr);
44 
46  yAssert (doubleBuffManager != nullptr);
47 
48  return true;
49 }
50 
52 {
53  if(helper!=nullptr)
54  {
55  delete castToMapper(helper);
56  helper=nullptr;
57  }
58 
59  if(intBuffManager)
60  {
61  delete intBuffManager;
62  intBuffManager=nullptr;
63  }
64 
66  {
67  delete doubleBuffManager;
68  doubleBuffManager=nullptr;
69  }
70 
71  return true;
72 }
73 
75 {
76  (*axes)=castToMapper(helper)->axes();
77  return true;
78 }
79 
80 bool ImplementPositionDirect::setPosition(int j, double ref)
81 {
83  int k;
84  double enc;
85  castToMapper(helper)->posA2E(ref, j, enc, k);
86  return iPDirect->setPositionRaw(k, enc);
87 }
88 
89 bool ImplementPositionDirect::setPositions(const int n_joint, const int *joints, const double *refs)
90 {
91  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
92  return false;
93 
96 
97  if(n_joint > (int)intBuffManager->getBufferSize())
98  return false;
99 
100  for(int idx=0; idx<n_joint; idx++)
101  {
102  buffJoints.setValue(idx, castToMapper(helper)->toHw(joints[idx]));
103  buffValues.setValue(idx, castToMapper(helper)->posA2E(refs[idx], joints[idx]));
104  }
105 
106  bool ret = iPDirect->setPositionsRaw(n_joint, buffJoints.getData(), buffValues.getData());
107 
108  doubleBuffManager->releaseBuffer(buffValues);
109  intBuffManager->releaseBuffer(buffJoints);
110 
111  return ret;
112 }
113 
115 {
117  castToMapper(helper)->posA2E(refs, buffValues.getData());
118  bool ret = iPDirect->setPositionsRaw(buffValues.getData());
119  doubleBuffManager->releaseBuffer(buffValues);
120  return ret;
121 }
122 
123 bool ImplementPositionDirect::getRefPosition(const int j, double* ref)
124 {
126  int k;
127  double tmp;
128  k=castToMapper(helper)->toHw(j);
129 
130  bool ret = iPDirect->getRefPositionRaw(k, &tmp);
131 
132  *ref=(castToMapper(helper)->posE2A(tmp, k));
133  return ret;
134 }
135 
136 bool ImplementPositionDirect::getRefPositions(const int n_joint, const int* joints, double* refs)
137 {
138  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
139  return false;
140 
142 
143  for(int idx=0; idx<n_joint; idx++)
144  {
145  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
146  }
147 
149  bool ret = iPDirect->getRefPositionsRaw(n_joint, buffJoints.getData(), buffValues.getData());
150 
151  for(int idx=0; idx<n_joint; idx++)
152  {
153  refs[idx]=castToMapper(helper)->posE2A(buffValues[idx], buffJoints[idx]);
154  }
155 
156  doubleBuffManager->releaseBuffer(buffValues);
157  intBuffManager->releaseBuffer(buffJoints);
158 
159  return ret;
160 }
161 
163 {
165  bool ret = iPDirect->getRefPositionsRaw(buffValues.getData());
166  castToMapper(helper)->posE2A(buffValues.getData(), refs);
167  doubleBuffManager->releaseBuffer(buffValues);
168  return ret;
169 }
170 
171 
172 // Stub impl
173 
174 bool StubImplPositionDirectRaw::NOT_YET_IMPLEMENTED(const char *func)
175 {
176  if (func)
177  yError("%s: not yet implemented\n", func);
178  else
179  yError("Function not yet implemented\n");
180 
181  return false;
182 }
JOINTIDCHECK
#define JOINTIDCHECK
Definition: ImplementPositionDirect.cpp:19
yarp::dev::IPositionDirectRaw::setPositionsRaw
virtual bool setPositionsRaw(const int n_joint, const int *joints, const double *refs)=0
Set new reference point for all axes.
yarp::dev::ControlBoardHelper::toHw
int toHw(int axis)
Definition: ControlBoardHelper.cpp:245
yarp::dev::ControlBoardHelper::posE2A
void posE2A(double enc, int j, double &ang, int &k)
Definition: ControlBoardHelper.cpp:290
yarp::dev::ImplementPositionDirect::~ImplementPositionDirect
virtual ~ImplementPositionDirect()
Destructor.
Definition: ImplementPositionDirect.cpp:29
yarp::dev::ImplementPositionDirect::setPosition
bool setPosition(int j, double ref) override
Set new position for a single axis.
Definition: ImplementPositionDirect.cpp:80
yarp::dev::ImplementPositionDirect::iPDirect
IPositionDirectRaw * iPDirect
Definition: ImplementPositionDirect.h:41
yarp::dev::impl::Buffer::getData
T * getData()
Return the data pointer.
Definition: FixedSizeBuffersManager-inl.h:29
yarp::dev::impl::FixedSizeBuffersManager< int >
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
yError
#define yError(...)
Definition: Log.h:282
yarp::dev::ImplementPositionDirect::setPositions
bool setPositions(const int n_joint, const int *joints, const double *refs) override
Set new reference point for all axes.
Definition: ImplementPositionDirect.cpp:89
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
Log.h
yarp::dev::impl::FixedSizeBuffersManager::getBufferSize
std::size_t getBufferSize()
Get the number of elements of a buffer.
Definition: FixedSizeBuffersManager-inl.h:193
yarp::dev::ControlBoardHelper::posA2E
void posA2E(double ang, int j, double &enc, int &k)
Definition: ControlBoardHelper.cpp:279
yarp::dev::ImplementPositionDirect::getRefPositions
bool getRefPositions(double *refs) override
Get the last position reference for all axes.
Definition: ImplementPositionDirect.cpp:162
yarp::dev::IPositionDirectRaw::getRefPositionRaw
virtual bool getRefPositionRaw(const int joint, double *ref)
Get the last position reference for the specified axis.
Definition: IPositionDirect.h:167
yarp::dev::IPositionDirectRaw::setPositionRaw
virtual bool setPositionRaw(int j, double ref)=0
Set new position for a single axis.
yarp::dev::ImplementPositionDirect::getRefPosition
bool getRefPosition(const int joint, double *ref) override
Get the last position reference for the specified axis.
Definition: ImplementPositionDirect.cpp:123
FixedSizeBuffersManager.h
yarp::dev::IPositionDirectRaw
Interface for a generic control board device implementing position control in encoder coordinates.
Definition: IPositionDirect.h:118
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::ImplementPositionDirect::intBuffManager
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
Definition: ImplementPositionDirect.h:43
ControlBoardHelper.h
yarp::dev::ImplementPositionDirect::helper
void * helper
Definition: ImplementPositionDirect.h:42
yarp::dev::impl::FixedSizeBuffersManager::releaseBuffer
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
Definition: FixedSizeBuffersManager-inl.h:156
yarp::dev::IPositionDirectRaw::getRefPositionsRaw
virtual bool getRefPositionsRaw(double *refs)
Get the last position reference for all axes.
Definition: IPositionDirect.h:178
yarp::dev::ImplementPositionDirect::initialize
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
Definition: ImplementPositionDirect.cpp:34
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::ImplementPositionDirect::doubleBuffManager
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
Definition: ImplementPositionDirect.h:44
yarp::dev::ControlBoardHelper
Definition: ControlBoardHelper.h:60
yarp::dev::ImplementPositionDirect::getAxes
bool getAxes(int *axes) override
Get the number of controlled axes.
Definition: ImplementPositionDirect.cpp:74
ControlBoardInterfacesImpl.h
yarp::dev::ControlBoardHelper::axes
int axes()
Definition: ControlBoardHelper.cpp:762
yarp::dev::impl::Buffer::setValue
void setValue(uint32_t index, T value)
Set the value @value in the buffer at index index.
Definition: FixedSizeBuffersManager-inl.h:54
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::dev::ImplementPositionDirect::uninitialize
bool uninitialize()
Clean up internal data and memory.
Definition: ImplementPositionDirect.cpp:51
yarp::dev::ImplementPositionDirect::ImplementPositionDirect
ImplementPositionDirect(yarp::dev::IPositionDirectRaw *y)
Constructor.
Definition: ImplementPositionDirect.cpp:21