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

Angled motor works

This commit is contained in:
Andrey Pohilko 2017-09-13 13:24:53 +03:00
parent 275a1b87b5
commit 3c82405040
3 changed files with 36 additions and 13 deletions

15
demo.py
View File

@ -11,7 +11,8 @@ log = logging.getLogger("demo")
def demo_all(conn):
movehub = MoveHub(conn)
# demo_led_colors(movehub)
demo_motors_timed(movehub)
# demo_motors_timed(movehub)
demo_motors_angled(movehub)
def demo_led_colors(movehub):
@ -35,8 +36,18 @@ def demo_motors_timed(movehub):
movehub.motor_AB.timed(0.5, -1)
def demo_motors_angled(movehub):
log.info("Motors movement demo: angled")
for angle in range(0, 361, 90):
log.info("Angle: %s", angle)
movehub.motor_B.angled(angle, 1)
sleep(1)
movehub.motor_B.angled(angle, -1)
sleep(1)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
try:
connection = DebugServerConnection()

View File

@ -1,8 +1,11 @@
import logging
import struct
import time
from pylegoboost.constants import *
log = logging.getLogger('movehub')
class MoveHub(object):
"""
@ -48,13 +51,14 @@ class LED(Peripheral):
class EncodedMotor(Peripheral):
TRAILER = b'\x64\x7f\x03' # NOTE: \x64 is 100, might mean something
PACKET_VER = b'\x01'
SET_PORT_VAL = b'\x81'
MOTOR_TIMED_END = b'\x64\x7f\x03'
TRAILER = b'\x64\x7f\x03' # NOTE: \x64 is 100, might mean something
TIMED_GROUP = b'\x0A'
TIMED_SINGLE = b'\x09'
MOVEMENT_TYPE = b'\x11'
TIMED_SINGLE = b'\x09'
TIMED_GROUP = b'\x0A'
ANGLED_SINGLE = b'\x0B'
ANGLED_GROUP = b'\x0C'
def __init__(self, parent, port):
super(EncodedMotor, self).__init__(parent)
@ -66,10 +70,10 @@ class EncodedMotor(Peripheral):
if relative < -1 or relative > 1:
raise ValueError("Invalid speed value: %s", relative)
relative *= 255
if relative < 0:
relative += 255
return int(relative)
absolute = round(relative * 100)
if absolute < 0:
absolute += 255
return int(absolute)
def _wrap_and_write(self, command, speed_primary, speed_secondary):
# set for port
@ -97,8 +101,16 @@ class EncodedMotor(Peripheral):
if not async:
time.sleep(seconds)
def angled(self, angle, speed_primary, speed_secondary):
pass
def angled(self, angle, speed_primary=1, speed_secondary=None):
if speed_secondary is None:
speed_secondary = speed_primary
# movement type
command = self.ANGLED_GROUP if self.port == PORT_AB else self.ANGLED_SINGLE
# angle
command += struct.pack('<I', angle)
self._wrap_and_write(command, speed_primary, speed_secondary)
class ColorDistanceSensor(Peripheral):

View File

@ -159,7 +159,7 @@ class DebugServer(object):
buf = buf[buf.index("\n") + 1:]
if line:
log.debug("Cmd line: %s", line)
log.info("Cmd line: %s", line)
try:
self._handle_cmd(json.loads(line))
except BaseException: