|
17 | 17 | import wpilib |
18 | 18 | import logging |
19 | 19 |
|
| 20 | +import math |
| 21 | + |
20 | 22 | # Configure logging |
21 | 23 | logging.basicConfig(level=logging.DEBUG) |
22 | 24 |
|
@@ -77,24 +79,32 @@ def configureButtonBindings(self) -> None: |
77 | 79 | # Cache the multiplier |
78 | 80 | self._driveMultiplier = -1.0 if self.isRedAlliance() else 1.0 |
79 | 81 |
|
| 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 | + |
80 | 105 | # Note that X is defined as forward according to WPILib convention, |
81 | 106 | # 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)) |
98 | 108 | # reset the field-centric heading on left bumper press |
99 | 109 | self._joystick.x().onTrue( |
100 | 110 | self.drivetrain.runOnce(lambda: self.resetHeading()) |
|
0 commit comments