Skip to content

Commit b1fd671

Browse files
committed
pybricks.ev3devices.GyroSensor: Delay first mode switch.
It is possible for the user to start a program before the sensor is ready. This is correctly awaited for most sensors, but this sensor needs some extra care.
1 parent adda6a5 commit b1fd671

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pybricks/ev3devices/pb_type_ev3devices_gyrosensor.c

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

66
#if PYBRICKS_PY_EV3DEVICES
77

8+
#include "py/mphal.h"
9+
810
#include <pybricks/common.h>
911
#include <pybricks/ev3devices.h>
1012
#include <pybricks/parameters.h>
@@ -44,8 +46,15 @@ static mp_obj_t ev3devices_GyroSensor_make_new(const mp_obj_type_t *type, size_t
4446
ev3devices_GyroSensor_obj_t *self = mp_obj_malloc(ev3devices_GyroSensor_obj_t, type);
4547
pb_type_device_init_class(&self->device_base, port_in, LEGO_DEVICE_TYPE_ID_EV3_GYRO_SENSOR);
4648

47-
// Make sure device is in expected mode.
48-
pb_type_device_get_data_blocking(MP_OBJ_FROM_PTR(self), LEGO_DEVICE_MODE_EV3_GYRO_SENSOR__G_A);
49+
// Make sure device is in expected mode. If not, wait briefly to allow some
50+
// data to come in before we switch. This sensor does not appear to accept
51+
// the mode change otherwise. The sensor stays in this mode, so we only
52+
// need this once.
53+
void *data;
54+
if (pbio_port_lump_get_data(self->device_base.lump_dev, LEGO_DEVICE_MODE_EV3_GYRO_SENSOR__G_A, &data) != PBIO_SUCCESS) {
55+
mp_hal_delay_ms(100);
56+
pb_type_device_get_data_blocking(MP_OBJ_FROM_PTR(self), LEGO_DEVICE_MODE_EV3_GYRO_SENSOR__G_A);
57+
}
4958

5059
self->direction = pb_type_enum_get_value(direction_in, &pb_enum_type_Direction);
5160

0 commit comments

Comments
 (0)