@@ -15,7 +15,7 @@ void pbio_control_update(pbio_control_t *ctl, int32_t time_now, int32_t count_no
1515 int32_t time_ref ;
1616 int32_t count_ref , count_ref_ext , count_err , count_feedback , count_err_integral , rate_err_integral ;
1717 int32_t rate_err , rate_feedback ;
18- int32_t duty , duty_due_to_proportional , duty_due_to_integral , duty_due_to_derivative ;
18+ int32_t torque , torque_due_to_proportional , torque_due_to_integral , torque_due_to_derivative ;
1919
2020 // Get the time at which we want to evaluate the reference position/velocities.
2121 // This compensates for any time we may have spent pausing when the motor was stalled.
@@ -43,28 +43,28 @@ void pbio_control_update(pbio_control_t *ctl, int32_t time_now, int32_t count_no
4343 }
4444
4545 // Corresponding PID control signal
46- duty_due_to_proportional = ctl -> settings .pid_kp * count_err ;
47- duty_due_to_derivative = ctl -> settings .pid_kd * rate_err ;
48- duty_due_to_integral = (ctl -> settings .pid_ki * (count_err_integral / US_PER_MS )) / MS_PER_SECOND ;
46+ torque_due_to_proportional = ctl -> settings .pid_kp * count_err ;
47+ torque_due_to_derivative = ctl -> settings .pid_kd * rate_err ;
48+ torque_due_to_integral = (ctl -> settings .pid_ki * (count_err_integral / US_PER_MS )) / MS_PER_SECOND ;
4949
50- // Total duty signal, capped by the actuation limit
51- duty = duty_due_to_proportional + duty_due_to_integral + duty_due_to_derivative ;
52- duty = max (- ctl -> settings .max_control , min (duty , ctl -> settings .max_control ));
50+ // Total torque signal, capped by the actuation limit
51+ torque = torque_due_to_proportional + torque_due_to_integral + torque_due_to_derivative ;
52+ torque = max (- ctl -> settings .max_torque , min (torque , ctl -> settings .max_torque ));
5353
5454 // This completes the computation of the control signal.
5555 // The next steps take care of handling windup, or triggering a stop if we are on target.
5656
57- // We want to stop building up further errors if we are at the proportional duty limit. So, we pause the trajectory
57+ // We want to stop building up further errors if we are at the proportional torque limit. So, we pause the trajectory
5858 // if we get at this limit. We wait a little longer though, to make sure it does not fall back to below the limit
5959 // within one sample, which we can predict using the current rate times the loop time, with a factor two tolerance.
60- int32_t max_windup_duty = ctl -> settings .max_control + (ctl -> settings .pid_kp * abs (rate_now ) * PBIO_CONTROL_LOOP_TIME_MS * 2 ) / MS_PER_SECOND ;
60+ int32_t max_windup_torque = ctl -> settings .max_torque + (ctl -> settings .pid_kp * abs (rate_now ) * PBIO_CONTROL_LOOP_TIME_MS * 2 ) / MS_PER_SECOND ;
6161
62- // Position anti-windup: pause trajectory or integration if falling behind despite using maximum duty
62+ // Position anti-windup: pause trajectory or integration if falling behind despite using maximum torque
6363
6464 // Position anti-windup in case of angle control (accumulated position error may not get too high)
6565 if (ctl -> type == PBIO_CONTROL_ANGLE ) {
66- if (abs (duty_due_to_proportional ) >= max_windup_duty && pbio_math_sign (duty_due_to_proportional ) == pbio_math_sign (rate_err )) {
67- // We are at the duty limit and we should prevent further position error integration.
66+ if (abs (torque_due_to_proportional ) >= max_windup_torque && pbio_math_sign (torque_due_to_proportional ) == pbio_math_sign (rate_err )) {
67+ // We are at the torque limit and we should prevent further position error integration.
6868 pbio_count_integrator_pause (& ctl -> count_integrator , time_now , count_now , count_ref );
6969 } else {
7070 // Not at the limit so continue integrating errors
@@ -73,8 +73,8 @@ void pbio_control_update(pbio_control_t *ctl, int32_t time_now, int32_t count_no
7373 }
7474 // Position anti-windup in case of timed speed control (speed integral may not get too high)
7575 else {
76- if (abs (duty_due_to_proportional ) >= max_windup_duty && pbio_math_sign (duty_due_to_proportional ) == pbio_math_sign (rate_err )) {
77- // We are at the duty limit and we should prevent further speed error integration.
76+ if (abs (torque_due_to_proportional ) >= max_windup_torque && pbio_math_sign (torque_due_to_proportional ) == pbio_math_sign (rate_err )) {
77+ // We are at the torque limit and we should prevent further speed error integration.
7878 pbio_rate_integrator_pause (& ctl -> rate_integrator , time_now , count_now , count_ref );
7979 } else {
8080 // Not at the limit so continue integrating errors
@@ -102,11 +102,11 @@ void pbio_control_update(pbio_control_t *ctl, int32_t time_now, int32_t count_no
102102
103103 // The new hold control does not take effect until the next iteration, so keep actuating for now.
104104 * actuation = PBIO_ACTUATION_DUTY ;
105- * control = duty ;
105+ * control = torque ;
106106 } else {
107- // The end point not reached, or we have to keep holding, so return the calculated duty for actuation
107+ // The end point not reached, or we have to keep holding, so return the calculated torque for actuation
108108 * actuation = PBIO_ACTUATION_DUTY ;
109- * control = duty ;
109+ * control = torque ;
110110 }
111111
112112 // Log control data
@@ -120,9 +120,9 @@ void pbio_control_update(pbio_control_t *ctl, int32_t time_now, int32_t count_no
120120 * rate_ref ,
121121 count_est ,
122122 rate_est ,
123- duty_due_to_proportional ,
124- duty_due_to_integral ,
125- duty_due_to_derivative ,
123+ torque_due_to_proportional ,
124+ torque_due_to_integral ,
125+ torque_due_to_derivative ,
126126 };
127127 pbio_logger_update (& ctl -> log , log_data );
128128}
@@ -326,17 +326,21 @@ int32_t pbio_control_user_to_counts(pbio_control_settings_t *s, int32_t user) {
326326 return pbio_math_mul_i32_fix16 (user , s -> counts_per_unit );
327327}
328328
329- void pbio_control_settings_get_limits (pbio_control_settings_t * s , int32_t * speed , int32_t * acceleration , int32_t * actuation ) {
329+ void pbio_control_settings_get_limits (pbio_control_settings_t * s , int32_t * speed , int32_t * acceleration , int32_t * duty , int32_t * torque ) {
330330 * speed = pbio_control_counts_to_user (s , s -> max_rate );
331331 * acceleration = pbio_control_counts_to_user (s , s -> abs_acceleration );
332+ * duty = s -> max_duty / 100 ;
333+ * torque = s -> max_torque ;
332334}
333335
334- pbio_error_t pbio_control_settings_set_limits (pbio_control_settings_t * s , int32_t speed , int32_t acceleration , int32_t actuation ) {
335- if (speed < 1 || acceleration < 1 || actuation < 1 ) {
336+ pbio_error_t pbio_control_settings_set_limits (pbio_control_settings_t * s , int32_t speed , int32_t acceleration , int32_t duty , int32_t torque ) {
337+ if (speed < 1 || acceleration < 1 || duty < 1 || torque < 1 || duty > 100 ) {
336338 return PBIO_ERROR_INVALID_ARG ;
337339 }
338340 s -> max_rate = pbio_control_user_to_counts (s , speed );
339341 s -> abs_acceleration = pbio_control_user_to_counts (s , acceleration );
342+ s -> max_duty = duty * 100 ;
343+ s -> max_torque = torque ;
340344 return PBIO_SUCCESS ;
341345}
342346
@@ -396,8 +400,8 @@ int32_t pbio_control_settings_get_max_integrator(pbio_control_settings_t *s) {
396400 if (s -> pid_ki <= 10 ) {
397401 return 1000000000 ;
398402 }
399- // Get the maximum integrator value for which ki*integrator does not exceed max_control
400- return ((s -> max_control * US_PER_MS ) / s -> pid_ki ) * MS_PER_SECOND ;
403+ // Get the maximum integrator value for which ki*integrator does not exceed max_torque
404+ return ((s -> max_torque * US_PER_MS ) / s -> pid_ki ) * MS_PER_SECOND ;
401405}
402406
403407int32_t pbio_control_get_ref_time (pbio_control_t * ctl , int32_t time_now ) {
0 commit comments