Skip to content

Commit

Permalink
pybricks.common.Motor: Make angle reset optional.
Browse files Browse the repository at this point in the history
This adds a reset_angle=True keyword argument to the Motor class initialization.

Setting it to false allows the encoder offset to be preserved between consecutive scripts. This way, you'd need to reset robot mechanisms only once instead of every time you run a program.

pybricks/support#389
  • Loading branch information
laurensvalk committed Jul 13, 2021
1 parent 9bed194 commit 1dbef57
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pybricks/common/pb_type_motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ STATIC mp_obj_t common_Motor_make_new(const mp_obj_type_t *type, size_t n_args,
PB_PARSE_ARGS_CLASS(n_args, n_kw, args,
PB_ARG_REQUIRED(port),
PB_ARG_DEFAULT_OBJ(positive_direction, pb_Direction_CLOCKWISE_obj),
PB_ARG_DEFAULT_NONE(gears));
PB_ARG_DEFAULT_NONE(gears),
PB_ARG_DEFAULT_TRUE(reset_angle));

// Configure the motor with the selected arguments at pbio level
pbio_port_id_t port = pb_type_enum_get_value(port_in, &pb_enum_type_Port);
pbio_direction_t positive_direction = pb_type_enum_get_value(positive_direction_in, &pb_enum_type_Direction);
bool reset_angle = mp_obj_is_true(reset_angle_in);
pbio_error_t err;
pbio_servo_t *srv;

Expand Down Expand Up @@ -82,7 +84,7 @@ STATIC mp_obj_t common_Motor_make_new(const mp_obj_type_t *type, size_t n_args,
pb_assert(pbio_motor_process_get_servo(port, &srv));

// Set up servo
while ((err = pbio_servo_setup(srv, positive_direction, gear_ratio)) == PBIO_ERROR_AGAIN) {
while ((err = pbio_servo_setup(srv, positive_direction, gear_ratio, reset_angle)) == PBIO_ERROR_AGAIN) {
mp_hal_delay_ms(1000);
}
pb_assert(err);
Expand Down

0 comments on commit 1dbef57

Please sign in to comment.