1
0
mirror of https://github.com/undera/pylgbst.git synced 2020-11-18 19:37:26 -08:00

New machine is ready, start creating playground code

This commit is contained in:
Andrey Pokhilko 2019-08-14 11:05:16 +03:00
parent fc08a495b1
commit 3f2b3dcb3a
2 changed files with 33 additions and 8 deletions

View File

@ -27,10 +27,10 @@ if __name__ == "__main__":
try:
# joystick.on_color_sensor(set_bb_color)
#joystick.on_color_sensor(set_bb_color)
joystick.on_external_motor(set_heading)
print("All set up")
time.sleep(60)
time.sleep(600)
finally:
joystick.disconnect()
bb8.disconnect()

View File

@ -1,18 +1,43 @@
import logging
import time
from pylgbst.hub import MoveHub
from pylgbst.peripherals import VisionSensor, EncodedMotor
from pylgbst.peripherals import EncodedMotor
class Joystick(object):
def __init__(self):
super(Joystick, self).__init__()
self._hub = MoveHub()
self._sensor = []
self.button_pressed = False
self._hub.motor_external.subscribe(print, EncodedMotor.SENSOR_ANGLE, granularity=1)
self._hub.motor_A.subscribe(print, EncodedMotor.SENSOR_ANGLE, granularity=1)
self._hub.motor_B.subscribe(print, EncodedMotor.SENSOR_ANGLE, granularity=1)
self._hub.button.subscribe(self._on_btn)
def disconnect(self):
self._hub.disconnect()
def on_color_sensor(self, callback):
self._hub.vision_sensor.subscribe(callback, VisionSensor.COLOR_RGB, granularity=5)
def _on_btn(self, state):
self.button_pressed = state
def on_external_motor(self, callback):
self._hub.motor_external.subscribe(callback, EncodedMotor.SENSOR_ANGLE, granularity=5)
def on_button(self, callback):
"""
Notifies about button state change. ``callback(state)`` gets single bool parameter
"""
def wrapper(state):
if state in (0, 1):
logging.debug("Pressed button: %s", state)
callback(bool(state))
self._hub.button.subscribe(wrapper)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
stick = Joystick()
stick.on_button(lambda x: print("Button: %s" % x))
time.sleep(100)