Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add typing to servos #651

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 37 additions & 38 deletions boxes/servos.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

import math

import boxes.vectors


class EyeEdge(boxes.edges.FingerHoleEdge):

char = "m"

def __init__(self, boxes, servo, fingerHoles=None, driven=False,
outset=False, **kw) -> None:
def __init__(self, boxes, servo, fingerHoles=None, driven: bool = False, outset: bool = False, **kw) -> None:
self.servo = servo
self.outset = outset
self.driven = driven
Expand All @@ -20,103 +20,102 @@ def __call__(self, length, bedBolts=None, bedBoltSettings=None, **kw):

pos_axle = self.servo.hinge_depth()
self.ctx.save()
self.hole(length/2.0, -pos_axle,
self.servo.axle/2.0 if self.driven else
self.servo.servo_axle/2.0)
self.hole(length / 2.0,
-pos_axle,
self.servo.axle / 2.0 if self.driven else self.servo.servo_axle / 2.0)
if self.outset:
self.fingerHoles(t, self.thickness / 2, length-2*t, 0)
self.fingerHoles(t, self.thickness / 2, length - 2 * t, 0)
else:
self.fingerHoles(0, self.thickness / 2, length, 0)
self.ctx.restore()
r = self.servo.servo_axle * 2
a, l = boxes.vectors.tangent(length/2, pos_axle, r)
a, l = boxes.vectors.tangent(length / 2, pos_axle, r)
angle = math.degrees(a)
self.polyline(0, -angle, l, (2*angle, r), l, -angle, 0)
self.polyline(0, -angle, l, (2 * angle, r), l, -angle, 0)

def startwidth(self) -> float:
return self.fingerHoles.settings.thickness

def margin(self) -> float:
return self.servo.hinge_depth() + self.fingerHoles.settings.thickness + self.servo.servo_axle * 2

def buildEdges(boxes, servo, chars="mMnN"):

def buildEdges(boxes, servo, chars: str = "mMnN"):
result = {}
for n, char in enumerate(chars):
e = EyeEdge(boxes, servo, outset=(n<2), driven=(n % 2))
e = EyeEdge(boxes, servo, outset=(n < 2), driven=((n % 2) == 1))
e.char = char
result[char] = e
return result

class ServoArg:

def __init__(self, includenone=False) -> None:
class ServoArg:
def __init__(self, includeNone: bool = False) -> None:
self.servos = ["Servo9g"]
if includenone:
if includeNone:
self.servos[0:0] = ["none"]

def __call__(self, arg):
def __call__(self, arg) -> str:
return str(arg)

def choices(self):
def choices(self) -> list[str]:
return [name for name in self.servos]

def html(self, name, default, translate):
options = "\n".join(
"""<option value="%s"%s>%s</option>""" %
(name, ' selected="selected"' if name == default else "",
name) for name in self.servos)
def html(self, name: str, default: str, translate) -> str:
options = "\n".join("""<option value="{}"{}>{}</option>""".format(name, ' selected="selected"' if name == default else "", name) for name in self.servos)
return f"""<select name="{name}" size="1">\n{options}</select>\n"""


class Servo:
def __init__(self, boxes, axle=3) -> None:
def __init__(self, boxes, axle: float = 3) -> None:
self.boxes = boxes
self.axle = axle
self._edges = buildEdges(boxes, self)

def edges(self, edges):
return [self._edges.get(e, e) for e in edges]

class Servo9g(Servo):

height = 22.5
length = 28.0 # one tab in the wall
width = 12.0
axle_pos = 6.0
servo_axle = 4.6 # 6.9 for servo arm
class Servo9g(Servo):
height: float = 22.5
length: float = 28.0 # one tab in the wall
width: float = 12.0
axle_pos: float = 6.0
servo_axle: float = 4.6 # 6.9 for servo arm

def top(self, x=0.0, y=0.0, angle=90.0):
def top(self, x: float = 0.0, y: float = 0.0, angle: float = 90.0) -> None:
self.boxes.moveTo(x, y, angle)
self.boxes.hole(6, 0, 6)
self.boxes.hole(12, 0, 3)

def bottom(self, x=0.0, y=0.0, angle=90.0):
def bottom(self, x: float = 0.0, y: float = 0.0, angle: float = 90.0) -> None:
self.boxes.moveTo(x, y, angle)
self.boxes.hole(6, 0, self.axle/2.0)
self.boxes.hole(6, 0, self.axle / 2.0)

def front(self, x=0.0, y=0.0, angle=90.0):
def front(self, x: float = 0.0, y: float = 0.0, angle: float = 90.0) -> None:
self.boxes.moveTo(x, y, angle)
self.boxes.rectangularHole(5.4, 0, 2.4, 12)
self.boxes.rectangularHole(17, 0, 4, 16)

def hinge_width(self):
def hinge_width(self) -> float:
return self.height + self.boxes.thickness + 4.5

def hinge_depth(self):
return self.height # XXX
def hinge_depth(self) -> float:
return self.height # XXX


class Servo9gt(Servo9g):
height = 35

def top(self, x=0.0, y=0.0, angle=90.0):
def top(self, x: float = 0.0, y: float = 0.0, angle: float = 90.0) -> None:
self.boxes.moveTo(x, y, angle)
self.boxes.hole(6, 0, 6)
self.boxes.hole(12, 0, 5)

def bottom(self, x=0.0, y=0.0, angle=90.0):
def bottom(self, x: float = 0.0, y: float = 0.0, angle: float = 90.0) -> None:
self.boxes.moveTo(x, y, angle)
self.boxes.hole(6, 0, self.axle)

def front(self, x=0.0, y=0.0, angle=90.0):
def front(self, x: float = 0.0, y: float = 0.0, angle: float = 90.0) -> None:
self.boxes.moveTo(x, y, angle)
self.boxes.rectangularHole(5.4, 0, 2.4, 12)