Skip to content

Commit a34d7cb

Browse files
committed
pybricks.robotics: Add GyroDriveBase.
This is a variant that uses the gyro for steering. This makes it easier to configure compared to specifying even more kwargs. This also lets you toggle between drive bahaviors by just changing the class name in your script, so you can determine what works best for a robot. Since this can be completely disabled on hubs without a gyro, the reduced number of parameters also reduces move hub build size a bit.
1 parent 99351ec commit a34d7cb

File tree

10 files changed

+49
-25
lines changed

10 files changed

+49
-25
lines changed

CHANGELOG.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
not calibrate properly ([support#1026]).
1010
- Fixed discrepancy in heading value across hubs by accounting for sampling
1111
time ([support#1022]).
12-
13-
[support#1026]: https://github.com/pybricks/support/issues/1022
14-
[support#1026]: https://github.com/pybricks/support/issues/1026
12+
- Fixed iterator for `Matrix` objects giving bad values.
13+
- Fixed Bluetooth sometimes locking up on Technic/City hubs ([support#567]).
1514

1615
### Added
1716
- Added `pybricks.geometry.cross(a, b)` to get a vector cross product.
@@ -21,16 +20,23 @@
2120
required, or even undesired.
2221
- Added `hub.imu.ready()` to check that the IMU has been calibrated and is
2322
ready for use.
23+
- Added `GyroDriveBase` class to control drivebase steering with the gyro.
2424
- Added optional `window` parameter to `Motor.speed` to specify the
2525
differentiation window size that determines the average speed. This lets the
2626
user choose smaller values to get a more responsive (but noisier) or higher
2727
values to get a smoother (but more delayed) speed signal.
2828

29+
### Removed
30+
- Removed `positive_direction` from `DriveBase` initializer. This was
31+
temporarily added in the previous beta release to facilitate gyro support,
32+
but made it more complicated than needed ([support#992]).
33+
2934
### Fixed
30-
- Fixed iterator for `Matrix` objects giving bad values.
31-
- Fixed Bluetooth sometimes locking up on Technic/City hubs ([support#567]).
3235

3336
[support#567]: https://github.com/pybricks/support/issues/567
37+
[support#992]: https://github.com/pybricks/support/issues/992
38+
[support#1022]: https://github.com/pybricks/support/issues/1022
39+
[support#1026]: https://github.com/pybricks/support/issues/1026
3440

3541
## [3.3.0b3] - 2023-03-28
3642

bricks/cityhub/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define PYBRICKS_PY_PARAMETERS_ICON (0)
3737
#define PYBRICKS_PY_PUPDEVICES (1)
3838
#define PYBRICKS_PY_ROBOTICS (1)
39+
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO (0)
3940
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (0)
4041
#define PYBRICKS_PY_TOOLS (1)
4142

bricks/essentialhub/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define PYBRICKS_PY_PARAMETERS_ICON (0)
3838
#define PYBRICKS_PY_PUPDEVICES (1)
3939
#define PYBRICKS_PY_ROBOTICS (1)
40+
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO (1)
4041
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (1)
4142
#define PYBRICKS_PY_TOOLS (1)
4243

bricks/movehub/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define PYBRICKS_PY_PARAMETERS_ICON (0)
3333
#define PYBRICKS_PY_PUPDEVICES (1)
3434
#define PYBRICKS_PY_ROBOTICS (1)
35+
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO (0)
3536
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (0)
3637
#define PYBRICKS_PY_TOOLS (1)
3738

bricks/nxt/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define PYBRICKS_PY_PARAMETERS_ICON (0)
3939
#define PYBRICKS_PY_PUPDEVICES (0)
4040
#define PYBRICKS_PY_ROBOTICS (1)
41+
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO (0)
4142
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (0)
4243
#define PYBRICKS_PY_TOOLS (1)
4344

bricks/primehub/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define PYBRICKS_PY_PARAMETERS_ICON (1)
3939
#define PYBRICKS_PY_PUPDEVICES (1)
4040
#define PYBRICKS_PY_ROBOTICS (1)
41+
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO (1)
4142
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (1)
4243
#define PYBRICKS_PY_TOOLS (1)
4344

bricks/technichub/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define PYBRICKS_PY_PARAMETERS_ICON (0)
3737
#define PYBRICKS_PY_PUPDEVICES (1)
3838
#define PYBRICKS_PY_ROBOTICS (1)
39+
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO (1)
3940
#define PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE (0)
4041
#define PYBRICKS_PY_TOOLS (1)
4142

pybricks/robotics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
#include "pybricks/util_mp/pb_obj_helper.h"
1616

1717
extern const mp_obj_type_t pb_type_drivebase;
18+
19+
#if PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO
20+
extern const mp_obj_type_t pb_type_gyrodrivebase;
21+
#endif
22+
23+
#if PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE
1824
extern const mp_obj_type_t pb_type_spikebase;
25+
#endif
26+
1927

2028
#endif // PYBRICKS_PY_ROBOTICS
2129

pybricks/robotics/pb_module_robotics.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ STATIC const mp_rom_map_elem_t robotics_globals_table[] = {
1111
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_robotics) },
1212
#if PYBRICKS_PY_COMMON_MOTORS
1313
{ MP_ROM_QSTR(MP_QSTR_DriveBase), MP_ROM_PTR(&pb_type_drivebase) },
14-
#if (PYBRICKS_HUB_PRIMEHUB || PYBRICKS_HUB_ESSENTIALHUB)
14+
#if PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO
15+
{ MP_ROM_QSTR(MP_QSTR_GyroDriveBase), MP_ROM_PTR(&pb_type_gyrodrivebase) },
16+
#endif
17+
#if PYBRICKS_PY_ROBOTICS_DRIVEBASE_SPIKE
1518
{ MP_ROM_QSTR(MP_QSTR_SpikeBase), MP_ROM_PTR(&pb_type_spikebase) },
1619
#endif
1720
#endif

pybricks/robotics/pb_type_drivebase.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <pybricks/common.h>
1717
#include <pybricks/parameters.h>
18+
#include <pybricks/robotics.h>
1819

1920
#include <pybricks/util_mp/pb_kwarg_helper.h>
2021
#include <pybricks/util_mp/pb_obj_helper.h>
@@ -53,36 +54,22 @@ STATIC mp_obj_t pb_type_DriveBase_make_new(const mp_obj_type_t *type, size_t n_a
5354
PB_ARG_REQUIRED(left_motor),
5455
PB_ARG_REQUIRED(right_motor),
5556
PB_ARG_REQUIRED(wheel_diameter),
56-
PB_ARG_REQUIRED(axle_track),
57-
PB_ARG_DEFAULT_OBJ(positive_direction, pb_Direction_CLOCKWISE_obj),
58-
PB_ARG_DEFAULT_FALSE(use_gyro));
57+
PB_ARG_REQUIRED(axle_track));
5958

6059
pb_type_DriveBase_obj_t *self = mp_obj_malloc(pb_type_DriveBase_obj_t, type);
6160

62-
// REVISIT: Allow angle getter callable on any platform.
63-
bool use_gyro = mp_obj_is_true(use_gyro_in);
64-
#if !PBIO_CONFIG_IMU
65-
if (use_gyro) {
66-
pb_assert(PBIO_ERROR_NOT_SUPPORTED);
67-
}
68-
#endif
69-
pbio_direction_t positive_direction = pb_type_enum_get_value(positive_direction_in, &pb_enum_type_Direction);
70-
// REVISIT: Gyro is currently only compatible with counterclockwise drivebase.
71-
if (use_gyro && positive_direction != PBIO_DIRECTION_COUNTERCLOCKWISE) {
72-
pb_assert(PBIO_ERROR_INVALID_ARG);
73-
}
74-
7561
// Pointers to servos
7662
pbio_servo_t *srv_left = ((common_Motor_obj_t *)pb_obj_get_base_class_obj(left_motor_in, &pb_type_Motor))->srv;
7763
pbio_servo_t *srv_right = ((common_Motor_obj_t *)pb_obj_get_base_class_obj(right_motor_in, &pb_type_Motor))->srv;
7864

7965
// Create drivebase
8066
pb_assert(pbio_drivebase_get_drivebase(&self->db,
81-
positive_direction == PBIO_DIRECTION_CLOCKWISE ? srv_left : srv_right,
82-
positive_direction == PBIO_DIRECTION_CLOCKWISE ? srv_right : srv_left,
67+
srv_left,
68+
srv_right,
8369
pb_obj_get_scaled_int(wheel_diameter_in, 1000),
8470
pb_obj_get_scaled_int(axle_track_in, 1000),
85-
use_gyro));
71+
// Use gyro if creating instance of GyroDriveBase.
72+
type != &pb_type_drivebase));
8673

8774
#if PYBRICKS_PY_COMMON_CONTROL
8875
// Create instances of the Control class
@@ -339,4 +326,18 @@ const mp_obj_type_t pb_type_drivebase = {
339326
.locals_dict = (mp_obj_dict_t *)&pb_type_DriveBase_locals_dict,
340327
};
341328

329+
#if PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO
330+
// type(pybricks.robotics.GyroDriveBase)
331+
const mp_obj_type_t pb_type_gyrodrivebase = {
332+
{ &mp_type_type },
333+
.name = MP_QSTR_GyroDriveBase,
334+
.make_new = pb_type_DriveBase_make_new,
335+
#if PYBRICKS_PY_COMMON_CONTROL
336+
.attr = pb_attribute_handler,
337+
.protocol = pb_type_DriveBase_attr_dict,
338+
#endif
339+
.locals_dict = (mp_obj_dict_t *)&pb_type_DriveBase_locals_dict,
340+
};
341+
#endif // PYBRICKS_PY_ROBOTICS_DRIVEBASE_GYRO
342+
342343
#endif // PYBRICKS_PY_ROBOTICS && PYBRICKS_PY_COMMON_MOTORS

0 commit comments

Comments
 (0)