Skip to content

ATOM-STORM-6766/BaseSwerve

Repository files navigation

BaseSwerve


This template includes everything we require for a basic Swerve Drive robot.

Includes:

  • Phoenix Pro Implementation
  • Basic Swerve Code
  • PathPlanner functionality
  • Basic examples for autonomous routines
  • Shuffleboard functionality
  • Various Utilty classes

This is based on Team 364's (dirtbikerxz) BaseTalonFXSwerve, though it is heavily modified.



CHANGE TEAM NUMBER

Open the Command Palette (Ctrl+Shift+P) then type Set Team Number.



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)

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

  2. MODULE_TYPE:
    Set the module and drive ratio you are using here.
    For our uses, it will typically be the SDS MK4/MK4i Module. Make sure to select the correct gear ratio - for us it will most likely be Level 3.
    This will automatically set these constants required for the module to function properly:

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

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

  5. GEAR_RATIO(Drive): Total gear ratio for the drive motor.
    This value will be automatically set by the selected module.

  6. GEAR_RATIO(Steer): Total gear ratio for the steer motor.
    This value will be automatically set by the selected module.

  7. MOTOR_INVERT(Steer): Must be set such that it is CCW+.
    This value will be automatically set by the selected module, but checking is recommended.

  8. MOTOR_INVERT(Drive): 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.
    This value will be automatically set by the selected module.

  9. ModuleConstants: set the Can Id's of the motors and CANcoders for the respective modules, see the next step for setting offsets.

  10. Setting Offsets

    • Open Phoenix Tuner X
    • Select the CANCoder you want to offset
    • Click the "Zero CANcoder" button
    • Go to the config tab and copy the value in the "Magnet Offset" config. (If it shows 0, refresh using "Refresh/Reload Configs")
    • Paste the value in the angleOffset parameter of the corresponding module constants.
      Note: The offset value from Phoenix Tuner X is in rotations. You must use Rotation2d.fromRotations(value_here).
  11. Angle Motor PID Values:
    This value will be automatically set through the selected module. If you prefer it to be more or less aggressive, see instructions below

    • 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.
  12. MAX_SPEED: In Meters Per Second. MAX_ANGULAR_VELOCITY: 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.

  13. KS(Drive), KV(Drive), and KA(Drive)
    Leave these as the default values. If for some reason they require a change, you can use the WPILib characterization tool, found here. You will need to lock the modules straight forward, and complete the characterization as if it was a standard tank drive.

  14. KP(Drive):
    After inserting the KS, KV, and KA values into the code, tune the drive motor kP until it doesn't overshoot or oscillate around a target velocity.
    Leave KI(Drive), KD(Drive), and KF(Drive) at 0.0.



Controller Mappings

The code is natively setup to use a Xbox controller, though other controllers will work.

Note: To add additional button bindings, create methods in Controlboard.java and reference them in RobotContainer.java. See configButtonBindings.

  • Left Stick: Translation Control (forwards and sideways movement)
  • Right Stick: Rotation Control
  • Back Button: Zero Gyro (useful if the gyro drifts mid match, just rotate the robot forwards, and press Back to rezero)
  • Start Button: Toggles field-centric mode



Shuffleboard Configuration


The following relates to using tabs and entries with Shuffleboard.
Refer to the WPILib Wiki for additional help.


Creating tabs and entries

  • Create a class extending ShuffleboardTabBase and structure it as follows:
    • GenericEntry objects for each of the values you want to display/get with Shuffleboard.
    • Initialize the tab and use create methods from ShuffleboardTabBase.java to create entries with the respective data types in createEntries()
    • Either set or get the entry's value in periodic()
    • See SwerveTab.java for an example of this structure.


Putting tabs on Shuffleboard

  • Add tabs to the list in ShuffleboardTabManager.java, either as a debug tab or regular tab.
    • Debug tabs will only be shown if includeDebug is true
      • Displaying to Shuffleboard is resource-intensive, so make sure it is true only when you are debugging/developing code.


Using the Shuffleboard application

  • Shuffleboard should be automatically installed, but you may have to select it in DriverStation.
  • All tabs and corresponding entries should appear with the values/names they are set with, assuming you have deployed the code.
    • If they do not appear, simply restart the robot code and re-open Shuffleboard.



设置常量

以下内容必须根据您的机器人和模块的具体常量在Constants.java文件中进行调整(所有距离单位必须为米,旋转单位为弧度):

  1. 陀螺仪设置:PIGEON_IDGYRO_INVERT(确保陀螺仪旋转为逆时针+)

  2. MODULE_TYPE:
    在此设置您使用的模块和驱动比率。
    对我们的使用而言,通常是 SDS MK4/MK4i 模块。确保选择正确的齿轮比 - 对我们来说,很可能是3级。
    这将自动设置模块正常运行所需的以下常量:

    • 车轮周长
    • 转向电机反转
    • 驱动电机反转
    • CANcoder 传感器反转
    • 转向电机齿轮比
    • 驱动电机齿轮比
    • 转向 Falcon 电机 PID 值
  3. TRACK_WIDTH: 左右模块中心到中心的距离,以米为单位。

  4. WHEEL_BASE: 前后模块车轮的中心到中心的距离,以米为单位。

  5. GEAR_RATIO(Drive): 驱动电机的总齿轮比。
    此值将由所选模块自动设置。

  6. GEAR_RATIO(Steer): 转向电机的总齿轮比。
    此值将由所选模块自动设置。

  7. MOTOR_INVERT(Steer): 必须设置为逆时针+。
    此值将由所选模块自动设置,但建议检查。

  8. MOTOR_INVERT(Drive): 由于在步骤#11中设置了偏移量,因此始终可以保持为false,这样驱动电机的正输入会导致机器人向前行驶。
    此值将由所选模块自动设置。

  9. ModuleConstants: 设置各个模块的电机和CANcoder的Can Id,参见下一步以设置偏移量。

  10. 设置偏移量

    • 打开 Phoenix Tuner X
    • 选择要偏移的 CANCoder
    • 点击“Zero CANcoder”按钮
    • 转到配置选项卡并复制“Magnet Offset”配置中的值。(如果显示0,请使用“Refresh/Reload Configs”刷新)
    • 将值粘贴到相应模块常量的 angleOffset 参数中。
      注意:Phoenix Tuner X 中的偏移值以旋转为单位。必须使用 Rotation2d.fromRotations(value_here)
  11. 角度电机 PID 值:
    此值将通过所选模块自动设置。如果您希望其更或更不激进,请参见以下说明

    • 调整时从较低的P值(0.01)开始。
    • 乘以10,直到模块开始围绕设定点振荡
    • 缩小搜索值(例如,如果在P值为10时开始振荡,则尝试(10 -> 5 -> 7.5 -> 等等)),直到模块不再围绕设定点振荡。
    • 如果有任何超调,可以通过重复相同过程添加一些D值,如果没有则保持为0。始终将I保持为0。
  12. MAX_SPEED: 每秒米数。MAX_ANGULAR_VELOCITY: 每秒弧度数。可以使用理论值,但最好物理驾驶机器人以找到实际最大值。

  13. KS(Drive), KV(Drive), 和 KA(Drive)
    将这些保持为默认值。如果出于某种原因需要更改,可以使用 WPILib 表征工具,见这里。需要将模块直锁,完成表征如同标准坦克驱动。

  14. KP(Drive):
    将 KS、KV 和 KA 值插入代码后,调整驱动电机的 kP 值,直到它不会超调或围绕目标速度振荡。
    KI(Drive), KD(Drive), 和 KF(Drive) 保持为0.0。



控制器映射

代码本身设置为使用Xbox控制器,尽管其他控制器也可以使用。

注意:要添加其他按钮绑定,请在Controlboard.java中创建方法,并在RobotContainer.java中引用它们。参见configButtonBindings

  • 左摇杆:平移控制(前后和侧向移动)
  • 右摇杆:旋转控制
  • 后退按钮:零陀螺仪(如果比赛中陀螺仪漂移,旋转机器人向前,然后按下后退按钮以重新归零)
  • 开始按钮:切换字段中心模式



Shuffleboard 配置


以下内容涉及使用 Shuffleboard 的标签和条目。
如需更多帮助,请参阅 WPILib Wiki


创建标签和条目

Swerve/blob/main/src/main/java/frc/robot/shuffleboard/ShuffleboardTabManager.java) 中设置或获取条目的值


在 Shuffleboard 上放置标签

  • ShuffleboardTabManager.java 的列表中添加标签,作为调试标签或常规标签。
    • 调试标签只有在 includeDebug 为 true 时才会显示
      • 显示到 Shuffleboard 是资源密集型的,因此仅在调试/开发代码时才将其设置为 true。


使用 Shuffleboard 应用程序

  • Shuffleboard 应该会自动安装,但您可能需要在 DriverStation 中选择它。
  • 假设您已部署代码,所有标签及相应条目应以其设置的值/名称出现。
    • 如果没有出现,只需重新启动机器人代码并重新打开 Shuffleboard。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages