-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.py
More file actions
277 lines (249 loc) · 11.6 KB
/
Utilities.py
File metadata and controls
277 lines (249 loc) · 11.6 KB
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
#Put all utility functions here so they do not pollute the space
#in the main file. Put functions such as "Drive for X Time", etc. here.
#Raven Robotics 2018 PowerUp
import wpilib
from wpilib import RobotDrive
class UtilityFunctions():
#Function to handle the gyro and turning the robot num degrees from the current heading
#As long as this function returns false, we are not done turning
#When this function returns true we are done turning
def turnNumDegrees(robot, num): #-num means turn left (0 to -179), +num means turn right (1 to 180)
robot.initialHeading = UtilityFunctions.getAnInitialHeading(robot, robot.initialHeading, robot.autoSafeToGetHeading)
robot.autoSafeToGetHeading = False
angle = robot.gyro.getAngle()
angle = angle % 360
if angle < 0:
angle = angle + 360
desired_heading = robot.initialHeading + (-1*num)#Gyro is mounted upside down! THIS SUCKS! We have to invert the direction!
if desired_heading < 0:
desired_heading = desired_heading + 360
elif desired_heading > 360:
desired_heading = desired_heading - 360
#desired_degrees = angle - desired_heading
#value = current - desired
#if value < 0 and |value| < 180 or value > 0 and |value| > 180 turn right
#if value < 0 and |value| > 180 or value >0 and |value| < 180 turn left
value = angle - desired_heading
if ((value <= 0 and abs(value) <= 180) or (value >= 0 and abs(value) >= 180)): #TURN RIGHT
if abs(value) <= robot.acceptable_heading_error:
robot.leftFrontMotor.set(0)
robot.leftBackMotor.set(0)
robot.rightFrontMotor.set(0)
robot.rightBackMotor.set(0)
robot.autoSafeToGetHeading = True
return True
elif abs(value) <= robot.slower_speed_band:
robot.leftFrontMotor.set(-1*robot.autoSlowTurnSpeed)
robot.leftBackMotor.set(-1*robot.autoSlowTurnSpeed)
robot.rightFrontMotor.set(1*robot.autoSlowTurnSpeed)
robot.rightBackMotor.set(1*robot.autoSlowTurnSpeed)
return False
else:
robot.leftFrontMotor.set(-1*robot.autoNormalTurnSpeed)
robot.leftBackMotor.set(-1*robot.autoNormalTurnSpeed)
robot.rightFrontMotor.set(1*robot.autoNormalTurnSpeed)
robot.rightBackMotor.set(1*robot.autoNormalTurnSpeed)
return False
elif ((value < 0 and abs(value) > 180) or (value > 0 and abs(value) < 180)): #TURN LEFT
if abs(value) <= robot.acceptable_heading_error:
robot.leftFrontMotor.set(0)
robot.leftBackMotor.set(0)
robot.rightFrontMotor.set(0)
robot.rightBackMotor.set(0)
robot.autoSafeToGetHeading = True
return True
elif abs(value) <= robot.slower_speed_band:
robot.leftFrontMotor.set(1*robot.autoSlowTurnSpeed)
robot.leftBackMotor.set(1*robot.autoSlowTurnSpeed)
robot.rightFrontMotor.set(-1*robot.autoSlowTurnSpeed)
robot.rightBackMotor.set(-1*robot.autoSlowTurnSpeed)
return False
else:
robot.leftFrontMotor.set(1*robot.autoNormalTurnSpeed)
robot.leftBackMotor.set(1*robot.autoNormalTurnSpeed)
robot.rightFrontMotor.set(-1*robot.autoNormalTurnSpeed)
robot.rightBackMotor.set(-1*robot.autoNormalTurnSpeed)
return False
else:
#hopefully we never get here
return False
#function meant to get the current heading only once to be used as the starting point for a gyro turn
def getAnInitialHeading(robot, initHeading, safeToGetHeading):
if safeToGetHeading:
#return the current gyro provided angle
angle = robot.gyro.getAngle()
angle = angle % 360
if angle < 0:
angle = angle + 360
return angle
else:
return initHeading #we don't want to remove the heading from the variable if we keep calling this function
# Returns an initial timestamp in seconds (need to multiply getMsClock() by 1000)
def getAnInitialTimeStamp(robot, initTime, safeToGetTime):
if safeToGetTime:
time = robot.timer.getMsClock() / 1000
return time
else:
return initTime
def resetEncoderValue(robot, myEncoder, safeToResetEncoder):
if safeToResetEncoder:
myEncoder.reset()
else:
pass
#Drive a motor a certain num seconds at the specified speed and direction
#Direction of 1 means FORWARD, -1 means BACKWARDS
def driveNumSeconds(robot, motor, direction, speed, num, initTime):
time = robot.timer.getMsClock() / 1000
if time - initTime < num:
motor.set(speed * direction)
return False
else:
motor.set(0)
return True
#Drive motors a certain num seconds at the specified speed and direction
#motors list: motorName, Speed (INCLUDES direction)
def driveMotorsNumSeconds(robot, motors, num, initTime):
time = robot.timer.getMsClock() / 1000
if time - initTime < num:
for motor in motors:
motor[0].set(motor[1])
return False
else:
for motor in motors:
motor[0].set(0)
return True
#Function to handle the digital encoder for driving forward num inches
# Direction will either be 1 (FORWARD) or -1 (REVERSE)
def driveNumInches(robot, num, direction, speed):
UtilityFunctions.resetEncoderValue(robot, robot.encoder, robot.autoSafeToResetEncoder)
inches_distance = abs(robot.encoder.get())# * .0267 # (100 ticks ~ 2.67 inches)
#print(abs(robot.encoder.get()))
if inches_distance < num:
#not there yet, keep going and return false (FORWARD)
robot.leftFrontMotor.set(direction * speed)
robot.leftBackMotor.set(direction * speed)
robot.rightFrontMotor.set(direction * speed)
robot.rightBackMotor.set(direction * speed)
robot.autoSafeToResetEncoder = False
print("DRIVING NUM INCHES")
return False
else:
#reached the end, stop and return true (STOP)
robot.leftFrontMotor.set(0)
robot.leftBackMotor.set(0)
robot.rightFrontMotor.set(0)
robot.rightBackMotor.set(0)
robot.autoSafeToResetEncoder = True
print("FINISHED DRIVE NUM INCHES")
return True
#Use the values from networkTables from the camera to determine which direction to turn the robot to align
#to the goal.
def getDirectionToGoal(robot):
#keep track of the last value we got. If either COG is 0, then we do not see a target and should just
#use the last valid value (assuming we will occasionally drop sight of the target)
#If too much time passes without getting a valid target, then assume we are not looking in the right
#direction and stop trying
direction = robot.ERROR
#print(robot.sd.getValue('COG_X'))
#Get the COG_X (and maybe COG_Y) network table values. X is what is important, we want X to be close to 80 (160 x 120 images) to be center
if robot.sd.containsKey('COG_X'):
x = robot.sd.getValue('COG_X')
#make sure we can see a target, otherwise use last valid value
if x == 0:
if robot.lastCOG_X == 0:
#there is nothing we can do, no target, don't do anything!
direction = robot.ERROR
else:
x = robot.lastCOG_X
else:
robot.lastCOG_X = x
#check the direction
#TODO: Update Target COG Location
if x >= 1 and x <= 75: #Guessing
direction = robot.GO_RIGHT
elif x > 75 and x <= 85:
direction = robot.ON_TARGET
elif x > 85 and x <= 160:
direction = robot.GO_LEFT
else:
direction = robot.ERROR
else:
#no keys, can't look for the target
direction = robot.ERROR
#Lets NOT use this for now. we can enable it if necessary
#if direction == robot.ERROR:
# robot.noGoalFoundCount += 1
#
#if robot.noGoalFoundCount >= 300:
# robot.noGoalFoundCount = 0
# robot.lastCOG_X = 0
# robot.lastCOG_Y = 0
return direction
def driveForTime(robot, speed, time):
done = False
robot.initialTime = UtilityFunctions.getAnInitialTimeStamp(robot, robot.initialTime, robot.autoSafeToGetTime)
#Create the list of motors to command
motorsList = []
motorsList.append((robot.leftBackMotor, -speed))
motorsList.append((robot.leftFrontMotor, -speed))
motorsList.append((robot.rightFrontMotor, speed))
motorsList.append((robot.rightBackMotor, speed))
#Command the motors to turn for some time
if UtilityFunctions.driveMotorsNumSeconds(robot, motorsList, time, robot.initialTime) == False:
robot.autoSafeToGetTime = False
else:
robot.autoSafeToGetTime = True
robot.leftBackMotor.set(0)
robot.leftFrontMotor.set(0)
robot.rightBackMotor.set(0)
robot.rightFrontMotor.set(0)
done = True
return done
def waitForTime(robot, num):
done = False
robot.initialTime = UtilityFunctions.getAnInitialTimeStamp(robot, robot.initialTime, robot.autoSafeToGetTime)
time = robot.timer.getMsClock() / 1000
if time - initTime < num:
return False
else:
return True
def shootSwitch(robot):
done = False
robot.shooterLeftFront.set(robot.slowShootSpeed)
robot.shooterLeftBack.set(robot.slowShootSpeed)
robot.shooterRightFront.set(robot.slowShootSpeed)
robot.shooterRightBack.set(robot.slowShootSpeed)
doneSpinning = waitForTime(robot, 2)
if doneSpinning:
robot.shooterDoor.set(2)
doneShooting = waitForTime(robot, 2)
if doneShooting:
robot.shooterLeftFront.set(0)
robot.shooterLeftBack.set(0)
robot.shooterRightFront.set(0)
robot.shooterRightBack.set(0)
return True
else:
return False
else:
return False
def shootScale(robot):
done = false
robot.shooterLeftFront.set(robot.fastShootSpeed)
robot.shooterLeftBack.set(robot.fastShootSpeed)
robot.shooterRightFront.set(robot.fastShootSpeed)
robot.shooterRightBack.set(robot.fastShootSpeed)
doneSpinning = waitForTime(robot, 2)
if doneSpinning:
robot.shooterDoor.set(2)
doneShooting = waitForTime(robot, 2)
if doneShooting:
robot.shooterLeftFront.set(0)
robot.shooterLeftBack.set(0)
robot.shooterRightFront.set(0)
robot.shooterRightBack.set(0)
return True
else:
return False
else:
return False