Git Product home page Git Product logo

basetalonfxswerve's Introduction

Note: While this code intends to provide plug n play configs/tuning numbers for COTS Modules, there are several configs/tunes that are untested with the change to Phoenix 6. If this code is used for one of those untested modules, the module specific configs and tuning will most likely need to be edited.

If anyone would like to contribute a config/tune to be added as "tested" please send a message or make a PR.

Scroll down for a full list of tested/untested configs.

BaseTalonFXSwerve

Basic Swerve Code for a Swerve Module using TalonFX Controllers, a CTRE CANCoder, and a CTRE Pigeon Gyro
This code was designed with Swerve Drive Specialties MK3, MK4, MK4i, and WCP SwerveX style modules in mind, but should be easily adaptable to other styles of modules.

Setting Constants

The following things must be adjusted to your robot and module's specific constants in the Constants.java file (all distance units must be in meters, and rotation units in radians):
These instructions are mostly followable from Step

  1. Gyro Settings: pigeonID(ensure that the gyro rotation is CCW+ (Counter Clockwise Positive)

  2. chosenModule:
    Note: Not every module's configs are tested. See the note at the top
    If you are using a supported COTS Module, set the module and drive ratio you are using here.
    This will automatically set certain constants for the specific module required to function properly.
    If you are not using a COTS supported module, you should delete this variable, and fix all the errors that pop up with correct values for the module you are using
    Here is a list of the constants that will automatically be set if you are using a supported module:

    • Wheel Circumference
    • Angle Motor Invert
    • Drive Motor Invert
    • CANCoder Sensor Invert
    • Angle Motor Gear Ratio
    • Drive Motor Gear Ratio
    • Angle Falcon Motor PID Values
  3. trackWidth: Center to Center distance of left and right modules in meters.

  4. wheelBase: Center to Center distance of front and rear module wheels in meters.

  5. wheelCircumference: Cirumference of the wheel (including tread) in meters.
    If you are using a supported module, this value will be automatically set.

  6. driveGearRatio: Total gear ratio for the drive motor.
    If you are using a supported module, this value will be automatically set.

  7. angleGearRatio: Total gear ratio for the angle motor.
    If you are using a supported module, this value will be automatically set.

  8. canCoderInvert and angleMotorInvert: Both must be set such that they are CCW+.
    If you are using a supported module, this value will be automatically set.

  9. driveMotorInvert: If you are using a supported module, this value will be automatically set.
    This can always remain false, since you set your offsets in step 11 such that a positive input to the drive motor will cause the robot to drive forwards.
    However this can be set to true if for some reason you prefer the bevel gears on the wheel to face one direction or another when setting offsets. See Step 11 for more information.

  10. Module Specific Constants: set the Can Id's of the motors and CANCoders for the respective modules, see the next step for setting offsets.

  11. Setting Offsets

    • For finding the offsets, use a piece of 1x1 metal that is straight against the forks of the front and back modules (on the left and right side) to ensure that the modules are straight.
    • Point the bevel gears of all the wheels in the same direction (either facing left or right), where a postive input to the drive motor drives the robot forward (you can use phoenix tuner to test this). If for some reason you set the offsets with the wheels backwards, you can change the driveMotorInvert value to fix.
    • Open smartdashboard (or shuffleboard and go to the smartdashboard tab), you will see 4 printouts called "Mod 0 Cancoder", "Mod 1 Cancoder", etc.
      If you have already straightened the modules, copy those 4 numbers exactly (to 2 decimal places) to their respective angleOffset variable in constants.
      Note: The CANcoder values printed to smartdashboard are in degrees, when copying the values to angleOffset you must use Rotation2d.fromDegrees("copied value").
  12. Angle Motor PID Values:
    If you are using a supported module, this value will be automatically set. If you are not, or prefer a more or less aggressive response, you can use the below instructions to tune.

    • To tune start with a low P value (0.01).
    • Multiply by 10 until the module starts oscilating around the set point
    • Scale back by searching for the value (for example, if it starts oscillating at a P of 10, then try (10 -> 5 -> 7.5 -> etc)) until the module doesn't oscillate around the setpoint.
    • If there is any overshoot you can add in some D by repeating the same process, leave at 0 if not. Always leave I at 0.
  13. maxSpeed: In Meters Per Second. maxAngularVelocity: In Radians Per Second. For these you can use the theoretical values, but it is better to physically drive the robot and find the actual max values.

  14. Get the drive characterization values (KS, KV, KA) by using the WPILib characterization tool, found here. You will need to lock your modules straight forward, and complete the characterization as if it was a standard tank drive.

  15. driveKP:
    After completeing characterization and inserting the KS, KV, and KA values into the code, tune the drive motor kP until it doesn't overshoot and doesnt oscilate around a target velocity.
    Leave driveKI, and driveKD at 0.0.

Controller Mappings

This code is natively setup to use a xbox controller to control the swerve drive.

  • Left Stick: Translation Control (forwards and sideways movement)
  • Right Stick: Rotation Control
  • Y button: Zero Gyro (useful if the gyro drifts mid match, just rotate the robot forwards, and press Y to rezero)
  • Left Bumper: Switches To Robot Centric Control while held

Tested Modules

Vendor Module Motor Configs (Motor Inverts etc) Rotation Motor Tuning
WCP SwerveX Standard Kraken X60 Untested Untested
WCP SwerveX Standard Falcon 500 Untested Untested
WCP SwerveX Flipped Kraken X60 Untested Untested
WCP SwerveX Flipped Falcon 500 Untested Untested
SDS MK3 Kraken X60 Untested Untested
SDS MK3 Falcon 500 Tested Untested
SDS MK4 Kraken X60 Untested Untested
SDS MK4 Falcon 500 Tested Untested
SDS MK4i Kraken X60 Untested Untested
SDS MK4i Falcon 500 Tested Tested

Changelog

  • 1/4/24:
    • Updated Phoenix to Phoenix 6 - 24.0.0-beta-7
      • Implemented Phoenix 6's SensorToMechanismRatio removing the need to convert gear ratios in code
      • Implemented Phoenix 6's ContinuousWrap removing the need for a custom SwerveModuleState.optimize function.
      • Updated conversions from Phoenix 5's units ("counts") to Phoenix 6's units (rotations).
    • Updated WPILib to 2024.1.1-beta-4
    • Added SwerveX Modules and Kraken X60 Motors
    • Moved from directly using the Gyro angle in Swerve for heading (getGyroYaw()) to using the odometry output for rotation getHeading() as intended by WPILib
    • Moved repository off of 364's Github

basetalonfxswerve's People

Contributors

dirtbikerxz avatar rzblue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

basetalonfxswerve's Issues

Wheels need re-alignment after restart

My team is having a similar issue to issue #8 , after restarting we have to re-align the wheels. But it seems to have a different cause, as we're using the new code with the fix, and binding the "resetModulesToAbsolute" to a button causes the wheels to do a 360, and not turn to where they should be. We are using the Mk4i L2's, and a NavX 2 (We made changes to the code to make it work with the navX, so maybe that messed something up?) Here's our code: (https://github.com/BlueCrewRobotics/FrenchToast) help would be appreciated!

error report: "internal issue with print and error tags."

Description of the Issue
When the robot is enabled, it only moves for approximately 5 to 20 and then stops. Additionally, an error is reported: "internal issue with print and error tags." on the FRC Driver Station.

Steps to Reproduce
After uploading the code, press [Enable].

image

Desktop Environment:

  • OS: Windows 10

  • Project Information:

    plugins {
        id "java"
        id "edu.wpi.first.GradleRIO" version "2024.1.1"
    }
    
    java {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    {
        "fileName": "Phoenix6.json",
        "name": "CTRE-Phoenix (v6)",
        "version": "24.0.0-beta-7",
        "frcYear": 2024,
        "uuid": "e995de00-2c64-4df5-8831-c1441420ff19",
        "mavenUrls": [
            "https://maven.ctr-electronics.com/release/"
        ],
        "jsonUrl": "https://maven.ctr-electronics.com/release/com/ctre/phoenix6/latest/Phoenix6-frc2024-beta-latest.json"...
    }
    
    {
      "fileName": "WPILibNewCommands.json",
      "name": "WPILib-New-Commands",
      "version": "1.0.0",
      "uuid": "111e20f7-815e-48f8-9dd6-e675ce75b266",
      "frcYear": "2024",
      "mavenUrls": [],
      "jsonUrl": ""...
    }

PathPlanner 24 integration

Hi! How would I implement the new pathplanner API? It requires methods that don't exist and I'm not sure how to get the relative speeds and set the relative speeds.

SwerveDriveOdometry is undefined

This line:
swerveOdometry = new SwerveDriveOdometry(Constants.Swerve.swerveKinematics, getYaw());

I am getting a error that SwerveDriveOdometry is undefined. How can I fix this?

Make sure to change team number when using code

I just wanted to let other teams know to change the team number in the WPILib settings to their own team number because we were having issues uploading it to our RoboRio since we have our team number set on the RoboRio, which is different than Team 364. This might not happen to everyone else but just wanted to put it out there in case people were having issues during deployment.

Also, thanks to Team 364 for creating this amazing repository that saved us so much time from continuing to try to make the swerve-lib code from swerve drive specialties work.

Sensor Reading Problems

We are preparing for 2024 swerve and we imported this project and made the necessary changes (team number, motor IDs, etc). we keep getting errors when trying to get the CANCoder positions

CTR: CAN frame not received/too-stale.
from: Talon FX 3 (cani) GetSelectedSensorPosition
at: Stack Trace not received for performance reasons.

we keep getting zero for our position readouts even when purposefully moving the robot.

we aren't sure if this is a problem with the code for 2024 or not, but we wanted to let yall know in case it is an issue with changing WPIlib versions

Swerve module optimization not working correctly

During testing of our swerve modules, I noticed that a swerve module would sometimes do multiple rotations in the "wrong" direction rather than a short rotation in the "correct" direction. After debugging, the values returned from the CTREModuleState.placeInAppropriate0To360Scope function are sometimes outside of the -360 to +360 range (or -180 to +180? Not sure of the intended return range). This may be caused by the scopeReference parameter being somehow set to a value outside of the -360 to +360 range. This is causing the behavior described above. Is this a known issue and is there a known fix?

(Question) How to enable FOC for falcons mk4i?

What would I have to change it in the code to use the FOC features? For example what would you change the position/velocity voltage methods in SwerveModule.java to? And would you be able to use a fusedcancoder object?

When using Mk3 Modules Right Thumb Stick does not Operate correctly

When using the code base and modifying for the NavX gyro instead of the Pigeon and using Mk3 Modules the left thumb stick operates correctly and robot moves as desired. When using the right thumb stick to spin the robot the swerve modules form an X on the bottom of the robot where all the modules point towards the center of the robot. The modules should form an O to spin the robot. Code base is here and any help would be appreciated: https://github.com/github3658/BaseFalconServe

Thanks for making this code base available!

How can I control the speed of the swerve modules?

I was wondering how I could limit the speed of the drive motors for testing. Is there a preexisting line of code that can alter the speed or do we need to add a new line? If we need to add a line what would the syntax be?

Question on Gyro Angle Wrapping

Does 364 have support for wrapping the gyro angle so it fits within the range of [-180, 180)? If not, any recommendations on how to implement this? Would using something from the IEEE library work? @dirtbikerxz

The method getPosition() is undefined for the type SwerveModule

In swerve.java,
the lines
positions[mod.moduleNumber] = mod.getPosition();
&
SmartDashboard.putNumber("Mod " + mod.moduleNumber + " Integrated", mod.getPosition().angle.getDegrees());
throw an error "The method getPosition() is undefined for the type SwerveModule"


sorry, my bad

Offset problem

We are having a problem with the offsets or the angles of the modules.We set up the code and changed the constants to suit our robot, and then we tried moving the robot at first everything runs smoothly, the angles are close to perfect and the directions are correct . After a bit of time the modules have a slight change in their angle which builds up slowly overtime , but the bigger problem happens when we reupload the code, the angles become WAY off.
The same thing happened with us last year, and the solution was to make a delay for one sec in the swerve subsystem and then reset the modules to absolute, and that didn't work either.
We checked the constants; trackwidth,type of module,offsets ...etc. But nothing works.
Btw we are using MK4i L2 falcon modules.
Does anyone experience the same thing?
And how can we solve this?

Turning and Driving modules off

Is anyone else having the issue where if they turn it won’t turn in place? With driving it doesn’t really go forward or sideways but it also starts turning

Thanks in Advance!

Wheels aren't fully turning 90 degrees

Thanks for publishing this code! Very helpful. One problem we're having - when we attempt to move the robot right or left, the wheels only turn 45 degrees. The encoders think the wheels are at 90 degrees. When we attempt the move the robot forward or backward, the wheels align to 0 as expected. We're using the Mk4 modules. Thoughts on what might be causing this?

Module 3 occasionally starts at the wrong angle

We had a problem where module 3 would sometimes get initialized to the wrong angle. This started happening more frequently at our first competition. Our drive system was based on the latest BaseFalconSwerve with the 1 second delay added before "resetModulesToAbsolute" in the Swerve constructor. Grasping at straws and thinking it to be an initialization issue we added another 1 second delay prior to the creation of the swerve module objects to allow all devices time to come up. We have not had the problem since. Could the delay have been less? Probably, but why rock the boat during competition. I wanted to record this in case someone else was having a similar issue.

Configuration: Original Roborio, Pigeon 2 and Canivore with Falcons, Cancoders and Pigeon 2. All other CAN devices are running on the Roborio CAN interface.

Issue with one module running at slower speed

I'm hoping someone can help us.

We have a brand new setup. MK4is, KrakenX60, cancoders, and a NavX.

We have it all wired up and coded with this base code, configured for our can ids, our dimensions, etc.

When we drive, 3 modules behave as we would expect, but one of the drive motors runs at a much slower pace (less than half the speed, if that).

We have swapped hardware (we have another 4 mk4is for our other chassis), power wires, can wires, and even assigned the can ids to a different module to eliminate hardware. The problem is always with the module assigned to #2 (back left). The problem must be in our code or configuration.

Any suggestions on how else to debug this?

Initial motor offsets seem to be changing

Something odd we're experiencing, it appears our motor offsets are changing every time we reset the robot or deploy code. It's only off by a few degrees each time, but it affects wheel positioning. Does anyone have any thoughts on how to troubleshoot what might be happening?

Kraken X60 MK4i L2 Config: Motor Inverts

We have confirmed that the Kraken X60 on the MK4i in L2 Configuration is correct!

--
Stay Cool,
Jimmy McCosker
Lynk 9496 Programmer / Omegabytes 5727 Alumni
CD: Jimmyy
GitHub: witherslayer67
"Words have power, but only when people will listen. When they won't, actions speak loud enough for anyone." -Ryosuke Takahashi

Kraken Turn Motor no Worky

had to change the Kp value in "src/main/java/frc/lib/util/COTSTalonFXSwerveConstants.java" line 289 to 30.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.