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

Preparing joystick operations

This commit is contained in:
Andrey Pokhilko 2019-08-14 13:58:55 +03:00
parent 33364e4e3b
commit f3e4a9dbdb
2 changed files with 79 additions and 9 deletions

View File

@ -2,7 +2,10 @@ import logging
import time
from pylgbst.hub import MoveHub
from pylgbst.peripherals import EncodedMotor
def _clamp(minvalue, value, maxvalue):
return max(minvalue, min(value, maxvalue))
class Joystick(object):
@ -10,18 +13,30 @@ class Joystick(object):
super(Joystick, self).__init__()
self._hub = MoveHub()
self.button_pressed = False
self._reset_sensors()
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.button_pressed = False
self._hub.button.subscribe(self._on_btn)
self.angle_A = 0
self.on_motor_a(self._on_a)
self.angle_B = 0
self.on_button(self._on_b)
self.angle_C = 0
self.on_motor_a(self._on_c)
logging.info("Done initializing")
def disconnect(self):
self._hub.disconnect()
def _on_btn(self, state):
self.button_pressed = state
def _reset_sensors(self):
logging.info("Resetting motor encoders")
self._hub.motor_A.preset_encoder()
self._hub.motor_B.preset_encoder()
self._hub.motor_external.preset_encoder()
def on_button(self, callback):
"""
@ -30,14 +45,69 @@ class Joystick(object):
def wrapper(state):
if state in (0, 1):
logging.debug("Pressed button: %s", state)
callback(bool(state))
self._hub.button.subscribe(wrapper)
def on_motor_a(self, callback):
"""
Notifies about A motor rotation. ``callback(state)`` gets single int parameter from 0 to 359
"""
def wrapper(angle):
logging.debug("Raw angle: %s", angle)
range = 25
angle = _clamp(-range, angle, range)
callback(angle)
self._hub.motor_A.subscribe(wrapper)
def on_motor_b(self, callback):
"""
Notifies about B motor rotation. ``callback(state)`` gets single int parameter from 0 to 359
"""
def wrapper(angle):
logging.debug("Raw angle: %s", angle)
val = angle % 360
callback(val if val >= 0 else 360 - val)
self._hub.motor_B.subscribe(wrapper)
def on_motor_c(self, callback):
"""
Notifies about C motor rotation. ``callback(state)`` gets single int parameter from 0 to 359
"""
def wrapper(angle):
logging.debug("Raw angle: %s", angle)
range = 25
angle = _clamp(-range, angle, range)
callback(angle)
self._hub.motor_external.subscribe(wrapper)
def _on_btn(self, state):
self.button_pressed = bool(state)
def _on_a(self, angle):
logging.debug("A rotated: %s", angle)
self.angle_A = angle
def _on_b(self, angle):
logging.debug("B rotated: %s", angle)
self.angle_B = angle
def _on_c(self, angle):
logging.debug("C rotated: %s", angle)
self.angle_C = angle
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
stick = Joystick()
stick.on_button(lambda x: print("Button: %s" % x))
stick.on_motor_a(lambda x: print("Motor A: %s" % x))
stick.on_motor_b(lambda x: print("Motor B: %s" % x))
stick.on_motor_c(lambda x: print("Motor C: %s" % x))
time.sleep(100)

View File

@ -126,7 +126,7 @@ class Peripheral(object):
self.set_port_mode(self._port_mode.mode, False)
def _notify_subscribers(self, *args, **kwargs):
for subscriber in self._subscribers:
for subscriber in self._subscribers.copy():
subscriber(*args, **kwargs)
return args