YARP
Yet Another Robot Platform
ImplementPositionDirect.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 
6 #include <cstdio>
7 
10 #include <yarp/os/Log.h>
12 
13 using namespace yarp::dev;
14 using namespace yarp::os;
15 
16 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
17 
19  iPDirect(y),
20  helper(nullptr),
21  intBuffManager(nullptr),
22  doubleBuffManager(nullptr)
23 {;}
24 
25 
27 {
28  uninitialize();
29 }
30 
31 bool ImplementPositionDirect::initialize(int size, const int *amap, const double *enc, const double *zos)
32 {
33  if (helper != nullptr) {
34  return false;
35  }
36 
37  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos));
38  yAssert(helper != nullptr);
39 
41  yAssert (intBuffManager != nullptr);
42 
44  yAssert (doubleBuffManager != nullptr);
45 
46  return true;
47 }
48 
50 {
51  if(helper!=nullptr)
52  {
53  delete castToMapper(helper);
54  helper=nullptr;
55  }
56 
57  if(intBuffManager)
58  {
59  delete intBuffManager;
60  intBuffManager=nullptr;
61  }
62 
64  {
65  delete doubleBuffManager;
66  doubleBuffManager=nullptr;
67  }
68 
69  return true;
70 }
71 
73 {
74  (*axes)=castToMapper(helper)->axes();
75  return true;
76 }
77 
78 bool ImplementPositionDirect::setPosition(int j, double ref)
79 {
81  int k;
82  double enc;
83  castToMapper(helper)->posA2E(ref, j, enc, k);
84  return iPDirect->setPositionRaw(k, enc);
85 }
86 
87 bool ImplementPositionDirect::setPositions(const int n_joint, const int *joints, const double *refs)
88 {
89  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
90  return false;
91  }
92 
95 
96  if (n_joint > (int)intBuffManager->getBufferSize()) {
97  return false;
98  }
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  }
141 
143 
144  for(int idx=0; idx<n_joint; idx++)
145  {
146  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
147  }
148 
150  bool ret = iPDirect->getRefPositionsRaw(n_joint, buffJoints.getData(), buffValues.getData());
151 
152  for(int idx=0; idx<n_joint; idx++)
153  {
154  refs[idx]=castToMapper(helper)->posE2A(buffValues[idx], buffJoints[idx]);
155  }
156 
157  doubleBuffManager->releaseBuffer(buffValues);
158  intBuffManager->releaseBuffer(buffJoints);
159 
160  return ret;
161 }
162 
164 {
166  bool ret = iPDirect->getRefPositionsRaw(buffValues.getData());
167  castToMapper(helper)->posE2A(buffValues.getData(), refs);
168  doubleBuffManager->releaseBuffer(buffValues);
169  return ret;
170 }
171 
172 
173 // Stub impl
174 
175 bool StubImplPositionDirectRaw::NOT_YET_IMPLEMENTED(const char *func)
176 {
177  if (func) {
178  yError("%s: not yet implemented\n", func);
179  } else {
180  yError("Function not yet implemented\n");
181  }
182 
183  return false;
184 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yError(...)
Definition: Log.h:279
#define yAssert(x)
Definition: Log.h:294
void posE2A(double enc, int j, double &ang, int &k)
void posA2E(double ang, int j, double &enc, int &k)
Interface for a generic control board device implementing position control in encoder coordinates.
virtual bool setPositionsRaw(const int n_joint, const int *joints, const double *refs)=0
Set new reference point for all axes.
virtual bool setPositionRaw(int j, double ref)=0
Set new position for a single axis.
virtual bool getRefPositionsRaw(double *refs)
Get the last position reference for all axes.
virtual bool getRefPositionRaw(const int joint, double *ref)
Get the last position reference for the specified axis.
bool getRefPositions(double *refs) override
Get the last position reference for all axes.
ImplementPositionDirect(yarp::dev::IPositionDirectRaw *y)
Constructor.
bool setPositions(const int n_joint, const int *joints, const double *refs) override
Set new reference point for all axes.
bool getAxes(int *axes) override
Get the number of controlled axes.
bool uninitialize()
Clean up internal data and memory.
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
bool setPosition(int j, double ref) override
Set new position for a single axis.
bool getRefPosition(const int joint, double *ref) override
Get the last position reference for the specified axis.
bool initialize(int size, const int *amap, const double *enc, const double *zos)
Initialize the internal data and alloc memory.
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
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.
void setValue(uint32_t index, T value)
Set the value @value in the buffer at index index.
std::size_t getBufferSize()
Get the number of elements of a buffer.
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.