Skip to content

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

Merged
merged 5 commits into from
Jul 15, 2025
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
39 changes: 39 additions & 0 deletions pslab/external/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Loading