Skip to content

Commit

Permalink
saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Duckett committed Jan 3, 2024
1 parent eb54292 commit 54d972b
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/main/java/frc/lib/hardware/HardwareRequirments.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ public interface HardwareRequirments extends AutoCloseable, LoggableInputs {

boolean connected();

default void toLog(LogTable table, String path) {}
default void toLog(LogTable table, String path) {
}

default void fromLog(LogTable table) {}
default void fromLog(LogTable table) {
}

default void toLog(LogTable table) {}
default void toLog(LogTable table) {
}
}
200 changes: 200 additions & 0 deletions src/main/java/frc/lib/hardware/Motors/Motor2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package frc.lib.hardware.Motors;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj.simulation.FlywheelSim;
import frc.lib.hardware.HardwareRequirments;
import frc.lib.hardware.Motors.MotorControlers.MotorController;
import frc.lib.hardware.Motors.MotorControlers.Talon_SRX;
import frc.lib.hardware.Motors.PID.EncoderConfigs;
import frc.lib.hardware.Motors.PID.PIDConfigs;
import frc.lib.hardware.Motors.PID.SimConfig;
import frc.lib.hardware.sensor.encoders.Encoder;
import frc.robot.Robot;
import org.littletonrobotics.junction.LogTable;

public class Motor2 implements HardwareRequirments {
// Enums --------------
public enum Type {
CIM(DCMotor.getFalcon500(1)),
Falcon500(DCMotor.getFalcon500Foc(1)),
VP775(DCMotor.getFalcon500(1));

private DCMotor dcMotor;

private Type(DCMotor dcMotor) {
this.dcMotor = dcMotor;
}

protected DCMotor config() {
return this.dcMotor;
}
}

public enum ControllerType {
TallonSRX(new Talon_SRX());

MotorController mController;

private ControllerType(MotorController mController) {
this.mController = mController;
}

public MotorController config(int canID) {
return mController.config(canID);
}
}

// -----------------------------------------
private final MotorController mController;
private final Type mType;
private Encoder mEncoder;

private boolean hasEncoder = false;
private boolean isSimulated = false;

private String name;

public Motor2(ControllerType motorControllerType, int canID, Type motor) {
this.mController = motorControllerType.config(canID);
this.mType = motor;
this.name = "Motor" + canID;
this.isSimulated = RobotBase.isSimulation();
}

// ----------------- COnfigs ---------------------
public Motor addEncoder(Encoder encoder) {
this.mEncoder = encoder;
hasEncoder = true;
return this;
}

public Motor inverted(boolean enable) {
mController.setInverted(enable);

return this;
}

public Motor brakeMode(boolean enable) {
mController.brakeMode(enable);

return this;
}

private PIDController mFeedback;

public Motor pidConfigs(PIDConfigs PIDConfigs) {
mFeedback = new PIDController(1, 0, 0);

return this;
}

public Motor EncoderConfigs(EncoderConfigs encoderConfigs) {

return this;
}

public Motor setName(String name) {
this.name = name;
return this;
}

// controlling Motor contoler ---------------------------------
public void setVolts(double Volts) {
mController.runVolts(Volts);
}

public void setPositoin(double Positoin) {
if (!hasEncoder) {
throw new Error("you dumb. This motor has no encoder");
}

mSimFeedback.setSetpoint(Positoin);
// mFeedback.setSetpoint(Positoin);

// mAppliedVolts = mFeedback.calculate(getPositoin());
// mAppliedVolts = MathUtil.clamp(mAppliedVolts, -12.0, 12.0);

setVolts(mAppliedVolts);
}

public void setVelocity(double velocity) {
if (!hasEncoder) {
throw new Error("you dumb. This motor has no encoder");
}
mSimFeedback.setSetpoint(velocity);
// mFeedback.setSetpoint(velocity);

// mAppliedVolts = mFeedback.calculate(getVelocity());
// mAppliedVolts = MathUtil.clamp(mAppliedVolts, -12.0, 12.0);

setVolts(mAppliedVolts);
}

public void disable() {
mController.disable();
}

// get ---------------------------------
public int getCanID() {
return mController.getCanID();
}

@Override
public void close() throws Exception {
mController.close();
mEncoder.close();
}

public double getPositoin() {
if (!hasEncoder) {
throw new Error("you dumb. This motor has no encoder");
}
if (RobotBase.isReal()) {
return mEncoder.getPositoin();
} else {
if (simVelocity)
return mSimPositionMeters;
else
return mSimPositionRad;
}
}

public double getVelocity() {
if (!hasEncoder) {
throw new Error("you dumb. This motor has no encoder");
}
if (RobotBase.isReal()) {
return mEncoder.getVelocity();
} else {
return mSimVelocityDeg;
}
}

// Required ---------------------------

@Override
public void toLog(LogTable table) {
mController.toLog(table, name);
table.put(name + "/Motor Type", mType.toString());
if (hasEncoder) {
table.put(name + "/Position (degrees)", getPositoin());
table.put(name + "/Velocity (degreesPerSecond)", getVelocity());
table.put(name + "/Velocity (MetersPerSecond)", getVelocity() * Units.inchesToMeters(2));
// table.put(name + "/AppliedVolts (Volts)", mAppliedVolts);
// table.put(name + "/CurrentDraw (Amps)", Math.abs(mSim.getCurrentDrawAmps()));
}
}

@Override
public boolean connected() {
if (!mController.connected())
return false;
if (hasEncoder)
return mEncoder.connected();
return true;
}
}
8 changes: 1 addition & 7 deletions src/main/java/frc/robot/Subsytems/Intake/IntakeHardware.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package frc.robot.Subsytems.Intake;

import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.wpilibj.Preferences;
import frc.lib.hardware.Motors.*;
import frc.lib.hardware.sensor.proximitySwitch.*;
Expand Down Expand Up @@ -61,6 +60,7 @@ public void setSimpleName(String SimpleName) {
@Override
public void close() throws Exception {
switch1.close();
mIntake.close();
}

// prefrances ------------------
Expand All @@ -72,12 +72,6 @@ public void loadPreferences() {
setPercentOut = Preferences.getDouble(SimpleName + "/maxVolts", setPercentOut);
}

@Override
public void initSendable(SendableBuilder builder) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'initSendable'");
}

@Override
public void toLog(LogTable table) {
table.put("setPercentOut", setPercentOut);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package frc.robot.Subsytems.Intake;

import edu.wpi.first.util.sendable.Sendable;
import frc.robot.Subsytems.SubsytemRequirments;

public interface IntakeRequirments extends SubsytemRequirments, Sendable {
public interface IntakeRequirments extends SubsytemRequirments {

// Controling the hardware
void setOutakeSpeed();
Expand Down

0 comments on commit 54d972b

Please sign in to comment.