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

Adding Support for PWM Absolute Mag Encoders #22

Draft
wants to merge 248 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
248 commits
Select commit Hold shift + click to select a range
335b302
Added Absolute Encoder file
Dec 7, 2022
4e5ec0f
Removed unused import
Dec 7, 2022
faf8604
Working absolute encoders
Dec 9, 2022
ed96222
Reformatting/simplifying code + update encoder offsets
Jan 4, 2023
2383fb3
updated constants
Jan 11, 2023
6739998
Fixed absolute encoder issue
Jan 13, 2023
c99b785
Change offsets
Jan 14, 2023
b9be095
Added Autonomous Functionality
Jan 18, 2023
32b5c7f
Merge branch '22FallUpdates' into forked-updates-testing
Dylan-Powers Jan 18, 2023
9068b5b
fixed naming issue
Dylan-Powers Jan 19, 2023
887c7eb
updates
Dylan-Powers Jan 19, 2023
b00c806
testing
Dylan-Powers Jan 19, 2023
9182fa7
wpilib update
Jan 21, 2023
0b17a0a
Merge branch '22FallUpdates' into AbsoluteEncoder
Jan 21, 2023
fae21ab
Merge branch 'forked-updates-testing' into AbsoluteEncoder
Jan 21, 2023
721eb51
many paths lol
Dylan-Powers Jan 21, 2023
5bd5ddf
Merge branch 'forked-updates-testing' of https://github.com/MoreRobot…
Dylan-Powers Jan 21, 2023
2015be3
Merge branch 'forked-updates-testing' into AbsoluteEncoder
Jan 21, 2023
61bb51f
dylan broke things but then fixed them
Dylan-Powers Jan 21, 2023
e3dd816
PID testing
Dylan-Powers Jan 21, 2023
e8d3302
Merge branch 'forked-updates-testing' into AbsoluteEncoder
Dylan-Powers Jan 21, 2023
ef5f070
Revert "PID testing"
Dylan-Powers Jan 21, 2023
a5cf171
Revert "Revert "PID testing""
Dylan-Powers Jan 21, 2023
151ad5f
Tested PID Values and showed x,y values
Jan 25, 2023
2ce7759
Added Sys IDs
Jan 26, 2023
d8d54b4
Changed Pathplanner
Jan 27, 2023
fc6ad10
Added an intake
Jan 28, 2023
14d5b2c
Added LED functionality (error)
robotics1714 Feb 7, 2023
69efc3e
Fixed errors
robotics1714 Feb 8, 2023
d63e6b0
Merge pull request #2 from MoreRobotics/LEDs
Samuel-Moore-1714 Feb 8, 2023
846baa5
Added Extend and Retract Elevator
Dylan-Powers Feb 9, 2023
112ef6b
changed initializations to declarations
Dylan-Powers Feb 9, 2023
661fefa
updated gitignore for advantagekit
Dylan-Powers Feb 11, 2023
5f2b7f8
Merge branch 'main' into elevator
Dylan-Powers Feb 11, 2023
680fcd4
Merge branch 'main' into Add-Intake
Dylan-Powers Feb 11, 2023
e08a1fb
Merge pull request #3 from MoreRobotics/Add-Intake
Dylan-Powers Feb 11, 2023
82d2114
Fixed the elevator limit (MORE squirrels)
Dylan-Powers Feb 11, 2023
abedebd
deleted Go to Position
Dylan-Powers Feb 11, 2023
85e3bb7
Added gyro offset
robotics1714 Feb 11, 2023
def87da
Mid Testing Might Not Work
Dylan-Powers Feb 11, 2023
44d19e8
Configuring to actual drivetrain
robotics1714 Feb 12, 2023
6a7cb19
PIDF testing and added commands for scoring positions
Dylan-Powers Feb 15, 2023
71a682d
renamed controller buttons
Dylan-Powers Feb 17, 2023
a1d981e
removed feedforward and converted to inches
Dylan-Powers Feb 17, 2023
cf9a924
added inches to motor rotations conversion method
Dylan-Powers Feb 17, 2023
1b15bed
Added intake solenoids
Feb 17, 2023
8bc7b39
began implementing arm code
Dylan-Powers Feb 17, 2023
3233b0f
Set up the arm subsystem
robotics1714 Feb 18, 2023
147f3d5
finished and debugged solenoid code
Feb 18, 2023
065775e
Merge pull request #4 from MoreRobotics/Intake-solenoid
Samuel-Moore-1714 Feb 18, 2023
f34e6bf
Reorganized commands
robotics1714 Feb 18, 2023
0bda389
Reorgenized commands + added LED Idle animation
Feb 18, 2023
393c8be
pre-merge changes
Feb 18, 2023
424f8a1
Merge pull request #5 from MoreRobotics/Command
Dylan-Powers Feb 18, 2023
bf6f6a4
Made blue led color more distinct
robotics1714 Feb 18, 2023
f971f6b
Merge branch 'main' into elevator
robotics1714 Feb 18, 2023
27d426b
Added Move Arm Up and Down Commands
Dylan-Powers Feb 18, 2023
74b4c5d
fixed getting arm angle
Dylan-Powers Feb 19, 2023
8e62320
Added manual elevator functionality + tuned softlimits
robotics1714 Feb 19, 2023
e457c4d
comp
robotics1714 Feb 19, 2023
ce4b210
Merge branch 'main' into elevator
robotics1714 Feb 19, 2023
86f0440
Competiton changes
robotics1714 Feb 19, 2023
9ee5167
Merge branch 'main' into arm
robotics1714 Feb 19, 2023
e17f45a
Added auto balancing
robotics1714 Feb 21, 2023
0d5cffc
got encoder offset for level arm and gear ratio
Dylan-Powers Feb 21, 2023
a0669dd
Updated PID calculate to account for BALANCE_LEVEL
Dylan-Powers Feb 21, 2023
e93fa21
arm debugging
robotics1714 Feb 22, 2023
04c8842
Added arm position control
Samuel-Moore-1714 Feb 23, 2023
fa51b84
Made todo notes for next meeting
Samuel-Moore-1714 Feb 23, 2023
9dca37a
added wheel lock when centered
Samuel-Moore-1714 Feb 23, 2023
7a8eba7
Added constant + added todo notes for next meeting
Samuel-Moore-1714 Feb 23, 2023
16afe63
moved manual methods to execute
robotics1714 Feb 24, 2023
4a575e3
tuned PID and feedforward for Arm
robotics1714 Feb 24, 2023
7f1747f
added lock wheels method
robotics1714 Feb 24, 2023
5b1b4b4
Rough teleop v1
robotics1714 Feb 25, 2023
a3f36f9
Changed buttons for scoring so they are all on 2 buttons
robotics1714 Feb 25, 2023
1a12092
Disable compressor and changed intake speed and max speed
robotics1714 Feb 25, 2023
7e49b12
added disable and enable compressor
robotics1714 Feb 25, 2023
97b7343
I really commited this time
robotics1714 Feb 25, 2023
62a35e7
Changed New New New Path to Balancing Path
robotics1714 Feb 25, 2023
6633d28
changed PID value and uncommented swerve
robotics1714 Feb 25, 2023
b184fbd
Added autonomous
robotics1714 Feb 28, 2023
a458fe5
fixed arm seizure
Dylan-Powers Feb 28, 2023
98c74a9
Optimized elevator and arm movement
Dylan-Powers Feb 28, 2023
6fd98e2
Working on auto
robotics1714 Mar 1, 2023
85a0e2c
Merge branch 'Auto-Balancing'
robotics1714 Mar 1, 2023
b8658a5
Merge branch 'main' into elevator-arm-integration
robotics1714 Mar 1, 2023
8d0636f
added full autos
robotics1714 Mar 1, 2023
b69296d
optimized arm and elevator
Dylan-Powers Mar 1, 2023
6ac4c84
fixed current limiting issue
Dylan-Powers Mar 1, 2023
e690b05
Cleaned up and added to smart dashboard
robotics1714 Mar 1, 2023
8911fac
fixed led buttons and compressor displays
robotics1714 Mar 1, 2023
5898b9f
Merge pull request #7 from MoreRobotics/elevator-arm-integration
robotics1714 Mar 1, 2023
16cb10f
Updated Speed Value
Samuel-Moore-1714 Mar 1, 2023
b14aa49
Pre-comp changes
robotics1714 Mar 2, 2023
4fb2c00
added displays for the intake
robotics1714 Mar 3, 2023
a521c4b
practice day changes
robotics1714 Mar 3, 2023
de2dcd8
Added New Paths And Other Improvements During Duluth
robotics1714 Mar 5, 2023
73890e5
Merge branch 'duluth-wip'
robotics1714 Mar 5, 2023
1d07183
Added PID to Rotate to Score Button
robotics1714 Mar 9, 2023
d0526e5
added PID to rotate to score button (NOT TESTED)
Dylan-Powers Mar 10, 2023
9514ecb
added slow mode (UNTESTED)
Dylan-Powers Mar 10, 2023
52cc3c0
Merge pull request #8 from MoreRobotics/rotate-to-score-button
robotics1714 Mar 11, 2023
5b5b841
Added title for slow mode
robotics1714 Mar 11, 2023
9a541fa
updated RotateToScore
Mar 15, 2023
a53530a
Fixed Go to Ground Position and changed closed loop
Dylan-Powers Mar 15, 2023
1f0c4bf
leds should start with the blue color now
Dylan-Powers Mar 15, 2023
280abe8
changed absolute encoder to integrated changed brake to coast
Mar 16, 2023
c5cc262
tuned rotate to score
Dylan-Powers Mar 18, 2023
6f7e19f
Merge branch 'slow-mode' into rotate-to-score-button
Dylan-Powers Mar 18, 2023
a3c5e3a
merged slow mode and turn to score
Dylan-Powers Mar 18, 2023
b3985ab
Merge branch 'rotate-to-score-button'
Dylan-Powers Mar 18, 2023
55bafad
add current limits to the intake motor
robotics1714 Mar 18, 2023
9fe5956
Tune auto rotate
robotics1714 Mar 18, 2023
60ef105
930 practice changes
robotics1714 Mar 18, 2023
0faa33d
930 changes
robotics1714 Mar 18, 2023
6a98eed
small fixes
Dylan-Powers Mar 20, 2023
999c337
Merge pull request #9 from MoreRobotics/bears-practice
Dylan-Powers Mar 20, 2023
8224730
Added basic logging
Dylan-Powers Mar 21, 2023
af43ec1
started adding logging to arm
Dylan-Powers Mar 21, 2023
c15f7e0
tested logging
Mar 21, 2023
3dc3090
begin fix
Dylan-Powers Mar 22, 2023
9b4e389
broken fix
Dylan-Powers Mar 22, 2023
c9fae95
added rotate to load to the driver right trigger
Dylan-Powers Mar 22, 2023
a6d776a
fixed arm math
Dylan-Powers Mar 23, 2023
c2cdd40
MIilwaukee Changes
robotics1714 Mar 23, 2023
3334db0
Fixed rotating bug
robotics1714 Mar 23, 2023
6b817b7
added auto stuff
robotics1714 Mar 24, 2023
26fd1a9
Update Arm.java
Dylan-Powers Mar 24, 2023
f154a70
Merge branch 'main' into logging
Dylan-Powers Mar 24, 2023
a2eb910
added arm pos to logging
Dylan-Powers Mar 24, 2023
5d4bce0
logs from testing
robotics1714 Mar 24, 2023
451e8c1
added pose data
robotics1714 Mar 24, 2023
6af1335
pulled logs
robotics1714 Mar 24, 2023
d98517a
Update mid auto
robotics1714 Mar 24, 2023
b9bfa3f
pulled logs
robotics1714 Mar 24, 2023
6b08262
Updated elevator zero position
robotics1714 Mar 24, 2023
f1e9a4d
added logging for swerve speed and current
robotics1714 Mar 24, 2023
4918d75
Debugged Auto Choosing issue
robotics1714 Mar 24, 2023
c83db8e
changed back to open loop driving
Dylan-Powers Mar 25, 2023
3a1d201
reduced current limit on the intake down to 20
Dylan-Powers Mar 25, 2023
a638cbc
Merge branch 'logging'
Dylan-Powers Mar 25, 2023
dcbacea
added logging to elevator
Dylan-Powers Mar 25, 2023
e15649c
added encoder velocity logging to arm
Dylan-Powers Mar 25, 2023
839fd65
added more CANCoder logging
Dylan-Powers Mar 25, 2023
14a0b40
added pid output to logging
Dylan-Powers Mar 25, 2023
686c04c
added logging to intake
Dylan-Powers Mar 25, 2023
e3ff852
Merge branch 'main' into logging
robotics1714 Mar 25, 2023
2a52bf7
added more logs
robotics1714 Mar 25, 2023
c2f10e4
Merge branch 'logging' of https://github.com/MoreRobotics/2023-Code i…
Dylan-Powers Mar 29, 2023
c53dbee
put swerve drive odometry pose on SmartDashboard
Dylan-Powers Mar 29, 2023
fa9819f
updated desktop support
Dylan-Powers Mar 31, 2023
a0d0675
Added logs and changed angle offset and a timeout for the LEDs
robotics1714 Mar 31, 2023
0752c8d
Merge branch 'main' of https://github.com/MoreRobotics/2023-Code
robotics1714 Mar 31, 2023
4b6376a
updated arm encoder offset
robotics1714 Apr 1, 2023
7bdceec
added some print statements
robotics1714 Apr 1, 2023
9b61d1c
logged loop time
robotics1714 Apr 1, 2023
3044b0c
removed logging and fixed arm
Apr 2, 2023
511dcd4
Deleted Logs
Apr 3, 2023
307694d
added logs
robotics1714 Apr 4, 2023
a23b5cc
logged loop time
robotics1714 Apr 1, 2023
1066256
added logs
robotics1714 Apr 4, 2023
a6b51d4
logged loop time
robotics1714 Apr 1, 2023
bff68bd
add log
robotics1714 Apr 4, 2023
726236a
pulled logs
robotics1714 Apr 4, 2023
ec689ba
Revert "added logging to elevator"
robotics1714 Apr 4, 2023
30a146a
pulled logging
robotics1714 Apr 4, 2023
ba92969
comment out loggin
robotics1714 Apr 4, 2023
fe34716
added logging to intake
Dylan-Powers Mar 25, 2023
f7a1e61
Revert "Revert "added logging to elevator""
robotics1714 Apr 4, 2023
7a31abd
removed logging
robotics1714 Apr 4, 2023
f06f3cd
changed names of logs
Apr 4, 2023
d4ecf42
comment back in logging
robotics1714 Apr 4, 2023
04731ba
removed all logging
Dylan-Powers Apr 5, 2023
f1359cf
small fix
Dylan-Powers Apr 5, 2023
d747290
added back log data
Apr 5, 2023
ca5a42e
got rid of disfuncional logs
robotics1714 Apr 6, 2023
159588e
Merge branch 'main' into has-logging
Apr 6, 2023
a0f6d89
Merge pull request #10 from MoreRobotics/has-logging
robotics1714 Apr 6, 2023
7137094
removed reset swerve encoders from auton and teleopinit
Apr 6, 2023
25e6884
new logs
Dylan-Powers Apr 2, 2023
b739152
Merge branch '2023-04-01-rollbackForTVInterview'
Dylan-Powers Apr 6, 2023
8526974
removed duplicate objects
Dylan-Powers Apr 6, 2023
d6876ef
removed unused method
Apr 6, 2023
528f290
fixed Rotate to Score +Load
robotics1714 Apr 7, 2023
3d3a57b
Merge branch 'main' into pid-rotate-fix
robotics1714 Apr 7, 2023
ccf3535
final fix for auto-rotate
robotics1714 Apr 7, 2023
9322130
moved loop time logging into Robot.java
Apr 7, 2023
e3925f0
pulled logs
robotics1714 Apr 8, 2023
12c5776
removed angle optimization
robotics1714 Apr 8, 2023
457c92d
Create RobotMode.java
Dylan-Powers Apr 8, 2023
772d560
fixed loop time overrun
robotics1714 Apr 8, 2023
61e504e
code cleanup and new logs
robotics1714 Apr 8, 2023
5652ec7
increased max speed
robotics1714 Apr 8, 2023
df8fdda
Started Subsystem and driver code for robot 2.0 (read discription)
Dylan-Powers Apr 8, 2023
b175248
Added elevator positioning system for cone mode
Dylan-Powers Apr 8, 2023
1049c11
fixed auto rotate finally (maybe)
robotics1714 Apr 8, 2023
4fd0800
Started Wrist Code
Dylan-Powers Apr 8, 2023
e30604d
Added Wrist Positional Functionality (NOT TUNED)
Dylan-Powers Apr 8, 2023
8741193
Added different position constants for both cones and cubes
Dylan-Powers Apr 8, 2023
0e91ad3
Merge branch 'main' into Robot-2.0
Dylan-Powers Apr 8, 2023
264115b
Added Auto Rotate to Single Substation with alliance detection
Dylan-Powers Apr 8, 2023
ed1e123
added method to set the target angle on tele and auto init
Apr 8, 2023
6899939
Merge branch 'Rotate-To-Substations' into Robot-2.0
Apr 8, 2023
edd435c
Reorganized Autos
Dylan-Powers Apr 8, 2023
5fbbd66
Added/Edited 15 autos + reorganized the auto selection process to wor…
Apr 11, 2023
0bdebe6
Added Pathplanner to Duluth Auto
Apr 11, 2023
dcd84d1
tuning
robotics1714 Apr 12, 2023
a279eb0
I broke my wrist :(
Apr 14, 2023
ecb4325
Added standing Cone Position
Apr 15, 2023
c05fe0e
Pre 930 changes
robotics1714 Apr 15, 2023
8e0767e
Merge branch 'Auto-Reorganization' into Robot-2.0
Apr 15, 2023
d4d799d
Fixed Positioning bug
Apr 15, 2023
cb6420c
not sure if this works
robotics1714 Apr 15, 2023
53089ae
930 changes + bug fixing
robotics1714 Apr 16, 2023
d194653
Assigned mode change to one toggle button
robotics1714 Apr 16, 2023
814396c
Everen wanted this not me.
robotics1714 Apr 16, 2023
2da8f38
Everen also wanted this
robotics1714 Apr 16, 2023
1db692e
Added Manual Elevator Control
Apr 16, 2023
f96501c
Added Manual Elevator Control
Apr 16, 2023
fd3e3a5
Reorganized Autos and removed animation for lights
robotics1714 Apr 17, 2023
6734a41
Merge branch 'Control-Optimization' into Robot-2.0
robotics1714 Apr 17, 2023
7eb5edf
Merge branch 'Rotate-To-Substations' into Robot-2.0
Apr 17, 2023
f18f71a
Updated autos
robotics1714 Apr 20, 2023
6559edf
removed driver button command for testing autos
robotics1714 Apr 20, 2023
d8c2c33
day 1 changes
robotics1714 Apr 20, 2023
fb04202
Day 2 changes
robotics1714 Apr 21, 2023
27c1932
pulled logs
robotics1714 Apr 21, 2023
d5805ca
added command logging to auton periodic
robotics1714 Apr 21, 2023
65ff055
changed the alliance specific balance offset to 0
robotics1714 Apr 21, 2023
f243770
Edited mid auto path
robotics1714 Apr 21, 2023
16fb8bd
Increased length of outside auto
robotics1714 Apr 21, 2023
8c78274
Fixed Auto Balance bug
robotics1714 Apr 21, 2023
bda89c1
started tuning with new elevator
robotics1714 Jul 7, 2023
3f33ba6
tuning new cone positions
robotics1714 Jul 12, 2023
e5d13a9
updated controls for lakeshore
Samuel-Moore-1714 Jul 12, 2023
7ccde8f
pre-lakeshore changes
robotics1714 Jul 13, 2023
7be9ac8
resolved merge conflict
robotics1714 Jul 13, 2023
90ccaf4
MORE pre-lakeshore changes
robotics1714 Jul 13, 2023
2cfd3f7
Lakeshore Changes
robotics1714 Jul 18, 2023
376d5b1
removed bad slow mode
robotics1714 Jul 18, 2023
1e3bdc7
Added pose logging
robotics1714 Jul 19, 2023
f2ebab6
removed files specific to us
Dylan-Powers Jul 20, 2023
c385bd5
removed more things
Dylan-Powers Jul 20, 2023
13d0e9b
removed more things
Dylan-Powers Jul 20, 2023
396847e
small changes to remove difs
Dylan-Powers Jul 20, 2023
b8613a4
removing very small difs
Dylan-Powers Jul 20, 2023
b906cd9
began integration
Dylan-Powers Jul 20, 2023
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
42 changes: 42 additions & 0 deletions src/main/java/frc/lib/util/AbsoluteEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package frc.lib.util;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.Constants;

public class AbsoluteEncoder {

private final AnalogInput analogInput;

private Rotation2d rotation2d = new Rotation2d();
private Rotation2d home = new Rotation2d();

public AbsoluteEncoder(int port) {

analogInput = new AnalogInput(port);


}

public void homeEncoder() {

this.home = this.rotation2d;

}

public double getAbsolutePosition() {
double voltage = analogInput.getVoltage();
double degrees = 360 * (voltage / Constants.MAX_ENCODER_VOLTAGE);
return degrees;
}

/*
public void getRotationPeriodic() {

this.rotation2d = Rotation2d.fromDegrees(360 * (analogInput.getVoltage() / 5.0));
System.out.println(rotation2d);

}
*/
}
34 changes: 34 additions & 0 deletions src/main/java/frc/lib/util/SwerveAbsoluteEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package frc.lib.util;

public abstract class SwerveAbsoluteEncoder {
/**
* Reset the encoder to factory defaults.
*/
public abstract void factoryDefault();

/**
* Clear sticky faults on the encoder.
*/
public abstract void clearStickyFaults();

/**
* Configure the absolute encoder to read from [0, 360) per second.
*
* @param inverted Whether the encoder is inverted.
*/
public abstract void configure(boolean inverted);

/**
* Get the absolute position of the encoder.
*
* @return Absolute position in degrees from [0, 360).
*/
public abstract double getAbsolutePosition();

/**
* Get the instantiated absolute encoder Object.
*
* @return Absolute encoder object.
*/
public abstract Object getAbsoluteEncoder();
}
8 changes: 4 additions & 4 deletions src/main/java/frc/lib/util/SwerveModuleConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
public class SwerveModuleConstants {
public final int driveMotorID;
public final int angleMotorID;
public final int cancoderID;
public final int encoderID;
public final Rotation2d angleOffset;

/**
* Swerve Module Constants to be used when creating swerve modules.
* @param driveMotorID
* @param angleMotorID
* @param canCoderID
* @param encoderID
* @param angleOffset
*/
public SwerveModuleConstants(int driveMotorID, int angleMotorID, int canCoderID, Rotation2d angleOffset) {
public SwerveModuleConstants(int driveMotorID, int angleMotorID, int encoderID, Rotation2d angleOffset) {
this.driveMotorID = driveMotorID;
this.angleMotorID = angleMotorID;
this.cancoderID = canCoderID;
this.encoderID = encoderID;
this.angleOffset = angleOffset;
}
}
22 changes: 14 additions & 8 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package frc.robot;

import com.ctre.phoenix.motorcontrol.NeutralMode;
import com.ctre.phoenix.sensors.CANCoder;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
Expand All @@ -12,11 +13,16 @@

public final class Constants {
public static final double stickDeadband = 0.1;
public static final double MAX_ENCODER_VOLTAGE = 4.926;

public static final class Swerve {
public enum EncoderType { CANCODER, PWM }

public static final int pigeonID = 1;
public static final boolean invertGyro = false; // Always ensure Gyro is CCW+ CW-

public static final EncoderType encoderType = EncoderType.CANCODER;

public static final COTSFalconSwerveConstants chosenModule = //TODO: This must be tuned to specific robot
COTSFalconSwerveConstants.SDSMK4i(COTSFalconSwerveConstants.driveGearRatios.SDSMK4i_L2);

Expand Down Expand Up @@ -93,40 +99,40 @@ public static final class Swerve {
public static final class Mod0 { //TODO: This must be tuned to specific robot
public static final int driveMotorID = 1;
public static final int angleMotorID = 2;
public static final int canCoderID = 1;
public static final int encoderID = 1;
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(0.0);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, canCoderID, angleOffset);
new SwerveModuleConstants(driveMotorID, angleMotorID, encoderID, angleOffset);
}

/* Front Right Module - Module 1 */
public static final class Mod1 { //TODO: This must be tuned to specific robot
public static final int driveMotorID = 3;
public static final int angleMotorID = 4;
public static final int canCoderID = 2;
public static final int encoderID = 2;
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(0.0);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, canCoderID, angleOffset);
new SwerveModuleConstants(driveMotorID, angleMotorID, encoderID, angleOffset);
}

/* Back Left Module - Module 2 */
public static final class Mod2 { //TODO: This must be tuned to specific robot
public static final int driveMotorID = 5;
public static final int angleMotorID = 6;
public static final int canCoderID = 3;
public static final int encoderID = 3;
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(0.0);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, canCoderID, angleOffset);
new SwerveModuleConstants(driveMotorID, angleMotorID, encoderID, angleOffset);
}

/* Back Right Module - Module 3 */
public static final class Mod3 { //TODO: This must be tuned to specific robot
public static final int driveMotorID = 7;
public static final int angleMotorID = 8;
public static final int canCoderID = 4;
public static final int encoderID = 4;
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(0.0);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, canCoderID, angleOffset);
new SwerveModuleConstants(driveMotorID, angleMotorID, encoderID, angleOffset);
}
}

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/frc/robot/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import edu.wpi.first.math.kinematics.SwerveModuleState;

import frc.lib.math.Conversions;
import frc.lib.util.AbsoluteEncoder;
import frc.lib.util.CTREModuleState;
import frc.lib.util.SwerveModuleConstants;

Expand All @@ -14,14 +15,14 @@
import com.ctre.phoenix.motorcontrol.can.TalonFX;
import com.ctre.phoenix.sensors.CANCoder;

public class SwerveModule {
public class SwerveModule<T> {
public int moduleNumber;
private Rotation2d angleOffset;
private Rotation2d lastAngle;

private TalonFX mAngleMotor;
private TalonFX mDriveMotor;
private CANCoder angleEncoder;
private T angleEncoder;

SimpleMotorFeedforward feedforward = new SimpleMotorFeedforward(Constants.Swerve.driveKS, Constants.Swerve.driveKV, Constants.Swerve.driveKA);

Expand All @@ -33,6 +34,9 @@ public SwerveModule(int moduleNumber, SwerveModuleConstants moduleConstants){
angleEncoder = new CANCoder(moduleConstants.cancoderID);
configAngleEncoder();

// absoluteEncoder = new AbsoluteEncoder(moduleConstants.encoderID);
// configAngleEncoder();

/* Angle Motor Config */
mAngleMotor = new TalonFX(moduleConstants.angleMotorID);
configAngleMotor();
Expand Down Expand Up @@ -64,7 +68,6 @@ private void setSpeed(SwerveModuleState desiredState, boolean isOpenLoop){

private void setAngle(SwerveModuleState desiredState){
Rotation2d angle = (Math.abs(desiredState.speedMetersPerSecond) <= (Constants.Swerve.maxSpeed * 0.01)) ? lastAngle : desiredState.angle; //Prevent rotating module if speed is less then 1%. Prevents Jittering.

mAngleMotor.set(ControlMode.Position, Conversions.degreesToFalcon(angle.getDegrees(), Constants.Swerve.angleGearRatio));
lastAngle = angle;
}
Expand All @@ -73,18 +76,22 @@ private Rotation2d getAngle(){
return Rotation2d.fromDegrees(Conversions.falconToDegrees(mAngleMotor.getSelectedSensorPosition(), Constants.Swerve.angleGearRatio));
}

public Rotation2d getCanCoder(){
return Rotation2d.fromDegrees(angleEncoder.getAbsolutePosition());
public Rotation2d getEncoderAngle(){

//return Rotation2d.fromDegrees(angleEncoder.getAbsolutePosition());

return Rotation2d.fromDegrees(absoluteEncoder.getAbsolutePosition());
}


public void resetToAbsolute(){
double absolutePosition = Conversions.degreesToFalcon(getCanCoder().getDegrees() - angleOffset.getDegrees(), Constants.Swerve.angleGearRatio);
double absolutePosition = Conversions.degreesToFalcon(absoluteEncoder.getRotation().getDegrees() - angleOffset.getDegrees(), Constants.Swerve.angleGearRatio);
mAngleMotor.setSelectedSensorPosition(absolutePosition);
}

private void configAngleEncoder(){
angleEncoder.configFactoryDefault();
angleEncoder.configAllSettings(Robot.ctreConfigs.swerveCanCoderConfig);
// angleEncoder.configFactoryDefault();
// angleEncoder.configAllSettings(Robot.ctreConfigs.swerveCanCoderConfig);
}

private void configAngleMotor(){
Expand All @@ -103,6 +110,8 @@ private void configDriveMotor(){
mDriveMotor.setSelectedSensorPosition(0);
}

private

public SwerveModuleState getState(){
return new SwerveModuleState(
Conversions.falconToMPS(mDriveMotor.getSelectedSensorVelocity(), Constants.Swerve.wheelCircumference, Constants.Swerve.driveGearRatio),
Expand Down
74 changes: 71 additions & 3 deletions src/main/java/frc/robot/commands/TeleopSwerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,102 @@
import java.util.function.DoubleSupplier;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.CommandBase;


public class TeleopSwerve extends CommandBase {
private Swerve s_Swerve;
public Swerve s_Swerve;
private DoubleSupplier translationSup;
private DoubleSupplier strafeSup;
private DoubleSupplier rotationSup;
private BooleanSupplier robotCentricSup;
private BooleanSupplier slowModeSup;
private double rotationSpeed;
private ProfiledPIDController PID;
private DoubleSupplier targetRotation;
private boolean isAutoRotating;
private static final double AUTO_ROTATE_DEADBAND = 0.3;
// private Timer timer;

public TeleopSwerve(Swerve s_Swerve, DoubleSupplier translationSup, DoubleSupplier strafeSup, DoubleSupplier rotationSup, BooleanSupplier robotCentricSup) {
public TeleopSwerve(Swerve s_Swerve, DoubleSupplier translationSup, DoubleSupplier strafeSup,
DoubleSupplier rotationSup, BooleanSupplier robotCentricSup, DoubleSupplier targetRotation,
BooleanSupplier slowModeSup, double rotationSpeed, boolean isAutoRotating) {
this.s_Swerve = s_Swerve;
addRequirements(s_Swerve);

this.translationSup = translationSup;
this.strafeSup = strafeSup;
this.rotationSup = rotationSup;
this.robotCentricSup = robotCentricSup;
this.slowModeSup = slowModeSup;
this.rotationSpeed = rotationSpeed;
this.targetRotation = targetRotation;
this.isAutoRotating = isAutoRotating;

PID = new ProfiledPIDController(
Constants.ROTATE_KP,
Constants.ROTATE_KI,
Constants.ROTATE_KD,
new Constraints(Constants.ROTATE_VELOCITY, Constants.ROTATE_ACCELERATION)
);

// timer = new Timer();
}

@Override
public void execute() {
/* Get Values, Deadband*/
double translationVal = MathUtil.applyDeadband(translationSup.getAsDouble(), Constants.stickDeadband);
double strafeVal = MathUtil.applyDeadband(strafeSup.getAsDouble(), Constants.stickDeadband);
double strafeVal = MathUtil.applyDeadband(strafeSup.getAsDouble(), Constants.stickDeadband) * rotationSpeed;
double rotationVal = MathUtil.applyDeadband(rotationSup.getAsDouble(), Constants.stickDeadband);

/*slowmode*/
if (slowModeSup.getAsBoolean()) {
translationVal = translationVal * Constants.SLOW_MODE_PERCENT_TRANSLATION;
strafeVal = strafeVal * Constants.SLOW_MODE_PERCENT_STRAFE;
rotationVal = rotationVal * Constants.SLOW_MODE_PERCENT_ROTATION;
}

/* Rotate to Score */
if (-AUTO_ROTATE_DEADBAND <= rotationVal && rotationVal <= AUTO_ROTATE_DEADBAND && isAutoRotating) {
double yaw = s_Swerve.getYaw().getDegrees() % 360;

// convert the yaw from [-360, 360] to [-180, 180]
if (yaw > 180) {
yaw = yaw - 360;
} else if (yaw < -180) {
yaw = yaw + 360;
}

// Convert to Rotation2d for rotate command
Rotation2d rotationYaw = Rotation2d.fromDegrees(yaw);

// Get the target rotationVal as a Rotation2d object
Rotation2d targetRotationVal = Rotation2d.fromDegrees(targetRotation.getAsDouble());

// Rotate rotationYaw to make the 0 of it at the target angle
// If targetRotationVal is 180 degrees the rotationYaw's 0 will be pointing towards 180
rotationYaw = rotationYaw.rotateBy(targetRotationVal);

// double error = yaw - targetRotationVal;
//SmartDashboard.putNumber("debug/yaw", yaw);
// SmartDashboard.putNumber("debug/targetRotationVal", targetRotationVal);
// SmartDashboard.putNumber("debug/error", error);
// if (Math.abs(error) > 180) {
// yaw = Math.abs(360 - yaw);
// }
// SmartDashboard.putNumber("debug/calculated yaw", yaw);

SmartDashboard.putNumber("debug/calculated yaw", rotationYaw.getDegrees());
rotationVal = PID.calculate(rotationYaw.getDegrees(), 0.0);
}

/* Drive */
s_Swerve.drive(
new Translation2d(translationVal, strafeVal).times(Constants.Swerve.maxSpeed),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/subsystems/Swerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void periodic(){
swerveOdometry.update(getYaw(), getModulePositions());

for(SwerveModule mod : mSwerveMods){
SmartDashboard.putNumber("Mod " + mod.moduleNumber + " Cancoder", mod.getCanCoder().getDegrees());
SmartDashboard.putNumber("Mod " + mod.moduleNumber + " Cancoder", mod.getEncoderAngle().getDegrees());
SmartDashboard.putNumber("Mod " + mod.moduleNumber + " Integrated", mod.getPosition().angle.getDegrees());
SmartDashboard.putNumber("Mod " + mod.moduleNumber + " Velocity", mod.getState().speedMetersPerSecond);
}
Expand Down