Skip to content

Commit 153108e

Browse files
authored
fix: exponent smoothing math (#103)
1 parent c6e5cac commit 153108e

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

pybot/robotcontainer.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import wpilib
1818
import logging
1919

20+
import math
21+
2022
# Configure logging
2123
logging.basicConfig(level=logging.DEBUG)
2224

@@ -77,24 +79,32 @@ def configureButtonBindings(self) -> None:
7779
# Cache the multiplier
7880
self._driveMultiplier = -1.0 if self.isRedAlliance() else 1.0
7981

82+
def calculateJoystick():
83+
x0 = self._joystick.getLeftX()
84+
y0 = self._joystick.getLeftY()
85+
86+
mag = self.apply_exponential(math.sqrt((x0 * x0) + (y0 * y0)), self._deadband, self._exponent) * self._max_speed
87+
theta = math.atan2(y0, x0)
88+
89+
x1 = mag * math.cos(theta)
90+
y1 = mag * math.sin(theta)
91+
92+
print('0: %f, %f 1: %f %f' % (x0, y0, x1, y1))
93+
94+
return (x1, y1)
95+
96+
def applyRequest():
97+
(new_vx, new_vy) = calculateJoystick()
98+
99+
return (self._drive.with_velocity_x(self._driveMultiplier * new_vy) # Drive forward with negative Y (forward)
100+
.with_velocity_y(self._driveMultiplier * new_vx) # Drive left with negative X (left)
101+
.with_rotational_rate(
102+
self._driveMultiplier * self.apply_exponential(self._joystick.getRightX(), self._deadband, self._exponent) * self._max_angular_rate
103+
)) # Drive counterclockwise with negative X (left)
104+
80105
# Note that X is defined as forward according to WPILib convention,
81106
# and Y is defined as to the left according to WPILib convention.
82-
self.drivetrain.setDefaultCommand(
83-
# Drivetrain will execute this command periodically
84-
self.drivetrain.apply_request(
85-
lambda: (
86-
self._drive.with_velocity_x(
87-
self._driveMultiplier * self.apply_exponential(self._joystick.getLeftY(), self._deadband, self._exponent) * self._max_speed
88-
) # Drive forward with negative Y (forward)
89-
.with_velocity_y(
90-
self._driveMultiplier * self.apply_exponential(self._joystick.getLeftX(), self._deadband, self._exponent) * self._max_speed
91-
) # Drive left with negative X (left)
92-
.with_rotational_rate(
93-
self._driveMultiplier * self.apply_exponential(self._joystick.getRightX(), self._deadband, self._exponent) * self._max_angular_rate
94-
) # Drive counterclockwise with negative X (left)
95-
)
96-
)
97-
)
107+
self.drivetrain.setDefaultCommand(self.drivetrain.apply_request(applyRequest))
98108
# reset the field-centric heading on left bumper press
99109
self._joystick.x().onTrue(
100110
self.drivetrain.runOnce(lambda: self.resetHeading())

0 commit comments

Comments
 (0)