Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 0a6e369

Browse files
committed
Finished all commands
Only Pathplanner left
1 parent c688b24 commit 0a6e369

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.commands;
6+
7+
import edu.wpi.first.wpilibj2.command.Command;
8+
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
9+
10+
import frc.robot.subsystems.SwerveBase;
11+
12+
public class RunSwerve extends Command {
13+
private final SwerveBase swerveBase;
14+
private final CommandXboxController controller;
15+
16+
public RunSwerve(SwerveBase swerve, CommandXboxController ctrl) {
17+
this.swerveBase = swerve;
18+
this.controller = ctrl;
19+
addRequirements(swerveBase);
20+
}
21+
22+
@Override
23+
public void initialize() {
24+
swerveBase.coast();
25+
}
26+
27+
@Override
28+
public void execute() {
29+
swerveBase.setSwerve(controller.getLeftX(), controller.getLeftY(), controller.getRightX());
30+
}
31+
32+
@Override
33+
public void end(boolean interrupted) {
34+
swerveBase.brake();
35+
}
36+
37+
@Override
38+
public boolean isFinished() {
39+
return false;
40+
}
41+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.subsystems;
6+
7+
import com.ctre.phoenix6.hardware.CANcoder;
8+
import com.ctre.phoenix6.hardware.Pigeon2;
9+
import com.ctre.phoenix6.hardware.TalonFX;
10+
11+
import edu.wpi.first.math.kinematics.ChassisSpeeds;
12+
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
13+
import edu.wpi.first.math.kinematics.SwerveModuleState;
14+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
15+
import edu.wpi.first.wpilibj2.command.SubsystemBase;
16+
17+
public class SwerveBase extends SubsystemBase {
18+
private final SwerveModule frontLeft;
19+
private final SwerveModule frontRight;
20+
private final SwerveModule backLeft;
21+
private final SwerveModule backRight;
22+
private final Pigeon2 gyro;
23+
24+
public SwerveBase() {
25+
frontLeft = new SwerveModule(new TalonFX(8), new TalonFX(9), new CANcoder(10), 98.35);
26+
frontRight = new SwerveModule(new TalonFX(11), new TalonFX(12), new CANcoder(13), 3.5);
27+
backLeft = new SwerveModule(new TalonFX(14), new TalonFX(15), new CANcoder(16), 34.84);
28+
backRight = new SwerveModule(new TalonFX(17), new TalonFX(18), new CANcoder(19), 74.19);
29+
gyro = new Pigeon2(20);
30+
}
31+
32+
public void setSwerve(double vx, double vy, double rx){
33+
setChassisSpeed(ChassisSpeeds.fromFieldRelativeSpeeds(vx, vy, rx, gyro.getRotation2d()));
34+
}
35+
public void setChassisSpeed(ChassisSpeeds chassisSpeeds) {
36+
setSwerveStates(new SwerveDriveKinematics().toSwerveModuleStates(chassisSpeeds));
37+
}
38+
39+
public void setSwerveStates(SwerveModuleState[] states){
40+
backLeft.setSwerveState(states[0]);
41+
backRight.setSwerveState(states[1]);
42+
frontLeft.setSwerveState(states[2]);
43+
frontRight.setSwerveState(states[3]);
44+
}
45+
46+
public void coast() {
47+
frontLeft.coast();
48+
frontRight.coast();
49+
backLeft.coast();
50+
backRight.coast();
51+
}
52+
53+
public void brake() {
54+
frontLeft.brake();
55+
frontRight.brake();
56+
backLeft.brake();
57+
backRight.brake();
58+
}
59+
60+
@Override
61+
public void periodic() {
62+
SmartDashboard.putNumber("Front Left Motor Angle", frontLeft.getAngle());
63+
SmartDashboard.putNumber("Front Right Motor Angle", frontRight.getAngle());
64+
SmartDashboard.putNumber("Back Left Motor Angle", backLeft.getAngle());
65+
SmartDashboard.putNumber("Back Right Motor Angle", backRight.getAngle());
66+
67+
SmartDashboard.putNumber("Front Left Motor Speed", frontLeft.getSpeed());
68+
SmartDashboard.putNumber("Front Right Motor Speed", frontRight.getSpeed());
69+
SmartDashboard.putNumber("Back Left Motor Speed", backLeft.getSpeed());
70+
SmartDashboard.putNumber("Back Right Motor Speed", backRight.getSpeed());
71+
}
72+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.subsystems;
6+
7+
import com.ctre.phoenix6.hardware.CANcoder;
8+
import com.ctre.phoenix6.hardware.TalonFX;
9+
import com.ctre.phoenix6.signals.NeutralModeValue;
10+
11+
import edu.wpi.first.math.controller.PIDController;
12+
import edu.wpi.first.math.kinematics.SwerveModuleState;
13+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
14+
import edu.wpi.first.wpilibj2.command.SubsystemBase;
15+
16+
public class SwerveModule extends SubsystemBase {
17+
private final TalonFX turnMotor;
18+
private final TalonFX moveMotor;
19+
private final PIDController pidController;
20+
private final CANcoder encoder;
21+
private double speed;
22+
23+
private final double angleOffset;
24+
25+
public SwerveModule(TalonFX turnMotor, TalonFX moveMotor, CANcoder encoder, double angleOffset) {
26+
this.turnMotor = turnMotor;
27+
this.moveMotor = moveMotor;
28+
this.encoder = encoder;
29+
this.angleOffset = angleOffset;
30+
this.pidController = new PIDController(0.5, 0.5, 0.5);
31+
this.pidController.setTolerance(1);
32+
this.speed = 0;
33+
}
34+
35+
public void setSwerveState(SwerveModuleState swerveState) {
36+
speed = swerveState.speedMetersPerSecond;
37+
moveMotor.set(speed);
38+
39+
double adjustedAngle = swerveState.angle.getDegrees() - angleOffset;
40+
pidController.setSetpoint(adjustedAngle);
41+
pidController.calculate(turnMotor.getPosition().getValueAsDouble(), adjustedAngle);
42+
}
43+
44+
public double getAngle() {
45+
return encoder.getPosition().getValueAsDouble();
46+
}
47+
48+
public double getSpeed() {
49+
return speed;
50+
}
51+
52+
public void coast() {
53+
turnMotor.setNeutralMode(NeutralModeValue.Coast);
54+
moveMotor.setNeutralMode(NeutralModeValue.Coast);
55+
}
56+
57+
public void brake() {
58+
turnMotor.setNeutralMode(NeutralModeValue.Brake);
59+
moveMotor.setNeutralMode(NeutralModeValue.Brake);
60+
}
61+
62+
@Override
63+
public void periodic() {
64+
SmartDashboard.putNumber("Turn Motor Temp", turnMotor.getDeviceTemp().getValueAsDouble());
65+
SmartDashboard.putNumber("Move Motor Temp", moveMotor.getDeviceTemp().getValueAsDouble());
66+
}
67+
}

0 commit comments

Comments
 (0)