Skip to content

Commit

Permalink
reorder docs tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansen4857 committed Dec 31, 2024
1 parent 5302397 commit 149a117
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions Writerside/topics/pplib-Swerve-Setpoint-Generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,6 @@ public class SwerveSubsystem extends Subsystem {
}
```

</tab>
<tab title="Python" group-key="python">

```Python
from pathplannerlib.config import RobotConfig
from pathplannerlib.util import DriveFeedforwards
from pathplannerlib.util.swerve import SwerveSetpoint, SwerveSetpointGenerator
from wpimath.kinematics import ChassisSpeeds
from wpimath.units import rotationsToRadians

class SwerveSubsystem(Subsystem):
def __init__(self):
# All other subsystem initialization
# ...

# Load the RobotConfig from the GUI settings. You should probably
# store this in your Constants file
config = RobotConfig.fromGUISettings()

self.setpointGenerator = SwerveSetpointGenerator(
config, # The robot configuration. This is the same config used for generating trajectories and running path following commands.
rotationsToRadians(10.0) # The max rotation velocity of a swerve module in radians per second. This should probably be stored in your Constants file
)

# Initialize the previous setpoint to the robot's current speeds & module states
currentSpeeds = self.getCurrentSpeeds() # Method to get current robot-relative chassis speeds
currentStates = self.getCurrentModuleStates() # Method to get the current swerve module states
self.previousSetpoint = SwerveSetpoint(currentSpeeds, currentStates, DriveFeedforwards.zeros(config.numModules))

def driveRobotRelative(self, speeds: ChassisSpeeds):
"""
This method will take in desired robot-relative chassis speeds,
generate a swerve setpoint, then set the target state for each module
:param speeds: The desired robot-relative speeds
"""
# Note: it is important to not discretize speeds before or after
# using the setpoint generator, as it will discretize them for you
self.previousSetpoint = self.setpointGenerator.generateSetpoint(
self.previousSetpoint, # The previous setpoint
speeds, # The desired target speeds
0.02, # The loop time of the robot code, in seconds
)
self.setModuleStates(self.previousSetpoint.moduleStates) # Method that will drive the robot given target module states
```

</tab>

<tab title="C++" group-key="cpp">
Expand All @@ -137,7 +91,7 @@ SwerveSubsystem::SwerveSubsystem(){
setpointGenerator = SwerveSetpointGenerator(
config,
10_rad_per_s
)
);

// Initialize the previous setpoint to the robot's current speeds & module states
frc::ChassisSpeeds currentSpeeds = getCurrentSpeeds(); // Method to get current robot-relative chassis speeds
Expand All @@ -163,5 +117,52 @@ void SwerveSubsystem::driveRobotRelative(frc::ChassisSpeeds speeds) {
}
```
</tab>
<tab title="Python" group-key="python">
```Python
from pathplannerlib.config import RobotConfig
from pathplannerlib.util import DriveFeedforwards
from pathplannerlib.util.swerve import SwerveSetpoint, SwerveSetpointGenerator
from wpimath.kinematics import ChassisSpeeds
from wpimath.units import rotationsToRadians
class SwerveSubsystem(Subsystem):
def __init__(self):
# All other subsystem initialization
# ...
# Load the RobotConfig from the GUI settings. You should probably
# store this in your Constants file
config = RobotConfig.fromGUISettings()
self.setpointGenerator = SwerveSetpointGenerator(
config, # The robot configuration. This is the same config used for generating trajectories and running path following commands.
rotationsToRadians(10.0) # The max rotation velocity of a swerve module in radians per second. This should probably be stored in your Constants file
)
# Initialize the previous setpoint to the robot's current speeds & module states
currentSpeeds = self.getCurrentSpeeds() # Method to get current robot-relative chassis speeds
currentStates = self.getCurrentModuleStates() # Method to get the current swerve module states
self.previousSetpoint = SwerveSetpoint(currentSpeeds, currentStates, DriveFeedforwards.zeros(config.numModules))
def driveRobotRelative(self, speeds: ChassisSpeeds):
"""
This method will take in desired robot-relative chassis speeds,
generate a swerve setpoint, then set the target state for each module
:param speeds: The desired robot-relative speeds
"""
# Note: it is important to not discretize speeds before or after
# using the setpoint generator, as it will discretize them for you
self.previousSetpoint = self.setpointGenerator.generateSetpoint(
self.previousSetpoint, # The previous setpoint
speeds, # The desired target speeds
0.02, # The loop time of the robot code, in seconds
)
self.setModuleStates(self.previousSetpoint.moduleStates) # Method that will drive the robot given target module states
```

</tab>
</tabs>

0 comments on commit 149a117

Please sign in to comment.