YARP
Yet Another Robot Platform
ImplementTorqueControl.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  iTorqueRaw(tq),
21  helper(nullptr),
22  intBuffManager(nullptr),
23  doubleBuffManager(nullptr)
24 {;}
25 
27 {
28  uninitialize();
29 }
30 
31 bool ImplementTorqueControl::initialize(int size, const int *amap, const double *enc, const double *zos, const double *nw, const double* amps, const double* dutys, const double* bemfs, const double* ktaus)
32 {
33  if (helper!=nullptr)
34  return false;
35 
36  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos, nw, amps, nullptr, dutys,bemfs,ktaus));
37  yAssert (helper != nullptr);
38 
40  yAssert (intBuffManager != nullptr);
41 
43  yAssert (doubleBuffManager != nullptr);
44 
45  return true;
46 }
47 
49 {
50  if (helper!=nullptr)
51  {
52  delete castToMapper(helper);
53  helper=nullptr;
54  }
55 
56  if(intBuffManager)
57  {
58  delete intBuffManager;
59  intBuffManager=nullptr;
60  }
61 
63  {
64  delete doubleBuffManager;
65  doubleBuffManager=nullptr;
66  }
67 
68  return true;
69 }
70 
72 {
73  return iTorqueRaw->getAxes(axes);
74 }
75 
77 {
79  int k;
80  bool ret;
81  double torque;
82  k=castToMapper(helper)->toHw(j);
83  ret = iTorqueRaw->getRefTorqueRaw(k, &torque);
84  *r=castToMapper(helper)->trqS2N(torque, k);
85  return ret;
86 }
87 
89 {
91  int k;
92 
94  castToMapper(helper)->bemf_user2raw(params.bemf, j, params_raw.bemf, k);
95  castToMapper(helper)->ktau_user2raw(params.ktau, j, params_raw.ktau, k);
96  params_raw.bemf_scale = params.bemf_scale;
97  params_raw.ktau_scale = params.ktau_scale;
98 
99  return iTorqueRaw->setMotorTorqueParamsRaw(k, params_raw);
100 }
101 
103 {
105  int k=castToMapper(helper)->toHw(j);
106 
108  bool b = iTorqueRaw->getMotorTorqueParamsRaw(k, &params_raw);
109  int tmp_j;
110 
111  if (b)
112  {
113  *params = params_raw;
114  castToMapper(helper)->bemf_raw2user(params_raw.bemf, k, (*params).bemf, tmp_j);
115  castToMapper(helper)->ktau_raw2user(params_raw.ktau, k, (*params).ktau, tmp_j);
116  (*params).bemf_scale = params_raw.bemf_scale;
117  (*params).ktau_scale = params_raw.ktau_scale;
118  }
119  return b;
120 }
121 
123 {
125  bool ret = iTorqueRaw->getRefTorquesRaw(buffValues.getData());
126  castToMapper(helper)->trqS2N(buffValues.getData(),t);
127  doubleBuffManager->releaseBuffer(buffValues);
128  return ret;
129 }
130 
132 {
134  castToMapper(helper)->trqN2S(t, buffValues.getData());
135  bool ret = iTorqueRaw->setRefTorquesRaw(buffValues.getData());
136  doubleBuffManager->releaseBuffer(buffValues);
137  return ret;
138 }
139 
141 {
143  int k;
144  double sens;
145  castToMapper(helper)->trqN2S(t,j,sens,k);
146  return iTorqueRaw->setRefTorqueRaw(k, sens);
147 }
148 
150 {
152  bool ret = iTorqueRaw->getTorquesRaw(buffValues.getData());
153  castToMapper(helper)->toUser(buffValues.getData(), t);
154  doubleBuffManager->releaseBuffer(buffValues);
155  return ret;
156 }
157 
158 bool ImplementTorqueControl::setRefTorques(const int n_joint, const int *joints, const double *t)
159 {
160  if(!castToMapper(helper)->checkAxesIds(n_joint, joints))
161  return false;
162 
165 
166  for(int idx=0; idx<n_joint; idx++)
167  {
168  buffValues[idx] = castToMapper(helper)->trqN2S(t[idx], joints[idx]);
169  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
170  }
171  bool ret = iTorqueRaw->setRefTorquesRaw(n_joint, buffJoints.getData(), buffValues.getData());
172 
173  doubleBuffManager->releaseBuffer(buffValues);
174  intBuffManager->releaseBuffer(buffJoints);
175  return ret;
176 }
177 
179 {
181  int k;
182  k=castToMapper(helper)->toHw(j);
183  return iTorqueRaw->getTorqueRaw(k, t);
184 }
185 
186 bool ImplementTorqueControl::getTorqueRanges(double *min, double *max)
187 {
190 
191  bool ret = iTorqueRaw->getTorqueRangesRaw(buffMin.getData(),buffMax.getData());
192  castToMapper(helper)->toUser(buffMin.getData(), min);
193  castToMapper(helper)->toUser(buffMax.getData(), max);
196  return ret;
197 }
198 
199 bool ImplementTorqueControl::getTorqueRange(int j, double *min, double *max)
200 {
202  int k;
203  k=castToMapper(helper)->toHw(j);
204  return iTorqueRaw->getTorqueRangeRaw(k, min, max);
205 }
yarp::dev::ITorqueControlRaw::setRefTorqueRaw
virtual bool setRefTorqueRaw(int j, double t)=0
Set the reference value of the torque for a given joint.
yarp::dev::ImplementTorqueControl::helper
void * helper
Definition: ImplementTorqueControl.h:36
yarp::dev::MotorTorqueParameters::bemf
double bemf
Definition: ITorqueControl.h:26
yarp::dev::ImplementTorqueControl::doubleBuffManager
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
Definition: ImplementTorqueControl.h:38
yarp::dev::ControlBoardHelper::toHw
int toHw(int axis)
Definition: ControlBoardHelper.cpp:245
yarp::dev::ITorqueControlRaw::setMotorTorqueParamsRaw
virtual bool setMotorTorqueParamsRaw(int j, const yarp::dev::MotorTorqueParameters params)
Set the motor parameters.
Definition: ITorqueControl.h:231
yarp::dev::ControlBoardHelper::bemf_user2raw
double bemf_user2raw(double bemf_user, int j)
Definition: ControlBoardHelper.cpp:750
yarp::dev::ControlBoardHelper::toUser
int toUser(int axis)
Definition: ControlBoardHelper.cpp:248
yarp::dev::MotorTorqueParameters::ktau_scale
double ktau_scale
Definition: ITorqueControl.h:29
yarp::dev::ImplementTorqueControl::intBuffManager
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
Definition: ImplementTorqueControl.h:37
t
float t
Definition: FfmpegWriter.cpp:74
yarp::dev::ITorqueControlRaw::getTorqueRaw
virtual bool getTorqueRaw(int j, double *t)=0
Get the value of the torque on a given joint (this is the feedback if you have a torque sensor).
yarp::dev::MotorTorqueParameters
Definition: ITorqueControl.h:24
yarp::dev::ImplementTorqueControl::setRefTorques
bool setRefTorques(const double *t) override
Set the reference value of the torque for all joints.
Definition: ImplementTorqueControl.cpp:131
yarp::dev::ITorqueControlRaw::getMotorTorqueParamsRaw
virtual bool getMotorTorqueParamsRaw(int j, yarp::dev::MotorTorqueParameters *params)
Get the motor parameters.
Definition: ITorqueControl.h:224
yarp::dev::ImplementTorqueControl::getRefTorque
bool getRefTorque(int j, double *) override
Get the reference value of the torque for a given joint.
Definition: ImplementTorqueControl.cpp:76
yarp::dev::ITorqueControlRaw
Interface for control boards implementing torque control.
Definition: ITorqueControl.h:139
yarp::dev::impl::Buffer::getData
T * getData()
Return the data pointer.
Definition: FixedSizeBuffersManager-inl.h:29
yarp::dev::ITorqueControlRaw::getTorqueRangeRaw
virtual bool getTorqueRangeRaw(int j, double *min, double *max)=0
Get the full scale of the torque sensor of a given joint.
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
yarp::dev::ImplementTorqueControl::getTorques
bool getTorques(double *t) override
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
Definition: ImplementTorqueControl.cpp:149
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
JOINTIDCHECK
#define JOINTIDCHECK
Definition: ImplementTorqueControl.cpp:17
yarp::dev::ImplementTorqueControl::ImplementTorqueControl
ImplementTorqueControl(yarp::dev::ITorqueControlRaw *y)
Definition: ImplementTorqueControl.cpp:19
yarp::dev::ITorqueControlRaw::getAxes
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
yarp::dev::ITorqueControlRaw::getRefTorquesRaw
virtual bool getRefTorquesRaw(double *t)=0
Get the reference value of the torque for all joints.
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::MotorTorqueParameters::ktau
double ktau
Definition: ITorqueControl.h:28
yarp::dev::ImplementTorqueControl::getAxes
bool getAxes(int *ax) override
Get the number of controlled axes.
Definition: ImplementTorqueControl.cpp:71
yarp::dev::ImplementTorqueControl::getTorque
bool getTorque(int j, double *t) override
Get the value of the torque on a given joint (this is the feedback if you have a torque sensor).
Definition: ImplementTorqueControl.cpp:178
yarp::dev::ImplementTorqueControl::initialize
bool initialize(int size, const int *amap, const double *enc, const double *zos, const double *nw, const double *amps, const double *dutys, const double *bemfs, const double *ktaus)
Initialize the internal data and alloc memory.
Definition: ImplementTorqueControl.cpp:31
yarp::dev::ImplementTorqueControl::getTorqueRanges
bool getTorqueRanges(double *min, double *max) override
Get the full scale of the torque sensors of all joints.
Definition: ImplementTorqueControl.cpp:186
yarp::dev::ImplementTorqueControl::getMotorTorqueParams
bool getMotorTorqueParams(int j, yarp::dev::MotorTorqueParameters *params) override
Get a subset of motor parameters (bemf, ktau etc) useful for torque control.
Definition: ImplementTorqueControl.cpp:102
yarp::dev::ITorqueControlRaw::getTorquesRaw
virtual bool getTorquesRaw(double *t)=0
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
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
yarp::dev::ImplementTorqueControl::iTorqueRaw
yarp::dev::ITorqueControlRaw * iTorqueRaw
Definition: ImplementTorqueControl.h:35
yarp::dev::ImplementTorqueControl::setMotorTorqueParams
bool setMotorTorqueParams(int j, const yarp::dev::MotorTorqueParameters params) override
Set a subset of motor parameters (bemf, ktau etc) useful for torque control.
Definition: ImplementTorqueControl.cpp:88
yarp::dev::ControlBoardHelper::ktau_user2raw
double ktau_user2raw(double ktau_user, int j)
Definition: ControlBoardHelper.cpp:755
ControlBoardHelper.h
yarp::dev::impl::FixedSizeBuffersManager::releaseBuffer
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
Definition: FixedSizeBuffersManager-inl.h:156
yarp::dev::ControlBoardHelper::trqN2S
void trqN2S(double newtons, int j, double &sens, int &k)
Definition: ControlBoardHelper.cpp:326
yarp::dev::MotorTorqueParameters::bemf_scale
double bemf_scale
Definition: ITorqueControl.h:27
yarp::dev::ImplementTorqueControl::getTorqueRange
bool getTorqueRange(int j, double *min, double *max) override
Get the full scale of the torque sensor of a given joint.
Definition: ImplementTorqueControl.cpp:199
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::ImplementTorqueControl::~ImplementTorqueControl
virtual ~ImplementTorqueControl()
Destructor.
Definition: ImplementTorqueControl.cpp:26
yarp::dev::ITorqueControlRaw::getTorqueRangesRaw
virtual bool getTorqueRangesRaw(double *min, double *max)=0
Get the full scale of the torque sensors of all joints.
ControlBoardInterfacesImpl.h
yarp::dev::ControlBoardHelper::trqS2N
void trqS2N(const double *sens, double *newtons)
Definition: ControlBoardHelper.cpp:350
yarp::dev::ITorqueControlRaw::setRefTorquesRaw
virtual bool setRefTorquesRaw(const double *t)=0
Set the reference value of the torque for all joints.
yarp::dev::ImplementTorqueControl::getRefTorques
bool getRefTorques(double *t) override
Get the reference value of the torque for all joints.
Definition: ImplementTorqueControl.cpp:122
yarp::dev::ImplementTorqueControl::uninitialize
bool uninitialize()
Clean up internal data and memory.
Definition: ImplementTorqueControl.cpp:48
yarp::dev::ITorqueControlRaw::getRefTorqueRaw
virtual bool getRefTorqueRaw(int j, double *t)=0
Set the reference value of the torque for a given joint.
yarp::dev::ImplementTorqueControl::setRefTorque
bool setRefTorque(int j, double t) override
Set the reference value of the torque for a given joint.
Definition: ImplementTorqueControl.cpp:140
yarp::dev::ControlBoardHelper::bemf_raw2user
void bemf_raw2user(double bemf_raw, int k_raw, double &bemf_user, int &j_user)
Definition: ControlBoardHelper.cpp:738
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::dev::ControlBoardHelper::ktau_raw2user
void ktau_raw2user(double ktau_raw, int k_raw, double &ktau_user, int &j_user)
Definition: ControlBoardHelper.cpp:744