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

Minor tweaks

This commit is contained in:
Andrey Pohilko 2017-12-26 18:49:15 +03:00
parent 1dac915fbe
commit 4efe3705ec
3 changed files with 15 additions and 32 deletions

View File

@ -57,7 +57,7 @@ Methods to activate motors are:
- `angled(angle, speed_primary, speed_secondary)` - makes motor to rotate to specified angle, `angle` value is integer degrees, can be negative and can be more than 360 for several rounds - `angled(angle, speed_primary, speed_secondary)` - makes motor to rotate to specified angle, `angle` value is integer degrees, can be negative and can be more than 360 for several rounds
- `stop()` - stops motor at once, it is equivalent for `constant(0)` - `stop()` - stops motor at once, it is equivalent for `constant(0)`
Parameter `speed_secondary` is used when it is motor group of `motor_AB` running together. By default, `speed_secondary` equals `speed_primary`. Speed values range is `-1.0` to `1.0`, float values. Parameter `speed_secondary` is used when it is motor group of `motor_AB` running together. By default, `speed_secondary` equals `speed_primary`. Speed values range is `-1.0` to `1.0`, float values. _Note: In group angled mode, total rotation angle is distributed across 2 motors according to motor speeds ratio._
All these methods are synchronous by default, means method does not return untill it gets confirmation from Hub that command has completed. You can pass `async=True` parameter to any of methods to switch into asynchronous, which means command will return immediately, without waiting for rotation to complete. Be careful with asynchronous calls, as they make Hub to stop reporting synchronizing statuses. All these methods are synchronous by default, means method does not return untill it gets confirmation from Hub that command has completed. You can pass `async=True` parameter to any of methods to switch into asynchronous, which means command will return immediately, without waiting for rotation to complete. Be careful with asynchronous calls, as they make Hub to stop reporting synchronizing statuses.

View File

@ -6,7 +6,7 @@ from pylgbst import ColorDistanceSensor, COLORS, COLOR_RED, COLOR_CYAN
class Plotter(object): class Plotter(object):
MOTOR_RATIO = 1.45 MOTOR_RATIO = 1.425
ROTATE_UNIT = 2100 ROTATE_UNIT = 2100
def __init__(self, hub, base_speed=1.0): def __init__(self, hub, base_speed=1.0):
@ -20,7 +20,6 @@ class Plotter(object):
self.both = self._hub.motor_AB self.both = self._hub.motor_AB
self.base_speed = float(base_speed) self.base_speed = float(base_speed)
self.field_width = 1.0 / self.base_speed
self.xpos = 0 self.xpos = 0
self.ypos = 0 self.ypos = 0
self.is_tool_down = False self.is_tool_down = False
@ -41,9 +40,9 @@ class Plotter(object):
return return
self._hub.color_distance_sensor.subscribe(self._on_distance, mode=ColorDistanceSensor.COLOR_DISTANCE_FLOAT) self._hub.color_distance_sensor.subscribe(self._on_distance, mode=ColorDistanceSensor.COLOR_DISTANCE_FLOAT)
self.caret.timed(0.5, self.base_speed) self.caret.timed(0.5, 1)
try: try:
self.caret.constant(-self.base_speed) self.caret.constant(-1)
count = 0 count = 0
max_tries = 50 max_tries = 50
while self._marker_color not in (COLOR_RED, COLOR_CYAN) and count < max_tries: while self._marker_color not in (COLOR_RED, COLOR_CYAN) and count < max_tries:
@ -59,9 +58,9 @@ class Plotter(object):
self._hub.color_distance_sensor.unsubscribe(self._on_distance) self._hub.color_distance_sensor.unsubscribe(self._on_distance)
if self._marker_color == COLOR_CYAN: if self._marker_color == COLOR_CYAN:
self.move(-self.field_width * 0.1, 0) self.move(- 0.1, 0)
else: else:
self.move(-self.field_width, 0) self.move(-1.0, 0)
def _on_distance(self, color, distance): def _on_distance(self, color, distance):
self._marker_color = None self._marker_color = None
@ -89,6 +88,7 @@ class Plotter(object):
def _tool_down(self): def _tool_down(self):
self.is_tool_down = True self.is_tool_down = True
self._hub.motor_external.angled(-270, 1) self._hub.motor_external.angled(-270, 1)
time.sleep(1.0) # for laser to heat up
def _tool_up(self): def _tool_up(self):
self._hub.motor_external.angled(270, 1) self._hub.motor_external.angled(270, 1)
@ -109,15 +109,6 @@ class Plotter(object):
logging.warning("No movement, ignored") logging.warning("No movement, ignored")
return return
if self.xpos + movx < -self.field_width:
logging.warning("Invalid xpos: %s", self.xpos)
# movx += self.xpos - self.field_width
if self.xpos + movx > self.field_width:
logging.warning("Invalid xpos: %s", self.xpos)
# movx -= self.xpos - self.field_width
# self.xpos -= self.xpos - self.field_width
self._compensate_wheels_backlash(movy) self._compensate_wheels_backlash(movy)
self.xpos += movx self.xpos += movx
@ -127,9 +118,9 @@ class Plotter(object):
logging.info("Motors: %.3f with %.3f/%.3f", length, speed_a, speed_b) logging.info("Motors: %.3f with %.3f/%.3f", length, speed_a, speed_b)
if not speed_b: if not speed_b:
self.caret.angled(length * 2.0, -speed_a * self.base_speed) self.caret.angled(length * 2.0, -speed_a * self.base_speed * 0.75) # slow down to cut better
elif not speed_a: elif not speed_a:
self.wheels.angled(length * 2.0, -speed_b * self.base_speed) self.wheels.angled(length * 2.0, -speed_b * self.base_speed * 0.75) # slow down to cut better
else: else:
self.both.angled(length, -speed_a * self.base_speed, -speed_b * self.base_speed) self.both.angled(length, -speed_a * self.base_speed, -speed_b * self.base_speed)
@ -235,19 +226,11 @@ class Plotter(object):
self.line(0, -height) self.line(0, -height)
if solid: if solid:
max_step = 0.1 max_step = 0.01
rounds = int(math.ceil(height / max_step)) rounds = int(math.ceil(height / max_step))
step = height / rounds step = height / rounds
flip = 1 flip = 1
self.line(0, step) # luft for r in range(1, rounds):
for r in range(1, rounds + 3):
self.line(0, step) self.line(0, step)
self.line(width * flip, 0) self.line(width * flip, 0)
flip = -flip flip = -flip
class LaserPlotter(Plotter):
def _tool_down(self):
super(LaserPlotter, self)._tool_down()
time.sleep(1.0)

View File

@ -2,7 +2,7 @@ import logging
import time import time
import traceback import traceback
from examples.plotter import LaserPlotter from examples.plotter import Plotter
from pylgbst import EncodedMotor, PORT_AB, PORT_C, PORT_A, PORT_B, MoveHub from pylgbst import EncodedMotor, PORT_AB, PORT_C, PORT_A, PORT_B, MoveHub
from pylgbst.comms import DebugServerConnection, BLEConnection from pylgbst.comms import DebugServerConnection, BLEConnection
from tests import HubMock from tests import HubMock
@ -269,8 +269,8 @@ if __name__ == '__main__':
hub = MoveHub(conn) if 1 else get_hub_mock() hub = MoveHub(conn) if 1 else get_hub_mock()
plotter = LaserPlotter(hub, 0.4) plotter = Plotter(hub, 0.75)
FIELD_WIDTH = plotter.field_width * 0.9 FIELD_WIDTH = 0.9
try: try:
plotter.initialize() plotter.initialize()
@ -290,7 +290,7 @@ if __name__ == '__main__':
# plotter.spiral(4, 0.02) # plotter.spiral(4, 0.02)
# plotter.rectangle(FIELD_WIDTH / 5.0, FIELD_WIDTH / 5.0, solid=True) # plotter.rectangle(FIELD_WIDTH / 5.0, FIELD_WIDTH / 5.0, solid=True)
square_spiral() # square_spiral()
# lego() # lego()
# christmas_tree() # christmas_tree()
# snowflake(0.5) # snowflake(0.5)