-
Notifications
You must be signed in to change notification settings - Fork 226
feat: RoboticArm CSV import/export #256
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
+60
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,11 @@ | |
import time | ||
from typing import List | ||
from typing import Union | ||
import csv | ||
import os | ||
|
||
from pslab.instrument.waveform_generator import PWMGenerator | ||
from datetime import datetime | ||
|
||
MICROSECONDS = 1e6 | ||
|
||
|
@@ -96,7 +99,7 @@ def run_schedule(self, timeline: List[List[int]], time_step: float = 1.0) -> Non | |
with angles corresponding to each servo. | ||
|
||
time_step : float, optional | ||
Delay in seconds between each timestep. Default is 1.0. | ||
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") | ||
|
@@ -107,5 +110,60 @@ def run_schedule(self, timeline: List[List[int]], time_step: float = 1.0) -> Non | |
|
||
for tl in timeline: | ||
for i, s in enumerate(self.servos): | ||
s.angle = tl[i] | ||
if tl[i] is not None: | ||
s.angle = tl[i] | ||
time.sleep(time_step) | ||
|
||
def import_timeline_from_csv(self, filepath: str) -> List[List[int]]: | ||
"""Import timeline from a CSV file. | ||
|
||
Parameters | ||
---------- | ||
filepath : str | ||
Absolute or relative path to the CSV file to be read. | ||
|
||
Returns | ||
------- | ||
List[List[int]] | ||
A timeline consisting of servo angle values per timestep. | ||
""" | ||
timeline = [] | ||
|
||
with open(filepath, mode="r", newline="") as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
angles = [] | ||
for key in ["Servo1", "Servo2", "Servo3", "Servo4"]: | ||
value = row[key] | ||
if value == "null": | ||
angles.append(None) | ||
else: | ||
angles.append(int(value)) | ||
timeline.append(angles) | ||
|
||
return timeline | ||
|
||
def export_timeline_to_csv( | ||
self, timeline: List[List[Union[int, None]]], folderpath: str | ||
) -> None: | ||
"""Export timeline to a CSV file. | ||
|
||
Parameters | ||
---------- | ||
timeline : List[List[Union[int, None]]] | ||
A list of timesteps where each sublist contains servo angles. | ||
|
||
folderpath : str | ||
Directory path where the CSV file will be saved. The filename | ||
will include a timestamp to ensure uniqueness. | ||
""" | ||
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | ||
filename = f"Robotic_Arm{timestamp}.csv" | ||
filepath = os.path.join(folderpath, filename) | ||
|
||
with open(filepath, mode="w", newline="") as csvfile: | ||
writer = csv.writer(csvfile) | ||
writer.writerow(["Timestep", "Servo1", "Servo2", "Servo3", "Servo4"]) | ||
for i, row in enumerate(timeline): | ||
pos = ["null" if val is None else val for val in row] | ||
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. suggestion: Inconsistent handling of None values between import and export. Consider using a single representation for None values (e.g., always 'null') in both import and export, and document this behavior for clarity. Suggested implementation: # Export timeline to CSV, using 'null' to represent None values.
# When importing, 'null' will be interpreted as None.
with open(filepath, mode="w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"])
for i, row in enumerate(timeline):
pos = ["null" if val is None else val for val in row]
writer.writerow([i] + pos) def import_timeline_from_csv(filepath):
"""
Import a timeline from a CSV file, interpreting 'null' as None.
Args:
filepath (str): Path to the CSV file.
Returns:
list: Timeline data as a list of lists, with None for 'null' values.
"""
timeline = []
with open(filepath, mode="r", newline="") as csvfile:
reader = csv.reader(csvfile)
headers = next(reader) # Skip header
for row in reader:
# row[0] is the timestamp/index, skip or store as needed
positions = [None if val == "null" else float(val) for val in row[1:]]
timeline.append(positions)
return timeline
|
||
writer.writerow([i] + pos) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.