Skip to content

Commit d0070e2

Browse files
authored
Merge pull request #258 from thedropbears/heading-pure-pid
Use pure PID controller for heading lock
2 parents a2b01e0 + 07fae51 commit d0070e2

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

autonomous/auto_base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818

1919
x_controller = PIDController(0.5, 0.0, 0.0)
2020
y_controller = PIDController(0.5, 0.0, 0.0)
21-
heading_controller = PIDController(3.0, 0, 0)
22-
heading_controller.enableContinuousInput(-math.pi, math.pi)
2321

2422
wpilib.SmartDashboard.putData("Auto X PID", x_controller)
2523
wpilib.SmartDashboard.putData("Auto Y PID", y_controller)
26-
wpilib.SmartDashboard.putData("Auto Heading PID", heading_controller)
2724

2825

2926
class AutoBase(AutonomousStateMachine):
@@ -129,7 +126,9 @@ def follow_trajectory(self, sample: SwerveSample):
129126
sample.vx + x_controller.calculate(pose.X(), sample.x),
130127
sample.vy + y_controller.calculate(pose.Y(), sample.y),
131128
sample.omega
132-
+ heading_controller.calculate(pose.rotation().radians(), sample.heading),
129+
+ self.chassis.heading_controller.calculate(
130+
pose.rotation().radians(), sample.heading
131+
),
133132
)
134133

135134
# Apply the generated speeds

components/chassis.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from phoenix6.hardware import CANcoder, Pigeon2, TalonFX
1717
from phoenix6.signals import InvertedValue, NeutralModeValue
1818
from wpimath.controller import (
19-
ProfiledPIDControllerRadians,
19+
PIDController,
2020
SimpleMotorFeedforwardMeters,
2121
)
2222
from wpimath.estimator import SwerveDrive4PoseEstimator
@@ -27,7 +27,6 @@
2727
SwerveModulePosition,
2828
SwerveModuleState,
2929
)
30-
from wpimath.trajectory import TrapezoidProfileRadians
3130

3231
from ids import CancoderId, TalonId
3332
from utilities.ctre import FALCON_FREE_RPS
@@ -246,13 +245,13 @@ class ChassisComponent:
246245
swerve_lock = magicbot.tunable(False)
247246

248247
# TODO: Read from positions.py once autonomous is finished
249-
250248
def __init__(
251249
self, track_width: float, wheel_base: float, swerve_config: SwerveConfig
252250
) -> None:
253251
self.imu = Pigeon2(0)
254-
self.heading_controller = ProfiledPIDControllerRadians(
255-
3, 0, 0, TrapezoidProfileRadians.Constraints(100, 100)
252+
self.heading_controller = PIDController(3.0, 0.0, 0.0)
253+
wpilib.SmartDashboard.putData(
254+
"Chassis heading_controller", self.heading_controller
256255
)
257256
self.heading_controller.enableContinuousInput(-math.pi, math.pi)
258257
self.snapping_to_heading = False
@@ -384,7 +383,7 @@ def drive_local(self, vx: float, vy: float, omega: float) -> None:
384383
def snap_to_heading(self, heading: float) -> None:
385384
"""set a heading target for the heading controller"""
386385
self.snapping_to_heading = True
387-
self.heading_controller.setGoal(heading)
386+
self.heading_controller.setSetpoint(heading)
388387

389388
def stop_snapping(self) -> None:
390389
"""stops the heading_controller"""
@@ -399,9 +398,7 @@ def execute(self) -> None:
399398
self.get_rotation().radians()
400399
)
401400
else:
402-
self.heading_controller.reset(
403-
self.get_rotation().radians(), self.get_rotational_velocity()
404-
)
401+
self.heading_controller.reset()
405402

406403
desired_speed_translation = Translation2d(
407404
self.chassis_speeds.vx, self.chassis_speeds.vy
@@ -529,7 +526,7 @@ def get_rotation(self) -> Rotation2d:
529526

530527
@feedback
531528
def at_desired_heading(self) -> bool:
532-
return self.heading_controller.atGoal()
529+
return abs(self.heading_controller.getError()) <= self.HEADING_TOLERANCE
533530

534531
def set_coast_in_neutral(self, coast_mode: bool = True) -> None:
535532
mode = NeutralModeValue.COAST if coast_mode else NeutralModeValue.BRAKE

0 commit comments

Comments
 (0)