Skip to content

Commit

Permalink
pbio/servo: restore duty limit
Browse files Browse the repository at this point in the history
This was temporarily removed during revisions of the drivers, so restore it.
  • Loading branch information
laurensvalk committed Jan 6, 2021
1 parent 7aeadf0 commit 5ab82ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/pbio/src/dcmotor.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pbio_error_t pbio_dcmotor_set_duty_cycle_sys(pbio_dcmotor_t *dcmotor, int32_t du
}

pbio_error_t pbio_dcmotor_set_duty_cycle_usr(pbio_dcmotor_t *dcmotor, int32_t duty_steps) {
pbio_error_t err = pbio_dcmotor_set_duty_cycle_sys(dcmotor, duty_steps * PBDRV_MAX_DUTY / PBIO_DUTY_USER_STEPS);
pbio_error_t err = pbio_dcmotor_set_duty_cycle_sys(dcmotor, duty_steps);
if (err != PBIO_SUCCESS) {
return err;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/pbio/src/servo.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pbio_error_t pbio_servo_control_update(pbio_servo_t *srv) {

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

// Actutate the servo
err = pbio_servo_actuate(srv, actuation, duty_cycle);
Expand Down Expand Up @@ -220,6 +221,9 @@ pbio_error_t pbio_servo_set_duty_cycle(pbio_servo_t *srv, int32_t duty_steps) {
return PBIO_ERROR_INVALID_OP;
}

// Limit to maximum configured value
duty_steps = max(-srv->control.settings.max_duty, min(duty_steps, srv->control.settings.max_duty));

pbio_control_stop(&srv->control);
return pbio_dcmotor_set_duty_cycle_usr(srv->dcmotor, duty_steps);
}
Expand Down
4 changes: 2 additions & 2 deletions pybricks/common/pb_type_dcmotor.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ STATIC mp_obj_t common_DCMotor_duty(size_t n_args, const mp_obj_t *pos_args, mp_

if (is_servo) {
common_Motor_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
pb_assert(pbio_servo_set_duty_cycle(self->srv, duty));
pb_assert(pbio_servo_set_duty_cycle(self->srv, duty * 100));
} else {
common_DCMotor_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
pb_assert(pbio_dcmotor_set_duty_cycle_usr(self->dcmotor, duty));
pb_assert(pbio_dcmotor_set_duty_cycle_usr(self->dcmotor, duty * 100));
}

return mp_const_none;
Expand Down

0 comments on commit 5ab82ef

Please sign in to comment.