mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
refactoring
This commit is contained in:
parent
48eef133a1
commit
275a1b87b5
@ -63,44 +63,43 @@ class EncodedMotor(Peripheral):
|
|||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def _speed_abs(self, relative):
|
def _speed_abs(self, relative):
|
||||||
relative *= 100
|
if relative < -1 or relative > 1:
|
||||||
|
raise ValueError("Invalid speed value: %s", relative)
|
||||||
|
|
||||||
|
relative *= 255
|
||||||
if relative < 0:
|
if relative < 0:
|
||||||
relative += 255
|
relative += 255
|
||||||
return int(relative)
|
return int(relative)
|
||||||
|
|
||||||
def timed(self, seconds, speed_primary=1, speed_secondary=None, async=False):
|
def _wrap_and_write(self, command, speed_primary, speed_secondary):
|
||||||
if speed_primary < -1 or speed_primary > 1:
|
|
||||||
raise ValueError("Invalid primary motor speed value: %s", speed_primary)
|
|
||||||
|
|
||||||
if speed_secondary is None:
|
|
||||||
speed_secondary = speed_primary
|
|
||||||
|
|
||||||
if speed_secondary < -1 or speed_secondary > 1:
|
|
||||||
raise ValueError("Invalid secondary motor speed value: %s", speed_primary)
|
|
||||||
|
|
||||||
time_ms = int(seconds * 1000)
|
|
||||||
|
|
||||||
# set for port
|
# set for port
|
||||||
command = self.SET_PORT_VAL + chr(self.port)
|
command = self.SET_PORT_VAL + chr(self.port) + self.MOVEMENT_TYPE + command
|
||||||
|
|
||||||
# movement type
|
|
||||||
command += self.MOVEMENT_TYPE
|
|
||||||
command += self.TIMED_GROUP if self.port == PORT_AB else self.TIMED_SINGLE
|
|
||||||
|
|
||||||
command += struct.pack('<H', time_ms)
|
|
||||||
|
|
||||||
command += chr(self._speed_abs(speed_primary))
|
command += chr(self._speed_abs(speed_primary))
|
||||||
|
|
||||||
###
|
|
||||||
if self.port == PORT_AB:
|
if self.port == PORT_AB:
|
||||||
command += chr(self._speed_abs(speed_secondary))
|
command += chr(self._speed_abs(speed_secondary))
|
||||||
|
|
||||||
command += self.TRAILER
|
command += self.TRAILER
|
||||||
|
|
||||||
self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE, chr(len(command)) + self.PACKET_VER + command)
|
self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE, chr(len(command)) + self.PACKET_VER + command)
|
||||||
|
|
||||||
|
def timed(self, seconds, speed_primary=1, speed_secondary=None, async=False):
|
||||||
|
if speed_secondary is None:
|
||||||
|
speed_secondary = speed_primary
|
||||||
|
|
||||||
|
# movement type
|
||||||
|
command = self.TIMED_GROUP if self.port == PORT_AB else self.TIMED_SINGLE
|
||||||
|
# time
|
||||||
|
command += struct.pack('<H', int(seconds * 1000))
|
||||||
|
|
||||||
|
self._wrap_and_write(command, speed_primary, speed_secondary)
|
||||||
|
|
||||||
if not async:
|
if not async:
|
||||||
time.sleep(seconds)
|
time.sleep(seconds)
|
||||||
|
|
||||||
|
def angled(self, angle, speed_primary, speed_secondary):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ColorDistanceSensor(Peripheral):
|
class ColorDistanceSensor(Peripheral):
|
||||||
pass
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user