Skip to content

Commit c4b06ea

Browse files
committed
Handle readyToContinue correctly
1 parent 12fed41 commit c4b06ea

File tree

6 files changed

+70
-47
lines changed

6 files changed

+70
-47
lines changed

internal/app/engine/common.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ func (e *Engine) robotsInsideRadius(robots []*Robot, pos *geom.Vector2, radius f
3939
return false
4040
}
4141

42-
func (e *Engine) timeSinceLastChange() time.Duration {
43-
if e.stateStore.LatestEntry() != nil {
44-
lastChangeTs := goTime(e.stateStore.LatestEntry().Timestamp)
45-
now := e.timeProvider()
46-
return now.Sub(lastChangeTs)
47-
}
48-
return 0
49-
}
50-
5142
func goDur(duration *duration.Duration) time.Duration {
5243
goDur, err := ptypes.Duration(duration)
5344
if err != nil {

internal/app/engine/consume_tracker.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@ func (e *Engine) ProcessTrackerFrame(wrapperFrame *tracker.TrackerWrapperPacket)
1818
Robots: convertRobots(wrapperFrame.TrackedFrame.Robots),
1919
}
2020

21-
readyToContinue := e.readyToContinue()
22-
2321
e.gcState.TrackerState[*wrapperFrame.Uuid] = &state
2422

2523
// for now, all tracker sources update the GC state
2624
e.gcState.TrackerStateGc = &state
27-
28-
if e.gcState.ReadyToContinue == nil {
29-
e.gcState.ReadyToContinue = new(bool)
30-
}
31-
*e.gcState.ReadyToContinue = readyToContinue
3225
}
3326

3427
func convertRobots(robots []*tracker.TrackedRobot) (rs []*Robot) {
@@ -51,13 +44,3 @@ func convertBalls(balls []*tracker.TrackedBall) *Ball {
5144
}
5245
return &ball
5346
}
54-
55-
func (e *Engine) readyToContinue() bool {
56-
radius := e.gameConfig.DistanceToBallInStop + robotRadius + distanceThreshold
57-
if e.gcState.TrackerStateGc.Ball == nil ||
58-
!e.ballSteady() ||
59-
e.robotsInsideRadius(e.gcState.TrackerStateGc.Robots, e.gcState.TrackerStateGc.Ball.Pos.ToVector2(), radius) {
60-
return false
61-
}
62-
return false
63-
}

internal/app/engine/process_prepare.go renamed to internal/app/engine/process_continue.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@ import (
1010
const minPreparationTime = time.Second * 2
1111
const distanceToBallDuringPenalty = 1.0
1212

13-
func (e *Engine) processPrepare() {
14-
if !e.currentState.Command.IsPrepare() ||
13+
func (e *Engine) processContinue() {
14+
if !(e.currentState.Command.IsPrepare() || *e.currentState.Command.Type == state.Command_STOP) ||
1515
e.gcState.TrackerStateGc.Ball == nil ||
16-
e.gcState.ReadyToContinue == nil ||
17-
!*e.gcState.ReadyToContinue ||
18-
e.currentState.NextCommand == nil ||
19-
!e.currentState.GetAutoContinue() ||
20-
e.timeSinceLastChange() < minPreparationTime {
16+
e.currentState.NextCommand == nil {
17+
e.gcState.ReadyToContinue = nil
18+
return
19+
}
20+
21+
readyToContinue := false
22+
23+
defer func() {
24+
// set at the end of this function when the flag has its final value
25+
e.gcState.ReadyToContinue = &readyToContinue
26+
}()
27+
28+
if e.timeSinceLastChange() < minPreparationTime {
29+
// Too early
2130
return
2231
}
2332

@@ -75,12 +84,21 @@ func (e *Engine) processPrepare() {
7584
}
7685
}
7786

78-
e.Enqueue(&statemachine.Change{
79-
Origin: &changeOriginEngine,
80-
Change: &statemachine.Change_Continue{
81-
Continue: &statemachine.Continue{},
82-
},
83-
})
87+
if *e.currentState.Command.Type == state.Command_STOP &&
88+
!e.readyToContinueFromStop() {
89+
return
90+
}
91+
92+
readyToContinue = true
93+
94+
if e.currentState.GetAutoContinue() {
95+
e.Enqueue(&statemachine.Change{
96+
Origin: &changeOriginEngine,
97+
Change: &statemachine.Change_Continue{
98+
Continue: &statemachine.Continue{},
99+
},
100+
})
101+
}
84102
}
85103

86104
func (e *Engine) penaltyKeeperId() *state.RobotId {
@@ -109,3 +127,22 @@ func (e *Engine) posInsideGoal(pos *geom.Vector2) bool {
109127
goalArea := geom.NewRectangleFromCenter(goalCenter, robotRadius*2, e.getGeometry().GoalWidth)
110128
return goalArea.IsPointInside(pos)
111129
}
130+
131+
func (e *Engine) readyToContinueFromStop() bool {
132+
radius := e.gameConfig.DistanceToBallInStop + robotRadius + distanceThreshold
133+
if e.gcState.TrackerStateGc.Ball == nil ||
134+
!e.ballSteady() ||
135+
e.robotsInsideRadius(e.gcState.TrackerStateGc.Robots, e.gcState.TrackerStateGc.Ball.Pos.ToVector2(), radius) {
136+
return false
137+
}
138+
return true
139+
}
140+
141+
func (e *Engine) timeSinceLastChange() time.Duration {
142+
if e.stateStore.LatestEntry() != nil {
143+
lastChangeTs := goTime(e.stateStore.LatestEntry().Timestamp)
144+
now := e.timeProvider()
145+
return now.Sub(lastChangeTs)
146+
}
147+
return 0
148+
}

internal/app/engine/process_runningtostop.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package engine
22

33
import (
44
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/geom"
5+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
56
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/statemachine"
67
"log"
78
)
@@ -21,12 +22,23 @@ func (e *Engine) processRunningToStop() {
2122
})
2223
} else {
2324
log.Printf("Ball is already placed, no need for ball placement")
24-
e.Enqueue(&statemachine.Change{
25-
Origin: &changeOriginEngine,
26-
Change: &statemachine.Change_Continue{
27-
Continue: &statemachine.Continue{},
28-
},
29-
})
25+
if e.currentState.GetAutoContinue() {
26+
e.Enqueue(&statemachine.Change{
27+
Origin: &changeOriginEngine,
28+
Change: &statemachine.Change_Continue{
29+
Continue: &statemachine.Continue{},
30+
},
31+
})
32+
} else {
33+
e.Enqueue(&statemachine.Change{
34+
Origin: &changeOriginEngine,
35+
Change: &statemachine.Change_NewCommand{
36+
NewCommand: &statemachine.NewCommand{
37+
Command: state.NewCommandNeutral(state.Command_STOP),
38+
},
39+
},
40+
})
41+
}
3042
}
3143
}
3244

internal/app/engine/process_tick.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (e *Engine) processTick() {
4040

4141
e.noProgressDetector.process()
4242
e.ballPlacementCoordinator.process()
43-
e.processPrepare()
43+
e.processContinue()
4444
e.processBotNumber()
4545

4646
stateCopy := e.currentState.Clone()

internal/app/statemachine/change_gameevent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (s *StateMachine) processChangeAddGameEvent(newState *state.State, change *
132132
byTeam.Known() &&
133133
*newState.TeamInfo(byTeam).BallPlacementFailures > 0 {
134134
*newState.TeamInfo(byTeam).BallPlacementFailures--
135-
if byTeam == *newState.NextCommand.ForTeam {
135+
if byTeam == *newState.NextCommand.ForTeam && newState.GetAutoContinue() {
136136
log.Printf("Placement succeeded by team %v, which is also in favor. Can continue.", byTeam)
137137
changes = append(changes, &Change{
138138
Change: &Change_Continue{

0 commit comments

Comments
 (0)