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:
parent
fc08a495b1
commit
3f2b3dcb3a
@ -30,7 +30,7 @@ if __name__ == "__main__":
|
|||||||
#joystick.on_color_sensor(set_bb_color)
|
#joystick.on_color_sensor(set_bb_color)
|
||||||
joystick.on_external_motor(set_heading)
|
joystick.on_external_motor(set_heading)
|
||||||
print("All set up")
|
print("All set up")
|
||||||
time.sleep(60)
|
time.sleep(600)
|
||||||
finally:
|
finally:
|
||||||
joystick.disconnect()
|
joystick.disconnect()
|
||||||
bb8.disconnect()
|
bb8.disconnect()
|
||||||
|
@ -1,18 +1,43 @@
|
|||||||
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
from pylgbst.hub import MoveHub
|
from pylgbst.hub import MoveHub
|
||||||
from pylgbst.peripherals import VisionSensor, EncodedMotor
|
from pylgbst.peripherals import EncodedMotor
|
||||||
|
|
||||||
|
|
||||||
class Joystick(object):
|
class Joystick(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(Joystick, self).__init__()
|
super(Joystick, self).__init__()
|
||||||
self._hub = MoveHub()
|
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):
|
def disconnect(self):
|
||||||
self._hub.disconnect()
|
self._hub.disconnect()
|
||||||
|
|
||||||
def on_color_sensor(self, callback):
|
def _on_btn(self, state):
|
||||||
self._hub.vision_sensor.subscribe(callback, VisionSensor.COLOR_RGB, granularity=5)
|
self.button_pressed = state
|
||||||
|
|
||||||
def on_external_motor(self, callback):
|
def on_button(self, callback):
|
||||||
self._hub.motor_external.subscribe(callback, EncodedMotor.SENSOR_ANGLE, granularity=5)
|
"""
|
||||||
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user