@@ -175,8 +175,8 @@ void Pid::reset()
175
175
{
176
176
p_error_last_ = 0.0 ;
177
177
p_error_ = 0.0 ;
178
+ i_error_ = 0.0 ;
178
179
d_error_ = 0.0 ;
179
- i_term_ = 0.0 ;
180
180
cmd_ = 0.0 ;
181
181
}
182
182
@@ -269,7 +269,7 @@ double Pid::computeCommand(double error, ros::Duration dt)
269
269
// Get the gain parameters from the realtime buffer
270
270
Gains gains = *gains_buffer_.readFromRT ();
271
271
272
- double p_term, d_term;
272
+ double p_term, d_term, i_term ;
273
273
p_error_ = error; // this is error = target - state
274
274
275
275
if (dt == ros::Duration (0.0 ) || std::isnan (error) || std::isinf (error))
@@ -278,11 +278,14 @@ double Pid::computeCommand(double error, ros::Duration dt)
278
278
// Calculate proportional contribution to command
279
279
p_term = gains.p_gain_ * p_error_;
280
280
281
- // Calculate integral contribution to command
282
- i_term_ = i_term_ + gains.i_gain_ * dt.toSec () * p_error_;
281
+ // Calculate the integral of the position error
282
+ i_error_ += dt.toSec () * p_error_;
283
+
284
+ // Calculate integral contribution to command
285
+ i_term = gains.i_gain_ * i_error_;
283
286
284
- // Limit i_term_ so that the limit is meaningful in the output
285
- i_term_ = std::max ( gains.i_min_ , std::min ( i_term_ , gains.i_max_ ) );
287
+ // Limit i_term so that the limit is meaningful in the output
288
+ i_term = std::max ( gains.i_min_ , std::min ( i_term , gains.i_max_ ) );
286
289
287
290
// Calculate the derivative error
288
291
if (dt.toSec () > 0.0 )
@@ -292,7 +295,9 @@ double Pid::computeCommand(double error, ros::Duration dt)
292
295
}
293
296
// Calculate derivative contribution to command
294
297
d_term = gains.d_gain_ * d_error_;
295
- cmd_ = p_term + i_term_ + d_term;
298
+
299
+ // Compute the command
300
+ cmd_ = p_term + i_term + d_term;
296
301
297
302
return cmd_;
298
303
}
@@ -302,7 +307,7 @@ double Pid::updatePid(double error, ros::Duration dt)
302
307
// Get the gain parameters from the realtime buffer
303
308
Gains gains = *gains_buffer_.readFromRT ();
304
309
305
- double p_term, d_term;
310
+ double p_term, d_term, i_term ;
306
311
p_error_ = error; // this is pError = pState-pTarget
307
312
308
313
if (dt == ros::Duration (0.0 ) || std::isnan (error) || std::isinf (error))
@@ -311,11 +316,14 @@ double Pid::updatePid(double error, ros::Duration dt)
311
316
// Calculate proportional contribution to command
312
317
p_term = gains.p_gain_ * p_error_;
313
318
314
- // Calculate integral contribution to command
315
- i_term_ = i_term_ + gains.i_gain_ * dt.toSec () * p_error_;
319
+ // Calculate the integral of the position error
320
+ i_error_ += dt.toSec () * p_error_;
321
+
322
+ // Calculate integral contribution to command
323
+ i_term = gains.i_gain_ * i_error_;
316
324
317
- // Limit i_term_ so that the limit is meaningful in the output
318
- i_term_ = std::max ( gains.i_min_ , std::min ( i_term_ , gains.i_max_ ) );
325
+ // Limit i_term so that the limit is meaningful in the output
326
+ i_term = std::max ( gains.i_min_ , std::min ( i_term , gains.i_max_ ) );
319
327
320
328
// Calculate the derivative error
321
329
if (dt.toSec () > 0.0 )
@@ -325,7 +333,9 @@ double Pid::updatePid(double error, ros::Duration dt)
325
333
}
326
334
// Calculate derivative contribution to command
327
335
d_term = gains.d_gain_ * d_error_;
328
- cmd_ = - p_term - i_term_ - d_term;
336
+
337
+ // Compute the command
338
+ cmd_ = - p_term - i_term - d_term;
329
339
330
340
return cmd_;
331
341
}
@@ -335,7 +345,7 @@ double Pid::computeCommand(double error, double error_dot, ros::Duration dt)
335
345
// Get the gain parameters from the realtime buffer
336
346
Gains gains = *gains_buffer_.readFromRT ();
337
347
338
- double p_term, d_term;
348
+ double p_term, d_term, i_term ;
339
349
p_error_ = error; // this is error = target - state
340
350
d_error_ = error_dot;
341
351
@@ -346,15 +356,20 @@ double Pid::computeCommand(double error, double error_dot, ros::Duration dt)
346
356
// Calculate proportional contribution to command
347
357
p_term = gains.p_gain_ * p_error_;
348
358
349
- // Calculate integral contribution to command
350
- i_term_ = i_term_ + gains.i_gain_ * dt.toSec () * p_error_;
359
+ // Calculate the integral of the position error
360
+ i_error_ += dt.toSec () * p_error_;
361
+
362
+ // Calculate integral contribution to command
363
+ i_term = gains.i_gain_ * i_error_;
351
364
352
- // Limit i_term_ so that the limit is meaningful in the output
353
- i_term_ = std::max ( gains.i_min_ , std::min ( i_term_ , gains.i_max_ ) );
365
+ // Limit i_term so that the limit is meaningful in the output
366
+ i_term = std::max ( gains.i_min_ , std::min ( i_term , gains.i_max_ ) );
354
367
355
368
// Calculate derivative contribution to command
356
369
d_term = gains.d_gain_ * d_error_;
357
- cmd_ = p_term + i_term_ + d_term;
370
+
371
+ // Compute the command
372
+ cmd_ = - p_term - i_term - d_term;
358
373
359
374
return cmd_;
360
375
}
@@ -364,7 +379,7 @@ double Pid::updatePid(double error, double error_dot, ros::Duration dt)
364
379
// Get the gain parameters from the realtime buffer
365
380
Gains gains = *gains_buffer_.readFromRT ();
366
381
367
- double p_term, d_term;
382
+ double p_term, d_term, i_term ;
368
383
p_error_ = error; // this is pError = pState-pTarget
369
384
d_error_ = error_dot;
370
385
@@ -375,15 +390,20 @@ double Pid::updatePid(double error, double error_dot, ros::Duration dt)
375
390
// Calculate proportional contribution to command
376
391
p_term = gains.p_gain_ * p_error_;
377
392
378
- // Calculate integral contribution to command
379
- i_term_ = i_term_ + gains.i_gain_ * dt.toSec () * p_error_;
393
+ // Calculate the integral of the position error
394
+ i_error_ += dt.toSec () * p_error_;
395
+
396
+ // Calculate integral contribution to command
397
+ i_term = gains.i_gain_ * i_error_;
380
398
381
- // Limit i_term_ so that the limit is meaningful in the output
382
- i_term_ = std::max ( gains.i_min_ , std::min ( i_term_ , gains.i_max_ ) );
399
+ // Limit i_term so that the limit is meaningful in the output
400
+ i_term = std::max ( gains.i_min_ , std::min ( i_term , gains.i_max_ ) );
383
401
384
402
// Calculate derivative contribution to command
385
403
d_term = gains.d_gain_ * d_error_;
386
- cmd_ = - p_term - i_term_ - d_term;
404
+
405
+ // Compute the command
406
+ cmd_ = - p_term - i_term - d_term;
387
407
388
408
return cmd_;
389
409
}
@@ -404,7 +424,7 @@ void Pid::getCurrentPIDErrors(double *pe, double *ie, double *de)
404
424
Gains gains = *gains_buffer_.readFromRT ();
405
425
406
426
*pe = p_error_;
407
- *ie = gains. i_gain_ ? i_term_/gains. i_gain_ : 0.0 ;
427
+ *ie = i_error_ ;
408
428
*de = d_error_;
409
429
}
410
430
@@ -420,8 +440,8 @@ void Pid::printValues()
420
440
<< " I_Min: " << gains.i_min_ << " \n "
421
441
<< " P_Error_Last: " << p_error_last_ << " \n "
422
442
<< " P_Error: " << p_error_ << " \n "
443
+ << " I_Error: " << i_error_ << " \n "
423
444
<< " D_Error: " << d_error_ << " \n "
424
- << " I_Term: " << i_term_ << " \n "
425
445
<< " Command: " << cmd_
426
446
);
427
447
0 commit comments