Skip to content

Commit

Permalink
Merge pull request #19 from Team3128/manip+wrist-inverted
Browse files Browse the repository at this point in the history
Wrist
  • Loading branch information
Mason-Lam authored Sep 29, 2023
2 parents 9a16e28 + fd2feb0 commit 09fbe00
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 7 deletions.
46 changes: 40 additions & 6 deletions src/main/java/frc/team3128/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,15 +605,49 @@ public static class IntakeConstants {

}

public static class WristConstants {

public static final double kP = 0.4;
public static final double kI = 0;
public static final double kD = 0;

public static final double kS = 0;
public static final double kV = 0;
public static final double kG = 0;

public static final double GEAR_RATIO = 108.0;

public static final double ROTATION_TO_DEGREES = 360;

public static final double ENCODER_CONVERSION_FACTOR_TO_DEGREES = 360;

public static final double ANGLE_OFFSET = 0;
public static final int ENCODER_DIO_ID = 8;

public static final int MIN_ANGLE = -45;
public static final int MAX_ANGLE = 90;

public static final double VELOCITY_SETPOINT = 0.5;
public static final double INTAKE_TOLERANCE = 7.5;

//Motor ID
public static final int WRIST_ID = 25;

//Sensor IDs

}

public static class ManipulatorConstants{
public static final int ROLLER_MOTOR_ID = 13;
public static final double ROLLER_POWER = 0.6;
public static final double STALL_POWER = 0.25;
public static final int ROLLER_MOTOR_ID = 0;
public static final double ROLLER_POWER = 0.9;
public static final double STALL_POWER_CONE = 0.2;
public static final double STALL_POWER_CUBE = 0.0;

public static final double CURRENT_THRESHOLD = 5;
public static final double ABSOLUTE_THRESHOLD = 20;

public static final double ROLLER_VOLTAGE = 8;
public static final double CURRENT_THRESHOLD_CONE = 20;
public static final double CURRENT_THRESHOLD_CUBE = 15;

public static final double ABSOLUTE_THRESHOLD = 20;
}

public static class ElevatorConstants {
Expand Down
49 changes: 48 additions & 1 deletion src/main/java/frc/team3128/commands/CmdManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,66 @@
import edu.wpi.first.wpilibj2.command.ScheduleCommand;
import edu.wpi.first.wpilibj2.command.StartEndCommand;
import edu.wpi.first.wpilibj2.command.WaitCommand;
import edu.wpi.first.wpilibj2.command.WaitUntilCommand;
import frc.team3128.RobotContainer;
import frc.team3128.Constants.WristConstants;
import frc.team3128.common.hardware.input.NAR_XboxController;
import frc.team3128.subsystems.Elevator;

import frc.team3128.common.narwhaldashboard.NarwhalDashboard;
import frc.team3128.subsystems.Led;
import frc.team3128.subsystems.Wrist;
import frc.team3128.subsystems.Wrist.WristPosition;
import frc.team3128.subsystems.Manipulator;
import frc.team3128.subsystems.Vision;
import frc.team3128.subsystems.Elevator;

public class CmdManager {
private static Led led = Led.getInstance();
private static Wrist wrist = Wrist.getInstance();
private static Manipulator manipulator = Manipulator.getInstance();
private static NAR_XboxController controller = RobotContainer.controller;
private static Elevator elevator = Elevator.getInstance();



private CmdManager() {}

public static CommandBase CmdWrist(double setpoint) {
return sequence(
runOnce(()-> wrist.startPID(setpoint), wrist),
waitUntil(()-> wrist.atSetpoint())
);
}

public static CommandBase CmdWrist(WristPosition position) {
return sequence(
runOnce(()-> wrist.startPID(position.wristAngle), wrist),
waitUntil(()-> wrist.atSetpoint())
);
}

public static CommandBase CmdMoveWrist(double power) {
return new InstantCommand(() -> wrist.set(power), wrist);
}

public static CommandBase CmdManipIntake(Boolean cone) {
return sequence(
runOnce(()-> manipulator.intake(cone), manipulator),
waitSeconds(0.4),
waitUntil(()-> manipulator.hasObjectPresent()),
waitSeconds(cone ? 0.15 : 0),
runOnce(()-> manipulator.stallPower())
);
}

public static CommandBase CmdManipOuttake() {
return new InstantCommand(()-> manipulator.outtake(), manipulator);
}

public static CommandBase CmdStopManip() {
return new InstantCommand(()-> manipulator.stopRoller(), manipulator);
}

public static CommandBase vibrateController() {
return new ScheduleCommand(new WaitCommand(0.5).deadlineWith(new StartEndCommand(() -> controller.startVibrate(), () -> RobotContainer.controller.stopVibrate())));
}
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/frc/team3128/subsystems/Manipulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package frc.team3128.subsystems;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.team3128.common.hardware.motorcontroller.NAR_TalonSRX;
import frc.team3128.common.utility.NAR_Shuffleboard;
import static frc.team3128.Constants.ManipulatorConstants.*;
import com.ctre.phoenix.motorcontrol.NeutralMode;

public class Manipulator extends SubsystemBase {

public NAR_TalonSRX m_roller;

private static Manipulator instance;

public static boolean isCone = true;

public boolean isOuttaking = false;

public static Manipulator getInstance() {
if (instance == null){
instance = new Manipulator();
}

return instance;
}

public Manipulator(){
configMotor();
initShuffleboard();
}

//TODO Move this to a trigger in robot container
@Override
public void periodic() {
if (Math.abs(getCurrent()) > ABSOLUTE_THRESHOLD + 40 && !isOuttaking)
stallPower();
}

private void configMotor(){
m_roller = new NAR_TalonSRX(ROLLER_MOTOR_ID);
m_roller.setInverted(true);
m_roller.setNeutralMode(NeutralMode.Brake);
}

public void intake(boolean cone) {
isOuttaking = false;
isCone = cone;
if (isCone) reverse();
else forward();
}

public void outtake(){
isOuttaking = true;
if (!isCone) reverse();
else forward();
}

public void stallPower() {
set(isCone ? -STALL_POWER_CONE : STALL_POWER_CUBE);
}

public void set(double power){
m_roller.set(power);
}

public void forward(){
m_roller.set(ROLLER_POWER);
}

public void reverse(){
m_roller.set(-ROLLER_POWER);
}

public void stopRoller(){
m_roller.set(0);
}

public boolean hasObjectPresent(){
return isCone ? Math.abs(getCurrent()) > CURRENT_THRESHOLD_CONE : Math.abs(getCurrent()) > CURRENT_THRESHOLD_CUBE;
}

public double getCurrent(){
return m_roller.getStatorCurrent();
}

public double getVoltage() {
return m_roller.getMotorOutputVoltage();
}

public void initShuffleboard() {
NAR_Shuffleboard.addData("Manipulator", "Manip current", () -> getCurrent(), 0, 1);
NAR_Shuffleboard.addData("Manipulator", "get", () -> m_roller.getMotorOutputPercent(), 0, 3);
NAR_Shuffleboard.addData("Manipulator", "ObjectPresent", ()-> hasObjectPresent(), 1, 1);
}
}
89 changes: 89 additions & 0 deletions src/main/java/frc/team3128/subsystems/Wrist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package frc.team3128.subsystems;

import com.revrobotics.CANSparkMax.IdleMode;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.util.Units;

import static frc.team3128.Constants.WristConstants.*;

import frc.team3128.common.hardware.motorcontroller.NAR_CANSparkMax;
import frc.team3128.common.hardware.motorcontroller.NAR_CANSparkMax.EncoderType;
import frc.team3128.common.utility.NAR_Shuffleboard;

public class Wrist extends NAR_PIDSubsystem {
public NAR_CANSparkMax m_wrist;

private static Wrist instance;

public enum WristPosition {
SCORE_CONE(0),
SCORE_CUBE(0),

NEUTRAL(0),

SINGLE_SHELF_CONE(0),
SINGLE_SHELF_CUBE(0),
DOUBLE_SHELF_CONE(0),
DOUBLE_SHELF_CUBE(0),
GROUND_PICKUP_CONE(0),
GROUND_PICKUP_CUBE(0);

public final double wristAngle;

private WristPosition(double wristAngle) {
this.wristAngle = wristAngle;
}
}

public static Wrist getInstance() {
if (instance == null){
instance = new Wrist();
}
return instance;
}

public Wrist() {
super(new PIDController(kP, kI, kD), kS, kV, kG);
setkG_Function(()-> Math.cos(Units.degreesToRadians(getMeasurement())));
setConstraints(MIN_ANGLE, MAX_ANGLE);
configMotor();
}

private void configMotor() {
m_wrist = new NAR_CANSparkMax(WRIST_ID, EncoderType.Absolute, MotorType.kBrushless);
m_wrist.setInverted(false);
m_wrist.setIdleMode(IdleMode.kBrake);
m_wrist.setSmartCurrentLimit(40);
resetEncoder();
}

@Override
protected void useOutput(double output, double setpoint) {
m_wrist.set(MathUtil.clamp(output / 12.0, -1, 1));
}

@Override
public double getMeasurement() {
// return m_wrist.getSelectedSensorPosition() * ROTATION_TO_DEGREES / GEAR_RATIO + ANGLE_OFFSET;
return 12.3;
// return MathUtil.inputModulus(-m_encoder.get() * ENCODER_CONVERSION_FACTOR_TO_DEGREES - ANGLE_OFFSET,-180, 180);
}

public void resetEncoder() {
m_wrist.setSelectedSensorPosition(0);
}

public void set(double power) {
m_wrist.set(power);
}

public void initShuffleboard() {
// NAR_Shuffleboard.addData("Manipulator", "Manip current", () -> getCurrent(), 0, 1);
// NAR_Shuffleboard.addData("Manipulator", "get", () -> m_roller.getMotorOutputPercent(), 0, 3);
NAR_Shuffleboard.addData("Wrist", "angle", ()-> m_wrist.getSelectedSensorPosition(), 1, 1);
}

}

0 comments on commit 09fbe00

Please sign in to comment.