Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/frc/robot/commands/SwerveModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands;
import com.ctre.phoenix.sensors.AbsoluteSensorRange;
import com.ctre.phoenix.sensors.CANCoder;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMax.ControlType;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;


/** Add your docs here. */
public class SwerveModule {
CANSparkMax drivemotor;
CANSparkMax swervemotor;
public CANCoder steerencoder;

final double steerReduction = (14.0 / 50.0) * (27.0 / 17.0) * (15.0 / 45.0);
public SwerveModule(int dmotorid, int smoterid, double encoderoffset){
drivemotor = new CANSparkMax(dmotorid, MotorType.kBrushless);
swervemotor = new CANSparkMax(smoterid, MotorType.kBrushless);
swervemotor.getEncoder().setPositionConversionFactor(2*Math.PI*steerReduction);
swervemotor.getPIDController().setP(1.0);
swervemotor.getPIDController().setD(0.1);
swervemotor.getPIDController().setI(0);
swervemotor.getEncoder().setPosition(0);
// setup swerve module encoder
steerencoder = new CANCoder(15);
steerencoder.configMagnetOffset(encoderoffset);
steerencoder.configAbsoluteSensorRange(AbsoluteSensorRange.Unsigned_0_to_360);
Comment on lines +30 to +31

Choose a reason for hiding this comment

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

Might want to configure statusFramePeriod and add timeouts. Sensor direction may also be a setting you want to configure.

}

public void resetSteerPosition() {

Choose a reason for hiding this comment

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

R should be capital

swervemotor.getEncoder().setPosition(Math.toRadians(steerencoder.getAbsolutePosition()));
}

public void move(double setSwerveMotorPosition, double drivemotorspeed){

Choose a reason for hiding this comment

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

M should be captial

resetSteerPosition();
swervemotor.set(setSwerveMotorPosition);
drivemotor.set(drivemotorspeed);
}

public void SetToZero(){
swervemotor.getEncoder().setPosition(0);
}

public void rotateto(double rotation){

Choose a reason for hiding this comment

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

camelCase should be used.

swervemotor.getPIDController().setReference(Math.toRadians(rotation), ControlType.kPosition);
}
}
Loading