-
Notifications
You must be signed in to change notification settings - Fork 29
/
ChaseBallStates.py
538 lines (460 loc) · 20.1 KB
/
ChaseBallStates.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
"""
Here we house all of the state methods used for chasing the ball
"""
from man.noggin.util import MyMath
from man.motion import SweetMoves
import ChaseBallConstants as constants
import ChaseBallTransitions as transitions
import KickingHelpers
import GoalieTransitions as goalTran
import PositionConstants
from .. import NogginConstants
from ..playbook.PBConstants import GOALIE
from math import fabs
def chase(player):
"""
Method to determine which chase state should be used.
We dump the robot into this state when we our switching from something else.
"""
player.isChasing = True
player.hasAlignedOnce = False
if player.brain.play.isRole(GOALIE):
if transitions.shouldScanFindBall(player):
return player.goNow('scanFindBall')
elif transitions.shouldApproachBall(player):
return player.goNow('approachBall')
elif transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldTurnToBall_ApproachBall(player):
return player.goNow('turnToBall')
else:
return player.goNow('scanFindBall')
if transitions.shouldScanFindBall(player):
return player.goNow('scanFindBall')
elif transitions.shouldApproachBallWithLoc(player):
return player.goNow('approachBallWithLoc')
elif transitions.shouldApproachBall(player):
return player.goNow('approachBall')
elif transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldTurnToBall_ApproachBall(player):
return player.goNow('turnToBall')
else:
return player.goNow('scanFindBall')
def chaseAfterKick(player):
if player.firstFrame():
player.brain.CoA.setRobotGait(player.brain.motion)
player.brain.tracker.trackBall()
if player.chosenKick == SweetMoves.LEFT_FAR_KICK or \
player.chosenKick == SweetMoves.RIGHT_FAR_KICK:
return player.goLater('chase')
if player.chosenKick == SweetMoves.LEFT_SIDE_KICK:
turnDir = constants.TURN_RIGHT
elif player.chosenKick == SweetMoves.RIGHT_SIDE_KICK:
turnDir = constants.TURN_LEFT
player.setWalk(0, 0, turnDir * constants.BALL_SPIN_SPEED)
return player.stay()
if player.brain.ball.framesOn > constants.BALL_ON_THRESH:
return player.goLater('chase')
elif player.counter > constants.CHASE_AFTER_KICK_FRAMES:
return player.goLater('spinFindBall')
return player.stay()
def turnToBall(player):
"""
Rotate to align with the ball. When we get close, we will approach it
"""
ball = player.brain.ball
if player.firstFrame():
player.hasAlignedOnce = False
player.brain.tracker.trackBall()
player.brain.CoA.setRobotGait(player.brain.motion)
# Determine the speed to turn to the ball
turnRate = MyMath.clip(ball.bearing*constants.BALL_SPIN_GAIN,
-constants.BALL_SPIN_SPEED,
constants.BALL_SPIN_SPEED)
# Avoid spinning so slowly that we step in place
if fabs(turnRate) < constants.MIN_BALL_SPIN_MAGNITUDE:
turnRate = MyMath.sign(turnRate)*constants.MIN_BALL_SPIN_MAGNITUDE
if ball.on:
player.setWalk(x=0,y=0,theta=turnRate)
if transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldPositionForKick(player):
return player.goNow('positionForKick')
elif transitions.shouldApproachBall(player):
return player.goLater('approachBall')
elif transitions.shouldScanFindBall(player):
return player.goLater('scanFindBall')
return player.stay()
def approachBallWithLoc(player):
if player.firstFrame():
player.brain.CoA.setRobotGait(player.brain.motion)
player.hasAlignedOnce = False
nav = player.brain.nav
my = player.brain.my
if player.brain.play.isRole(GOALIE):
if transitions.shouldKick(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goNow('waitBeforeKick')
elif transitions.shouldPositionForKickFromApproachLoc(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('positionForKick')
elif my.locScoreFramesBad > constants.APPROACH_NO_LOC_THRESH:
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('approachBall')
elif not player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBall(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('scanFindBall')
else:
if transitions.shouldKick(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goNow('waitBeforeKick')
elif transitions.shouldPositionForKickFromApproachLoc(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('positionForKick')
elif transitions.shouldNotGoInBox(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('ballInMyBox')
elif transitions.shouldChaseAroundBox(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('chaseAroundBox')
elif transitions.shouldAvoidObstacleDuringApproachBall(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('avoidObstacle')
elif my.locScoreFramesBad > constants.APPROACH_NO_LOC_THRESH:
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('approachBall')
elif not player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBall(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('scanFindBall')
elif player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBallActiveLoc(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('scanFindBall')
#if player.brain.ball.locDist > constants.APPROACH_ACTIVE_LOC_DIST:
if transitions.shouldActiveLoc(player):
player.brain.tracker.activeLoc()
else :
player.brain.tracker.trackBall()
dest = player.getApproachPosition()
useOmni = my.dist(dest) <= \
constants.APPROACH_OMNI_DIST
changedOmni = False
if useOmni != nav.movingOmni():
player.changeOmniGoToCounter += 1
else :
player.changeOmniGoToCounter = 0
if player.changeOmniGoToCounter > PositionConstants.CHANGE_OMNI_THRESH:
changedOmni = True
if player.firstFrame() or \
nav.dest != dest or \
changedOmni:
if not useOmni:
player.brain.CoA.setRobotGait(player.brain.motion)
nav.goTo(dest)
else:
player.brain.CoA.setRobotSlowGait(player.brain.motion)
nav.omniGoTo(dest)
return player.stay()
def approachBall(player):
"""
Once we are alligned with the ball, approach it
"""
if player.firstFrame():
player.hasAlignedOnce = False
player.brain.tracker.trackBall()
player.brain.CoA.setRobotGait(player.brain.motion)
#if player.brain.ball.locDist > constants.APPROACH_ACTIVE_LOC_DIST:
if transitions.shouldActiveLoc(player):
player.brain.tracker.activeLoc()
else :
player.brain.tracker.trackBall()
if player.penaltyKicking and \
player.ballInOppGoalBox():
return player.goNow('penaltyBallInOppGoalbox')
# Switch to other states if we should
if player.brain.play.isRole(GOALIE):
if transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldPositionForKick(player):
return player.goNow('positionForKick')
elif transitions.shouldTurnToBall_ApproachBall(player):
return player.goLater('turnToBall')
elif not player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBall(player):
return player.goLater('scanFindBall')
else:
if transitions.shouldDribble(player):
return player.goNow('dribble')
elif transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldPositionForKick(player):
return player.goNow('positionForKick')
return approachBallWalk(player)
def approachBallWalk(player):
"""
Method that is used by both approach ball and dribble
We use things as to when we should leave and how we should walk
"""
if not player.brain.play.isRole(GOALIE):
if transitions.shouldNotGoInBox(player):
return player.goLater('ballInMyBox')
elif transitions.shouldChaseAroundBox(player):
return player.goLater('chaseAroundBox')
elif transitions.shouldApproachBallWithLoc(player):
return player.goNow('approachBallWithLoc')
elif transitions.shouldTurnToBall_ApproachBall(player):
return player.goLater('turnToBall')
elif not player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBall(player):
return player.goLater('scanFindBall')
elif player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBallActiveLoc(player):
return player.goLater('scanFindBall')
elif transitions.shouldAvoidObstacleDuringApproachBall(player):
return player.goLater('avoidObstacle')
# Determine our speed for approaching the ball
ball = player.brain.ball
if player.brain.play.isRole(GOALIE) and goalTran.dangerousBall(player):
return player.goNow('approachDangerousBall')
if ball.dist < constants.APPROACH_WITH_GAIN_DIST:
sX = MyMath.clip(ball.dist*constants.APPROACH_X_GAIN,
constants.MIN_APPROACH_X_SPEED,
constants.MAX_APPROACH_X_SPEED)
else :
sX = constants.MAX_APPROACH_X_SPEED
# Determine the speed to turn to the ball
sTheta = MyMath.clip(ball.bearing*constants.APPROACH_SPIN_GAIN,
-constants.APPROACH_SPIN_SPEED,
constants.APPROACH_SPIN_SPEED)
# Avoid spinning so slowly that we step in place
if fabs(sTheta) < constants.MIN_APPROACH_SPIN_MAGNITUDE:
sTheta = 0.0
# Set our walk towards the ball
if ball.on:
player.setWalk(sX,0,sTheta)
return player.stay()
def positionForKick(player):
"""
State to align on the ball once we are near it
"""
if player.firstFrame():
player.brain.CoA.setRobotSlowGait(player.brain.motion)
ball = player.brain.ball
player.inKickingState = True
# Leave this state if necessary
if transitions.shouldKick(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goNow('waitBeforeKick')
elif transitions.shouldScanFindBall(player):
player.inKickingState = False
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('scanFindBall')
elif transitions.shouldTurnToBallFromPositionForKick(player):
player.inKickingState = False
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('turnToBall')
elif transitions.shouldApproachFromPositionForKick(player):
player.inKickingState = False
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('approachBall')
# Determine approach speed
targetY = ball.relY
sY = MyMath.clip(targetY * constants.PFK_Y_GAIN,
constants.PFK_MIN_Y_SPEED,
constants.PFK_MAX_Y_SPEED)
sY = max(constants.PFK_MIN_Y_MAGNITUDE,sY) * MyMath.sign(sY)
if transitions.shouldApproachForKick(player):
# targetX = (ball.relX -
# (constants.BALL_KICK_LEFT_X_CLOSE +
# constants.BALL_KICK_LEFT_X_FAR) / 2.0)
sX = MyMath.clip(ball.relX * constants.PFK_X_GAIN,
constants.PFK_MIN_X_SPEED,
constants.PFK_MAX_X_SPEED)
else:
sX = 0.0
if ball.on:
player.setWalk(sX,sY,0)
return player.stay()
def dribble(player):
"""
Keep running at the ball, but dribble
"""
if player.firstFrame():
player.brain.CoA.setRobotDribbleGait(player.brain.motion)
# if we should stop dribbling, see what else we should do
if transitions.shouldStopDribbling(player):
if transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldPositionForKick(player):
return player.goNow('positionForKick')
elif transitions.shouldApproachBall(player):
return player.goNow('approachBall')
return approachBallWalk(player)
def waitBeforeKick(player):
"""
Stop before we kick to make sure we want to kick
"""
player.inKickingState = True
if player.firstFrame():
player.brain.CoA.setRobotGait(player.brain.motion)
player.stopWalking()
if not player.brain.nav.isStopped():
return player.stay()
if transitions.shouldKick(player):
return player.goLater('getKickInfo')
elif transitions.shouldApproachForKick(player):
player.brain.tracker.trackBall()
player.inKickingState = False
return player.goLater('approachBall')
elif transitions.shouldScanFindBall(player):
player.inKickingState = False
player.brain.tracker.trackBall()
return player.goLater('scanFindBall')
elif transitions.shouldRepositionForKick(player):
player.brain.tracker.trackBall()
return player.goLater('positionForKick')
# Just don't get stuck here!
if player.counter > 50:
return player.goNow('scanFindBall')
return player.stay()
# WARNING: avoidObstacle could possibly go into our own box
def avoidObstacle(player):
"""
If we detect something in front of us, dodge it
"""
if player.firstFrame():
player.doneAvoidingCounter = 0
player.printf(player.brain.sonar)
player.brain.CoA.setRobotGait(player.brain.motion)
if (transitions.shouldAvoidObstacleLeft(player) and
transitions.shouldAvoidObstacleRight(player)):
# Backup
player.printf("Avoid by backup");
player.setWalk(constants.DODGE_BACK_SPEED, 0, 0)
elif transitions.shouldAvoidObstacleLeft(player):
# Dodge right
player.printf("Avoid by right dodge");
player.setWalk(0, constants.DODGE_RIGHT_SPEED, 0)
elif transitions.shouldAvoidObstacleRight(player):
# Dodge left
player.printf("Avoid by left dodge");
player.setWalk(0, constants.DODGE_LEFT_SPEED, 0)
if not transitions.shouldAvoidObstacle(player):
player.doneAvoidingCounter += 1
else :
player.doneAvoidingCounter -= 1
player.doneAvoidingCounter = max(0, player.doneAvoidingCounter)
if player.doneAvoidingCounter > constants.DONE_AVOIDING_FRAMES_THRESH:
player.shouldAvoidObstacleRight = 0
player.shouldAvoidObstacleLeft = 0
player.stopWalking()
return player.goLater(player.lastDiffState)
return player.stay()
def chaseAroundBox(player):
if player.firstFrame():
player.shouldNotChaseAroundBox = 0
player.brain.CoA.setRobotGait(player.brain.motion)
if transitions.shouldKick(player):
return player.goNow('waitBeforeKick')
elif transitions.shouldScanFindBall(player):
return player.goLater('scanFindBall')
elif transitions.shouldAvoidObstacle(player): # Has potential to go into box!
return player.goLater('avoidObstacle')
if not transitions.shouldChaseAroundBox(player):
player.shouldChaseAroundBox += 1
else :
player.shouldChaseAroundBox = 0
if player.shouldChaseAroundBox > constants.STOP_CHASING_AROUND_BOX:
return player.goLater('chase')
ball = player.brain.ball
my = player.brain.my
if my.x > NogginConstants.MY_GOALBOX_RIGHT_X:
# go to corner nearest ball
if ball.y > NogginConstants.MY_GOALBOX_TOP_Y:
player.brain.nav.goTo( (NogginConstants.MY_GOALBOX_RIGHT_X +
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOALBOX_TOP_Y +
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOAL_HEADING ))
if ball.y < NogginConstants.MY_GOALBOX_BOTTOM_Y:
player.brain.nav.goTo(( NogginConstants.MY_GOALBOX_RIGHT_X +
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOALBOX_BOTTOM_Y -
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOAL_HEADING ))
if my.x < NogginConstants.MY_GOALBOX_RIGHT_X:
# go to corner nearest ball
if my.y > NogginConstants.MY_GOALBOX_TOP_Y:
player.brain.nav.goTo(( NogginConstants.MY_GOALBOX_RIGHT_X +
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOALBOX_TOP_Y +
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOAL_HEADING ))
if my.y < NogginConstants.MY_GOALBOX_BOTTOM_Y:
player.brain.nav.goTo(( NogginConstants.MY_GOALBOX_RIGHT_X +
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOALBOX_BOTTOM_Y -
constants.GOALBOX_OFFSET,
NogginConstants.MY_GOAL_HEADING ))
return player.stay()
def steps(player):
if player.brain.nav.isStopped():
player.setSteps(3,3,0,5)
elif player.brain.nav.currentState != "stepping":
player.stopWalking()
return player.stay()
def ballInMyBox(player):
if player.firstFrame():
player.brain.tracker.activeLoc()
player.brain.CoA.setRobotGait(player.brain.motion)
ball = player.brain.ball
if fabs(ball.bearing) > constants.BALL_APPROACH_BEARING_THRESH:
player.setWalk(0, 0, constants.BALL_SPIN_SPEED *
MyMath.sign(ball.bearing) )
elif fabs(ball.bearing) < constants.BALL_APPROACH_BEARING_OFF_THRESH :
player.stopWalking()
if not player.ballInMyGoalBox():
return player.goLater('chase')
return player.stay()
# TODO
def approachDangerousBall(player):
if player.firstFrame():
player.stopWalking()
#print "approach dangerous ball"
#single steps towards ball and goal with spin
player.setSteps(0, 0, 0, 0)
if not goalTran.dangerousBall(player):
return player.goLater('approachBall')
if transitions.shouldScanFindBall(player):
return player.goLater('scanFindBall')
elif transitions.shouldTurnToBall_ApproachBall(player):
return player.goLater('turnToBall')
elif transitions.shouldSpinFindBall(player):
return player.goLater('spinFindBall')
return player.stay()
def orbitBeforeKick(player):
"""
State for circling the ball when we're facing our goal.
"""
brain = player.brain
my = brain.my
if player.firstFrame():
player.orbitStartH = my.h
brain.CoA.setRobotGait(brain.motion)
brain.tracker.trackBall()
shotPoint = KickingHelpers.getShotCloseAimPoint(player)
bearingToGoal = my.getRelativeBearing(shotPoint)
spinDir = -MyMath.sign(bearingToGoal)
player.brain.nav.orbitAngle(spinDir * 90)
if not player.brain.tracker.activeLocOn and \
transitions.shouldScanFindBall(player):
player.brain.CoA.setRobotGait(player.brain.motion)
return player.goLater('scanFindBall')
elif brain.ball.dist > constants.STOP_ORBIT_BALL_DIST:
return player.goLater('chase')
if player.brain.nav.isStopped() and not player.firstFrame():
return player.goLater('positionForKick')
return player.stay()