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

Added example SwerveControllerCommand #108

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
5857476
Added example SwerveControllerCommand
chaoticthegreat Jan 9, 2024
18fc1ae
added docstrings
chaoticthegreat Jan 9, 2024
c27dc3a
Update run_tests.sh
Suave101 Jan 9, 2024
7921298
Merge pull request #1 from Suave101/patch-1
chaoticthegreat Jan 9, 2024
0fff226
Lowercased all files and folders, reformmated code with black, fixed …
chaoticthegreat Jan 9, 2024
f126455
Converted header docstrings into comments
chaoticthegreat Jan 9, 2024
10a7f17
remove unnecessary files
chaoticthegreat Jan 10, 2024
8b66ef6
fixed the renaming of files and directories
chaoticthegreat Jan 10, 2024
5ecb389
reran black on directory
chaoticthegreat Jan 10, 2024
9df1182
Removed m_ in variables
chaoticthegreat Jan 10, 2024
09a1e17
add file header for robot.py
chaoticthegreat Jan 10, 2024
07b0365
fixed module import error when import drivesubsystem
chaoticthegreat Jan 10, 2024
b4d684d
removed Top-level constants class
chaoticthegreat Jan 11, 2024
4e6053b
Update test.yml
chaoticthegreat Jan 11, 2024
cac6b9c
Update drivesubsystems.py
chaoticthegreat Jan 11, 2024
b384b48
swervecontroller to 3
chaoticthegreat Jan 11, 2024
67347ef
Update constants.py
chaoticthegreat Jan 11, 2024
d2ecead
Update drivesubsystems.py
chaoticthegreat Jan 11, 2024
d97a8a7
Update constants.py
chaoticthegreat Jan 11, 2024
c2d71eb
made everything 4
chaoticthegreat Jan 11, 2024
1390c7e
added setdefault command
chaoticthegreat Jan 12, 2024
0ebecf6
Update drivesubsystems.py
chaoticthegreat Jan 12, 2024
cc0789a
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
d52d93f
Update drivesubsystems.py
chaoticthegreat Jan 12, 2024
8c34f73
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
5d82e0d
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
8a73f7e
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
1de8d65
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
3285c47
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
bdc25a1
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
d3ec49c
Update robotcontainer.py
chaoticthegreat Jan 12, 2024
ee841a5
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
4dc8861
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
954bbe1
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
d6e2c1e
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
c985aba
Update robot.py
chaoticthegreat Jan 13, 2024
9d1550a
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
bae17cd
Update robot.py
chaoticthegreat Jan 13, 2024
3e60dc8
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
2da1b6d
Update robotcontainer.py
chaoticthegreat Jan 13, 2024
d3b154e
fixed changes
chaoticthegreat Jan 20, 2024
a48c32f
Update robotcontainer.py
chaoticthegreat Jan 20, 2024
8de3ce8
Update robot.py
chaoticthegreat Jan 20, 2024
3139e04
Update drivesubsystems.py
chaoticthegreat Jan 20, 2024
6851048
Update swervemodule.py
chaoticthegreat Jan 20, 2024
ebeef3a
Merge branch 'SwerveControllerCommand' of https://github.com/ChaoticC…
chaoticthegreat Jan 20, 2024
73b42cd
fixed naming eror
chaoticthegreat Jan 20, 2024
d2f2248
fixed swerve controller command
chaoticthegreat Jan 20, 2024
0110bac
fixed changes
chaoticthegreat Jan 20, 2024
67be4bf
fixed some changes
chaoticthegreat Jan 20, 2024
4c99767
fixed docstring
chaoticthegreat Jan 20, 2024
6066bec
Merge branch 'SwerveControllerCommand' of https://github.com/ChaoticC…
chaoticthegreat Jan 20, 2024
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
93 changes: 93 additions & 0 deletions SwerveControllerCommand/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# 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.
#

from wpilib import TimedRobot
from wpimath.geometry import Translation2d
from wpimath.kinematics import SwerveDrive4Kinematics
from wpimath.trajectory import TrapezoidProfile
import math


class Constants:
chaoticthegreat marked this conversation as resolved.
Show resolved Hide resolved
class DriveConstants:
kFrontLeftDriveMotorPort = 0
kRearLeftDriveMotorPort = 2
kFrontRightDriveMotorPort = 4
kRearRightDriveMotorPort = 6

kFrontLeftTurningMotorPort = 1
kRearLeftTurningMotorPort = 3
kFrontRightTurningMotorPort = 5
kRearRightTurningMotorPort = 7

kFrontLeftTurningEncoderPorts = [0, 1]
kRearLeftTurningEncoderPorts = [2, 3]
kFrontRightTurningEncoderPorts = [4, 5]
kRearRightTurningEncoderPorts = [6, 7]

kFrontLeftTurningEncoderReversed = False
kRearLeftTurningEncoderReversed = True
kFrontRightTurningEncoderReversed = False
kRearRightTurningEncoderReversed = True

kFrontLeftDriveEncoderPorts = [8, 9]
kRearLeftDriveEncoderPorts = [10, 11]
kFrontRightDriveEncoderPorts = [12, 13]
kRearRightDriveEncoderPorts = [14, 15]

kFrontLeftDriveEncoderReversed = False
kRearLeftDriveEncoderReversed = True
kFrontRightDriveEncoderReversed = False
kRearRightDriveEncoderReversed = True

kDrivePeriod = TimedRobot.kDefaultPeriod

kTrackWidth = 0.5
kWheelBase = 0.7
kDriveKinematics = SwerveDrive4Kinematics(
Translation2d(kWheelBase / 2, kTrackWidth / 2),
Translation2d(kWheelBase / 2, -kTrackWidth / 2),
Translation2d(-kWheelBase / 2, kTrackWidth / 2),
Translation2d(-kWheelBase / 2, -kTrackWidth / 2),
)

kGyroReversed = False

ksVolts = 1
kvVoltSecondsPerMeter = 0.8
kaVoltSecondsSquaredPerMeter = 0.15

kMaxSpeedMetersPerSecond = 3

class ModuleConstants:
kMaxModuleAngularSpeedRadiansPerSecond = 2 * math.pi
kMaxModuleAngularAccelerationRadiansPerSecondSquared = 2 * math.pi

kEncoderCPR = 1024
kWheelDiameterMeters = 0.15
kDriveEncoderDistancePerPulse = (kWheelDiameterMeters * math.pi) / kEncoderCPR

kTurningEncoderDistancePerPulse = (2 * math.pi) / kEncoderCPR

kPModuleTurningController = 1
kPModuleDriveController = 1

class OIConstants:
kDriverControllerPort = 0

class AutoConstants:
kMaxSpeedMetersPerSecond = 3
kMaxAccelerationMetersPerSecondSquared = 3
kMaxAngularSpeedRadiansPerSecond = math.pi
kMaxAngularSpeedRadiansPerSecondSquared = math.pi

kPXController = 1
kPYController = 1
kPThetaController = 1

kThetaControllerConstraints = TrapezoidProfile.Constraints(
kMaxAngularSpeedRadiansPerSecond, kMaxAngularSpeedRadiansPerSecondSquared
)
66 changes: 66 additions & 0 deletions SwerveControllerCommand/robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
#
# 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.
#

from commands2 import TimedCommandRobot, CommandScheduler
from robotcontainer import RobotContainer


class Robot(TimedCommandRobot):
def robotInit(self):
"""
Instantiate our RobotContainer. This will perform all our button bindings, and put our
autonomous chooser on the dashboard.
"""
self.robotContainer = RobotContainer()

def robotPeriodic(self):
"""
Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled
commands, running already-scheduled commands, removing finished or interrupted commands,
and running subsystem periodic() methods. This must be called from the robot's periodic
block in order for anything in the Command-based framework to work.
"""

def disabledInit(self):
pass

def disabledPeriodic(self):
pass

def autonomousInit(self):
self.autonomousCommand = self.robotContainer.getAutonomousCommand()

"""
schedule the autonomous command (example)
"""
if self.autonomousCommand is not None:
self.autonomousCommand.schedule()

def autonomousPeriodic(self):
pass

def teleopInit(self):
"""
This makes sure that the autonomous stops running when
teleop starts running. If you want the autonomous to
continue until interrupted by another command, remove
this line or comment it out.
"""
if self.autonomousCommand is not None:
self.autonomousCommand.cancel()

def teleopPeriodic(self):
pass

def testInit(self):
"""
Cancels all running commands at the start of test mode.
"""
CommandScheduler.getInstance().cancelAll()

def testPeriodic(self):
pass
109 changes: 109 additions & 0 deletions SwerveControllerCommand/robotcontainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#
# 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.
#

from wpilib import XboxController
import commands2
import constants as Constants
from subsystems.drivesubsystems import DriveSubsystem
from wpimath.trajectory import Trajectory, TrajectoryConfig, TrajectoryGenerator
from wpimath.geometry import Translation2d, Pose2d, Rotation2d
from wpimath.controller import ProfiledPIDController, PIDController
import math


class RobotContainer:
def __init__(self):
"""
The robot's subsystems
"""
chaoticthegreat marked this conversation as resolved.
Show resolved Hide resolved
self.robotDrive = DriveSubsystem()

# Driver Controller
self.driverController = XboxController(
Constants.OIConstants.kDriverControllerPort
)

"""
Configure default commands
"""
self.robotDrive.setDefaultCommand(
# The left stick controls translation of the robot.
# turning is controlled by the X axis of the right stick.
commands2.RunCommand(
lambda: self.robotDrive.drive(
# Multiply by max speed to map the joystick unitless inputs to actual units.
# This will map the [-1, 1] to [max speed backwards, max speed forwards],
# converting them to actual units.
self.driverController.getYChannel()
* Constants.DriveConstants.kMaxSpeedMetersPerSecond,
self.driverController.getXChannel()
* Constants.DriveConstants.kMaxSpeedMetersPerSecond,
self.driverController.getX(Constants.Hand.kRight)
* Constants.ModuleConstants.kMaxModuleAngularSpeedRadiansPerSecond,
False,
),
self.robotDrive,
)
)

def configureButtonBindings(self):
pass

def getAutonomousCommand(self):
"""
Create config for trajectory
"""
config = TrajectoryConfig(
Constants.AutoConstants.kMaxSpeedMetersPerSecond,
Constants.AutoConstants.kMaxAccelerationMetersPerSecondSquared,
).setKinematics(Constants.DriveConstants.kDriveKinematics)

# An example trajectory to follow. All units in meters.

example_trajectory = TrajectoryGenerator.generateTrajectory(
# Start at the origin facing the +X direction
Pose2d(0, 0, Rotation2d(0)),
# Pass through these two interior waypoints, making an 's' curve path
[Translation2d(1, 1), Translation2d(2, -1)],
# End 3 meters straight ahead of where we started, facing forward
Pose2d(3, 0, Rotation2d(0)),
config,
)

theta_controller = ProfiledPIDController(
Constants.AutoConstants.kPThetaController,
0,
0,
Constants.AutoConstants.kThetaControllerConstraints,
)
theta_controller.enableContinuousInput(-math.pi, math.pi)

swerve_controller_command = commands2.Swerve4ControllerCommand(
example_trajectory,
self.robotDrive.getPose,
# Functional interface to feed supplier
Constants.DriveConstants.kDriveKinematics,
# Position controllers
PIDController(Constants.AutoConstants.kPXController, 0, 0),
PIDController(Constants.AutoConstants.kPYController, 0, 0),
theta_controller,
self.robotDrive.setModuleStates,
self.robotDrive,
)

"""
Reset odometry to the initial pose of the trajectory, run path following
command, then stop at the end.
"""
return commands2.Commands.sequence(
commands2.InstantCommand(
lambda: self.robotDrive.resetOdometry(
example_trajectory.getInitialPose()
)
),
swerve_controller_command,
commands2.InstantCommand(lambda: self.robotDrive.drive(0, 0, 0, False)),
)
Loading
Loading