Skip to content

Commit

Permalink
Add field stack light API and update PLC handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cpapplefamily committed Dec 22, 2024
1 parent fbe4dfe commit d8c32a6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
16 changes: 8 additions & 8 deletions field/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,9 @@ func (arena *Arena) handlePlcInputOutput() {
case PostTimeout:
// Set the stack light state -- solid alliance color(s) if robots are not connected, solid orange if scores are
// not input, or blinking green if ready.
//*greenStackLight := redAllianceReady && blueAllianceReady //&& arena.Plc.GetCycleState(2, 0, 2)
//*arena.Plc.SetStackLights(!redAllianceReady, !blueAllianceReady, false, greenStackLight)
//*arena.Plc.SetStackBuzzer(redAllianceReady && blueAllianceReady)
greenStackLight := redAllianceReady && blueAllianceReady //&& arena.Plc.GetCycleState(2, 0, 2)
arena.Plc.SetStackLights(!redAllianceReady, !blueAllianceReady, false, greenStackLight)
arena.Plc.SetStackBuzzer(redAllianceReady && blueAllianceReady)

// Turn off lights if all teams become ready.
if redAllianceReady && blueAllianceReady {
Expand All @@ -982,12 +982,12 @@ func (arena *Arena) handlePlcInputOutput() {
if arena.FieldReset {
//*arena.Plc.SetFieldResetLight(true)
}
//*scoreReady := arena.RedRealtimeScore.FoulsCommitted && arena.BlueRealtimeScore.FoulsCommitted &&
//* arena.alliancePostMatchScoreReady("red") && arena.alliancePostMatchScoreReady("blue")
//*arena.Plc.SetStackLights(false, false, !scoreReady, false)
scoreReady := arena.RedRealtimeScore.FoulsCommitted && arena.BlueRealtimeScore.FoulsCommitted &&
arena.alliancePostMatchScoreReady("red") && arena.alliancePostMatchScoreReady("blue")
arena.Plc.SetStackLights(false, false, !scoreReady, false)
case AutoPeriod, PausePeriod, TeleopPeriod:
//*arena.Plc.SetStackBuzzer(false)
//*arena.Plc.SetStackLights(!redAllianceReady, !blueAllianceReady, false, true)
arena.Plc.SetStackBuzzer(false)
arena.Plc.SetStackLights(!redAllianceReady, !blueAllianceReady, false, true)
}

// Get all the game-specific inputs and update the score.
Expand Down
6 changes: 6 additions & 0 deletions plc/plc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Plc interface {
//Freezy Arena
SetAlternateIOStopState(input int, state bool)
ResetEstops()
GetFieldStackLight() (bool, bool, bool, bool)
}

type ModbusPlc struct {
Expand Down Expand Up @@ -529,4 +530,9 @@ func (plc *ModbusPlc) ResetEstops(){
// used for Alternate IO stops
func (plc *ModbusPlc) SetAlternateIOStopState(input int, state bool){
plc.inputs[input] = state
}


func (plc *ModbusPlc) GetFieldStackLight() (bool, bool, bool, bool) {
return plc.coils[stackLightRed], plc.coils[stackLightBlue], plc.coils[stackLightOrange], plc.coils[stackLightGreen]
}
31 changes: 31 additions & 0 deletions web/alternateIO.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,34 @@ func (web *Web) eStopStatePostHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("eStop state updated successfully."))

}

type fieldStackLight struct {
Red bool `json:"redStackLight"`
Blue bool `json:"blueStackLight"`
Orange bool `json:"orangeStackLight"`
Green bool `json:"greenStackLight"`

}

func (web *Web) fieldStackLightGetHandler(w http.ResponseWriter, r *http.Request) {
// Ensure the request is a GET request.
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}

// Get the current state of the field stack light.
var stackLight fieldStackLight
stackLight.Red, stackLight.Blue, stackLight.Orange, stackLight.Green = web.arena.Plc.GetFieldStackLight()

// Marshal the response payload.
response, err := json.Marshal(stackLight)
if err != nil {
http.Error(w, "Failed to marshal eStop state", http.StatusInternalServerError)
return
}

// Send the response.
w.Write(response)
}

1 change: 1 addition & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (web *Web) newHandler() http.Handler {
mux.HandleFunc("GET /api/allianceStatus", web.allianceStatusApiHandler)
mux.HandleFunc("GET /field_monitor_help", web.fieldMonitorDisplayHelpHandler)
mux.HandleFunc("POST /freezy/eStopState", web.eStopStatePostHandler)
mux.HandleFunc("GET /field_stack_light", web.fieldStackLightGetHandler)
return mux
}

Expand Down

0 comments on commit d8c32a6

Please sign in to comment.