Torque.cpp
Go to the documentation of this file.
1#include "Torque.h"
2
3
4// armarx
6
8{
9
11 pid(),
12 targetFilter(100),
13 configData(
15 {
16 pid.proportional_gain = torqueConfigData->pid_proportional_gain;
17 pid.integral_gain = torqueConfigData->pid_integral_gain;
18 pid.derivative_gain = torqueConfigData->pid_derivative_gain;
19 pid.windup_guard = torqueConfigData->pid_windup_guard;
20 pid.max_value = torqueConfigData->pid_max_value;
21 pid.min_value = torqueConfigData->pid_min_value;
22 pid.dis = torqueConfigData->pid_dis;
23 pid.Kd = torqueConfigData->Kd;
24 pid.inertia = torqueConfigData->inertia;
25 pid.scalePI = torqueConfigData->scalePI;
26 pid.scaleTorque = torqueConfigData->scaleTorque;
27 pid.maxTorque = torqueConfigData->maxTorque;
28 pid.actuatorType = torqueConfigData->actuatorType;
29 filter.setImpulseResponse({// lowpass FIR-filter
30 0.1744f, 0.0205f, 0.0215f, 0.0225f, 0.0235f, 0.0244f, 0.0252f,
31 0.0259f, 0.0265f, 0.0270f, 0.0274f, 0.0276f, 0.0278f, 0.0282f,
32 0.0282f, 0.0278f, 0.0276f, 0.0274f, 0.0270f, 0.0265f, 0.0259f,
33 0.0252f, 0.0244f, 0.0235f, 0.0225f, 0.0215f, 0.0205f, 0.1744f});
34 }
35
37 {
38 zeroize();
39 }
40
41 void
43 {
44 prev_error = 0;
46 int_error = 0;
47 phi = 0;
48 old_pos = 0;
49 abs_vel = 0;
50 prev_angle = 0;
51 acc = 0;
52 }
53
54 void
55 TorquePID::findAcc(double Vel, double dt)
56 {
57 acc = (Vel - prev_vel) / dt;
58 prev_vel = Vel;
59 }
60
61 void
62 TorquePID::update(double curr_error, double dt)
63 {
64 // ==================================================== Digital Controller
65 // PID-Controller
66 double Kp = proportional_gain;
67 double Ki = integral_gain;
68 double Kd = derivative_gain;
69 double er0 = (Kp + (dt * Ki) + (Kd / dt)) * curr_error;
70 double er1 = (-Kp - ((2 * Kd) / dt)) * prev_error;
71 double er2 = ((Kd) / dt) * prev_prev_error;
72 double delta = (er0 + er1 + er2);
73 control = control + delta;
74 // Limit the output of the Controller/Current
75 if (control < (min_value))
76 {
78 }
79 else if (control > max_value)
80 {
82 }
84 prev_error = curr_error;
85 }
86
89 bool limitless,
90 float jointLimitLow,
91 float jointLimitHigh)
92 {
94 configData.pid_proportional_gain = config.getFloat("pid_proportional_gain");
95 configData.pid_integral_gain = config.getFloat("pid_integral_gain");
96 configData.pid_derivative_gain = config.getFloat("pid_derivative_gain");
97 configData.pid_windup_guard = config.getFloat("pid_windup_guard");
98 configData.pid_max_value = config.getFloat("pid_max_value");
99 configData.pid_min_value = config.getFloat("pid_min_value");
100 configData.pid_dis = config.getFloat("pid_dis");
101 configData.Kd = config.getFloat("Kd");
102 configData.inertia = config.getFloat("inertia");
103 configData.scalePI = config.getFloat("scalePI");
104 configData.scaleTorque = config.getFloat("scaleTorque");
105 configData.maxTorque = config.getFloat("maxTorque");
106 configData.actuatorType = config.getInt("actuatorType");
107 auto v = config.getFloatList("firFilterImpulseResponse");
108 for (auto& elem : v)
109 {
110 configData.firFilterImpulseResponse.push_back(elem);
111 }
112
113 configData.limitless = limitless;
114 configData.jointLimitLow = jointLimitLow;
115 configData.jointLimitHigh = jointLimitHigh;
116 // configData.torqueToCurrent = config.getFloat("joint_lim_avoid_torque_to_current_factor");
117
118 return std::make_shared<TorqueControllerConfiguration>(configData);
119 }
120
124
125 float
126 TorqueController::update(const IceUtil::Time& timeSinceLastIteration,
127 const std::string& jointName,
128 float gravity,
129 float actualTorque,
130 float targetTorque,
131 float actualVelocity,
132 float actualPosition)
133 {
134 double Kd = pid.Kd; // define default damping constant
135 double scalePI = pid.scalePI; // define scale factor for I-Term of PID-Controller
136 double dt = timeSinceLastIteration.toSecondsDouble(); // sampling time of torque values
137 pid.proportional_gain = 0; // the value will be re-defined later
138 pid.integral_gain = 0; // the value will be re-defined later
139 float omega = 0.55; // max. rotational speed in [rad/sec]
140 float actualTorqueF =
141 filter.update(actualTorque); // torque value will be filtered with low pass FIR-Filter
142 Kd = (-gravity + targetTorque + actualTorqueF) /
143 omega; // calculation of the damping constant
144 if (Kd < 0) // need to be done for the backd-rivability
145 {
146 Kd = -Kd;
147 }
148 if ((pid.actuatorType == 1) && (Kd < 6)) // define min. damping for large aktuators
149 {
150 Kd = 6;
151 }
152 if (Kd > 80) // define max. damping for all aktuators
153 {
154 Kd = 80;
155 }
156 if (pid.actuatorType == 1) // define I-Term for large aktuators
157 {
158 pid.integral_gain =
159 (-0.003721 * std::pow(Kd, 2.0) + 0.43429 * Kd - 1.4408) * (scalePI * 0.45);
160 }
161 else
162 {
163 pid.integral_gain = 1.2 * scalePI; // define I-Term for midle and small aktuators
164 pid.proportional_gain = 0.03; // define P-Term for midle and small aktuators
165 }
166 if (pid.integral_gain < 0) // define min. I-Term for all aktuators
167 {
168 pid.integral_gain = 0.5;
169 }
170 pid.findAcc(actualVelocity, dt);
171 // double protectionTorque = calcJointLimitProtectionTorque(actualPosition);
172 // // TODO testing joint lim avoidance
173 // double torqueToCurrentFactor = configData->torqueToCurrent;
174 // // auto jointLimitProtectionCurrent =
175 // // calcJointLimitProtectionTorque(actualPosition) *
176 // // (torqueToCurrentFactor == 0.0 ? 0.05 : torqueToCurrentFactor) * 1000;
177 // // -- end testing
178
179 float setTorque =
180 (-gravity + targetTorque - Kd * actualVelocity); // calculate desired Torque
181
182
183 double CurrError =
184 setTorque +
185 actualTorqueF; // calculate error between desired and actual(measured and filtered) torque
186 // send error to PID-Controller
187 pid.update(CurrError, dt);
188 // The Controller calculate the required current. The current will be converted to Elmo current value
189 float current = -(pid.control) * 1000.0;
190 return current;
191 // TODO testing joint lim avoidance
192 // return current + jointLimitProtectionCurrent;
193 }
194
195 double
197 {
198 double pushbackMargin = 5.0 / 180. * M_PI;
199 double pushBackTorque = 100;
200
201 double jointLimitProtection = 0;
202 if (!configData->limitless)
203 {
204 double borderHigh = configData->jointLimitHigh - pushbackMargin;
205 double borderLow = configData->jointLimitLow + pushbackMargin;
206 double penetration = 0;
207 if (actualPosition > borderHigh)
208 {
209 penetration = actualPosition - borderHigh;
210 }
211 else if (actualPosition < borderLow)
212 {
213 penetration = actualPosition - borderLow;
214 }
215 jointLimitProtection = penetration / pushbackMargin * pushBackTorque;
216 }
217 jointLimitProtection = math::MathUtils::LimitTo(jointLimitProtection, pushBackTorque);
218 return jointLimitProtection;
219 }
220
223 {
224 return filter;
225 }
226
227} // namespace armarx::control::joint_controller
#define M_PI
Definition MathTools.h:17
constexpr T dt
The Config class is the base class of all specialized configurations that have a direct key -> value ...
Definition Config.h:94
float getFloat(const std::string name)
Get a Float typed Config attribute by name.
Definition Config.cpp:16
std::list< float > getFloatList(const std::string name)
Get a FloatList (std::list<float>) typed Config attribute by name.
Definition Config.cpp:58
std::int32_t getInt(const std::string name)
Get a Int (std::int32_t) typed Config attribute by name.
Definition Config.cpp:28
static TorqueControllerConfigurationPtr CreateTorqueConfigData(hardware_config::Config &config, bool limitless, float jointLimitLow, float jointLimitHigh)
Definition Torque.cpp:88
const armarx::control::rt_filters::FirFilter & getFilter() const
Definition Torque.cpp:222
float update(const IceUtil::Time &timeSinceLastIteration, const std::string &jointName, float gravity, float actualTorque, float targetTorque, float actualVelocity, float actualPosition)
Definition Torque.cpp:126
TorqueController(const TorqueControllerConfigurationPtr &torqueConfigData)
Definition Torque.cpp:10
double calcJointLimitProtectionTorque(float actualPosition)
Definition Torque.cpp:196
void update(double curr_error, double dt)
Definition Torque.cpp:62
void findAcc(double Vel, double dt)
Definition Torque.cpp:55
static double LimitTo(double value, double absThreshold)
Definition MathUtils.h:71
std::shared_ptr< class TorqueControllerConfiguration > TorqueControllerConfigurationPtr
Definition Torque.h:13
This file is part of ArmarX.