Skip to content

Commit 19a1119

Browse files
committed
Fix geometry for CI connection
The geometry was overwritten when division changed. When sending the desired geometry only with first message, it got overwritten and never updated anymore. Now, it is not updated on division change, when a geometry was set manually within last 5 min.
1 parent ab31d45 commit 19a1119

File tree

5 files changed

+54
-36
lines changed

5 files changed

+54
-36
lines changed

internal/app/ci/ci.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package ci
22

33
import (
44
"bufio"
5+
"log"
6+
"net"
7+
"sync"
8+
"time"
9+
510
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/engine"
611
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/sslconn"
712
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
813
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/tracker"
914
"google.golang.org/protobuf/proto"
10-
"log"
11-
"net"
12-
"sync"
13-
"time"
1415
)
1516

1617
type Handler func(time.Time, *tracker.TrackerWrapperPacket) *state.Referee
@@ -107,23 +108,26 @@ func (s *Server) serve(conn net.Conn) {
107108
}
108109
}
109110

110-
if input.Geometry != nil {
111-
s.gcEngine.ProcessGeometry(input.Geometry)
112-
}
113-
114111
if input.Timestamp != nil {
115112
sec := *input.Timestamp / 1e9
116113
nSec := *input.Timestamp - sec*1e9
117114
s.mutex.Lock()
118115
s.latestTime = time.Unix(sec, nSec)
119116
s.mutex.Unlock()
117+
} else {
118+
log.Println("CiInput without timestamp!")
119+
}
120+
121+
if input.Geometry != nil {
122+
s.gcEngine.ProcessGeometry(input.Geometry)
123+
}
124+
125+
if input.Timestamp != nil {
120126
select {
121127
case s.tickChan <- s.latestTime:
122128
case <-time.After(1 * time.Second):
123129
log.Printf("tickChan unresponsive! Failed to sent %v", s.latestTime)
124130
}
125-
} else {
126-
log.Println("CiInput without timestamp!")
127131
}
128132
}
129133
}

internal/app/engine/consume_geometry.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package engine
22

33
import (
4-
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/vision"
5-
"log"
64
"math"
5+
6+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/vision"
77
)
88

99
func (e *Engine) ProcessGeometry(data *vision.SSL_GeometryData) {
@@ -59,9 +59,5 @@ func (e *Engine) ProcessGeometry(data *vision.SSL_GeometryData) {
5959
newGeometry.PlacementOffsetGoalLine = defaultPlacementOffsetGoalLine + newGeometry.DefenseAreaDepth
6060
}
6161

62-
e.stateMachine.Geometry = newGeometry
63-
64-
if currentGeometry != newGeometry {
65-
log.Printf("Geometry changed from \n%+v to \n%+v", currentGeometry, newGeometry)
66-
}
62+
e.stateMachine.UpdateGeometry(newGeometry, e.timeProvider())
6763
}

internal/app/engine/engine.go

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

33
import (
4+
"log"
5+
"math/rand"
6+
"slices"
7+
"sync"
8+
"time"
9+
410
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
511
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/geom"
612
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
@@ -12,11 +18,6 @@ import (
1218
"google.golang.org/protobuf/proto"
1319
"google.golang.org/protobuf/types/known/durationpb"
1420
"google.golang.org/protobuf/types/known/timestamppb"
15-
"log"
16-
"math/rand"
17-
"slices"
18-
"sync"
19-
"time"
2021
)
2122

2223
var changeOriginEngine = "Engine"
@@ -244,8 +245,7 @@ func (e *Engine) Start() error {
244245
e.currentState.MaxBotsPerTeam = new(int32)
245246
}
246247

247-
e.stateMachine.Geometry = e.gameConfig.DefaultGeometry[e.currentState.Division.Div()]
248-
log.Printf("Loaded default geometry for DivA: %+v", e.stateMachine.Geometry)
248+
e.stateMachine.UpdateGeometry(e.gameConfig.DefaultGeometry[e.currentState.Division.Div()], time.Time{})
249249
go e.processChanges()
250250
return nil
251251
}

internal/app/statemachine/change_config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package statemachine
22

33
import (
4-
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
54
"log"
5+
"time"
6+
7+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
68
)
79

810
func (s *StateMachine) processChangeUpdateConfig(newState *state.State, change *Change_UpdateConfig) (changes []*Change) {
@@ -11,8 +13,11 @@ func (s *StateMachine) processChangeUpdateConfig(newState *state.State, change *
1113
newState.Division = change.Division
1214
*newState.MaxBotsPerTeam = s.gameConfig.MaxBots[change.Division.Div()]
1315
s.updateMaxBots(newState)
14-
s.Geometry = s.gameConfig.DefaultGeometry[change.Division.Div()]
15-
log.Printf("Updated geometry to %+v", s.Geometry)
16+
17+
if s.timeProvider().Sub(s.GeometryUpdated) > time.Minute*5 {
18+
log.Printf("Set geometry to default for division %v", change.Division.Div())
19+
s.UpdateGeometry(s.gameConfig.DefaultGeometry[change.Division.Div()], time.Time{})
20+
}
1621
if *change.Division == state.Division_DIV_A {
1722
// in division A, both teams must be able to place ball
1823
for _, team := range state.BothTeams() {

internal/app/statemachine/statemachine.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
package statemachine
22

33
import (
4+
"log"
5+
"math/rand"
6+
"time"
7+
48
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
59
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
610
"github.com/RoboCup-SSL/ssl-game-controller/pkg/timer"
711
"google.golang.org/protobuf/proto"
8-
"log"
9-
"math/rand"
10-
"time"
1112
)
1213

1314
var changeOriginStateMachine = "StateMachine"
1415

1516
// StateMachine describes the state machine that translates changes into new states
1617
type StateMachine struct {
17-
gameConfig config.Game
18-
Geometry config.Geometry
19-
stageTimes map[state.Referee_Stage]time.Duration
20-
rand *rand.Rand
21-
timeProvider timer.TimeProvider
18+
gameConfig config.Game
19+
Geometry config.Geometry
20+
// GeometryUpdated is the time when the geometry was last updated externally
21+
GeometryUpdated time.Time
22+
stageTimes map[state.Referee_Stage]time.Duration
23+
rand *rand.Rand
24+
timeProvider timer.TimeProvider
2225
}
2326

2427
// NewStateMachine creates a new state machine
2528
func NewStateMachine(gameConfig config.Game) (s *StateMachine) {
2629
s = new(StateMachine)
2730
s.gameConfig = gameConfig
2831
s.Geometry = gameConfig.DefaultGeometry[config.DivA]
32+
s.GeometryUpdated = time.Time{}
2933
s.stageTimes = loadStageTimes(gameConfig)
3034
s.rand = rand.New(rand.NewSource(time.Now().Unix()))
31-
log.Printf("Loaded default geometry for DivA: %+v", s.Geometry)
3235
return
3336
}
3437

@@ -109,3 +112,13 @@ func (s *StateMachine) Process(currentState *state.State, change *Change) (newSt
109112

110113
return
111114
}
115+
116+
func (s *StateMachine) UpdateGeometry(geometry config.Geometry, updated time.Time) {
117+
currentGeometry := s.Geometry
118+
s.Geometry = geometry
119+
s.GeometryUpdated = updated
120+
121+
if currentGeometry != geometry {
122+
log.Printf("Geometry changed from \n%+v to \n%+v\nUpdated: %v", currentGeometry, geometry, updated)
123+
}
124+
}

0 commit comments

Comments
 (0)