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

figured out directions of joystick

This commit is contained in:
Andrey Pokhilko 2019-08-15 10:54:37 +03:00
parent c9112d8fe4
commit e1e650220f
3 changed files with 16 additions and 12 deletions

View File

@ -28,15 +28,17 @@ if __name__ == "__main__":
def roll(speed, direction):
print("Roll", speed, direction)
if speed < 0.1:
if speed < 2:
speed = 0
bb8.roll(speed, direction)
def stop(state):
if not state:
if state:
print("Stop")
bb8.roll(0, 0)
else:
print("Stabilize")
bb8.stabilize()
@ -47,8 +49,7 @@ if __name__ == "__main__":
joystick.on_joystick(roll)
print("All set up")
while joystick._hub.connection.is_alive():
time.sleep(1)
time.sleep(300)
finally:
bb8.disconnect()
joystick.disconnect()

View File

@ -73,12 +73,13 @@ class BB8(object):
def heading(self, heading):
self._wait_loop()
# self._loop.run_until_complete(self._sphero.set_heading(heading))
self._loop.run_until_complete(self._sphero.roll(1, heading, spheropy.RollMode.IN_PLACE_ROTATE))
self._loop.run_until_complete(self._sphero.set_heading(heading))
# self._loop.run_until_complete(self._sphero.roll(1, heading, spheropy.RollMode.IN_PLACE_ROTATE))
def roll(self, speed=1.0, direction=0):
def roll(self, speed=10, direction=0):
self._wait_loop()
speed = int(255 * speed)
speed = int(255 * speed / 10)
speed *= 0.5 # throttle down a bit
self._loop.run_until_complete(self._sphero.roll(speed, direction))
def stop(self):

View File

@ -69,7 +69,9 @@ class Joystick(object):
def wrapper(angle):
logging.debug("Raw angle: %s", angle)
val = angle % 360
callback(val if val >= 0 else 360 - val - 1)
val = val if val >= 0 else 360 - val - 1
val = 359 - val
callback(val)
self._hub.motor_B.subscribe(wrapper)
@ -101,14 +103,14 @@ class Joystick(object):
def on_joystick(self, callback):
"""
Notifies about joystick change. ``callback(speed, direction)`` gets parameters:
- ``speed`` - float value from 0.0 to 1.0
- ``speed`` - int value from 0 to 10
- ``direction`` - int value from 0 to 359
"""
self._on_joystick.add(callback)
def _calc_joystick(self):
norm_a = self._angle_A / self.RANGE_A
norm_a = -self._angle_A / self.RANGE_A
norm_b = self._angle_C / self.RANGE_C
logging.debug("%s / %s", self._angle_A, self._angle_C)
logging.debug("%s / %s", norm_a, norm_b)
@ -128,7 +130,7 @@ class Joystick(object):
direction = 270 - direction
for callback in self._on_joystick:
callback(speed, 360 - int(direction))
callback(round(10 * speed), 359 - int(direction))
if __name__ == '__main__':