Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
836690f
added orchestra stuff
ShmayaR Feb 14, 2025
51bd0c6
changed name from TrigonOrchestra to Orchestra
ShmayaR Feb 14, 2025
ae0cd14
Ugliness so @ShmayaR can clean :)
Strflightmight09 Feb 16, 2025
38060ab
Hard code 💪
Strflightmight09 Feb 16, 2025
4f06706
added java docs
ShmayaR Feb 16, 2025
c6169f9
rewrote java docs
ShmayaR Feb 16, 2025
8c7f53a
rewrote java docs again
ShmayaR Feb 16, 2025
567a210
Update Orchestra.java
ShmayaR Feb 18, 2025
9ae124b
fixed up java doc
ShmayaR Feb 25, 2025
52dd814
Update Orchestra.java
ShmayaR Feb 25, 2025
4488b3e
Update TalonFXMotor.java
ShmayaR Mar 2, 2025
b015457
Update Orchestra.java
ShmayaR Mar 4, 2025
ac91426
shortened a java doc into one @return
ShmayaR Mar 5, 2025
d6da940
Fix error message
Strflightmight09 Mar 6, 2025
f621f70
No more throwing
Strflightmight09 Jun 24, 2025
f44fc18
Merge branch 'main' into orchestra
Strflightmight09 Jul 3, 2025
3012beb
Quick fix
Strflightmight09 Jul 3, 2025
5289a52
WOW
Strflightmight09 Jul 3, 2025
32e3df0
Try this
Strflightmight09 Jul 3, 2025
9048ec2
ok sure
Strflightmight09 Jul 3, 2025
cc95c54
tets
Strflightmight09 Jul 3, 2025
33c5a01
NOw tis goinna work trust
Strflightmight09 Jul 3, 2025
52c9fe9
I thiikn we got it guys
Strflightmight09 Jul 3, 2025
4183fa2
A bit of an update
Strflightmight09 Jul 5, 2025
dba95ee
ID 0 😢
Strflightmight09 Jul 5, 2025
4b50c8a
Oops
Strflightmight09 Jul 5, 2025
cf18c47
Fix
Strflightmight09 Jul 5, 2025
b8b63f6
Test idk
Strflightmight09 Jul 5, 2025
9e42192
oops
Strflightmight09 Jul 5, 2025
e5669f2
Optimization
Strflightmight09 Jul 6, 2025
4dccd5b
test
Strflightmight09 Jul 7, 2025
ed478c8
more test
Strflightmight09 Jul 7, 2025
bee510a
Smth idek
Strflightmight09 Jul 16, 2025
4091b2c
For testing purposes
Strflightmight09 Jul 16, 2025
5ab2372
test
Strflightmight09 Jul 16, 2025
533042a
wowow
Strflightmight09 Jul 16, 2025
4a2c572
ArrayList is goated
Strflightmight09 Jul 16, 2025
5828631
?
Strflightmight09 Jul 16, 2025
06bd78d
Ughhhhh
Strflightmight09 Jul 16, 2025
8ce853f
O
Strflightmight09 Jul 16, 2025
f475382
WTHELLYYYYYYYYYYYYY
Strflightmight09 Jul 16, 2025
0da6572
whatttttttttttt
Strflightmight09 Jul 16, 2025
b0e0a2c
JIOHSEF"DS
Strflightmight09 Jul 16, 2025
ee67c54
test ig
Strflightmight09 Jul 16, 2025
c4a0c2f
Fine
Strflightmight09 Jul 16, 2025
cead20d
Simplify
Strflightmight09 Jul 16, 2025
609f645
Small improvement
Strflightmight09 Jul 17, 2025
f0f5c87
Replay protection and used IDs instead of skipped
Strflightmight09 Aug 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.trigon.hardware.phoenix6.talonfx.io.RealTalonFXIO;
import org.trigon.hardware.phoenix6.talonfx.io.SimulationTalonFXIO;
import org.trigon.hardware.simulation.MotorPhysicsSimulation;
import org.trigon.utilities.Orchestra;

/**
* A class that represents a TalonFX motor controller.
Expand Down Expand Up @@ -46,6 +47,9 @@ public TalonFXMotor(int id, String motorName, String canbus) {
this.motorInputs = new Phoenix6Inputs(motorName, !canbus.isEmpty());
this.id = id;
motorIO.optimizeBusUsage();

if (!RobotHardwareStats.isReplay())
Orchestra.addMotor(motorIO.getTalonFX(), id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ protected void setPosition(double positionRotations) {

@Override
public void applyConfiguration(TalonFXConfiguration configuration) {
configuration.Audio.BeepOnBoot = false;
configuration.Audio.BeepOnConfig = false;
configuration.Audio.AllowMusicDurDisable = true;

talonFX.getConfigurator().apply(configuration);
}

Expand Down
234 changes: 234 additions & 0 deletions src/main/java/org/trigon/utilities/Orchestra.java
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();
}

/**
* Pauses the music.
*/
private static void pause() {
ORCHESTRA.pause();
}

/**
* Plays the stored .chrp file.
*/
private static void play() {
ORCHESTRA.play();
}

/**
* 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) {
ORCHESTRA.loadMusic(filePath);
play();
}

private static boolean shouldUseMotor(Integer id) {
return USED_MOTOR_IDS.contains(id);
}
}
Loading