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