-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
268 lines (215 loc) · 8.34 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
import wpilib
from wpilib.drive import DifferentialDrive
from networktables import NetworkTables
import encoderPID2018
import gyroscopePID2018
import math
import time
class Robot(wpilib.IterativeRobot):
"""
This block initializes everything and gets run once when the robot is turned on.
If there is something that needs to be used throughout the entire code, then
it should go here.
"""
def robotInit(self):
self.lf_motor = wpilib.VictorSP(0)
self.lb_motor = wpilib.VictorSP(1)
self.left = wpilib.SpeedControllerGroup(self.lf_motor, self.lb_motor)
self.rf_motor = wpilib.VictorSP(6)
self.rb_motor = wpilib.VictorSP(7)
self.right = wpilib.SpeedControllerGroup(self.rf_motor, self.rb_motor)
self.roller = wpilib.VictorSP(2)
self.l_encoder = wpilib.Encoder(0, 1)
self.r_encoder = wpilib.Encoder(2, 3)
self.gyro = wpilib.ADXRS450_Gyro(0)
self.drive = DifferentialDrive(self.left, self.right)
self.drive.setSafetyEnabled(False)
self.controller = wpilib.XboxController(0)
self.timer = wpilib.Timer()
"""
an array for all of the various "stages" inside of an autonomous program.
This includes every stage (in our case, 5) to be executed sequentially.
"""
self.right_center_auto = []
self.right_center_auto.append(encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, 2.8*(650.1875/math.pi)))
self.right_center_auto.append(gyroscopePID2018.GyroscopePID_Controller(self.gyro, self.drive, 45))
self.right_center_auto.append(encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, (7/math.sqrt(2))*(650.1875/math.pi)))
self.right_center_auto.append(gyroscopePID2018.GyroscopePID_Controller(self.gyro, self.drive, -45))
self.right_center_auto.append(encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, 1.2*(650.1875/math.pi)))
self.left_center_auto = []
self.left_center_auto.append(encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, 2.8*(650.1875/math.pi)))
self.left_center_auto.append(gyroscopePID2018.GyroscopePID_Controller(self.gyro, self.drive, -45))
self.left_center_auto.append(encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, (7/math.sqrt(2))*(650.1875/math.pi)))
self.left_center_auto.append(gyroscopePID2018.GyroscopePID_Controller(self.gyro, self.drive, 45))
self.left_center_auto.append(encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, 1.2*(650.1875/math.pi)))
self.forward_auto = encoderPID2018.EncoderPID_Controller(self.l_encoder, self.r_encoder, self.drive, 11*(650.1875/math.pi))
"""
RoboRIO IP is always 10.XX.XX.2 where XX.XX is the team number
"""
NetworkTables.initialize(server="10.41.18.2")
self.dash = NetworkTables.getTable('Data')
"""
Since we are going to determine which autonomous to run by using the NetworkTables
value inside of "position", we initialize it to "none" until we update it.
"""
self.dash.putString("position", "none")
"""
This function resets everything to it's default values as if the robot was just
rebooted
"""
def reset(self):
self.drive.arcadeDrive(0.0, 0.0)
self.gyro.reset()
self.l_encoder.reset()
self.r_encoder.reset()
self.timer.reset()
for stage in self.right_center_auto:
stage.lock = "locked"
for stage in self.left_center_auto:
stage.lock = "locked"
self.forward_auto.lock = "locked"
"""
run whenever teleop starts
"""
def teleopInit(self):
self.reset()
"""
run periodically in teleop. In our case, just drive the robot with the controller
"""
def teleopPeriodic(self):
forward = self.controller.getY(0)
angle = self.controller.getX(1)
self.drive.arcadeDrive(forward*0.65, angle*0.65)
suck = self.controller.getTriggerAxis(0)
out = -self.controller.getTriggerAxis(1)
if (suck != 0):
self.roller.set(suck)
elif (out != 0):
self.roller.set(out)
else:
self.roller.set(0)
"""
right-center autonomous execution thread
"""
def right_center(self):
if self.right_center_auto[0].lock == "locked":
self.right_center_auto[0].execute()
if self.right_center_auto[0].lock == "released" and self.right_center_auto[1].lock == "locked":
self.right_center_auto[1].execute()
if self.right_center_auto[1].lock == "released":
self.l_encoder.reset()
self.r_encoder.reset()
self.right_center_auto[1].lock = "released1"
if self.right_center_auto[2].lock == "locked" and self.right_center_auto[1].lock == "released1":
self.right_center_auto[2].execute()
if self.right_center_auto[2].lock == "released":
self.gyro.reset()
self.right_center_auto[2].lock = "released1"
if self.right_center_auto[3].lock == "locked" and self.right_center_auto[2].lock == "released1":
self.right_center_auto[3].execute()
if self.right_center_auto[3].lock == "released":
self.l_encoder.reset()
self.r_encoder.reset()
self.right_center_auto[3].lock = "released1"
if self.right_center_auto[4].lock == "locked" and self.right_center_auto[3].lock == "released1":
self.roller.set(1.0)
self.right_center_auto[4].execute()
if self.right_center_auto[3].lock == "released":
if self.timer.get() < 10:
self.roller.set(1.0)
else:
self.roller.set(0.0)
self.drive.arcadeDrive(0.0, 0.0)
"""
left-center autonomous execution thread
"""
def left_center(self):
if self.left_center_auto[0].lock == "locked":
self.left_center_auto[0].execute()
if self.left_center_auto[0].lock == "released" and self.left_center_auto[1].lock == "locked":
self.left_center_auto[1].execute()
if self.left_center_auto[1].lock == "released":
self.l_encoder.reset()
self.r_encoder.reset()
self.left_center_auto[1].lock = "released1"
if self.left_center_auto[2].lock == "locked" and self.left_center_auto[1].lock == "released1":
self.left_center_auto[2].execute()
if self.left_center_auto[2].lock == "released":
self.gyro.reset()
self.left_center_auto[2].lock = "released1"
if self.left_center_auto[3].lock == "locked" and self.left_center_auto[2].lock == "released1":
self.left_center_auto[3].execute()
if self.left_center_auto[3].lock == "released":
self.l_encoder.reset()
self.r_encoder.reset()
self.left_center_auto[3].lock = "released1"
if self.left_center_auto[4].lock == "locked" and self.left_center_auto[3].lock == "released1":
self.roller.set(1.0)
self.left_center_auto[4].execute()
if self.left_center_auto[3].lock == "released":
if self.timer.get() < 10:
self.roller.set(1.0)
else:
self.roller.set(0.0)
self.drive.arcadeDrive(0.0, 0.0)
"""
right-right autonomous execution thread
"""
def right_right(self):
if self.forward_auto.lock == "released" or self.timer.get() > 5:
self.roller.set(1.0)
self.forward_auto.execute()
"""
left-left autonomous execution thread
"""
def left_left(self):
if self.forward_auto.lock == "released" or self.timer.get() > 5:
self.roller.set(1.0)
self.forward_auto.execute()
"""
go-to-autoline autonomous execution thread
"""
def go_to_autoline(self):
self.forward_auto.execute()
"""
run once at start of autonomous. Two possible options:
1) Playback recorded data (PhaseAlpha)
2) Follow a specific route
"""
def autonomousInit(self):
self.reset()
self.timer.start()
self.game_data = wpilib.DriverStation.getInstance().getGameSpecificMessage()
self.switch = self.game_data[0]
self.position = self.dash.getString("position", "none")
"""
Either determine which autonomous to execute given the position in NetworkTables
or simply playback a recording determined in "test" mode (see below)
"""
def autonomousPeriodic(self):
if self.position == "solid":
if self.timer.get() < 3:
self.drive.arcadeDrive(-0.5, 0.0)
else:
self.drive.arcadeDrive(0.0, 0.0)
if self.position == "none":
self.drive.arcadeDrive(0.0, 0.0)
if self.position == "center" and self.switch == "R":
self.right_center()
if self.position == "center" and self.switch == "L":
self.left_center()
if self.position == "right" and self.switch == "R":
if self.timer.get() > 5:
self.roller.set(1.0)
self.right_right()
if self.position == "left" and self.switch == "L":
if self.timer.get() > 5:
self.roller.set(1.0)
self.left_left()
if (self.position == "right" and self.switch == "L") or (self.position == "left" and self.switch == "R"):
self.go_to_autoline()
"""
only execute as a script
"""
if __name__ == '__main__':
wpilib.run(Robot)