@@ -154,6 +154,8 @@ RTAPI_MP_ARRAY_STRING(names, MAX_CHAN,"at_pid names");
154154static int debug = 0 ; /* flag to export optional params */
155155RTAPI_MP_INT (debug , "enables optional params" );
156156
157+ #define NAME "AT_PID"
158+
157159#define AUTO_TUNER 1
158160#ifdef AUTO_TUNER
159161#include "rtapi_math.h"
@@ -296,7 +298,7 @@ int rtapi_app_main(void)
296298 /* test for number of channels */
297299 if ((howmany <= 0 ) || (howmany > MAX_CHAN )) {
298300 rtapi_print_msg (RTAPI_MSG_ERR ,
299- "AT_PID : ERROR: invalid number of channels: %d\n" , howmany );
301+ NAME " : ERROR: invalid number of channels: %d\n" , howmany );
300302 return -1 ;
301303 }
302304 /* have good config info, connect to the HAL */
@@ -308,7 +310,7 @@ int rtapi_app_main(void)
308310 /* allocate shared memory for pid loop data */
309311 pid_array = hal_malloc (howmany * sizeof (hal_pid_t ));
310312 if (pid_array == 0 ) {
311- rtapi_print_msg (RTAPI_MSG_ERR , "AT_PID : ERROR: hal_malloc() failed\n" );
313+ rtapi_print_msg (RTAPI_MSG_ERR , NAME " : ERROR: hal_malloc() failed\n" );
312314 hal_exit (comp_id );
313315 return -1 ;
314316 }
@@ -326,12 +328,12 @@ int rtapi_app_main(void)
326328
327329 if (retval != 0 ) {
328330 rtapi_print_msg (RTAPI_MSG_ERR ,
329- "AT_PID : ERROR: loop %d var export failed\n" , n );
331+ NAME " : ERROR: loop %d var export failed\n" , n );
330332 hal_exit (comp_id );
331333 return -1 ;
332334 }
333335 }
334- rtapi_print_msg (RTAPI_MSG_INFO , "AT_PID : installed %d PID loops\n" ,
336+ rtapi_print_msg (RTAPI_MSG_INFO , NAME " : installed %d PID loops\n" ,
335337 howmany );
336338 hal_ready (comp_id );
337339 return 0 ;
@@ -444,22 +446,22 @@ Pid_AutoTune(hal_pid_t *pid, long period)
444446 if (pid -> cycleCount < * (pid -> tuneCycles ))
445447 break ;
446448
447- // Calculate PID.
449+ // Calculate PID using Relay (Åström-Hägglund) method
448450 * (pid -> ultimateGain ) = (4.0 * fabs (* (pid -> tuneEffort )))/(PI * pid -> avgAmplitude );
449451 * (pid -> ultimatePeriod ) = 2.0 * pid -> totalTime / * (pid -> tuneCycles );
450452 * (pid -> ff0gain ) = 0 ;
451453 * (pid -> ff2gain ) = 0 ;
452454
453455 if (* (pid -> tuneType ) == TYPE_PID ){
454- // PID.
456+ // insert ultimate gain and period in Ziegler-Nichols PID method
455457 * (pid -> pgain ) = 0.6 * * (pid -> ultimateGain );
456- * (pid -> igain ) = * (pid -> pgain ) / (* (pid -> ultimatePeriod ) / 2.0 );
457- * (pid -> dgain ) = * (pid -> pgain ) * ( * (pid -> ultimatePeriod ) / 8.0 );
458+ * (pid -> igain ) = 1.2 * * (pid -> ultimateGain ) / (* (pid -> ultimatePeriod ));
459+ * (pid -> dgain ) = ( 3.0 / 40.0 ) * * (pid -> ultimateGain ) * * (pid -> ultimatePeriod );
458460 * (pid -> ff1gain ) = 0 ;
459461 }else {
460- // PI FF1.
462+ // insert ultimate gain and period in Ziegler-Nichols PI method
461463 * (pid -> pgain ) = 0.45 * * (pid -> ultimateGain );
462- * (pid -> igain ) = * (pid -> pgain ) / (* (pid -> ultimatePeriod ) / 1.2 );
464+ * (pid -> igain ) = 0.54 * * (pid -> ultimateGain ) / (* (pid -> ultimatePeriod ));
463465 * (pid -> dgain ) = 0 ;
464466
465467 // Scaling must be set so PID output is in user units per second.
@@ -470,8 +472,8 @@ Pid_AutoTune(hal_pid_t *pid, long period)
470472
471473 case STATE_TUNE_ABORT :
472474 default :
473- // Force output to zero .
474- * pid -> output = 0 ;
475+ // Force output to bias .
476+ * pid -> output = * ( pid -> bias ) ;
475477
476478 // Abort any tuning cycle in progress.
477479 * pid -> pTuneStart = 0 ;
@@ -693,8 +695,8 @@ static int export_pid(hal_pid_t * addr, char * prefix)
693695 logging if msg_level is at INFO or ALL. So we save the current value
694696 of msg_level and restore it later. If you actually need to log this
695697 function's actions, change the second line below */
696- // msg = rtapi_get_msg_level();
697- // rtapi_set_msg_level(RTAPI_MSG_WARN);
698+ msg = rtapi_get_msg_level ();
699+ rtapi_set_msg_level (RTAPI_MSG_WARN );
698700
699701 /* export pins */
700702 retval = hal_pin_bit_newf (HAL_IN , & (addr -> enable ), comp_id ,
@@ -966,11 +968,11 @@ static int export_pid(hal_pid_t * addr, char * prefix)
966968 hal_export_funct (buf , calc_pid , addr , 1 , 0 , comp_id );
967969 if (retval != 0 ) {
968970 rtapi_print_msg (RTAPI_MSG_ERR ,
969- "AT_PID : ERROR: do_pid_calcs funct export failed\n" );
971+ NAME " : ERROR: do_pid_calcs funct export failed\n" );
970972 hal_exit (comp_id );
971973 return -1 ;
972974 }
973975 /* restore saved message level */
974- // rtapi_set_msg_level(msg);
976+ rtapi_set_msg_level (msg );
975977 return 0 ;
976978}
0 commit comments