Skip to content

Commit 047c46b

Browse files
committed
extmod/modpupdevices: Add TiltSensor
This adds a very minimal implementation, exposing tilt relative to the horizontal plane only (pitch, roll). Other more useful methods may be added once the generic attitude/orientation API is in place and implemented for the IMUs internal to the hubs.
1 parent b9295f8 commit 047c46b

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

extmod/modpupdevices.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,55 @@ STATIC const mp_obj_type_t pupdevices_InfraredSensor_type = {
601601
.locals_dict = (mp_obj_dict_t *)&pupdevices_InfraredSensor_locals_dict,
602602
};
603603

604+
// Class structure for TiltSensor
605+
typedef struct _pupdevices_TiltSensor_obj_t {
606+
mp_obj_base_t base;
607+
pbdevice_t *pbdev;
608+
} pupdevices_TiltSensor_obj_t;
609+
610+
// pybricks.pupdevices.TiltSensor.__init__
611+
STATIC mp_obj_t pupdevices_TiltSensor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
612+
PB_PARSE_ARGS_CLASS(n_args, n_kw, args,
613+
PB_ARG_REQUIRED(port));
614+
615+
pupdevices_TiltSensor_obj_t *self = m_new_obj(pupdevices_TiltSensor_obj_t);
616+
self->base.type = (mp_obj_type_t *)type;
617+
618+
mp_int_t port_num = pb_type_enum_get_value(port, &pb_enum_type_Port);
619+
620+
// Get iodevice
621+
self->pbdev = pbdevice_get_device(port_num, PBIO_IODEV_TYPE_ID_WEDO2_TILT_SENSOR);
622+
623+
return MP_OBJ_FROM_PTR(self);
624+
}
625+
626+
// pybricks.pupdevices.TiltSensor.tilt
627+
STATIC mp_obj_t pupdevices_TiltSensor_tilt(mp_obj_t self_in) {
628+
pupdevices_TiltSensor_obj_t *self = MP_OBJ_TO_PTR(self_in);
629+
int32_t tilt[2];
630+
pbdevice_get_values(self->pbdev, PBIO_IODEV_MODE_PUP_WEDO2_TILT_SENSOR__ANGLE, tilt);
631+
mp_obj_t ret[2];
632+
ret[0] = mp_obj_new_int(tilt[1]);
633+
ret[1] = mp_obj_new_int(tilt[0]);
634+
return mp_obj_new_tuple(2, ret);
635+
}
636+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pupdevices_TiltSensor_tilt_obj, pupdevices_TiltSensor_tilt);
637+
638+
// dir(pybricks.pupdevices.TiltSensor)
639+
STATIC const mp_rom_map_elem_t pupdevices_TiltSensor_locals_dict_table[] = {
640+
{ MP_ROM_QSTR(MP_QSTR_tilt), MP_ROM_PTR(&pupdevices_TiltSensor_tilt_obj) },
641+
};
642+
STATIC MP_DEFINE_CONST_DICT(pupdevices_TiltSensor_locals_dict, pupdevices_TiltSensor_locals_dict_table);
643+
644+
// type(pybricks.pupdevices.TiltSensor)
645+
STATIC const mp_obj_type_t pupdevices_TiltSensor_type = {
646+
{ &mp_type_type },
647+
.name = MP_QSTR_TiltSensor,
648+
.make_new = pupdevices_TiltSensor_make_new,
649+
.locals_dict = (mp_obj_dict_t *)&pupdevices_TiltSensor_locals_dict,
650+
};
651+
652+
604653
// Class structure for Light
605654
typedef struct _pupdevices_Light_obj_t {
606655
mp_obj_base_t base;
@@ -668,7 +717,8 @@ STATIC const mp_rom_map_elem_t pupdevices_globals_table[] = {
668717
{ MP_ROM_QSTR(MP_QSTR_ColorSensor), MP_ROM_PTR(&pupdevices_ColorSensor_type) },
669718
{ MP_ROM_QSTR(MP_QSTR_UltrasonicSensor), MP_ROM_PTR(&pupdevices_UltrasonicSensor_type) },
670719
{ MP_ROM_QSTR(MP_QSTR_ForceSensor), MP_ROM_PTR(&pupdevices_ForceSensor_type) },
671-
{ MP_ROM_QSTR(MP_QSTR_InfraredSensor), MP_ROM_PTR(&pupdevices_InfraredSensor_type) },
720+
{ MP_ROM_QSTR(MP_QSTR_InfraredSensor), MP_ROM_PTR(&pupdevices_InfraredSensor_type) },
721+
{ MP_ROM_QSTR(MP_QSTR_TiltSensor), MP_ROM_PTR(&pupdevices_TiltSensor_type) },
672722
{ MP_ROM_QSTR(MP_QSTR_Light), MP_ROM_PTR(&pupdevices_Light_type) },
673723
};
674724

extmod/pbdevice.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ enum {
118118
PBIO_IODEV_MODE_NXT_ENERGY_METER_ALL = 7,
119119
};
120120

121+
// LEGO POWERED UP WEDO 2.0 Tilt sensor
122+
enum {
123+
PBIO_IODEV_MODE_PUP_WEDO2_TILT_SENSOR__ANGLE = 0, // read 2x int8_t
124+
PBIO_IODEV_MODE_PUP_WEDO2_TILT_SENSOR__DIR = 1, // read 1x int8_t
125+
PBIO_IODEV_MODE_PUP_WEDO2_TILT_SENSOR__CNT = 2, // read 3x int8_t
126+
PBIO_IODEV_MODE_PUP_WEDO2_TILT_SENSOR__CAL = 2, // read 3x int8_t
127+
};
128+
121129
// LEGO POWERED UP WEDO 2.0 Infrared "Motion" sensor
122130
enum {
123131
PBIO_IODEV_MODE_PUP_WEDO2_MOTION_SENSOR__DETECT = 0, // read 1x int8_t

0 commit comments

Comments
 (0)