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