Skip to content

Commit

Permalink
intake, robotcontainer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JadedHearth committed Feb 10, 2024
1 parent 4c885a1 commit 33b7082
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 32 deletions.
37 changes: 36 additions & 1 deletion src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@ public static final class FlywheelConstants {
// CAN ID's
public static final int kTopFlywheelSparkMaxCanId = 13;
public static final int kBottomFlywheelSparkMaxCanId = 14;

// TODO implement the below
public static final int kCurrentLimit = 30;

public static final int startingAngle = 30; // TODO TUNE

public static final double armConversion = 1; // TODO TUNE

// Arm PID constants
public static final double kP = 0.1; // TODO TUNE
public static final double kI = 0.0; // TODO TUNE
public static final double kD = 0.0; // TODO TUNE

// Arm Feedforward characterization constants
public static final double ks = 0.10; // TODO TUNE
public static final double kv = 0.05; // TODO TUNE
}

public static final class IntakeConstants {
public static final int kIntakeSparkMaxCanId = 12341; // ! Change before testing

public static final int kCurrentLimit = 30;

public static final int startingAngle = 30; // TODO TUNE

public static final double armConversion = 1; // TODO TUNE

// Arm PID constants
public static final double kP = 0.1; // TODO TUNE
public static final double kI = 0.0; // TODO TUNE
public static final double kD = 0.0; // TODO TUNE

// Arm Feedforward characterization constants
public static final double ks = 0.10; // TODO TUNE
public static final double kv = 0.05; // TODO TUNE
}

public static final class ArmConstants {
Expand Down Expand Up @@ -111,7 +146,7 @@ public static final class Presets {
}

public static final class WristConstants {
public static final int kWristMotorId = 103; // ! Change before testing
public static final int kWristMotorId = 104; // ! Change before testing

public static final int kCurrentLimit = 30;

Expand Down
24 changes: 21 additions & 3 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.commands.DriveCommands;
import frc.robot.commands.FeedForwardCharacterization;
import frc.robot.commands.DriveParts.DriveCommands;
import frc.robot.subsystems.arm.Arm;
import frc.robot.subsystems.arm.ArmIO;
import frc.robot.subsystems.arm.ArmIOSim;
import frc.robot.subsystems.arm.ArmIOSparkMax;
import frc.robot.subsystems.armElevator.ArmElevator;
import frc.robot.subsystems.armElevator.ArmElevatorIO;
import frc.robot.subsystems.armElevator.ArmElevatorIOSim;
import frc.robot.subsystems.armElevator.ArmElevatorIOSparkMax;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.GyroIO;
import frc.robot.subsystems.drive.GyroIOPigeon2;
Expand All @@ -38,7 +42,12 @@
import frc.robot.subsystems.flywheel.Flywheel;
import frc.robot.subsystems.flywheel.FlywheelIO;
import frc.robot.subsystems.flywheel.FlywheelIOSim;
import frc.robot.subsystems.flywheel.TopFlywheelIOSparkMax;
import frc.robot.subsystems.flywheel.FlywheelIOSparkMax;
import frc.robot.subsystems.wrist.Wrist;
import frc.robot.subsystems.wrist.WristIO;
import frc.robot.subsystems.wrist.WristIOSim;
import frc.robot.subsystems.wrist.WristIOSparkMax;

import org.littletonrobotics.junction.networktables.LoggedDashboardChooser;
import org.littletonrobotics.junction.networktables.LoggedDashboardNumber;

Expand All @@ -54,6 +63,8 @@ public class RobotContainer {
private final Flywheel sTopFlywheel;
private final Flywheel sBottomFlywheel;
private final Arm sArm;
private final ArmElevator sArmElevator;
private final Wrist sWrist;

// Controller
private final CommandXboxController driverController = new CommandXboxController(0);
Expand All @@ -79,9 +90,11 @@ public RobotContainer() {
new ModuleIOSparkMax(1),
new ModuleIOSparkMax(2),
new ModuleIOSparkMax(3));
sTopFlywheel = new Flywheel(new TopFlywheelIOSparkMax());
sTopFlywheel = new Flywheel(new FlywheelIOSparkMax());
sBottomFlywheel = new Flywheel(new BottomFlywheelIOSparkMax());
sArm = new Arm(new ArmIOSparkMax() {});
sArmElevator = new ArmElevator(new ArmElevatorIOSparkMax() {});
sWrist = new Wrist(new WristIOSparkMax() {});
// drive = new Drive(
// new GyroIOPigeon2(true),
// new ModuleIOTalonFX(0),
Expand All @@ -103,6 +116,8 @@ public RobotContainer() {
sTopFlywheel = new Flywheel(new FlywheelIOSim());
sBottomFlywheel = new Flywheel(new FlywheelIOSim());
sArm = new Arm(new ArmIOSim() {});
sArmElevator = new ArmElevator(new ArmElevatorIOSim() {});
sWrist = new Wrist(new WristIOSim() {});

break;

Expand All @@ -118,6 +133,9 @@ public RobotContainer() {
sTopFlywheel = new Flywheel(new FlywheelIO() {});
sBottomFlywheel = new Flywheel(new FlywheelIO() {});
sArm = new Arm(new ArmIO() {});
sArmElevator = new ArmElevator(new ArmElevatorIO() {});
sWrist = new Wrist(new WristIO() {});

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package frc.robot.commands;
package frc.robot.commands.DriveParts;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.geometry.Pose2d;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2021-2024 FRC 6328, FRC 5829
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 3 as published by the Free Software Foundation or
// available in the root directory of this project.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package frc.robot.subsystems.flywheel;

import com.revrobotics.CANSparkBase.ControlType;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.CANSparkMax;
import com.revrobotics.RelativeEncoder;
import com.revrobotics.SparkPIDController;
import com.revrobotics.SparkPIDController.ArbFFUnits;
import edu.wpi.first.math.util.Units;
import frc.robot.Constants.FlywheelConstants;

/**
* NOTE: To use the Spark Flex / NEO Vortex, replace all instances of "CANSparkMax" with
* "CANSparkFlex".
*/
public class FlywheelIOSparkMax implements FlywheelIO {
private static final double GEAR_RATIO = 52.0 / 34.0; // May be reciprocal

private final CANSparkMax topShooterMotor =
new CANSparkMax(FlywheelConstants.kTopFlywheelSparkMaxCanId, MotorType.kBrushless);
private final RelativeEncoder topShooterEncoder = topShooterMotor.getEncoder();
private final SparkPIDController topShooterPID = topShooterMotor.getPIDController();

private final CANSparkMax bottomShooterMotor =
new CANSparkMax(FlywheelConstants.kBottomFlywheelSparkMaxCanId, MotorType.kBrushless);
private final RelativeEncoder bottomShooterEncoder = bottomShooterMotor.getEncoder();
private final SparkPIDController bottomShooterPID = bottomShooterMotor.getPIDController();

public FlywheelIOSparkMax() {
topShooterMotor.restoreFactoryDefaults();

topShooterMotor.setCANTimeout(250);

topShooterMotor.setInverted(false);

topShooterMotor.enableVoltageCompensation(12.0);
topShooterMotor.setSmartCurrentLimit(30);

topShooterMotor.burnFlash();
}

@Override
public void updateInputs(FlywheelIOInputs inputs) {
inputs.positionRad = Units.rotationsToRadians(topShooterEncoder.getPosition() / GEAR_RATIO);
inputs.velocityRadPerSec =
Units.rotationsPerMinuteToRadiansPerSecond(topShooterEncoder.getVelocity() / GEAR_RATIO);
inputs.appliedVolts = topShooterMotor.getAppliedOutput() * topShooterMotor.getBusVoltage();
inputs.currentAmps = new double[] {topShooterMotor.getOutputCurrent()};
}

@Override
public void setVoltage(double volts) {
topShooterMotor.setVoltage(volts);
}

@Override
public void setVelocity(double velocityRadPerSec, double ffVolts) {
topShooterPID.setReference(
Units.radiansPerSecondToRotationsPerMinute(velocityRadPerSec) * GEAR_RATIO,
ControlType.kVelocity,
0,
ffVolts,
ArbFFUnits.kVoltage);
}

@Override
public void stop() {
topShooterMotor.stopMotor();
}

@Override
public void configurePID(double kP, double kI, double kD) {
topShooterPID.setP(kP, 0);
topShooterPID.setI(kI, 0);
topShooterPID.setD(kD, 0);
topShooterPID.setFF(0, 0);
}
}
85 changes: 85 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/Intake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2021-2024 FRC 6328, FRC 5829
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 3 as published by the Free Software Foundation or
// available in the root directory of this project.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package frc.robot.subsystems.intake;

import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants.EnvironmentalConstants;
import org.littletonrobotics.junction.AutoLogOutput;
import org.littletonrobotics.junction.Logger;

public class Intake extends SubsystemBase {
private final IntakeIO io;
private final IntakeIOInputsAutoLogged inputs = new IntakeIOInputsAutoLogged();
private final SimpleMotorFeedforward ffModel;

/** Creates a new Flywheel. */
public Intake(IntakeIO io) {
this.io = io;

// Switch constants based on mode (the physics simulator is treated as a
// separate robot with different tuning)
switch (EnvironmentalConstants.currentMode) {
case REAL:
case REPLAY:
ffModel = new SimpleMotorFeedforward(0.1, 0.05);
io.configurePID(1.0, 0.0, 0.0);
break;
case SIM:
ffModel = new SimpleMotorFeedforward(0.0, 0.03);
io.configurePID(0.5, 0.0, 0.0);
break;
default:
ffModel = new SimpleMotorFeedforward(0.0, 0.0);
break;
}
}

@Override
public void periodic() {
io.updateInputs(inputs);
Logger.processInputs("Flywheel", inputs);
}

/** Run open loop at the specified voltage. */
public void runVolts(double volts) {
io.setVoltage(volts);
}

/** Run closed loop at the specified velocity. */
public void runVelocity(double velocityRPM) {
var velocityRadPerSec = Units.rotationsPerMinuteToRadiansPerSecond(velocityRPM);
io.setVelocity(velocityRadPerSec, ffModel.calculate(velocityRadPerSec));

// Log flywheel setpoint
Logger.recordOutput("Flywheel/SetpointRPM", velocityRPM);
}

/** Stops the flywheel. */
public void stop() {
io.stop();
}

/** Returns the current velocity in RPM. */
@AutoLogOutput
public double getVelocityRPM() {
return Units.radiansPerSecondToRotationsPerMinute(inputs.velocityRadPerSec);
}

/** Returns the current velocity in radians per second. */
public double getCharacterizationVelocity() {
return inputs.velocityRadPerSec;
}
}
41 changes: 41 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/IntakeIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021-2024 FRC 6328, FRC 5829
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 3 as published by the Free Software Foundation or
// available in the root directory of this project.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package frc.robot.subsystems.intake;

import org.littletonrobotics.junction.AutoLog;

public interface IntakeIO {
@AutoLog
public static class IntakeIOInputs {
public double positionRad = 0.0;
public double velocityRadPerSec = 0.0;
public double appliedVolts = 0.0;
public double[] currentAmps = new double[] {};
}

/** Updates the set of loggable inputs. */
public default void updateInputs(IntakeIOInputs inputs) {}

/** Run open loop at the specified voltage. */
public default void setVoltage(double volts) {}

/** Run closed loop at the specified velocity. */
public default void setVelocity(double velocityRadPerSec, double ffVolts) {}

/** Stop in open loop. */
public default void stop() {}

/** Set velocity PID constants. */
public default void configurePID(double kP, double kI, double kD) {}
}
Loading

0 comments on commit 33b7082

Please sign in to comment.