mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
BB8 joystick is ready
This commit is contained in:
parent
e1e650220f
commit
3a8d17737b
@ -9,7 +9,7 @@ from examples.bb8joystick.joystick import Joystick
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.DEBUG if 'pydevd' in sys.modules else logging.WARNING)
|
||||
|
||||
bb8 = BB8("BB-CC13")
|
||||
bb8 = BB8()
|
||||
joystick = Joystick()
|
||||
|
||||
|
||||
@ -28,7 +28,7 @@ if __name__ == "__main__":
|
||||
|
||||
def roll(speed, direction):
|
||||
print("Roll", speed, direction)
|
||||
if speed < 2:
|
||||
if speed < 3:
|
||||
speed = 0
|
||||
bb8.roll(speed, direction)
|
||||
|
||||
@ -39,7 +39,9 @@ if __name__ == "__main__":
|
||||
bb8.roll(0, 0)
|
||||
else:
|
||||
print("Stabilize")
|
||||
bb8.color(255, 0, 255)
|
||||
bb8.stabilize()
|
||||
bb8.color(0, 0, 0)
|
||||
|
||||
|
||||
try:
|
||||
|
@ -48,7 +48,8 @@ class _SpheroImproved(spheropy.Sphero):
|
||||
|
||||
|
||||
class BB8(object):
|
||||
def __init__(self, name):
|
||||
def __init__(self, name="BB-CC13"):
|
||||
self._heading = 0
|
||||
# marry sync with async https://www.aeracode.org/2018/02/19/python-async-simplified/
|
||||
self._loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self._loop)
|
||||
@ -73,14 +74,18 @@ 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))
|
||||
heading = 359 - heading
|
||||
self._heading = heading
|
||||
# self._loop.run_until_complete(self._sphero.set_heading(359 - heading))
|
||||
self._loop.run_until_complete(self._sphero.roll(1, heading, spheropy.RollMode.IN_PLACE_ROTATE))
|
||||
|
||||
def roll(self, speed=10, direction=0):
|
||||
self._wait_loop()
|
||||
direction += self._heading
|
||||
direction %= 360
|
||||
speed = int(255 * speed / 10)
|
||||
speed *= 0.5 # throttle down a bit
|
||||
self._loop.run_until_complete(self._sphero.roll(speed, direction))
|
||||
speed *= 0.75 # throttle down a bit
|
||||
self._loop.run_until_complete(self._sphero.roll(int(speed), direction))
|
||||
|
||||
def stop(self):
|
||||
self._wait_loop()
|
||||
|
@ -110,7 +110,7 @@ class Joystick(object):
|
||||
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)
|
||||
@ -130,7 +130,7 @@ class Joystick(object):
|
||||
direction = 270 - direction
|
||||
|
||||
for callback in self._on_joystick:
|
||||
callback(round(10 * speed), 359 - int(direction))
|
||||
callback(int(round(10 * speed)), int(direction))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@ -139,7 +139,7 @@ if __name__ == '__main__':
|
||||
stick = Joystick()
|
||||
|
||||
stick.on_button(lambda x: logging.info("Button: %s" % x))
|
||||
stick.on_rotation(lambda x: logging.info("Motor B: %s" % x))
|
||||
stick.on_joystick(lambda speed, head: logging.info("Speed: %.2f, dir: %s" % (speed, head)))
|
||||
stick.on_rotation(lambda x: logging.info("Rotation: %s" % x))
|
||||
stick.on_joystick(lambda speed, head: logging.info("Speed: %s, Direction: %s" % (speed, head)))
|
||||
|
||||
time.sleep(100)
|
||||
|
Loading…
x
Reference in New Issue
Block a user