YARP
Yet Another Robot Platform
ImplementControlMode.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 
9 
10 #include <cstdio>
11 using namespace yarp::dev;
12 using namespace yarp::os;
13 
14 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
15 
17  helper(nullptr),
18  raw(r),
19  buffManager(nullptr)
20 {;}
21 
22 bool ImplementControlMode::initialize(int size, const int *amap)
23 {
24  if (helper != nullptr) {
25  return false;
26  }
27 
28  helper=(void *)(new ControlBoardHelper(size, amap));
29  yAssert (helper != nullptr);
30 
31  buffManager = new yarp::dev::impl::FixedSizeBuffersManager<int> (size);
32  yAssert (buffManager != nullptr);
33  return true;
34 }
35 
37 {
38  uninitialize();
39 }
40 
42 {
43  if (helper!=nullptr)
44  {
45  delete castToMapper(helper);
46  helper=nullptr;
47  }
48 
49  if (buffManager!=nullptr)
50  {
51  delete buffManager;
52  buffManager=nullptr;
53  }
54  return true;
55 }
56 
58 {
60  int k=castToMapper(helper)->toHw(j);
61  return raw->getControlModeRaw(k, f);
62 }
63 
65 {
66  yarp::dev::impl::Buffer<int> buffValues = buffManager->getBuffer();
67 
68  bool ret=raw->getControlModesRaw(buffValues.getData());
69  castToMapper(helper)->toUser(buffValues.getData(), modes);
70 
71  buffManager->releaseBuffer(buffValues);
72  return ret;
73 }
74 
75 bool ImplementControlMode::getControlModes(const int n_joint, const int *joints, int *modes)
76 {
77  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
78  return false;
79  }
80 
81  yarp::dev::impl::Buffer<int> buffValues = buffManager->getBuffer();
82 
83  for(int idx=0; idx<n_joint; idx++)
84  {
85  buffValues[idx] = castToMapper(helper)->toHw(joints[idx]);
86  }
87  bool ret = raw->getControlModesRaw(n_joint, buffValues.getData(), modes);
88 
89  buffManager->releaseBuffer(buffValues);
90  return ret;
91 }
92 
93 bool ImplementControlMode::setControlMode(const int j, const int mode)
94 {
96  int k=castToMapper(helper)->toHw(j);
97  return raw->setControlModeRaw(k, mode);
98 }
99 
100 bool ImplementControlMode::setControlModes(const int n_joint, const int *joints, int *modes)
101 {
102  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
103  return false;
104  }
105 
106  yarp::dev::impl::Buffer<int> buffValues = buffManager->getBuffer();
107 
108  for(int idx=0; idx<n_joint; idx++)
109  {
110  buffValues[idx] = castToMapper(helper)->toHw(joints[idx]);
111  }
112  bool ret = raw->setControlModesRaw(n_joint, buffValues.getData(), modes);
113 
114  buffManager->releaseBuffer(buffValues);
115  return ret;
116 }
117 
119 {
120  yarp::dev::impl::Buffer<int> buffValues = buffManager->getBuffer();
121  for(int idx=0; idx<castToMapper(helper)->axes(); idx++)
122  {
123  buffValues[castToMapper(helper)->toHw(idx)] = modes[idx];
124  }
125  bool ret = raw->setControlModesRaw(buffValues.getData());
126  buffManager->releaseBuffer(buffValues);
127  return ret;
128 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:294
Interface for setting control mode in control board.
Definition: IControlMode.h:101
virtual bool getControlModeRaw(int j, int *mode)=0
virtual bool setControlModeRaw(const int j, const int mode)=0
virtual bool setControlModesRaw(const int n_joint, const int *joints, int *modes)=0
virtual bool getControlModesRaw(int *modes)=0
bool setControlModes(const int n_joint, const int *joints, int *modes) override
Set the current control mode for a subset of axes.
bool initialize(int k, const int *amap)
bool getControlModes(int *modes) override
Get the current control mode (multiple joints).
bool setControlMode(const int j, const int mode) override
Set the current control mode.
bool getControlMode(int j, int *f) override
Get the current control mode.
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.