YARP
Yet Another Robot Platform
ImplementTorqueControl.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  iTorqueRaw(tq),
18  helper(nullptr),
19  intBuffManager(nullptr),
20  doubleBuffManager(nullptr)
21 {;}
22 
24 {
25  uninitialize();
26 }
27 
28 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)
29 {
30  if (helper != nullptr) {
31  return false;
32  }
33 
34  helper=(void *)(new ControlBoardHelper(size, amap, enc, zos, nw, amps, nullptr, dutys,bemfs,ktaus));
35  yAssert (helper != nullptr);
36 
38  yAssert (intBuffManager != nullptr);
39 
41  yAssert (doubleBuffManager != nullptr);
42 
43  return true;
44 }
45 
47 {
48  if (helper!=nullptr)
49  {
50  delete castToMapper(helper);
51  helper=nullptr;
52  }
53 
54  if(intBuffManager)
55  {
56  delete intBuffManager;
57  intBuffManager=nullptr;
58  }
59 
61  {
62  delete doubleBuffManager;
63  doubleBuffManager=nullptr;
64  }
65 
66  return true;
67 }
68 
70 {
71  return iTorqueRaw->getAxes(axes);
72 }
73 
75 {
77  int k;
78  bool ret;
79  double torque;
80  k=castToMapper(helper)->toHw(j);
81  ret = iTorqueRaw->getRefTorqueRaw(k, &torque);
82  *r=castToMapper(helper)->trqS2N(torque, k);
83  return ret;
84 }
85 
87 {
89  int k;
90 
92  castToMapper(helper)->bemf_user2raw(params.bemf, j, params_raw.bemf, k);
93  castToMapper(helper)->ktau_user2raw(params.ktau, j, params_raw.ktau, k);
94  params_raw.bemf_scale = params.bemf_scale;
95  params_raw.ktau_scale = params.ktau_scale;
96 
97  return iTorqueRaw->setMotorTorqueParamsRaw(k, params_raw);
98 }
99 
101 {
103  int k=castToMapper(helper)->toHw(j);
104 
106  bool b = iTorqueRaw->getMotorTorqueParamsRaw(k, &params_raw);
107  int tmp_j;
108 
109  if (b)
110  {
111  *params = params_raw;
112  castToMapper(helper)->bemf_raw2user(params_raw.bemf, k, (*params).bemf, tmp_j);
113  castToMapper(helper)->ktau_raw2user(params_raw.ktau, k, (*params).ktau, tmp_j);
114  (*params).bemf_scale = params_raw.bemf_scale;
115  (*params).ktau_scale = params_raw.ktau_scale;
116  }
117  return b;
118 }
119 
121 {
123  bool ret = iTorqueRaw->getRefTorquesRaw(buffValues.getData());
124  castToMapper(helper)->trqS2N(buffValues.getData(),t);
125  doubleBuffManager->releaseBuffer(buffValues);
126  return ret;
127 }
128 
130 {
132  castToMapper(helper)->trqN2S(t, buffValues.getData());
133  bool ret = iTorqueRaw->setRefTorquesRaw(buffValues.getData());
134  doubleBuffManager->releaseBuffer(buffValues);
135  return ret;
136 }
137 
139 {
141  int k;
142  double sens;
143  castToMapper(helper)->trqN2S(t,j,sens,k);
144  return iTorqueRaw->setRefTorqueRaw(k, sens);
145 }
146 
148 {
150  bool ret = iTorqueRaw->getTorquesRaw(buffValues.getData());
151  castToMapper(helper)->toUser(buffValues.getData(), t);
152  doubleBuffManager->releaseBuffer(buffValues);
153  return ret;
154 }
155 
156 bool ImplementTorqueControl::setRefTorques(const int n_joint, const int *joints, const double *t)
157 {
158  if (!castToMapper(helper)->checkAxesIds(n_joint, joints)) {
159  return false;
160  }
161 
164 
165  for(int idx=0; idx<n_joint; idx++)
166  {
167  buffValues[idx] = castToMapper(helper)->trqN2S(t[idx], joints[idx]);
168  buffJoints[idx] = castToMapper(helper)->toHw(joints[idx]);
169  }
170  bool ret = iTorqueRaw->setRefTorquesRaw(n_joint, buffJoints.getData(), buffValues.getData());
171 
172  doubleBuffManager->releaseBuffer(buffValues);
173  intBuffManager->releaseBuffer(buffJoints);
174  return ret;
175 }
176 
178 {
180  int k;
181  k=castToMapper(helper)->toHw(j);
182  return iTorqueRaw->getTorqueRaw(k, t);
183 }
184 
185 bool ImplementTorqueControl::getTorqueRanges(double *min, double *max)
186 {
189 
190  bool ret = iTorqueRaw->getTorqueRangesRaw(buffMin.getData(),buffMax.getData());
191  castToMapper(helper)->toUser(buffMin.getData(), min);
192  castToMapper(helper)->toUser(buffMax.getData(), max);
195  return ret;
196 }
197 
198 bool ImplementTorqueControl::getTorqueRange(int j, double *min, double *max)
199 {
201  int k;
202  k=castToMapper(helper)->toHw(j);
203  return iTorqueRaw->getTorqueRangeRaw(k, min, max);
204 }
yarp::dev::ControlBoardHelper * castToMapper(void *p)
float t
bool ret
#define JOINTIDCHECK
#define yAssert(x)
Definition: Log.h:294
void ktau_raw2user(double ktau_raw, int k_raw, double &ktau_user, int &j_user)
void trqN2S(double newtons, int j, double &sens, int &k)
void bemf_raw2user(double bemf_raw, int k_raw, double &bemf_user, int &j_user)
double ktau_user2raw(double ktau_user, int j)
void trqS2N(const double *sens, double *newtons)
double bemf_user2raw(double bemf_user, int j)
Interface for control boards implementing torque control.
virtual bool getTorqueRangeRaw(int j, double *min, double *max)=0
Get the full scale of the torque sensor of a given joint.
virtual bool setRefTorquesRaw(const double *t)=0
Set the reference value of the torque for all joints.
virtual bool setMotorTorqueParamsRaw(int j, const yarp::dev::MotorTorqueParameters params)
Set the motor parameters.
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
virtual bool getRefTorquesRaw(double *t)=0
Get the reference value of the torque for all joints.
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).
virtual bool getRefTorqueRaw(int j, double *t)=0
Set the reference value of the torque for a given joint.
virtual bool setRefTorqueRaw(int j, double t)=0
Set the reference value of the torque for a given joint.
virtual bool getMotorTorqueParamsRaw(int j, yarp::dev::MotorTorqueParameters *params)
Get the motor parameters.
virtual bool getTorqueRangesRaw(double *min, double *max)=0
Get the full scale of the torque sensors of all joints.
virtual bool getTorquesRaw(double *t)=0
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
bool getRefTorques(double *t) override
Get the reference value of the torque for all joints.
bool setMotorTorqueParams(int j, const yarp::dev::MotorTorqueParameters params) override
Set a subset of motor parameters (bemf, ktau etc) useful for torque control.
yarp::dev::impl::FixedSizeBuffersManager< double > * doubleBuffManager
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).
yarp::dev::impl::FixedSizeBuffersManager< int > * intBuffManager
bool getTorqueRanges(double *min, double *max) override
Get the full scale of the torque sensors of all joints.
bool setRefTorques(const double *t) override
Set the reference value of the torque for all joints.
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.
yarp::dev::ITorqueControlRaw * iTorqueRaw
ImplementTorqueControl(yarp::dev::ITorqueControlRaw *y)
bool uninitialize()
Clean up internal data and memory.
bool getTorqueRange(int j, double *min, double *max) override
Get the full scale of the torque sensor of a given joint.
bool getTorques(double *t) override
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
bool getRefTorque(int j, double *) override
Get the reference value of the torque for a given joint.
bool getAxes(int *ax) override
Get the number of controlled axes.
bool setRefTorque(int j, double t) override
Set the reference value of the torque for a given joint.
bool getMotorTorqueParams(int j, yarp::dev::MotorTorqueParameters *params) override
Get a subset of motor parameters (bemf, ktau etc) useful for torque control.
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.