forked from thedropbears/pystronghold
/
robot.py
303 lines (267 loc) · 10.5 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
import wpilib
import magicbot
from components.chassis import Chassis, BlankPIDOutput, constrain_angle
from components.vision import Vision
from components.bno055 import BNO055
from components.range_finder import RangeFinder
from components.shooter import Shooter
from components import shooter
from components.intake import Intake
from components.defeater import Defeater
from networktables import NetworkTable
import logging
import math
class StrongholdRobot(magicbot.MagicRobot):
chassis = Chassis
intake = Intake
shooter = Shooter
defeater = Defeater
def createObjects(self):
self.logger = logging.getLogger("robot")
self.sd = NetworkTable.getTable('SmartDashboard')
self.intake_motor = wpilib.CANTalon(14)
self.shooter_motor = wpilib.CANTalon(12)
self.defeater_motor = wpilib.CANTalon(1)
self.joystick = wpilib.Joystick(0)
self.gamepad = wpilib.Joystick(1)
self.pressed_buttons_js = set()
self.pressed_buttons_gp = set()
# needs to be created here so we can pass it in to the PIDController
self.bno055 = BNO055()
self.vision = Vision()
self.range_finder = RangeFinder(0)
self.heading_hold_pid_output = BlankPIDOutput()
Tu = 1.6
Ku = 0.6
Kp = Ku * 0.3
self.heading_hold_pid = wpilib.PIDController(0.6,
2.0 * Kp / Tu * 0.1,
1.0 * Kp * Tu / 20.0 * 0,
self.bno055, self.heading_hold_pid_output)
self.heading_hold_pid.setTolerance(3.0*math.pi/180.0)
self.heading_hold_pid.setContinuous(True)
self.heading_hold_pid.setInputRange(-math.pi, math.pi)
self.heading_hold_pid.setOutputRange(-1.0, 1.0)
self.intake_motor.setFeedbackDevice(wpilib.CANTalon.FeedbackDevice.QuadEncoder)
self.intake_motor.reverseSensor(False)
def putData(self):
self.sd.putDouble("range_finder", self.range_finder.getDistance())
self.sd.putDouble("gyro", self.bno055.getHeading())
vision_array = self.vision.get()
vision_x = None
vision_w = None
vision_h = None
if not vision_array:
vision_x = -2
vision_w = -2
vision_h = -2
else:
vision_x = vision_array[0]
vision_w = vision_array[2]
vision_h = vision_array[3]
self.sd.putDouble("vision_pid_get", self.vision.pidGet())
self.sd.putDouble("vision_x", vision_x)
self.sd.putDouble("vision_w", vision_w)
self.sd.putDouble("vision_h", vision_h)
self.sd.putDouble("vx", self.chassis.vx)
self.sd.putDouble("vy", self.chassis.vy)
self.sd.putDouble("vz", self.chassis.vz)
self.sd.putDouble("input_twist", self.chassis.inputs[2])
self.sd.putDouble("field_oriented", self.chassis.field_oriented)
self.sd.putDouble("raw_yaw", self.bno055.getRawHeading())
self.sd.putDouble("raw_pitch", self.bno055.getPitch())
self.sd.putDouble("raw_roll", self.bno055.getRoll())
self.sd.putDouble("shooter_speed", -self.shooter._speed) # minus sign here so +ve is shooting
self.sd.putDouble("heading_pid_output", self.heading_hold_pid_output.output)
self.sd.putDouble("heading_hold_pid_setpoint", self.heading_hold_pid.getSetpoint())
self.sd.putDouble("intake_state", self.intake.state)
self.sd.putDouble("intake_speed", self.intake._speed)
self.sd.putDouble("distance_pid_output", self.chassis.distance_pid_output.output)
self.sd.putBoolean("track_vision", self.chassis.track_vision)
self.sd.putDouble("pov", self.joystick.getPOV())
self.sd.putDouble("gyro_z_rate", self.bno055.getHeadingRate())
self.sd.putDouble("heading_hold_error", self.heading_hold_pid.getSetpoint()-self.bno055.getAngle())
self.sd.putDouble("defeater_current", self.defeater_motor.getOutputCurrent())
self.sd.putDouble("defeater_speed", self.defeater_motor.get())
self.sd.putDouble("joystick_throttle", self.joystick.getThrottle())
self.sd.putDouble("range_pid_get", self.range_finder.pidGet())
self.sd.putDouble("encoder_distance", self.chassis.distance)
def disabledInit(self):
pass
def disabledPeriodic(self):
"""This function is called periodically when disabled."""
self.putData()
def teleopInit(self):
pass
def teleopPeriodic(self):
"""This function is called periodically during operator control."""
try:
if self.debounce(2) or self.debounce(1, gamepad=True):
self.intake.toggle()
except:
self.onException()
try:
if self.debounce(7):
self.chassis.toggle_field_oriented()
except:
self.onException()
try:
if self.debounce(8):
enabled = self.heading_hold_pid.isEnable()
self.heading_hold_pid.disable()
self.bno055.resetHeading()
self.heading_hold_pid.setSetpoint(constrain_angle(self.bno055.getAngle()))
self.heading_hold_pid.reset()
if enabled:
self.heading_hold_pid.enable()
except:
self.onException()
try:
if self.debounce(10):
self.chassis.toggle_vision_tracking()
except:
self.onException()
try:
if self.debounce(12):
self.chassis.toggle_range_holding(self.chassis.correct_range)
except:
self.onException()
try:
if self.debounce(1):
if self.shooter.state == shooter.States.off:
self.shooter.start_shoot()
else:
self.shooter.toggle()
self.intake.fire()
except:
self.onException()
try:
if self.debounce(9):
self.chassis.toggle_heading_hold()
except:
self.onException()
try:
if self.debounce(4):
self.defeater.up()
except:
self.onException()
try:
if self.debounce(5):
self.shooter.stop()
self.intake.stop()
except:
self.onException()
try:
if self.debounce(3):
self.chassis.track_vision = True
self.chassis.range_setpoint = self.chassis.correct_range
self.chassis.distance_pid.enable()
# self.shooter.start_shoot()
except:
self.onException()
try:
if self.debounce(6):
self.defeater.down()
except:
self.onException()
"""try:
if self.debounce(10):
self.shooter.backdrive()
self.intake.backdrive()
except:
self.onException()"""
try:
if self.joystick.getPOV() != -1:
self.chassis.heading_hold = True
direction = 0.0
if self.joystick.getPOV() == 0:
# shooter centre goal
direction = math.pi
elif self.joystick.getPOV() == 90:
# shooter right goal
direction = math.pi/3.0+math.pi
elif self.joystick.getPOV() == 270:
# shooter left goal
direction = -math.pi/3.0+math.pi
elif self.joystick.getPOV() == 180:
direction = 0.0
self.chassis.set_heading_setpoint(direction)
except:
self.onException()
try:
if self.joystick.getRawButton(11) or self.gamepad.getRawButton(2):
self.chassis.field_oriented = False
else:
self.chassis.field_oriented = True
except:
self.onException()
try:
if self.gamepad.getRawButton(3):
self.intake.backdrive_slow()
except:
self.onException()
try:
if self.debounce(1, gamepad=True):
self.chassis.zero_encoders()
self.chassis.distance_pid.setSetpoint(1.2)
self.chassis.distance_pid.enable()
except:
self.onException()
try:
if self.gamepad.getRawButton(4):
self.shooter.backdrive_recovery()
except:
self.onException()
self.chassis.inputs = [-rescale_js(self.joystick.getY(), deadzone=0.05, exponential=1.2),
- rescale_js(self.joystick.getX(), deadzone=0.05, exponential=1.2),
- rescale_js(self.joystick.getZ(), deadzone=0.2, exponential=15.0, rate=0.3),
(self.joystick.getThrottle() - 1.0) / -2.0
]
for input in self.chassis.inputs[0:3]:
if input != 0.0:
# Break out of auto if we move the stick
self.chassis.distance_pid.disable()
self.chassis.range_setpoint = None
self.chassis.track_vision = False
self.chassis.field_oriented = True
self.putData()
def testPeriodic(self):
"""This function is called periodically during test mode."""
wpilib.LiveWindow.run()
def debounce(self, button, gamepad = False):
device = None
if gamepad:
pressed_buttons = self.pressed_buttons_gp
device = self.gamepad
else:
pressed_buttons = self.pressed_buttons_js
device = self.joystick
if device.getRawButton(button):
if button in pressed_buttons:
return False
else:
pressed_buttons.add(button)
return True
else:
pressed_buttons.discard(button)
return False
def rescale_js(value, deadzone=0.0, exponential=0.0, rate=1.0):
value_negative = 1.0
if value < 0:
value_negative = -1.0
value = -value
# Cap to be +/-1
if abs(value) > 1.0:
value /= abs(value)
# Apply deadzone
if abs(value) < deadzone:
return 0.0
elif exponential == 0.0:
value = (value-deadzone)/(1-deadzone)
else:
a = math.log(exponential+1)/(1-deadzone)
value = (math.exp(a*(value - deadzone))-1)/exponential
return value*value_negative*rate
if __name__ == "__main__":
wpilib.run(StrongholdRobot)