Skip to content

Commit 4d6d172

Browse files
committed
pbio/servo: Make angle reset optional.
This adds a `reset_angle=True` keyword argument to the Motor class initializer. Setting it to false allows the encoder angle 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
1 parent de27858 commit 4d6d172

File tree

8 files changed

+24
-11
lines changed

8 files changed

+24
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
## [Unreleased]
77

8+
### Added
9+
- Added `reset_angle=False` keyword argument to `Motor()` class.
10+
This makes resetting the angle optional, allowing to maintain absolute
11+
positioning for robots with gears ([support#389]).
12+
813
### Fixed
914
- Fixed City hub not always powering off on shutdown ([support#385]).
1015

@@ -86,6 +91,7 @@ Prerelease changes are documented at [support#48].
8691
[support#361]: https://github.com/pybricks/support/issues/361
8792
[support#379]: https://github.com/pybricks/support/issues/379
8893
[support#385]: https://github.com/pybricks/support/issues/385
94+
[support#389]: https://github.com/pybricks/support/issues/389
8995

9096
[Unreleased]: https://github.com/pybricks/pybricks-micropython/compare/v3.1.0a2...HEAD
9197
[3.1.0a2]: https://github.com/pybricks/pybricks-micropython/compare/v3.0.0a1...v3.1.0a2

lib/pbio/include/pbio/servo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct _pbio_servo_t {
3838
pbio_log_t log;
3939
} pbio_servo_t;
4040

41-
pbio_error_t pbio_servo_setup(pbio_servo_t *srv, pbio_direction_t direction, fix16_t gear_ratio);
41+
pbio_error_t pbio_servo_setup(pbio_servo_t *srv, pbio_direction_t direction, fix16_t gear_ratio, bool reset_angle);
4242

4343
void pbio_servo_load_settings(pbio_control_settings_t *control_settings, const pbio_observer_settings_t **observer_settings, pbio_iodev_type_id_t id);
4444

lib/pbio/include/pbio/tacho.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef struct _pbio_tacho_t pbio_tacho_t;
1616

1717
#if PBIO_CONFIG_TACHO
1818

19-
pbio_error_t pbio_tacho_get(pbio_port_id_t port, pbio_tacho_t **tacho, pbio_direction_t direction, fix16_t gear_ratio);
19+
pbio_error_t pbio_tacho_get(pbio_port_id_t port, pbio_tacho_t **tacho, pbio_direction_t direction, fix16_t gear_ratio, bool reset_angle);
2020

2121
pbio_error_t pbio_tacho_get_count(pbio_tacho_t *tacho, int32_t *count);
2222
pbio_error_t pbio_tacho_get_angle(pbio_tacho_t *tacho, int32_t *angle);

lib/pbio/src/motor_process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void pbio_motor_process_reset(void) {
4747
pbio_servo_stop_force(srv);
4848

4949
// Run setup and set connected flag on success
50-
pbio_servo_set_connected(srv, pbio_servo_setup(srv, PBIO_DIRECTION_CLOCKWISE, fix16_one) == PBIO_SUCCESS);
50+
pbio_servo_set_connected(srv, pbio_servo_setup(srv, PBIO_DIRECTION_CLOCKWISE, fix16_one, false) == PBIO_SUCCESS);
5151
}
5252
}
5353

lib/pbio/src/servo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static pbio_error_t pbio_servo_observer_reset(pbio_servo_t *srv) {
3030
return PBIO_SUCCESS;
3131
}
3232

33-
pbio_error_t pbio_servo_setup(pbio_servo_t *srv, pbio_direction_t direction, fix16_t gear_ratio) {
33+
pbio_error_t pbio_servo_setup(pbio_servo_t *srv, pbio_direction_t direction, fix16_t gear_ratio, bool reset_angle) {
3434
pbio_error_t err;
3535

3636
// Return if this servo is already in use by higher level entity
@@ -39,7 +39,7 @@ pbio_error_t pbio_servo_setup(pbio_servo_t *srv, pbio_direction_t direction, fix
3939
}
4040

4141
// Get and reset tacho
42-
err = pbio_tacho_get(srv->port, &srv->tacho, direction, gear_ratio);
42+
err = pbio_tacho_get(srv->port, &srv->tacho, direction, gear_ratio, reset_angle);
4343
if (err != PBIO_SUCCESS) {
4444
return err;
4545
}

lib/pbio/src/tacho.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static pbio_error_t pbio_tacho_reset_count_to_abs(pbio_tacho_t *tacho, int32_t *
5656
return pbio_tacho_reset_count(tacho, *abs_count);
5757
}
5858

59-
static pbio_error_t pbio_tacho_setup(pbio_tacho_t *tacho, uint8_t counter_id, pbio_direction_t direction, fix16_t gear_ratio) {
59+
static pbio_error_t pbio_tacho_setup(pbio_tacho_t *tacho, uint8_t counter_id, pbio_direction_t direction, fix16_t gear_ratio, bool reset_angle) {
6060
// Assert that scaling factors are positive
6161
if (gear_ratio < 0) {
6262
return PBIO_ERROR_INVALID_ARG;
@@ -73,6 +73,11 @@ static pbio_error_t pbio_tacho_setup(pbio_tacho_t *tacho, uint8_t counter_id, pb
7373
return err;
7474
}
7575

76+
// If there's no need to reset the angle, we are done here.
77+
if (!reset_angle) {
78+
return PBIO_SUCCESS;
79+
}
80+
7681
// Reset count to absolute value if supported
7782
int32_t abs_count;
7883
err = pbio_tacho_reset_count_to_abs(tacho, &abs_count);
@@ -83,7 +88,7 @@ static pbio_error_t pbio_tacho_setup(pbio_tacho_t *tacho, uint8_t counter_id, pb
8388
return err;
8489
}
8590

86-
pbio_error_t pbio_tacho_get(pbio_port_id_t port, pbio_tacho_t **tacho, pbio_direction_t direction, fix16_t gear_ratio) {
91+
pbio_error_t pbio_tacho_get(pbio_port_id_t port, pbio_tacho_t **tacho, pbio_direction_t direction, fix16_t gear_ratio, bool reset_angle) {
8792
// Validate port
8893
if (port < PBDRV_CONFIG_FIRST_MOTOR_PORT || port > PBDRV_CONFIG_LAST_MOTOR_PORT) {
8994
return PBIO_ERROR_INVALID_PORT;
@@ -96,7 +101,7 @@ pbio_error_t pbio_tacho_get(pbio_port_id_t port, pbio_tacho_t **tacho, pbio_dire
96101
uint8_t counter_id = port - PBDRV_CONFIG_FIRST_MOTOR_PORT;
97102

98103
// Initialize and set up tacho properties
99-
return pbio_tacho_setup(*tacho, counter_id, direction, gear_ratio);
104+
return pbio_tacho_setup(*tacho, counter_id, direction, gear_ratio, reset_angle);
100105
}
101106

102107
pbio_error_t pbio_tacho_get_count(pbio_tacho_t *tacho, int32_t *count) {

lib/pbio/test/src/servo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static PT_THREAD(test_servo_run_func(struct pt *pt, const char *name, pbio_error
8989
tt_want(process_is_running(&pbio_motor_process));
9090

9191
tt_uint_op(pbio_motor_process_get_servo(PBIO_PORT_ID_A, &servo), ==, PBIO_SUCCESS);
92-
tt_uint_op(pbio_servo_setup(servo, PBIO_DIRECTION_CLOCKWISE, F16C(1, 0)), ==, PBIO_SUCCESS);
92+
tt_uint_op(pbio_servo_setup(servo, PBIO_DIRECTION_CLOCKWISE, F16C(1, 0), true), ==, PBIO_SUCCESS);
9393
pbio_servo_set_connected(servo, true);
9494

9595
// only logging one row since we read it after every iteration

pybricks/common/pb_type_motor.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ STATIC mp_obj_t common_Motor_make_new(const mp_obj_type_t *type, size_t n_args,
3434
PB_PARSE_ARGS_CLASS(n_args, n_kw, args,
3535
PB_ARG_REQUIRED(port),
3636
PB_ARG_DEFAULT_OBJ(positive_direction, pb_Direction_CLOCKWISE_obj),
37-
PB_ARG_DEFAULT_NONE(gears));
37+
PB_ARG_DEFAULT_NONE(gears),
38+
PB_ARG_DEFAULT_TRUE(reset_angle));
3839

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

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

8486
// Set up servo
85-
while ((err = pbio_servo_setup(srv, positive_direction, gear_ratio)) == PBIO_ERROR_AGAIN) {
87+
while ((err = pbio_servo_setup(srv, positive_direction, gear_ratio, reset_angle)) == PBIO_ERROR_AGAIN) {
8688
mp_hal_delay_ms(1000);
8789
}
8890
pb_assert(err);

0 commit comments

Comments
 (0)