From 0082ea5d0b9a8d86500b2745f27c9b7f8fee3ee4 Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Sat, 25 Aug 2018 14:54:13 -0700 Subject: [PATCH] Fix issues from testing PLC integration. --- field/arena.go | 11 ++++++----- field/coil_string.go | 4 ++-- field/plc.go | 6 ++++++ game/seesaw.go | 7 +++++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/field/arena.go b/field/arena.go index 92c503e..42d4f84 100644 --- a/field/arena.go +++ b/field/arena.go @@ -733,9 +733,9 @@ func (arena *Arena) handlePlcInput() { // Handle scale and switch ownership. scale, redSwitch, blueSwitch := arena.Plc.GetScaleAndSwitches() - arena.Scale.UpdateState(scale, currentTime) - arena.RedSwitch.UpdateState(redSwitch, currentTime) - arena.BlueSwitch.UpdateState(blueSwitch, currentTime) + ownershipChanged := arena.Scale.UpdateState(scale, currentTime) + ownershipChanged = arena.RedSwitch.UpdateState(redSwitch, currentTime) || ownershipChanged + ownershipChanged = arena.BlueSwitch.UpdateState(blueSwitch, currentTime) || ownershipChanged if arena.MatchState == AutoPeriod { redScore.AutoOwnershipPoints = 2 * int(arena.RedSwitch.GetRedSeconds(matchStartTime, currentTime)+ arena.Scale.GetRedSeconds(matchStartTime, currentTime)) @@ -773,7 +773,7 @@ func (arena *Arena) handlePlcInput() { arena.PlaySoundNotifier.Notify("match-" + newBluePowerUp) } - if !oldRedScore.Equals(redScore) || !oldBlueScore.Equals(blueScore) { + if !oldRedScore.Equals(redScore) || !oldBlueScore.Equals(blueScore) || ownershipChanged { arena.RealtimeScoreNotifier.Notify(nil) } } @@ -789,8 +789,9 @@ func (arena *Arena) handleLeds() { // Set the stack light state -- blinking green if ready, or solid alliance color(s) if not. redAllianceReady := arena.checkAllianceStationsReady("R1", "R2", "R3") == nil blueAllianceReady := arena.checkAllianceStationsReady("B1", "B2", "B3") == nil - greenStackLight := redAllianceReady && blueAllianceReady && arena.Plc.GetCycleState(2, 0, 25) + greenStackLight := redAllianceReady && blueAllianceReady && arena.Plc.GetCycleState(2, 0, 2) arena.Plc.SetStackLights(!redAllianceReady, !blueAllianceReady, greenStackLight) + arena.Plc.SetStackBuzzer(redAllianceReady && blueAllianceReady) // Turn off each alliance switch if all teams become ready. if redAllianceReady && !arena.lastRedAllianceReady { diff --git a/field/coil_string.go b/field/coil_string.go index b678023..dca43e6 100644 --- a/field/coil_string.go +++ b/field/coil_string.go @@ -4,9 +4,9 @@ package field import "strconv" -const _coil_name = "heartbeatmatchResetstackLightGreenstackLightOrangestackLightRedstackLightBluered1EthernetDisablered2EthernetDisablered3EthernetDisableblue1EthernetDisableblue2EthernetDisableblue3EthernetDisablecoilCount" +const _coil_name = "heartbeatmatchResetstackLightGreenstackLightOrangestackLightRedstackLightBluestackLightBuzzerred1EthernetDisablered2EthernetDisablered3EthernetDisableblue1EthernetDisableblue2EthernetDisableblue3EthernetDisablecoilCount" -var _coil_index = [...]uint8{0, 9, 19, 34, 50, 63, 77, 96, 115, 134, 154, 174, 194, 203} +var _coil_index = [...]uint8{0, 9, 19, 34, 50, 63, 77, 93, 112, 131, 150, 170, 190, 210, 219} func (i coil) String() string { if i < 0 || i >= coil(len(_coil_index)-1) { diff --git a/field/plc.go b/field/plc.go index 1411521..8afc753 100644 --- a/field/plc.go +++ b/field/plc.go @@ -95,6 +95,7 @@ const ( stackLightOrange stackLightRed stackLightBlue + stackLightBuzzer red1EthernetDisable red2EthernetDisable red3EthernetDisable @@ -208,6 +209,11 @@ func (plc *Plc) SetStackLights(red, blue, green bool) { plc.Coils[stackLightGreen] = green } +// Set the on/off state of the stack lights on the scoring table. +func (plc *Plc) SetStackBuzzer(state bool) { + plc.Coils[stackLightBuzzer] = state +} + func (plc *Plc) GetCycleState(max, index, duration int) bool { return plc.cycleCounter/duration%max == index } diff --git a/game/seesaw.go b/game/seesaw.go index b4865ec..b119825 100644 --- a/game/seesaw.go +++ b/game/seesaw.go @@ -30,8 +30,9 @@ type Ownership struct { endTime *time.Time } -// Updates the internal timing state of the scale or switch given the current state of the sensors. -func (seesaw *Seesaw) UpdateState(state [2]bool, currentTime time.Time) { +// Updates the internal timing state of the scale or switch given the current state of the sensors. Returns true if +// ownership has changed since the last cycle. +func (seesaw *Seesaw) UpdateState(state [2]bool, currentTime time.Time) bool { ownedBy := NeitherAlliance // Check if there is an active force power up for this seesaw. @@ -61,7 +62,9 @@ func (seesaw *Seesaw) UpdateState(state [2]bool, currentTime time.Time) { newOwnership := &Ownership{seesaw: seesaw, ownedBy: ownedBy, startTime: currentTime} seesaw.ownerships = append(seesaw.ownerships, newOwnership) } + return true } + return false } func (seesaw *Seesaw) GetOwnedBy() Alliance {