Skip to content

Commit

Permalink
Merge pull request #1 from bow4290/better-implementation
Browse files Browse the repository at this point in the history
Signifigantly better version of GenericMotor.java, using interfaces and composing implementations with official APIs
  • Loading branch information
JumpingPistachio authored Jan 25, 2024
2 parents 8c9e1f9 + 98afd6e commit 2623619
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 98 deletions.
98 changes: 0 additions & 98 deletions src/main/java/frc/robot/lib/GenericMotorHandler.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/frc/robot/lib/GenericMotors/GenericMotor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package frc.robot.lib.GenericMotors;

/**
* An unofficial generic for motors, similiar to MotorController interface. Not entirely intended for every-day use cases.
*/
public interface GenericMotor {

void setSpeed(double speedPercentage);

double getSpeed();

void setInverted(boolean isInverted);

boolean getInverted();





}
40 changes: 40 additions & 0 deletions src/main/java/frc/robot/lib/GenericMotors/GenericSparkMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package frc.robot.lib.GenericMotors;

import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkLowLevel.MotorType;

public class GenericSparkMax implements GenericMotor {
int motorID;

CANSparkMax motorSparkMax;
MotorType motorType;

public GenericSparkMax(int motorID, MotorType motorType){
this.motorID = motorID;
this.motorType = motorType;

this.motorSparkMax = new CANSparkMax(this.motorID, this.motorType);
System.out.printf("New CanSparkMax constructed with GenericSparkMax at motor ID: %d", this.motorID);
}

@Override
public void setSpeed(double speedPercentage) {
motorSparkMax.set(speedPercentage);
}

@Override
public double getSpeed() {
return motorSparkMax.get();
}

@Override
public void setInverted(boolean isInverted) {
motorSparkMax.setInverted(isInverted);
}

@Override
public boolean getInverted() {
return motorSparkMax.getInverted();
}

}
37 changes: 37 additions & 0 deletions src/main/java/frc/robot/lib/GenericMotors/GenericTalonFX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package frc.robot.lib.GenericMotors;


import com.ctre.phoenix6.hardware.TalonFX;

public class GenericTalonFX implements GenericMotor{
TalonFX motorTalon;
int motorID;

public GenericTalonFX(int motorID){
this.motorID = motorID;

this.motorTalon = new TalonFX(this.motorID);
System.out.printf("New TalonFX constructed with GenericTalonFX at motor ID: %d", this.motorID);
}

@Override
public void setSpeed(double speedPercentage) {
motorTalon.set(speedPercentage);
}

@Override
public double getSpeed() {
return motorTalon.get();
}

@Override
public void setInverted(boolean isInverted) {
motorTalon.setInverted(isInverted);
}

@Override
public boolean getInverted() {
return motorTalon.getInverted();
}

}

0 comments on commit 2623619

Please sign in to comment.