-
Notifications
You must be signed in to change notification settings - Fork 244
fix: make RoboticArm CSV import/export work for arms with fewer than 4 servos #289
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
thisisanubhav
wants to merge
5
commits into
fossasia:main
Choose a base branch
from
thisisanubhav:fix/robotic-arm-csv-servo-count
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f16f02c
fix: pad exported RoboticArm rows to four servos
thisisanubhav 82d8500
fix: import one angle per servo in RoboticArm timelines
thisisanubhav 341dba3
test: cover RoboticArm CSV round trips without hardware
thisisanubhav 57bd58d
fix: reject malformed RoboticArm CSV input and output
thisisanubhav 7e25a47
test: cover rejected RoboticArm CSV input and output
thisisanubhav 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
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Tests for pslab.external.motor.RoboticArm CSV import and export. | ||
|
|
||
| These tests do not require a connected PSLab. | ||
| """ | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from pslab.external.motor import RoboticArm, Servo | ||
|
|
||
|
|
||
| def make_arm(servo_count: int) -> RoboticArm: | ||
| pwm = MagicMock() | ||
| return RoboticArm([Servo(f"SQ{i + 1}", pwm) for i in range(servo_count)]) | ||
|
|
||
|
|
||
| def write_csv(path, rows): | ||
| header = "Timestep,Servo1,Servo2,Servo3,Servo4\n" | ||
| path.write_text(header + "".join(f"{row}\n" for row in rows)) | ||
| return str(path) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("servo_count", [1, 2, 3, 4]) | ||
| def test_export_import_round_trip(tmp_path, servo_count): | ||
| arm = make_arm(servo_count) | ||
| timeline = [ | ||
| [10 * (i + 1) for i in range(servo_count)], | ||
| [None] + [90] * (servo_count - 1), | ||
| ] | ||
|
|
||
| arm.export_timeline_to_csv(timeline, str(tmp_path)) | ||
| (exported,) = tmp_path.glob("*.csv") | ||
|
|
||
| assert arm.import_timeline_from_csv(str(exported)) == timeline | ||
|
|
||
|
|
||
| def test_export_pads_rows_to_four_servos(tmp_path): | ||
| make_arm(2).export_timeline_to_csv([[10, 20]], str(tmp_path)) | ||
| (exported,) = tmp_path.glob("*.csv") | ||
|
|
||
| assert exported.read_text().splitlines()[1] == "0,10,20,null,null" | ||
|
|
||
|
|
||
| def test_imported_timeline_runs_on_a_smaller_arm(tmp_path): | ||
| arm = make_arm(2) | ||
| path = write_csv(tmp_path / "t.csv", ["0,10,20,null,null"]) | ||
|
|
||
| arm.run_schedule(arm.import_timeline_from_csv(path), time_step=0) | ||
|
|
||
| assert [servo.angle for servo in arm.servos] == [10, 20] | ||
|
|
||
|
|
||
| def test_import_accepts_short_rows(tmp_path): | ||
| path = write_csv(tmp_path / "t.csv", ["0,10,20"]) | ||
|
|
||
| assert make_arm(2).import_timeline_from_csv(path) == [[10, 20]] | ||
|
|
||
|
|
||
| def test_import_rejects_angles_for_missing_servos(tmp_path): | ||
| path = write_csv(tmp_path / "t.csv", ["0,10,20,30,null"]) | ||
|
|
||
| with pytest.raises(ValueError, match="more than 2 servos"): | ||
| make_arm(2).import_timeline_from_csv(path) | ||
|
|
||
|
|
||
| def test_import_rejects_missing_servo_columns(tmp_path): | ||
| path = tmp_path / "t.csv" | ||
| path.write_text("Timestep,Servo1,Servo2\n0,10,20\n") | ||
|
|
||
| with pytest.raises(ValueError, match="Servo1-Servo4"): | ||
| make_arm(2).import_timeline_from_csv(str(path)) | ||
|
|
||
|
|
||
| def test_export_rejects_more_than_four_angles(tmp_path): | ||
| with pytest.raises(ValueError, match="more than 4 angles"): | ||
| make_arm(4).export_timeline_to_csv([[1, 2, 3, 4, 5]], str(tmp_path)) | ||
|
|
||
| assert list(tmp_path.iterdir()) == [] |
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.