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