-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjoint-torque-controller.cpp
287 lines (256 loc) · 12.5 KB
/
joint-torque-controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2015, Andrea Del Prete, LAAS-CNRS
*
*/
#include <dynamic-graph/factory.h>
#include <Eigen/Dense>
#include <sot/core/debug.hh>
#include <sot/torque_control/commands-helper.hh>
#include <sot/torque_control/joint-torque-controller.hh>
namespace dynamicgraph {
namespace sot {
namespace torque_control {
#define MODEL_INPUT_SIGNALS \
m_motorParameterKt_pSIN << m_motorParameterKt_nSIN \
<< m_motorParameterKf_pSIN \
<< m_motorParameterKf_nSIN \
<< m_motorParameterKv_pSIN \
<< m_motorParameterKv_nSIN \
<< m_motorParameterKa_pSIN \
<< m_motorParameterKa_nSIN << m_polySignDqSIN
#define ESTIMATOR_INPUT_SIGNALS \
m_jointsPositionsSIN << m_jointsVelocitiesSIN << m_jointsAccelerationsSIN \
<< m_jointsTorquesSIN << m_jointsTorquesDerivativeSIN
#define TORQUE_INTEGRAL_INPUT_SIGNALS \
m_KiTorqueSIN << m_torque_integral_saturationSIN
#define TORQUE_CONTROL_INPUT_SIGNALS \
m_jointsTorquesDesiredSIN << m_KpTorqueSIN << m_KdTorqueSIN \
<< m_coulomb_friction_compensation_percentageSIN
#define VEL_CONTROL_INPUT_SIGNALS m_dq_desSIN << m_KdVelSIN << m_KiVelSIN
#define ALL_INPUT_SIGNALS \
ESTIMATOR_INPUT_SIGNALS << TORQUE_INTEGRAL_INPUT_SIGNALS \
<< TORQUE_CONTROL_INPUT_SIGNALS \
<< VEL_CONTROL_INPUT_SIGNALS << MODEL_INPUT_SIGNALS
#define ALL_OUTPUT_SIGNALS \
m_uSOUT << m_torque_error_integralSOUT << m_smoothSignDqSOUT
namespace dynamicgraph = ::dynamicgraph;
using namespace dynamicgraph;
using namespace dynamicgraph::command;
using namespace std;
using namespace Eigen;
/// Define EntityClassName here rather than in the header file
/// so that it can be used by the macros DEFINE_SIGNAL_**_FUNCTION.
typedef JointTorqueController EntityClassName;
/* --- DG FACTORY ------------------------------------------------------- */
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(JointTorqueController,
"JointTorqueController");
/* --- CONSTRUCTION ----------------------------------------------------- */
/* --- CONSTRUCTION ----------------------------------------------------- */
/* --- CONSTRUCTION ----------------------------------------------------- */
JointTorqueController::JointTorqueController(const std::string& name)
: Entity(name),
CONSTRUCT_SIGNAL_IN(jointsPositions, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(jointsVelocities, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(jointsAccelerations, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(jointsTorques, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(jointsTorquesDerivative, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(jointsTorquesDesired, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(dq_des, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(KpTorque,
dynamicgraph::Vector) // proportional gain for torque
// feedback controller
,
CONSTRUCT_SIGNAL_IN(
KiTorque,
dynamicgraph::Vector) // integral gain for torque feedback controller
,
CONSTRUCT_SIGNAL_IN(KdTorque,
dynamicgraph::Vector) // derivative gain for torque
// feedback controller
,
CONSTRUCT_SIGNAL_IN(
KdVel, dynamicgraph::Vector) // derivative gain for velocity feedback
,
CONSTRUCT_SIGNAL_IN(
KiVel, dynamicgraph::Vector) // integral gain for velocity feedback
,
CONSTRUCT_SIGNAL_IN(torque_integral_saturation, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(coulomb_friction_compensation_percentage,
dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKt_p, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKt_n, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKf_p, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKf_n, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKv_p, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKv_n, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKa_p, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(motorParameterKa_n, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_IN(polySignDq, dynamicgraph::Vector),
CONSTRUCT_SIGNAL_OUT(
u, dynamicgraph::Vector,
ESTIMATOR_INPUT_SIGNALS
<< TORQUE_CONTROL_INPUT_SIGNALS << VEL_CONTROL_INPUT_SIGNALS
<< MODEL_INPUT_SIGNALS << m_torque_error_integralSOUT),
CONSTRUCT_SIGNAL_OUT(smoothSignDq, dynamicgraph::Vector,
m_jointsVelocitiesSIN),
CONSTRUCT_SIGNAL_OUT(torque_error_integral, dynamicgraph::Vector,
m_jointsTorquesSIN
<< m_jointsTorquesDesiredSIN
<< TORQUE_INTEGRAL_INPUT_SIGNALS) {
Entity::signalRegistration(ALL_INPUT_SIGNALS << ALL_OUTPUT_SIGNALS);
/* Commands. */
addCommand("getTimestep",
makeDirectGetter(*this, &m_dt,
docDirectGetter("Control timestep", "double")));
addCommand("init",
makeCommandVoid2(*this, &JointTorqueController::init,
docCommandVoid2("Initialize the controller.",
"Control timestep [s].",
"Robot reference (string)")));
addCommand("reset_integral",
makeCommandVoid0(*this, &JointTorqueController::reset_integral,
docCommandVoid0("Reset the integral error.")));
}
/* --- COMMANDS ---------------------------------------------------------- */
/* --- COMMANDS ---------------------------------------------------------- */
/* --- COMMANDS ---------------------------------------------------------- */
void JointTorqueController::init(const double& timestep,
const std::string& robot_ref) {
assert(timestep > 0.0 && "Timestep should be > 0");
if (!m_jointsVelocitiesSIN.isPlugged())
return SEND_MSG("Init failed: signal jointsVelocities is not plugged",
MSG_TYPE_ERROR);
if (!m_jointsTorquesSIN.isPlugged())
return SEND_MSG("Init failed: signal m_jointsTorquesSIN is not plugged",
MSG_TYPE_ERROR);
if (!m_jointsTorquesDesiredSIN.isPlugged())
return SEND_MSG(
"Init failed: signal m_jointsTorquesDesiredSIN is not plugged",
MSG_TYPE_ERROR);
if (!m_KpTorqueSIN.isPlugged())
return SEND_MSG("Init failed: signal m_KpTorqueSIN is not plugged",
MSG_TYPE_ERROR);
if (!m_KiTorqueSIN.isPlugged())
return SEND_MSG("Init failed: signal m_KiTorqueSIN is not plugged",
MSG_TYPE_ERROR);
/* Retrieve m_robot_util informations */
std::string localName(robot_ref);
if (isNameInRobotUtil(localName)) {
m_robot_util = getRobotUtil(localName);
} else {
SEND_MSG("You should have an entity controller manager initialized before",
MSG_TYPE_ERROR);
return;
}
m_dt = timestep;
m_tau_star.setZero(m_robot_util->m_nbJoints);
m_current_des.setZero(m_robot_util->m_nbJoints);
m_tauErrIntegral.setZero(m_robot_util->m_nbJoints);
// m_dqDesIntegral.setZero(m_robot_util->m_nbJoints);
m_dqErrIntegral.setZero(m_robot_util->m_nbJoints);
}
void JointTorqueController::reset_integral() {
m_tauErrIntegral.setZero();
m_dqErrIntegral.setZero();
}
/* --- SIGNALS ---------------------------------------------------------- */
/* --- SIGNALS ---------------------------------------------------------- */
/* --- SIGNALS ---------------------------------------------------------- */
DEFINE_SIGNAL_OUT_FUNCTION(u, dynamicgraph::Vector) {
// const Eigen::VectorXd& q = m_jointsPositionsSIN(iter);
const Eigen::VectorXd& dq = m_jointsVelocitiesSIN(iter);
const Eigen::VectorXd& ddq = m_jointsAccelerationsSIN(iter);
const Eigen::VectorXd& tau = m_jointsTorquesSIN(iter);
const Eigen::VectorXd& dtau = m_jointsTorquesDerivativeSIN(iter);
const Eigen::VectorXd& tau_d = m_jointsTorquesDesiredSIN(iter);
// const Eigen::VectorXd& dtau_d =
// m_jointsTorquesDesiredDerivativeSIN(iter);
const Eigen::VectorXd& dq_des = m_dq_desSIN(iter);
const Eigen::VectorXd& kp = m_KpTorqueSIN(iter);
const Eigen::VectorXd& kd = m_KdTorqueSIN(iter);
const Eigen::VectorXd& kd_vel = m_KdVelSIN(iter);
const Eigen::VectorXd& ki_vel = m_KiVelSIN(iter);
const Eigen::VectorXd& tauErrInt = m_torque_error_integralSOUT(iter);
const Eigen::VectorXd& colFricCompPerc =
m_coulomb_friction_compensation_percentageSIN(iter);
const Eigen::VectorXd& motorParameterKt_p = m_motorParameterKt_pSIN(iter);
const Eigen::VectorXd& motorParameterKt_n = m_motorParameterKt_nSIN(iter);
const Eigen::VectorXd& motorParameterKf_p = m_motorParameterKf_pSIN(iter);
const Eigen::VectorXd& motorParameterKf_n = m_motorParameterKf_nSIN(iter);
const Eigen::VectorXd& motorParameterKv_p = m_motorParameterKv_pSIN(iter);
const Eigen::VectorXd& motorParameterKv_n = m_motorParameterKv_nSIN(iter);
const Eigen::VectorXd& motorParameterKa_p = m_motorParameterKa_pSIN(iter);
const Eigen::VectorXd& motorParameterKa_n = m_motorParameterKa_nSIN(iter);
const Eigen::VectorXd& polySignDq = m_polySignDqSIN(iter);
// const Eigen::VectorXd& dq_thr = m_dq_thresholdSIN(iter);
m_tau_star =
tau_d + kp.cwiseProduct(tau_d - tau) + tauErrInt - kd.cwiseProduct(dtau);
int offset = 0;
if (dq.size() == (int)(m_robot_util->m_nbJoints + 6)) offset = 6;
m_dqErrIntegral += m_dt * ki_vel.cwiseProduct(dq_des - dq);
const Eigen::VectorXd& err_int_sat = m_torque_integral_saturationSIN(iter);
// saturate
bool saturating = false;
for (int i = 0; i < (int)m_robot_util->m_nbJoints; i++) {
if (m_dqErrIntegral(i) > err_int_sat(i)) {
saturating = true;
m_dqErrIntegral(i) = err_int_sat(i);
} else if (m_dqErrIntegral(i) < -err_int_sat(i)) {
saturating = true;
m_dqErrIntegral(i) = -err_int_sat(i);
}
}
if (saturating)
SEND_INFO_STREAM_MSG("Saturate dqErr integral: " +
toString(m_dqErrIntegral.head<12>()));
for (int i = 0; i < (int)m_robot_util->m_nbJoints; i++) {
m_current_des(i) = motorModel.getCurrent(
m_tau_star(i),
dq(i + offset) + kd_vel(i) * (dq_des(i) - dq(i + offset)) +
m_dqErrIntegral(i), // ki_vel(i)*(m_dqDesIntegral(i)-q(i)),
ddq(i + offset), motorParameterKt_p(i), motorParameterKt_n(i),
motorParameterKf_p(i) * colFricCompPerc(i),
motorParameterKf_n(i) * colFricCompPerc(i), motorParameterKv_p(i),
motorParameterKv_n(i), motorParameterKa_p(i), motorParameterKa_n(i),
static_cast<unsigned int>(polySignDq(i)));
}
s = m_current_des;
return s;
}
DEFINE_SIGNAL_OUT_FUNCTION(torque_error_integral, dynamicgraph::Vector) {
const Eigen::VectorXd& tau = m_jointsTorquesSIN(iter);
const Eigen::VectorXd& tau_d = m_jointsTorquesDesiredSIN(iter);
const Eigen::VectorXd& err_int_sat = m_torque_integral_saturationSIN(iter);
const Eigen::VectorXd& ki = m_KiTorqueSIN(iter);
// compute torque error integral and saturate
m_tauErrIntegral += m_dt * ki.cwiseProduct(tau_d - tau);
for (int i = 0; i < (int)m_robot_util->m_nbJoints; i++) {
if (m_tauErrIntegral(i) > err_int_sat(i))
m_tauErrIntegral(i) = err_int_sat(i);
else if (m_tauErrIntegral(i) < -err_int_sat(i))
m_tauErrIntegral(i) = -err_int_sat(i);
}
s = m_tauErrIntegral;
return s;
}
DEFINE_SIGNAL_OUT_FUNCTION(smoothSignDq, dynamicgraph::Vector) {
const Eigen::VectorXd& dq = m_jointsVelocitiesSIN(iter);
const Eigen::VectorXd& polySignDq = m_polySignDqSIN(iter);
if (s.size() != (int)m_robot_util->m_nbJoints)
s.resize(m_robot_util->m_nbJoints);
for (int i = 0; i < (int)m_robot_util->m_nbJoints; i++)
s(i) = motorModel.smoothSign(dq[i], 0.1,
static_cast<unsigned int>(polySignDq[i]));
// TODO Use Eigen binaryexpr
return s;
}
void JointTorqueController::display(std::ostream& os) const {
os << "JointTorqueController " << getName() << ":\n";
try {
getProfiler().report_all(3, os);
} catch (ExceptionSignal e) {
}
}
} // namespace torque_control
} // namespace sot
} // namespace dynamicgraph