generated from Programming-TRIGON/RobotTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Orchestra #47
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
ShmayaR
wants to merge
48
commits into
main
Choose a base branch
from
orchestra
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
Orchestra #47
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
836690f
added orchestra stuff
ShmayaR 51bd0c6
changed name from TrigonOrchestra to Orchestra
ShmayaR ae0cd14
Ugliness so @ShmayaR can clean :)
Strflightmight09 38060ab
Hard code 💪
Strflightmight09 4f06706
added java docs
ShmayaR c6169f9
rewrote java docs
ShmayaR 8c7f53a
rewrote java docs again
ShmayaR 567a210
Update Orchestra.java
ShmayaR 9ae124b
fixed up java doc
ShmayaR 52dd814
Update Orchestra.java
ShmayaR 4488b3e
Update TalonFXMotor.java
ShmayaR b015457
Update Orchestra.java
ShmayaR ac91426
shortened a java doc into one @return
ShmayaR d6da940
Fix error message
Strflightmight09 f621f70
No more throwing
Strflightmight09 f44fc18
Merge branch 'main' into orchestra
Strflightmight09 3012beb
Quick fix
Strflightmight09 5289a52
WOW
Strflightmight09 32e3df0
Try this
Strflightmight09 9048ec2
ok sure
Strflightmight09 cc95c54
tets
Strflightmight09 33c5a01
NOw tis goinna work trust
Strflightmight09 52c9fe9
I thiikn we got it guys
Strflightmight09 4183fa2
A bit of an update
Strflightmight09 dba95ee
ID 0 😢
Strflightmight09 4b50c8a
Oops
Strflightmight09 cf18c47
Fix
Strflightmight09 b8b63f6
Test idk
Strflightmight09 9e42192
oops
Strflightmight09 e5669f2
Optimization
Strflightmight09 4dccd5b
test
Strflightmight09 ed478c8
more test
Strflightmight09 bee510a
Smth idek
Strflightmight09 4091b2c
For testing purposes
Strflightmight09 5ab2372
test
Strflightmight09 533042a
wowow
Strflightmight09 4a2c572
ArrayList is goated
Strflightmight09 5828631
?
Strflightmight09 06bd78d
Ughhhhh
Strflightmight09 8ce853f
O
Strflightmight09 f475382
WTHELLYYYYYYYYYYYYY
Strflightmight09 0da6572
whatttttttttttt
Strflightmight09 b0e0a2c
JIOHSEF"DS
Strflightmight09 ee67c54
test ig
Strflightmight09 c4a0c2f
Fine
Strflightmight09 cead20d
Simplify
Strflightmight09 609f645
Small improvement
Strflightmight09 f0f5c87
Replay protection and used IDs instead of skipped
Strflightmight09 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
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,234 @@ | ||
| package org.trigon.utilities; | ||
|
|
||
| import com.ctre.phoenix6.hardware.TalonFX; | ||
| import edu.wpi.first.wpilibj2.command.Command; | ||
| import edu.wpi.first.wpilibj2.command.FunctionalCommand; | ||
| import edu.wpi.first.wpilibj2.command.InstantCommand; | ||
| import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * A class that uses the {@link com.ctre.phoenix6.Orchestra} library in Phoenix 6 to play a .chrp file. | ||
| */ | ||
| public class Orchestra extends SubsystemBase { | ||
| private static final Orchestra INSTANCE = new Orchestra(); | ||
| private static final com.ctre.phoenix6.Orchestra ORCHESTRA = new com.ctre.phoenix6.Orchestra(); | ||
| private static final HashMap<Integer, TalonFX> MOTORS = new HashMap<>(); | ||
| private static ArrayList<Integer> USED_MOTOR_IDS = new ArrayList<>(); | ||
| private static boolean SHOULD_UPDATE_USED_MOTORS = false; | ||
|
|
||
| /** | ||
| * Adds a motor to the Orchestra. | ||
| * The Orchestra can then play the music from those motors. | ||
| * | ||
| * @param motor the motor to be added to the Orchestra | ||
| */ | ||
| public static void addMotor(TalonFX motor, int id) { | ||
| MOTORS.put(id, motor); | ||
| } | ||
|
|
||
| public static Command getPlayFileCommand(String filePath, int totalTracks) { | ||
| return new FunctionalCommand( | ||
| () -> playFile(filePath, totalTracks), | ||
| () -> updateMotors(totalTracks), | ||
| (interrupted) -> stop(), | ||
| () -> false, | ||
| INSTANCE | ||
| ).ignoringDisable(true); | ||
| } | ||
|
|
||
| public static Command getPlayFileCommand(String filePath, int[] motorsPerTrack) { | ||
| return new FunctionalCommand( | ||
| () -> playFile(filePath, motorsPerTrack), | ||
| () -> updateMotors(motorsPerTrack), | ||
| (interrupted) -> stop(), | ||
| () -> false, | ||
| INSTANCE | ||
| ).ignoringDisable(true); | ||
| } | ||
|
|
||
| public static Command getStopCommand() { | ||
| return new InstantCommand( | ||
| Orchestra::stop, | ||
| INSTANCE | ||
| ).ignoringDisable(true); | ||
| } | ||
|
|
||
| public static Command getPauseCommand() { | ||
| return new InstantCommand( | ||
| Orchestra::pause, | ||
| INSTANCE | ||
| ).ignoringDisable(true); | ||
| } | ||
|
|
||
| public static Command getPlayCommand() { | ||
| return new InstantCommand( | ||
| Orchestra::play, | ||
| INSTANCE | ||
| ).ignoringDisable(true); | ||
| } | ||
|
|
||
| /** | ||
| * @return whether orchestra is currently playing a file | ||
| */ | ||
| public static boolean isOrchestraCurrentlyPlaying() { | ||
| return ORCHESTRA.isPlaying(); | ||
| } | ||
|
|
||
| /** | ||
| * @return the play time of the current file in seconds | ||
| */ | ||
| public static double getPlayTimeSeconds() { | ||
| return ORCHESTRA.getCurrentTime(); | ||
| } | ||
|
|
||
| /** | ||
| * Adds IDs of motors that should be used when playing a file. | ||
| * | ||
| * @param newIDs the IDs of the motors that should be used | ||
| */ | ||
| public static void addUsedMotorIDs(Integer... newIDs) { | ||
| if (USED_MOTOR_IDS.containsAll(Arrays.asList(newIDs))) | ||
| return; | ||
|
|
||
| USED_MOTOR_IDS.addAll(Arrays.asList(newIDs)); | ||
| setUsedMotorIDs(USED_MOTOR_IDS); | ||
| } | ||
|
|
||
| /** | ||
| * Removes IDs of motors that should no longer be used when playing a file. | ||
| * | ||
| * @param idsToRemove the IDs of motors that should no longer be used | ||
| */ | ||
| public static void removeUsedMotorIDs(Integer... idsToRemove) { | ||
| if (!USED_MOTOR_IDS.containsAll(Arrays.asList(idsToRemove))) | ||
| return; | ||
|
|
||
| USED_MOTOR_IDS.removeAll(Arrays.asList(idsToRemove)); | ||
| setUsedMotorIDs(USED_MOTOR_IDS); | ||
| } | ||
|
|
||
| /** | ||
| * Clears all used motor IDs. | ||
| */ | ||
| public static void clearUsedMotorIDs() { | ||
| setUsedMotorIDs(new ArrayList<>()); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the used motor IDs to a specific list. | ||
| * | ||
| * @param usedMotorIDs the list of motor IDs to be used | ||
| */ | ||
| public static void setUsedMotorIDs(ArrayList<Integer> usedMotorIDs) { | ||
| USED_MOTOR_IDS = usedMotorIDs; | ||
| SHOULD_UPDATE_USED_MOTORS = true; | ||
| } | ||
|
|
||
| /** | ||
| * Plays a .chrp file and assigns a track to each motor. | ||
| * A .chrp file stores music as tracks that can be played separately. | ||
| * The tracks are assigned linearly and loop back if there are extra motors. | ||
| * | ||
| * @param filePath the path of the .chrp file to be played by the Orchestra | ||
| * @param totalTracks the number of tracks in the .chrp file | ||
| */ | ||
| private static void playFile(String filePath, int totalTracks) { | ||
| applyTracks(totalTracks); | ||
| addAndPlayFile(filePath); | ||
| } | ||
|
|
||
| /** | ||
| * plays a .chrp file and assigns a track to each motor. | ||
| * A .chrp file stores music as tracks that can be played separately. | ||
| * The tracks are assigned by the {@code motorsPerTrack}. | ||
| * Each slot represents a track. | ||
| * Each value in the slot represents the number of motors that will be assigned that track. | ||
| * | ||
| * @param filePath the path of the .chrp file to be added to the Orchestra | ||
| * @param motorsPerTrack number of motors that should be assigned to each track | ||
| */ | ||
| private static void playFile(String filePath, int[] motorsPerTrack) { | ||
| applyTracks(motorsPerTrack); | ||
| addAndPlayFile(filePath); | ||
| } | ||
|
|
||
| private static void updateMotors(int totalTracks) { | ||
| if (!SHOULD_UPDATE_USED_MOTORS) | ||
| return; | ||
| ORCHESTRA.clearInstruments(); | ||
|
|
||
| applyTracks(totalTracks); | ||
| SHOULD_UPDATE_USED_MOTORS = false; | ||
| } | ||
|
|
||
| private static void updateMotors(int[] motorsPerTrack) { | ||
| if (!SHOULD_UPDATE_USED_MOTORS) | ||
| return; | ||
| ORCHESTRA.clearInstruments(); | ||
|
|
||
| applyTracks(motorsPerTrack); | ||
| SHOULD_UPDATE_USED_MOTORS = false; | ||
| } | ||
|
|
||
| private static void applyTracks(int totalTracks) { | ||
| for (int i = 1; i < MOTORS.size() + 1; i++) | ||
| if (shouldUseMotor(i) && MOTORS.containsKey(i)) | ||
| ORCHESTRA.addInstrument(MOTORS.get(i), i % totalTracks); | ||
| } | ||
|
|
||
| private static void applyTracks(int[] motorsPerTrack) { | ||
| int motorIndex = 1; | ||
|
|
||
| for (int trackIndex = 0; trackIndex < motorsPerTrack.length; trackIndex++) { | ||
| for (int motorsInCurrentTrack = 0; motorsInCurrentTrack < motorsPerTrack[trackIndex]; motorsInCurrentTrack++) { | ||
| if (motorIndex > MOTORS.size()) { | ||
| System.out.println("Orchestra: Not enough motors"); | ||
| return; | ||
| } | ||
| if (shouldUseMotor(motorIndex) && MOTORS.containsKey(motorIndex)) | ||
| ORCHESTRA.addInstrument(MOTORS.get(motorIndex), trackIndex); | ||
| motorIndex++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Stops the music and removes all tracks assigned to motors. | ||
| */ | ||
| private static void stop() { | ||
| ORCHESTRA.stop(); | ||
| ORCHESTRA.clearInstruments(); | ||
Strflightmight09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Pauses the music. | ||
| */ | ||
| private static void pause() { | ||
| ORCHESTRA.pause(); | ||
| } | ||
|
|
||
| /** | ||
| * Plays the stored .chrp file. | ||
| */ | ||
| private static void play() { | ||
| ORCHESTRA.play(); | ||
| } | ||
ShmayaR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Adds a .chrp file to the Orchestra and plays it. | ||
| * | ||
| * @param filePath the path of the .chrp file to be added to the Orchestra | ||
| */ | ||
| private static void addAndPlayFile(String filePath) { | ||
Strflightmight09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ORCHESTRA.loadMusic(filePath); | ||
| play(); | ||
| } | ||
|
|
||
| private static boolean shouldUseMotor(Integer id) { | ||
| return USED_MOTOR_IDS.contains(id); | ||
| } | ||
| } | ||
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.