YARP
Yet Another Robot Platform
ImplementControlMode.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 
12 
13 #include <cstdio>
14 using namespace yarp::dev;
15 using namespace yarp::os;
16 
17 #define JOINTIDCHECK if (j >= castToMapper(helper)->axes()){yError("joint id out of bound"); return false;}
18 
20  helper(nullptr),
21  raw(r),
22  buffManager(nullptr)
23 {;}
24 
25 bool ImplementControlMode::initialize(int size, const int *amap)
26 {
27  if (helper!=nullptr)
28  return false;
29 
30  helper=(void *)(new ControlBoardHelper(size, amap));
31  yAssert (helper != nullptr);
32 
33  buffManager = new yarp::dev::impl::FixedSizeBuffersManager<int> (size);
34  yAssert (buffManager != nullptr);
35  return true;
36 }
37 
39 {
40  uninitialize();
41 }
42 
44 {
45  if (helper!=nullptr)
46  {
47  delete castToMapper(helper);
48  helper=nullptr;
49  }
50 
51  if (buffManager!=nullptr)
52  {
53  delete buffManager;
54  buffManager=nullptr;
55  }
56  return true;
57 }
58 
60 {
62  int k=castToMapper(helper)->toHw(j);
63  return raw->getControlModeRaw(k, f);
64 }
65 
67 {
68  yarp::dev::impl::Buffer<int> buffValues = buffManager->getBuffer();
69 
70  bool ret=raw->getControlModesRaw(buffValues.getData());
71  castToMapper(helper)->toUser(buffValues.getData(), modes);
72 
73  buffManager->releaseBuffer(buffValues);
74  return ret;
75 }
76 
77 bool ImplementControlMode::getControlModes(const int n_joint, const int *joints, int *modes)
78 {
79  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
80  return false;
81 
82  yarp::dev::impl::Buffer<int> buffValues = buffManager->getBuffer();
83 
84  for(int idx=0; idx<n_joint; idx++)
85  {
86  buffValues[idx] = castToMapper(helper)->toHw(joints[idx]);
87  }
88  bool ret = raw->getControlModesRaw(n_joint, buffValues.getData(), modes);
89 
90  buffManager->releaseBuffer(buffValues);
91  return ret;
92 }
93 
94 bool ImplementControlMode::setControlMode(const int j, const int mode)
95 {
97  int k=castToMapper(helper)->toHw(j);
98  return raw->setControlModeRaw(k, mode);
99 }
100 
101 bool ImplementControlMode::setControlModes(const int n_joint, const int *joints, int *modes)
102 {
103  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
104  return false;
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::ImplementControlMode::setControlMode
bool setControlMode(const int j, const int mode) override
Set the current control mode.
Definition: ImplementControlMode.cpp:94
yarp::dev::ControlBoardHelper::toHw
int toHw(int axis)
Definition: ControlBoardHelper.cpp:245
yarp::dev::IControlModeRaw::getControlModesRaw
virtual bool getControlModesRaw(int *modes)=0
yarp::dev::ControlBoardHelper::toUser
int toUser(int axis)
Definition: ControlBoardHelper.cpp:248
ImplementControlMode.h
JOINTIDCHECK
#define JOINTIDCHECK
Definition: ImplementControlMode.cpp:17
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
yarp::dev::ImplementControlMode::initialize
bool initialize(int k, const int *amap)
Definition: ImplementControlMode.cpp:25
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::IControlModeRaw::setControlModeRaw
virtual bool setControlModeRaw(const int j, const int mode)=0
yarp::dev::ImplementControlMode::setControlModes
bool setControlModes(const int n_joint, const int *joints, int *modes) override
Set the current control mode for a subset of axes.
Definition: ImplementControlMode.cpp:101
yarp::dev::ImplementControlMode::getControlMode
bool getControlMode(int j, int *f) override
Get the current control mode.
Definition: ImplementControlMode.cpp:59
yarp::dev::ImplementControlMode::getControlModes
bool getControlModes(int *modes) override
Get the current control mode (multiple joints).
Definition: ImplementControlMode.cpp:66
yarp::dev::IControlModeRaw
Interface for setting control mode in control board.
Definition: IControlMode.h:104
yarp::dev::ImplementControlMode::ImplementControlMode
ImplementControlMode(IControlModeRaw *v)
Definition: ImplementControlMode.cpp:19
yarp::dev::ImplementControlMode::uninitialize
bool uninitialize()
Definition: ImplementControlMode.cpp:43
FixedSizeBuffersManager.h
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
ControlBoardHelper.h
yarp::dev::impl::FixedSizeBuffersManager::releaseBuffer
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
Definition: FixedSizeBuffersManager-inl.h:156
yarp::dev::IControlModeRaw::getControlModeRaw
virtual bool getControlModeRaw(int j, int *mode)=0
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::ControlBoardHelper
Definition: ControlBoardHelper.h:60
yarp::dev::IControlModeRaw::setControlModesRaw
virtual bool setControlModesRaw(const int n_joint, const int *joints, int *modes)=0
yarp::dev::ImplementControlMode::~ImplementControlMode
~ImplementControlMode()
Definition: ImplementControlMode.cpp:38
yarp::dev::ControlBoardHelper::axes
int axes()
Definition: ControlBoardHelper.cpp:762
yAssert
#define yAssert(x)
Definition: Log.h:297