-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.py
146 lines (117 loc) · 5.74 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import wpilib
import wpimath
import wpilib.drive
import wpimath.filter
import wpimath.controller
import navx
import drivesubsystem
import commands2
import elevator
import constants
import intake
from wpilib import Timer
# To see messages from networktables, you must setup logging
import logging
logging.basicConfig(level=logging.DEBUG)
class MyRobot(wpilib.TimedRobot):
def robotInit(self) -> None:
self.driver_controller = commands2.button.CommandXboxController(0)
self.gadget_controller = commands2.button.CommandXboxController(1)
self.swerve = drivesubsystem.DriveSubsystem()
self.elevator_subsystem = elevator.ElevatorSubsystem()
self.intake_subsystem = intake.IntakeSubsystem()
#CameraServer.startAutomaticCapture("frontcam",0)
# Slew rate limiters to make joystick inputs more gentle; 1/3 sec from 0 to 1.
self.x_speed_limiter = wpimath.filter.SlewRateLimiter(3)
self.y_speed_limiter = wpimath.filter.SlewRateLimiter(3)
self.rot_limiter = wpimath.filter.SlewRateLimiter(3)
self.gadget_controller.a().whileTrue(elevator.ElevatorDownCommand(self.elevator_subsystem))
self.gadget_controller.y().whileTrue(elevator.ElevatorL4Command(self.elevator_subsystem))
self.gadget_controller.x().whileTrue(elevator.ElevatorL3Command(self.elevator_subsystem))
self.gadget_controller.b().whileTrue(elevator.ElevatorL2Command(self.elevator_subsystem))
self.gadget_controller.leftBumper().whileTrue(intake.PrimeCoralCommand(self.intake_subsystem))
self.gadget_controller.leftTrigger().whileTrue(intake.BackCoralCommand(self.intake_subsystem))
self.gadget_controller.rightBumper().whileTrue(intake.slowForwardCoralCommand(self.intake_subsystem))#slow corla
self.gadget_controller.rightTrigger().whileTrue(intake.fastForwardCoralCommand(self.intake_subsystem))#fast coral
# #self.gadget_controller.rightTri
# gger().whileTrue(elevator.printHeightCommand(self.elevator_subsystem))
self.gadget_controller.povUp().whileTrue(elevator.UpCommand(self.elevator_subsystem))
self.gadget_controller.povDown().whileTrue(elevator.ElevatorDownManualCommand(self.elevator_subsystem))
def robotPeriodic(self):
commands2.CommandScheduler.getInstance().run()
commands2.CommandScheduler.registerSubsystem(self.elevator_subsystem)
commands2.CommandScheduler.registerSubsystem(self.intake_subsystem)
def autonomousInit(self) -> None:
commands2.CommandScheduler.getInstance().schedule(commands2.InstantCommand(lambda: self.swerve.drive(-0.2, 0, 0, False, True)))
def autonomousPeriodic(self) -> None:
pass
def teleopInit(self) -> None:
pass
def teleopPeriodic(self) -> None:
# Teleop periodic logic
if self.driver_controller.getLeftTriggerAxis() > 0.1:
self.slowdwj(False)
else:
self.driveWithJoystick(True)
def testPeriodic(self) -> None:
pass
def driveWithJoystick(self, field_relative: bool) -> None:
# Get the x speed. We are inverting this because Xbox controllers return
# negative values when we push forward.
x_speed = (
-self.x_speed_limiter.calculate(
wpimath.applyDeadband(self.driver_controller.getLeftY(), 0.08)
)
# * drivesubsystem.kMaxSpeed
)
# Get the y speed or sideways/strafe speed. We are inverting this because
# we want a positive value when we pull to the left. Xbox controllers
# return positive values when you pull to the right by default.
y_speed = (
-self.y_speed_limiter.calculate(
wpimath.applyDeadband(self.driver_controller.getLeftX(), 0.08)
)
# * drivesubsystem.kMaxSpeed
)
# Get the rate of angular rotation. We are inverting this because we want a
# positive value when we pull to the left (remember, CCW is positive in
# mathematics). Xbox controllers return positive values when you pull to
# the right by default.
rot = (
-self.rot_limiter.calculate(
wpimath.applyDeadband(self.driver_controller.getRightX(), 0.08)
)
# * drivesubsystem.kMaxSpeed
)
self.swerve.drive(x_speed, y_speed, rot, field_relative, rate_limit=True)
def slowdwj(self, field_relative: bool) -> None:
# Get the x speed. We are inverting this because Xbox controllers return
# negative values when we push forward.
x_speed = (
-self.x_speed_limiter.calculate(
wpimath.applyDeadband(self.driver_controller.getLeftY(), 0.08)
)
* 0.2
)
# Get the y speed or sideways/strafe speed. We are inverting this because
# we want a positive value when we pull to the left. Xbox controllers
# return positive values when you pull to the right by default.
y_speed = (
-self.y_speed_limiter.calculate(
wpimath.applyDeadband(self.driver_controller.getLeftX(), 0.08)
)
* 0.2
)
# Get the rate of angular rotation. We are inverting this because we want a
# positive value when we pull to the left (remember, CCW is positive in
# mathematics). Xbox controllers return positive values when you pull to
# the right by default.
rot = (
-self.rot_limiter.calculate(
wpimath.applyDeadband(self.driver_controller.getRightX(), 0.08)
)
* 0.2
)
self.swerve.drive(x_speed, y_speed, rot, field_relative, rate_limit=True)
if __name__ == "__main__":
wpilib.run(MyRobot)