Skip to content
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

add sound code for mech and driver station #1

Open
wants to merge 21 commits into
base: crol-mechs
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "java"
id "edu.wpi.first.GradleRIO" version "2023.2.1"
id "edu.wpi.first.GradleRIO" version "2023.4.3"
}

sourceCompatibility = JavaVersion.VERSION_11
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
17 changes: 17 additions & 0 deletions src/main/deploy/How_To_Run_DS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
To run the sound system on the 2023 Haunted House:

1. Plug the 3.5mm audio cable into the speaker and the audio jack on the driverstation computer.
2. Save 'driverstation_script.py' and 'explosion_sound.wav' to a directory on the driverstation computer.
3. If python3 is not installed, install it.
4. If desired (such as if running on non-GRT computer), create a virtual environment (venv) with python3.
5. Install pygame and pynetworktables:
Run 'python3 -m pip install pynetworktables' to install the module needed for networktables comms.
Run 'python3 -m pip install pygame' to install the module needed for sound playback.
6. Once on robot WiFi, trigger the python3 script:
either by entering 'python3 <filepath/driverstation_script.py>
or using the editor's run function (e.g. VSCode run)
7. Script should print an initialization confirmation.
8. When triggered via networktables on robot, code should play audio file.

Troubleshooting:
1. Change 'debug' variable to True in code to print out trigger value constantly (to check if values are being received).
33 changes: 33 additions & 0 deletions src/main/deploy/driverstation_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# BEFORE RUNNING THIS:
# run 'python3 -m pip install pynetworktables' to install the module needed for networktables comms
# run 'python3 -m pip install pygame' to install the module needed for sound playback

from networktables import NetworkTables # <--- this will raise an error when in the java env, create a python venv on DS and run this there
import pygame

NetworkTables.initialize(server='10.1.92.2')

audio_table = NetworkTables.getTable("audio")

pygame.mixer.init()
sound = pygame.mixer.Sound('explosion_sound.wav')

old_trigger_value = 0.0
debug = False

print("GRT HH Driverstation script initialized and running.")

while True:
trigger_value = audio_table.getNumber("trigger_audio", -1.0)
if debug:
print("Trigger value received: ", trigger_value)

if old_trigger_value == 0.0 and trigger_value == 1.0:
sound.play()

old_trigger_value = trigger_value





3 changes: 0 additions & 3 deletions src/main/deploy/example.txt

This file was deleted.

Binary file added src/main/deploy/explosion_sound.wav
Binary file not shown.
20 changes: 11 additions & 9 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package frc.robot;

import frc.robot.subsystems.AryaMech;
import frc.robot.subsystems.LukeMech;
import frc.robot.subsystems.SolenoidMech;
import edu.wpi.first.wpilibj.PneumaticsControlModule;

Expand All @@ -21,21 +23,21 @@ public class RobotContainer {
private final SolenoidMech vivienMech;
private final SolenoidMech seanMech;
private final SolenoidMech riyaMech;
private final SolenoidMech christineMech;
private final SolenoidMech gregMech;
private final SolenoidMech williamMech;

private final LukeMech lukeMech;
private final AryaMech aryaMech;


/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
christineMech = new SolenoidMech(pcm1.makeSolenoid(0), 5, 5);
gregMech = new SolenoidMech(pcm1.makeSolenoid(4), 5, 5);
williamMech = new SolenoidMech(pcm1.makeSolenoid(5), 5, 5);
seanMech = new SolenoidMech(pcm2.makeSolenoid(0), 5, 5);
riyaMech = new SolenoidMech(pcm2.makeSolenoid(1), 5, 5);
vivienMech = new SolenoidMech(pcm2.makeSolenoid(2), 5, 5);
// Configure the trigger bindings
gregMech = new SolenoidMech(pcm1.makeSolenoid(4), 20, 20);
williamMech = new SolenoidMech(pcm1.makeSolenoid(7), 3, 3, .5);
seanMech = new SolenoidMech(pcm2.makeSolenoid(0), 1.5, 6);
riyaMech = new SolenoidMech(pcm2.makeSolenoid(2), 7.5, 14);
vivienMech = new SolenoidMech(pcm2.makeSolenoid(4), 2, 5);
lukeMech = new LukeMech(pcm1.makeSolenoid(5), pcm1.makeSolenoid(6));
aryaMech = new AryaMech(pcm1.makeSolenoid(0), pcm1.makeSolenoid(1), pcm1.makeSolenoid(2));
}

}
128 changes: 128 additions & 0 deletions src/main/java/frc/robot/subsystems/AryaMech.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package frc.robot.subsystems;

import javax.swing.text.StyledEditorKit.BoldAction;

import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.subsystems.leds.LEDLayer;
import frc.robot.subsystems.leds.LEDStrip;
import edu.wpi.first.networktables.DoublePublisher;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;

public class AryaMech extends SubsystemBase{
private Timer timer;
private Solenoid shortSolenoid;
private Solenoid longSolenoid;
private final Solenoid ledStrip;

private double SHORT_OUT = 40;
private double END = SHORT_OUT + 16;
private double FLASH_INTERVAL = 0.15;
private double SHORT_INTERVAL_IN = 0.2;
private double SHORT_INTERVAL_OUT= 2.0;


private boolean out;

NetworkTableInstance inst;
NetworkTable table;
DoublePublisher audioPub;
private Timer audio_timer;
private final double time_to_wait_before_turning_off_audio_signal;
private boolean s40 = false;
private boolean s41 = false;
private boolean s42 = false;
private boolean s46 = false;
private boolean led = false;


private Timer ledTimer;
private Timer pumpTimer;

public AryaMech(Solenoid shortSolenoid, Solenoid longSolenoid, Solenoid ledStrip){
this.shortSolenoid = shortSolenoid;
this.longSolenoid = longSolenoid;
this.ledStrip = ledStrip;

inst = NetworkTableInstance.getDefault();
table = inst.getTable("audio");
audioPub = table.getDoubleTopic("trigger_audio").publish();
audio_timer = new Timer();
audio_timer.start();
pumpTimer = new Timer();
time_to_wait_before_turning_off_audio_signal = 1.0;

ledTimer = new Timer();

timer = new Timer();
timer.start();
pumpTimer.start();

out = true;

}

public void periodic() {
//skeleton pumps thing to set off tnt

if(timer.get() < SHORT_OUT && pumpTimer.advanceIfElapsed(out ? SHORT_INTERVAL_IN : SHORT_INTERVAL_OUT)){
shortSolenoid.set(out);
out = !out;
}

if(timer.hasElapsed(SHORT_OUT) && !s40){
shortSolenoid.set(true);
s40 = true;
}
if(timer.hasElapsed(SHORT_OUT + 1.7) && !s41){
shortSolenoid.set(false);
s41 = true;
}

// audioPub.set(1.0); // this publishes the trigger signal to networktables, which gets picked up by the driverstation python script
if(timer.hasElapsed(SHORT_OUT + 2) && !s42){
audioPub.set(1.0); // Set the value on networktables to zero so we don't restart the audio
ledTimer.reset();
ledTimer.start();
s42 = true;
}

if(timer.hasElapsed(SHORT_OUT + 6) && !s46){
longSolenoid.set(true);
s46 = true;
}

if(timer.hasElapsed(SHORT_OUT + 7) && ledTimer.advanceIfElapsed(FLASH_INTERVAL)){
System.out.println("asdfasdf");
led = !led;
ledStrip.set(led);
} else if (timer.hasElapsed(SHORT_OUT + 4) && ledTimer.advanceIfElapsed(FLASH_INTERVAL * 3)){
System.out.println("asdfasdf");
led = !led;
ledStrip.set(led);
} else if (timer.hasElapsed(SHORT_OUT + 2) && ledTimer.advanceIfElapsed(FLASH_INTERVAL * 5)){
System.out.println("asdfasdf");
led = !led;
ledStrip.set(led);
}

if (timer.advanceIfElapsed(END)){
longSolenoid.set(false);
ledStrip.set(false);
s40 = false;
s41 = false;
s42 = false;
s46 = false;
ledTimer.stop();
audioPub.set(0.0);
}
// if(audio_timer.advanceIfElapsed(.05)){
// System.out.println(((int) (timer.get() * 10)) / 10. + " SHORT: " + shortSolenoid.get() + " LONG: " + longSolenoid.get() + " LED:" + led);
// }

}

}
78 changes: 78 additions & 0 deletions src/main/java/frc/robot/subsystems/LukeMech.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package frc.robot.subsystems;

import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class LukeMech extends SubsystemBase{
private Timer timer;
private Solenoid verSolenoid;
private Solenoid horSolenoid;

private double vertUp;
private double vertDown;
private double horOut;
private double horIn;
private int state = 0;
private int oldState = 3;


public LukeMech(Solenoid verSolenoid, Solenoid horSolenoid){
this.verSolenoid = verSolenoid;
this.horSolenoid = horSolenoid;

//ik this looks sus but its for ease of change
vertUp = 7;
horOut = 5;
horIn = 3;
vertDown = 3;

timer= new Timer();
timer.start();

}

public void periodic() {

if((state == 1 || state == 2) && !verSolenoid.get()){
state = 0;
}


if(timer.hasElapsed(vertUp) && state == 0){
verSolenoid.set(true);
state = 1;
timer.reset();
timer.start();
}
else if (timer.hasElapsed(horOut) && state == 1){
if(verSolenoid.get()){
horSolenoid.set(true);
}
state = 2;
timer.reset();
timer.start();
}
else if (timer.hasElapsed(horIn) && state == 2){
if(verSolenoid.get()){
horSolenoid.set(false);
}
state = 3;
timer.reset();
timer.start();
}
else if (timer.hasElapsed(vertDown) && state == 3){
if(!horSolenoid.get()){
verSolenoid.set(false);
}
state = 0;
timer.reset();
timer.start();
}

// System.out.println((((int) (timer.get() * 10)) / 10.) +" " + state + "," + verSolenoid.get() + " , " + horSolenoid.get());


}
}
19 changes: 13 additions & 6 deletions src/main/java/frc/robot/subsystems/SolenoidMech.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

package frc.robot.subsystems;

import edu.wpi.first.wpilibj.PneumaticsControlModule;
import edu.wpi.first.wpilibj.PneumaticsModuleType;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.CommandBase;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class SolenoidMech extends SubsystemBase {

private double uptime;
private double downtime;
private double randomness = 0.0;
private double randomnessUp = 0.0;
private double randomnessDown = 0.0;
private Timer timer;
private Solenoid solenoid;

Expand All @@ -30,7 +28,14 @@ public SolenoidMech(Solenoid solenoid, double uptime, double downtime) {

public SolenoidMech(Solenoid solenoid, double uptime, double downtime, double randomness) {
this(solenoid, uptime, downtime);
this.randomness = randomness;
this.randomnessUp = randomness;
this.randomnessDown = randomness;
}

public SolenoidMech(Solenoid solenoid, double uptime, double downtime, double randomnessUp, double randomnessDown) {
this(solenoid, uptime, downtime);
this.randomnessUp = randomnessUp;
this.randomnessDown = randomnessDown;
}


Expand All @@ -41,7 +46,9 @@ public SolenoidMech(Solenoid solenoid, double uptime, double downtime, double ra
public void periodic() {
if(timer.advanceIfElapsed(timeUntilNextEvent)){
up = !up;
timeUntilNextEvent = (up ? uptime : downtime) * (1 + (Math.random() * 2 - 1) * randomness);
// Time is random in the range [uptime - uptime * randomnesesUp, uptime + uptime * randomnessUp] if switching to up
// or [downtime - downtime * randomnesesDown, downtime + downtime * randomnessDown] if switching to down
timeUntilNextEvent = (up ? uptime * (1 + (Math.random() * 2 - 1) * randomnessUp) : downtime * (1 + (Math.random() * 2 - 1) * randomnessDown)) ;
}

solenoid.set(up);
Expand Down
Loading