Skip to content

Commit

Permalink
Shooter Subsystem PID Control
Browse files Browse the repository at this point in the history
added basic PID functionality to shooter subsystem
  • Loading branch information
npwtub committed Jan 27, 2024
1 parent f1178a8 commit 7dd91e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/jmhsrobotics/frc2024/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.jmhsrobotics.frc2024.subsystems.drive.DriveSubsystem;
import org.jmhsrobotics.frc2024.subsystems.drive.commands.DriveCommand;
import org.jmhsrobotics.frc2024.subsystems.drive.commands.IntakeCommand;
import org.jmhsrobotics.frc2024.subsystems.shooter.ShooterSubsystem;
import org.jmhsrobotics.frc2024.subsystems.shooter.shooterCommands.ShooterCommand;
import org.jmhsrobotics.frc2024.subsystems.vision.VisionSubsystem;

import com.pathplanner.lib.auto.AutoBuilder;
Expand All @@ -35,6 +37,8 @@ public class RobotContainer {

private final VisionSubsystem vision = new VisionSubsystem(this.driveSubsystem);

private final ShooterSubsystem shooterSubsystem = new ShooterSubsystem();

private final SendableChooser<Command> autoChooser;

public RobotContainer() {
Expand All @@ -47,6 +51,8 @@ public RobotContainer() {
configurePathPlanner();
autoChooser = AutoBuilder.buildAutoChooser();
SmartDashboard.putData("Auto Chooser", autoChooser);
ShooterCommand shooterCommand = new ShooterCommand(2000, shooterSubsystem);
SmartDashboard.putData("Shooter Command", shooterCommand);
}

private void configurePathPlanner() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.jmhsrobotics.frc2024.subsystems.shooter;

import com.revrobotics.CANSparkMax;
import com.revrobotics.RelativeEncoder;
import com.revrobotics.CANSparkBase.IdleMode;
import com.revrobotics.CANSparkLowLevel.MotorType;

import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class ShooterSubsystem extends SubsystemBase {
private CANSparkMax motor1;
private CANSparkMax motor2;
private CANSparkMax motor1 = new CANSparkMax(7, MotorType.kBrushless);
private CANSparkMax motor2 = new CANSparkMax(8, MotorType.kBrushless);;
private RelativeEncoder encoder;

// private double speed;

Expand All @@ -26,6 +30,11 @@ private void initializeMotors() {
motor2.follow(motor1);
}

public double getRPM() {
encoder = motor1.getEncoder();
return encoder.getVelocity();
}

public void setSpeed(double speed) {
motor1.set(speed);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package org.jmhsrobotics.frc2024.subsystems.shooter.shooterCommands;

import org.jmhsrobotics.frc2024.subsystems.shooter.ShooterSubsystem;
import org.jmhsrobotics.frc2024.controlBoard.ControlBoard;

import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.wpilibj2.command.Command;

public class ShooterCommand extends Command {
private ShooterSubsystem shooterSubsystem;
private ControlBoard control;
private ShooterSubsystem shooter;
private double targetRPM;
private PIDController pid;

public ShooterCommand(double targetRPM, ShooterSubsystem shooterSubsystem) {
this.shooter = shooterSubsystem;
this.targetRPM = targetRPM;
pid = new PIDController(.1, 0, 0);

addRequirements(this.shooter);
}

@Override
public void initialize() {
pid.reset();
pid.setSetpoint(targetRPM);
}

public ShooterCommand(ShooterSubsystem shooterSubsystem, ControlBoard control) {
this.shooterSubsystem = shooterSubsystem;
this.control = control;
@Override
public boolean isFinished() {
return false;
}

addRequirements(this.shooterSubsystem);
@Override
public void end(boolean interrupted) {
shooter.setSpeed(0);
}

@Override
public void execute() {
double motorSpeed = control.shooterInput();
shooterSubsystem.setSpeed(motorSpeed);
double motorSpeed = pid.calculate(shooter.getRPM(), targetRPM);
shooter.setSpeed(motorSpeed);
}
}

0 comments on commit 7dd91e9

Please sign in to comment.