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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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")
Comment on lines +104 to +106
Copy link
Collaborator

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)).


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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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):

  1. asterisk (*) is the unpack operator. It unpacks the timeline list to its component lists.
  2. zip repacks the timeline lists into one combined parallel iterator, which yields n angles at a time, i.e. one timestep.
  3. We again zip the n servos and the n angles into a parallel iterator, which yields one servo and one angle at a time.
  4. We set each angle on its corresponding servo.

This should apply each step in timeline 1 to servo 1, each step in timeline 2 to servo 2, etc. in parallel.

Copy link
Author

Choose a reason for hiding this comment

The 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:
[
[10, 20, 30, 40], # timestep 1: servo1, servo2, servo3, servo4
[15, 25, 35, 45], # timestep 2
[20, 30, 40, 50], # timestep 3
]
So each inner list already represents one point in time, containing the angle values for all servos in order

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Loading