-
Notifications
You must be signed in to change notification settings - Fork 226
feat: Added RoboticArm #255
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
>>> servo.angle = 30 # Turn motor to 30 degrees position. | ||
""" | ||
|
||
import time | ||
from typing import List | ||
from typing import Union | ||
|
||
from pslab.instrument.waveform_generator import PWMGenerator | ||
|
@@ -70,3 +72,40 @@ def _get_duty_cycle(self, angle): | |
angle *= self._max_angle_pulse - self._min_angle_pulse # Scale | ||
angle += self._min_angle_pulse # Offset | ||
return angle / (self._frequency**-1 * MICROSECONDS) | ||
|
||
|
||
class RoboticArm: | ||
"""Robotic arm controller for up to 4 servos.""" | ||
|
||
MAX_SERVOS = 4 | ||
|
||
def __init__(self, servos: List[Servo]) -> None: | ||
if len(servos) > RoboticArm.MAX_SERVOS: | ||
raise ValueError( | ||
f"At most {RoboticArm.MAX_SERVOS} servos can be used, got {len(servos)}" | ||
) | ||
self.servos = servos | ||
|
||
def run_schedule(self, timeline: List[List[int]], time_step: float = 1.0) -> None: | ||
"""Run a time-based schedule to move servos. | ||
|
||
Parameters | ||
---------- | ||
timeline : List[List[int]] | ||
A list of timesteps,where each sublist represents one timestep, | ||
with angles corresponding to each servo. | ||
|
||
time_step : float, optional | ||
Delay in seconds between each timestep. Default is 1.0. | ||
""" | ||
if len(timeline[0]) != len(self.servos): | ||
raise ValueError("Each timestep must specify an angle for every servo") | ||
|
||
tl_len = len(timeline[0]) | ||
if not all(len(tl) == tl_len for tl in timeline): | ||
raise ValueError("All timeline entries must have the same length") | ||
|
||
for tl in timeline: | ||
for i, s in enumerate(self.servos): | ||
s.angle = tl[i] | ||
time.sleep(time_step) | ||
Comment on lines
+108
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this again, I think it is still not quite right. As is, this will apply every timeline to every servo in sequence, instead of the intended one timeline per servo in parallel. It should be something like for step in zip(*timeline):
for s, angle in zip(self.servos, step):
s.angle = angle Explanation of these steps (aka. crash course in Python iterators):
This should apply each step in timeline 1 to servo 1, each step in timeline 2 to servo 2, etc. in parallel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @bessman, for the detailed explanation! I just wanted to clarify that in Mobile app, the timeline is structured per timestep, like this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the clarification! Then, the code is correct as-is. Please document the timeline parameter in the run_schedule docstring, noting that it's one sublist per timestep. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, check that the number of timelines (
len(timeline)
) is equal to the number of servos (len(self.servos)
).