From e1e650220f280409d4e0505d9bbb5c1490b15440 Mon Sep 17 00:00:00 2001 From: Andrey Pokhilko Date: Thu, 15 Aug 2019 10:54:37 +0300 Subject: [PATCH] figured out directions of joystick --- examples/bb8joystick/__init__.py | 9 +++++---- examples/bb8joystick/bb8.py | 9 +++++---- examples/bb8joystick/joystick.py | 10 ++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/bb8joystick/__init__.py b/examples/bb8joystick/__init__.py index 6f2ba89..4b8dc31 100644 --- a/examples/bb8joystick/__init__.py +++ b/examples/bb8joystick/__init__.py @@ -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() diff --git a/examples/bb8joystick/bb8.py b/examples/bb8joystick/bb8.py index 0e05126..00eb637 100644 --- a/examples/bb8joystick/bb8.py +++ b/examples/bb8joystick/bb8.py @@ -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): diff --git a/examples/bb8joystick/joystick.py b/examples/bb8joystick/joystick.py index 256faf5..3546481 100644 --- a/examples/bb8joystick/joystick.py +++ b/examples/bb8joystick/joystick.py @@ -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__':