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

Aviv elbow #7

Open
wants to merge 13 commits into
base: on-robot
Choose a base branch
from
6 changes: 6 additions & 0 deletions src/main/java/training/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

public class Robot {

public static final RobotType ROBOT_TYPE = RobotType.Simulation;

// Enter your subsystems...
public enum RobotType {
Simulation,
Real,
greenblitz4590 marked this conversation as resolved.
Show resolved Hide resolved
}

public Robot() {
// Boot your subsystems...
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/training/TrainingRobotManager.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package training;

import edu.wpi.first.math.geometry.Rotation2d;
import training.subsystems.arm.elbow.Elbow;
import utils.DefaultRobotManager;
import utils.KeyboardController;

public class TrainingRobotManager extends DefaultRobotManager {

private Robot robot;
private KeyboardController keyboardController;
private Elbow elbow;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create in robot , also the bindings


@Override
public void trainingInit() {
this.robot = new Robot();
this.keyboardController = new KeyboardController();

keyboardController.A.onTrue(elbow.getCommmands().moveToPosition(Rotation2d.fromDegrees(0)));
keyboardController.W.onTrue(elbow.getCommmands().moveToPosition(Rotation2d.fromDegrees(324)));
keyboardController.D.onTrue(elbow.getCommmands().moveToPosition(Rotation2d.fromDegrees(20)));
keyboardController.S.onTrue(elbow.getCommmands().moveToPosition(Rotation2d.fromDegrees(80)));
Comment on lines +19 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meant for testing bruh

}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

@Override
public void trainingPeriodic() {
// add stuff...
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/CommandsBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package training.subsystems.arm.elbow;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.FunctionalCommand;

public class CommandsBuilder {

private final Elbow elbow;

public CommandsBuilder(Elbow elbow) {
this.elbow = elbow;
}

public Command moveToPosition(Rotation2d angle) {
return new FunctionalCommand(
() -> elbow.moveToAngle(angle),
() -> {},
interrupted -> elbow.stayInPlace(),
() -> elbow.isAtAngle(angle)
);
}

}
30 changes: 30 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package training.subsystems.arm.elbow;


import edu.wpi.first.math.controller.ArmFeedforward;
import edu.wpi.first.math.geometry.Rotation2d;

class Constants {

static int MOTOR_ID = 1;

static Rotation2d TOLERANCE = Rotation2d.fromDegrees(10);

static final double GEAR_RATIO = 1;

static final double KP = 0;

static final double KI = 0;

static final double KD = 0;

static final double KG = 0;

static final double KS = 0;

static final double KV = 0;

static final double BATTERY_VOLTAGE = 12;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isnt only for elbow.. put this in simulation constants

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in Global constants and name it Deafult battery voltage

static final ArmFeedforward ELBOW_FEEDFORWARD = new ArmFeedforward(KG, KS, KV);

}
53 changes: 53 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/Elbow.java
greenblitz4590 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package training.subsystems.arm.elbow;

import edu.wpi.first.math.geometry.Rotation2d;
import utils.GBSubsystem;

public class Elbow extends GBSubsystem {

@Override
protected String getLogPath() {
return null;
}

@Override
protected void subsystemPeriodic() {}

private CommandsBuilder commmands;
private IElbow iElbow;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final sweety


Comment on lines +16 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put these before the functions and delete the extra space line


public Elbow() {
this.commmands = new CommandsBuilder(this);
this.iElbow = ElbowFactory.create();
}

public CommandsBuilder getCommmands() {
return this.commmands;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

}

public Rotation2d getCurrentAngle() {
return iElbow.getCurrentAngle();
}

public void setPower(double power) {
iElbow.setPower(power);
}

public boolean isAtAngle(Rotation2d angle) {
return Math.abs((getCurrentAngle().getDegrees() - angle.getDegrees())) <= Constants.TOLERANCE.getDegrees();
}

public Rotation2d getCurrentVelocity() {
return iElbow.getCurrentVelocity();
}

public void moveToAngle(Rotation2d angle) {
iElbow.moveToAngle(angle);
}

public void stayInPlace() {
iElbow.stayInPlace();
}

}
14 changes: 14 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/ElbowFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package training.subsystems.arm.elbow;

import training.Robot;

public class ElbowFactory {

public static IElbow create() {
return switch (Robot.ROBOT_TYPE) {
case Real -> new NEOElbow();
case Simulation -> new SimulationElbow();
};
}

}
17 changes: 17 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/IElbow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package training.subsystems.arm.elbow;

import edu.wpi.first.math.geometry.Rotation2d;

public interface IElbow {

public Rotation2d getCurrentAngle();

public void setPower(double power);

public Rotation2d getCurrentVelocity();

public void moveToAngle(Rotation2d angle);

public void stayInPlace();

}
48 changes: 48 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/NEOElbow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package training.subsystems.arm.elbow;

import com.revrobotics.CANSparkBase;
import com.revrobotics.CANSparkLowLevel;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.math.geometry.Rotation2d;

public class NEOElbow implements IElbow {

private final CANSparkMax motor;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete space line


public NEOElbow() {
this.motor = new CANSparkMax(Constants.MOTOR_ID, CANSparkLowLevel.MotorType.kBrushless);
motor.getEncoder().setPositionConversionFactor(Constants.GEAR_RATIO);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set velocity factor too

motor.getPIDController().setP(Constants.KP);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant you just save a PIDController as a const and set it?

motor.getPIDController().setI(Constants.KI);
motor.getPIDController().setD(Constants.KD);
}

public Rotation2d getCurrentAngle() {
return Rotation2d.fromRotations(motor.getEncoder().getPosition());
}

public void setPower(double power) {
motor.set(power);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line..


public Rotation2d getCurrentVelocity() {
return Rotation2d.fromRotations(motor.getEncoder().getVelocity());
}

public void moveToAngle(Rotation2d angle) {
motor.getPIDController()
.setReference(
angle.getRotations(),
CANSparkBase.ControlType.kPosition,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a constant to me

0,
Constants.ELBOW_FEEDFORWARD.calculate(getCurrentAngle().getRotations(), getCurrentVelocity().getRotations())
);
}

public void stayInPlace() {
moveToAngle(Rotation2d.fromRotations(getCurrentAngle().getRotations()));
}

}
60 changes: 60 additions & 0 deletions src/main/java/training/subsystems/arm/elbow/SimulationElbow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package training.subsystems.arm.elbow;

import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim;
import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim;
import edu.wpi.first.math.system.plant.DCMotor;

public class SimulationElbow implements IElbow {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use single jointed arm simulation instead

private final SingleJointedArmSim elbowSimulation;
private final PIDController controller;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change name to pidController


public SimulationElbow() {
elbowSimulation = new SingleJointedArmSim(
DCMotor.getFalcon500(SimulationElbowConstants.NUMBER_OF_MOTORS),

SimulationElbowConstants.GEAR_RATIO,
SingleJointedArmSim.estimateMOI(SimulationElbowConstants.ARM_LENGTH, SimulationElbowConstants.ARM_MASS_KG),
SimulationElbowConstants.ARM_LENGTH,
SimulationElbowConstants.BACKWARD_ANGLE_LIMIT.getRadians(),
SimulationElbowConstants.FORWARD_ANGLE_LIMIT.getRadians(),
false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic boolean

SimulationElbowConstants.PresetPositions.STARTING.ANGLE.getRadians()
);
controller = SimulationElbowConstants.SIMULATION_PID;
}

@Override
public Rotation2d getCurrentAngle() {
return Rotation2d.fromRadians(elbowSimulation.getAngleRads());
}

@Override
public void setPower(double power) {
elbowSimulation.setInputVoltage(power * Constants.BATTERY_VOLTAGE);
}

@Override
public Rotation2d getCurrentVelocity() {
return Rotation2d.fromRotations(elbowSimulation.getVelocityRadPerSec() / SimulationElbowConstants.FULL_CIRCLE_RAD);
}

private void setVoltage(double voltage) {
double limited_voltage = Math.min((Math.max(voltage, -Constants.BATTERY_VOLTAGE)), Constants.BATTERY_VOLTAGE);
elbowSimulation.setInputVoltage(limited_voltage);
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line...

public void moveToAngle(Rotation2d angle) {
setVoltage(controller.calculate(elbowSimulation.getAngleRads(), angle.getRadians()));
}

@Override
public void stayInPlace() {
moveToAngle(getCurrentAngle());
}

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the measurement unit to the name of the constants that need it (like ARM_LENGTH_METERS)

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package training.subsystems.arm.elbow;


import edu.wpi.first.math.Pair;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.controller.PIDController;

public class SimulationElbowConstants {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of constants here are not type dependent, move them into elbow simulation



public enum PresetPositions {

SAFE(Rotation2d.fromDegrees(-67)),
INTAKE(Rotation2d.fromDegrees(-76)),
SCORE(Rotation2d.fromDegrees(55)),
STARTING(Rotation2d.fromDegrees(0)),
TRANSFER(Rotation2d.fromDegrees(-80));

public final Rotation2d ANGLE;

PresetPositions(Rotation2d angle) {
this.ANGLE = angle;
}

}

public static final int NUMBER_OF_MOTORS = 1;


public static final Rotation2d MINIMUM_ANGLE = Rotation2d.fromDegrees(-82);

public static final double ARM_LENGTH = 0.44;

public static final double ARM_DISTANCE_FROM_CENTER = 0.1;

public static final double ARM_MASS_KG = 0.44;

public static final Rotation2d BACKWARD_ANGLE_LIMIT = Rotation2d.fromDegrees(-81);

public static final Rotation2d FORWARD_ANGLE_LIMIT = Rotation2d.fromDegrees(90);

public static final Pair<
Rotation2d,
Rotation2d> SHOOTER_COLLISION_RANGE = new Pair<>(BACKWARD_ANGLE_LIMIT, PresetPositions.SAFE.ANGLE);
Comment on lines +43 to +45

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix the spacing


public static final Translation3d ELBOW_POSITION_RELATIVE_TO_ROBOT = new Translation3d(-0.1, 0, 0.6);

public static final double GEAR_RATIO = 1 / (28.0 * (60.0 / 16.0));

public static final Rotation2d TOLERANCE = Rotation2d.fromDegrees(3);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be in elbow constants


public static final int CURRENT_LIMIT = 40;

public static final PIDController SIMULATION_PID = new PIDController(Constants.KP, Constants.KI, Constants.KD);

public static final double FULL_CIRCLE_RAD = 2 * Math.PI;;

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this line