Skip to content

Commit 5ab82ef

Browse files
committed
pbio/servo: restore duty limit
This was temporarily removed during revisions of the drivers, so restore it.
1 parent 7aeadf0 commit 5ab82ef

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

lib/pbio/src/dcmotor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pbio_error_t pbio_dcmotor_set_duty_cycle_sys(pbio_dcmotor_t *dcmotor, int32_t du
101101
}
102102

103103
pbio_error_t pbio_dcmotor_set_duty_cycle_usr(pbio_dcmotor_t *dcmotor, int32_t duty_steps) {
104-
pbio_error_t err = pbio_dcmotor_set_duty_cycle_sys(dcmotor, duty_steps * PBDRV_MAX_DUTY / PBIO_DUTY_USER_STEPS);
104+
pbio_error_t err = pbio_dcmotor_set_duty_cycle_sys(dcmotor, duty_steps);
105105
if (err != PBIO_SUCCESS) {
106106
return err;
107107
}

lib/pbio/src/servo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ pbio_error_t pbio_servo_control_update(pbio_servo_t *srv) {
187187

188188
// Convert torques to duty cycle based on model
189189
duty_cycle = pbio_observer_torque_to_duty(&srv->observer, feedback_torque + feedforward_torque, battery_voltage);
190+
duty_cycle = max(-srv->control.settings.max_duty, min(duty_cycle, srv->control.settings.max_duty));
190191

191192
// Actutate the servo
192193
err = pbio_servo_actuate(srv, actuation, duty_cycle);
@@ -220,6 +221,9 @@ pbio_error_t pbio_servo_set_duty_cycle(pbio_servo_t *srv, int32_t duty_steps) {
220221
return PBIO_ERROR_INVALID_OP;
221222
}
222223

224+
// Limit to maximum configured value
225+
duty_steps = max(-srv->control.settings.max_duty, min(duty_steps, srv->control.settings.max_duty));
226+
223227
pbio_control_stop(&srv->control);
224228
return pbio_dcmotor_set_duty_cycle_usr(srv->dcmotor, duty_steps);
225229
}

pybricks/common/pb_type_dcmotor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ STATIC mp_obj_t common_DCMotor_duty(size_t n_args, const mp_obj_t *pos_args, mp_
7171

7272
if (is_servo) {
7373
common_Motor_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
74-
pb_assert(pbio_servo_set_duty_cycle(self->srv, duty));
74+
pb_assert(pbio_servo_set_duty_cycle(self->srv, duty * 100));
7575
} else {
7676
common_DCMotor_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
77-
pb_assert(pbio_dcmotor_set_duty_cycle_usr(self->dcmotor, duty));
77+
pb_assert(pbio_dcmotor_set_duty_cycle_usr(self->dcmotor, duty * 100));
7878
}
7979

8080
return mp_const_none;

0 commit comments

Comments
 (0)