-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.py
More file actions
345 lines (309 loc) · 14.2 KB
/
Utilities.py
File metadata and controls
345 lines (309 loc) · 14.2 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#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 2019 Deep Space
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
# Give an output so we can check if the gyro is working
print(angle)
desired_heading = robot.initialHeading + (1*num)#ADXRS450 gyro is backwards from previous gyro
print('Desired: ' + str(desired_heading))
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
print('Value: ' + str(value))
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()) * .087 # (100 ticks ~ 10.188 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")
#print(inches_distance)
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
def liftAllNumInches(robot, motors, num, speed):
UtitlityFunctions.resetEncoderValue(robot,robot.encoder, robot.autoSafeToResetEncoder)
inches_distance = abs(robot.encoder.get()) *.087 # (100 ticks ~ 10.188 inches)
if inches_distance < num:
robot.climberRight.set(direction * speed)
robot.climberLeft.set(direction * speed)
robot.climberBack.set(direction * speed)
robot.autoSafeToResetEncoder = False
return False
else:
robot.climberRight.set(0)
robot.climberLeft.set(0)
robot.climberBack.set(0)
return True
def liftFrontNumInches(robot, num, direction, speed):
UtitlityFunctions.resetEncoderValue(robot,robot.encoder, robot.autoSafeToResetEncoder)
inches_distance = abs(robot.encoder.get()) *.087 # (100 ticks ~ 10.188 inches)
if inches_distance < num:
robot.climberRight.set(direction * speed)
robot.climberLeft.set(direction * speed)
robot.autoSafeToResetEncoder = False
return False
else:
robot.climberRight.set(0)
robot.climberLeft.set(0)
return True
def liftBackNumInches(robot, num, direction, speed):
UtitlityFunctions.resetEncoderValue(robot,robot.encoder, robot.autoSafeToResetEncoder)
inches_distance = abs(robot.encoder.get()) *.087 # (100 ticks ~ 10.188 inches)
if inches_distance < num:
robot.climberBack.set(direction * speed)
robot.autoSafeToResetEncoder = False
return False
else:
robot.climberBack.set(0)
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, time):
done = False
robot.initialTime = UtilityFunctions.getAnInitialTimeStamp(robot, robot.initialTime, robot.autoSafeToGetTime)
if UtilityFunctions.waitNumSeconds(robot, time, robot.initialTime) == False:
robot.autoSafeToGetTime = False
else:
robot.autoSafeToGetTime = True
done = True
return done
def waitNumSeconds(robot, num, initTime):
time = robot.timer.getMsClock() / 1000
if time - initTime < num:
return False
else:
return True
'''
def encoderCompare(robot, encoders, motorMove):
for E in encoders:
Evalue = E.get()
for e in encoders:
evalue = e.get()
if Evalue - evalue > 0:
motorMove.update(E=False)
else:
motorMove.update(E=True)
print(motorMove)'''
def encoderCompareUp(robot, encoder, encoderList):
print(encoder)
for e in encoderList:
if e != encoder:
if encoder.get() - e.get() > 5000:
print("Stop Motor: " + str(e))
return False
else:
return True
def encoderCompareDown(robot, encoder, encoderList):
print(encoder)
for e in encoderList:
if e != encoder:
if e.get() - encoder.get() > 5000:
print("Stop Motor: " + str(e))
return False
else:
return True
def getHeight(robot, targetHeight):
targetHeightHigh = targetHeight + 100
targetHeightLow = targetHeight - 100
if self.armEncoder.get() <= targetHeightHigh:
self.drivingArm = True
self.armMotor1.set(.1)
self.armMotor2.set(.1)
elif self.armEncoder.get() >= targetHeightLow:
self.armMotor1.set(0)
self.armMotor2.set(0)
self.drivingArm = False
return False
elif self.armEncoder.get() >= targetHeightHigh:
self.drivingArm = True
self.armMotor1.set(-.1)
self.armMotor2.set(-.1)
elif self.armEncoder.get() <= targetHeightHigh:
self.armMotor1.set(0)
self.armMotor2.set(0)
self.drivingArm = False
return False
else:
return True