-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobotcontainer.py
215 lines (179 loc) · 6.68 KB
/
robotcontainer.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
#
from wpilib import XboxController
from commands2.button import CommandXboxController
from commands2 import InstantCommand, RunCommand, FunctionalCommand
from subsystems.mecanum import Mecanum
from subsystems.shooter import Shooter
from subsystems.odometry import Odometry
from subsystems.vision import Vision
from subsystems.arm import Arm
from wpimath.geometry import Rotation2d
from navx import AHRS
class DummyGyro:
def getRotation2d(self) -> Rotation2d:
return Rotation2d(0, 0)
def getAngle(self) -> float:
return 0
class RobotContainer:
"""
This class is where the bulk of the robot should be declared. Since Command-based is a
"declarative" paradigm, very little robot logic should actually be handled in the :class:`.Robot`
periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
subsystems, commands, and button mappings) should be declared here.
"""
__slots__ = (
"drivetrain",
"shooter",
"gyro",
"vision",
"odometry",
"arm",
"controller",
)
def __init__(self, is_fake: bool):
"""make a new robotcontainer. this class is where most of the actual robot logic is, and where the subsystems lie.
Args:
is_fake (bool): whether or not the robot is being simulated (including for tests). you can check with `wpilib.RobotBase.isSimulation()`.
"""
if is_fake:
self.gyro = DummyGyro()
else:
self.gyro = AHRS.create_spi() # ! since this breaks tests for some reason
# * initialize the robot's subsystems
# self.vision = None
self.vision = Vision("Global_Camera_Shutter")
self.drivetrain = Mecanum()
self.shooter = Shooter()
self.arm = Arm()
self.odometry = Odometry(self.gyro, self.drivetrain, self.vision) # type: ignore
# * The driver's controller
controller = XboxController(0)
# * the trigger controller
self.controller = CommandXboxController(0)
# Configure the button bindings
self.configureButtonBindings()
# * sets the default command, which will run when
# * no other command is running (for the drivetrain)
# * this one will drive the robot using controller input
self.drivetrain.setDefaultCommand(
RunCommand(
lambda: self.drivetrain.drive(
-controller.getLeftY(),
controller.getLeftX(),
controller.getRightX(),
),
self.drivetrain,
)
)
def configureButtonBindings(self):
"""
Use this method to define your button->command mappings. Buttons can be created via the button
factories on commands2.button.CommandGenericHID or one of its
subclasses (commands2.button.CommandJoystick or command2.button.CommandXboxController).
"""
# * left trigger for shooter intake
self.controller.leftTrigger(0.01).onTrue(
InstantCommand(
lambda: self.shooter.intake(),
self.shooter,
)
).onFalse(
InstantCommand(
lambda: self.shooter.stop(),
self.shooter,
)
)
# * right trigger to shoot
self.controller.rightTrigger(0.01).onTrue(
InstantCommand(
lambda: self.shooter.shoot(),
self.shooter,
)
).onFalse(
InstantCommand(
lambda: self.shooter.stop(),
self.shooter,
)
)
# * left bumper to arm intake
self.controller.leftBumper().onTrue(
InstantCommand(
lambda: self.shooter.arm_intake(),
self.shooter,
)
).onFalse(
InstantCommand(
lambda: self.shooter.arm_stop(),
self.shooter,
)
)
# * right bumper for arm spit
self.controller.rightBumper().onTrue(
InstantCommand(
lambda: self.shooter.arm_spit(),
self.shooter,
)
).onFalse(
InstantCommand(
lambda: self.shooter.arm_stop(),
self.shooter,
)
)
# * A button to invert the drivetrain
self.controller.start().onTrue(
InstantCommand(
lambda: self.drivetrain.invert(),
self.drivetrain,
)
)
# * toggle arm up and down with x
self.controller.x().onTrue(
InstantCommand(self.drivetrain.invert, self.drivetrain).andThen(
InstantCommand(self.arm.toggle, self.arm)
)
)
self.controller.y().onTrue(
InstantCommand(lambda: self.arm.motor.set(0.3))
).onFalse(InstantCommand(lambda: self.arm.motor.set(0)))
# self.controller.povUp().or_(self.controller.povUpLeft()).or_(
# self.controller.povUpLeft()
# ).onTrue(InstantCommand(lambda: self.arm.motor.set(1))).onFalse(
# InstantCommand(lambda: self.arm.motor.set(0))
# )
# self.controller.povDown().or_(self.controller.povDownLeft()).or_(
# self.controller.povDownLeft()
# ).onTrue(InstantCommand(lambda: self.arm.motor.set(-1))).onFalse(
# InstantCommand(lambda: self.arm.motor.set(0))
# )
self.controller.a().whileTrue(
InstantCommand(
lambda: self.drivetrain.drivetrain.driveCartesian(
*(self.vision.align_to_april_tag() or (0, 0, 0))
),
self.vision,
self.drivetrain,
)
)
def reset_arm(interrupted: bool):
if interrupted:
return self.arm.motor.stopMotor()
self.arm.motor.stopMotor()
self.arm.encoder.reset()
self.controller.back().onTrue(
FunctionalCommand(
onInit=self.arm.motor.stopMotor,
onExecute=lambda: self.arm.motor.set(-0.75),
onEnd=reset_arm,
isFinished=lambda: self.arm.encoder.getRate() == 0,
)
)
...
def getAutonomousCommand(self):
"""
Use this to pass the autonomous command to the main :class:`.Robot` class.
:returns: the command to run in autonomous
"""