Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Add: watchdog timer for moving elevator (#11)
Browse files Browse the repository at this point in the history
Times out after x seconds of movement without stopping
  • Loading branch information
tordnat authored Mar 13, 2024
1 parent 21c6bb6 commit 225e3d4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion elevatorAlgorithm/elevator/elevator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const N_FLOORS = 4
const N_BUTTONS = 3
const N_HALL_BUTTONS = 2
const DOOR_OPEN_DURATION_S = 2

const DOOR_OBSTRUCTION_TIMEOUT = 7
type ElevatorBehaviour int

const (
Expand Down
12 changes: 11 additions & 1 deletion elevatorAlgorithm/fsm/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"
)

var movingWatchdog *time.Timer

func FSM(orderAssignment chan [][]bool, clearOrders chan requests.ClearFloorOrders, floorEvent chan int, obstructionEvent chan bool, elevStateToSync chan elevator.ElevatorState) {
elevState := elevator.ElevatorState{elevator.EB_Idle, -1, elevio.MD_Stop, [][]bool{
{false, false, false},
Expand All @@ -18,8 +20,10 @@ func FSM(orderAssignment chan [][]bool, clearOrders chan requests.ClearFloorOrde

doorTimer := time.NewTimer(0 * time.Second)
obstructedTimer := time.NewTimer(0 * time.Second)
movingWatchdog = time.NewTimer(0*time.Second)
<-doorTimer.C // Drain channel
<-obstructedTimer.C // Drain channel
<-movingWatchdog.C
log.Println("Initializing Elevator FSM")
elevState = onInitBetweenFloors(elevState)
for elevio.GetFloor() == -1 {
Expand All @@ -39,6 +43,7 @@ func FSM(orderAssignment chan [][]bool, clearOrders chan requests.ClearFloorOrde
clearOrders <- OrdersToClear(elevState) // This must be run last to not clear orders at the wrong place

case floor := <-floorEvent:
elevio.SetFloorIndicator(floor)
elevState.Floor = floor
var clearOrder requests.ClearFloorOrders
elevState.Behaviour, clearOrder = OnFloorArrival(elevState, doorTimer)
Expand All @@ -60,14 +65,16 @@ func FSM(orderAssignment chan [][]bool, clearOrders chan requests.ClearFloorOrde
if elevState.Behaviour == elevator.EB_DoorOpen {
if isObstructed {
log.Println("Obstruction occured!")
obstructedTimer.Reset(time.Second * 7)
obstructedTimer.Reset(time.Second * elevator.DOOR_OBSTRUCTION_TIMEOUT)
} else {
obstructedTimer.Stop()
}
doorTimer.Reset(elevator.DOOR_OPEN_DURATION_S * time.Second)
}
case <-obstructedTimer.C:
os.Exit(1)
case <-movingWatchdog.C:
os.Exit(2)
}
elevStateToSync <- elevState //Must find out if this is a good place to sync elevState
}
Expand All @@ -94,6 +101,7 @@ func updateOrders(elevState elevator.ElevatorState, doorTimer *time.Timer) (elev
elevio.SetDoorOpenLamp(true)
doorTimer.Reset(elevator.DOOR_OPEN_DURATION_S * time.Second)
case elevator.EB_Moving:
movingWatchdog.Reset(time.Second * 10)
elevio.SetMotorDirection(elevState.Direction)
}
}
Expand All @@ -117,6 +125,7 @@ func OnFloorArrival(elevState elevator.ElevatorState, doorTimer *time.Timer) (el
switch elevState.Behaviour {
case elevator.EB_Moving:
if requests.ShouldStop(elevState.Direction, elevState.Floor, elevState.Requests) {
movingWatchdog.Stop()
elevio.SetMotorDirection(elevio.MD_Stop)
clearOrder.Floor = elevState.Floor
clearOrder.Cab = true
Expand Down Expand Up @@ -145,6 +154,7 @@ func OnDoorTimeOut(elevState elevator.ElevatorState, doorTimer *time.Timer) (ele
elevio.SetDoorOpenLamp(false)
elevio.SetMotorDirection(elevState.Direction)
case elevator.EB_Moving:
movingWatchdog.Reset(time.Second * 10)
elevio.SetDoorOpenLamp(false)
elevio.SetMotorDirection(elevState.Direction)
}
Expand Down
1 change: 0 additions & 1 deletion elevatorAlgorithm/hra/hra.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func Encode(system ElevatorSystem) string {
func AssignRequests(elevatorStates string) string {
out, err := exec.Command("./hall_request_assigner", "--includeCab", "-i", (elevatorStates)).Output()


if err != nil {
fmt.Println("Error ", err)
}
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func main() {
log.Println("Elevator starting 🛗")
elevatorPort, elevatorId := cmd.InitCommandLineArgs(os.Args)
elevio.Init(fmt.Sprintf("localhost:%d", elevatorPort), elevator.N_FLOORS)

buttonEvent := make(chan elevio.ButtonEvent)
floorEvent := make(chan int)
obstructionEvent := make(chan bool)
Expand Down

0 comments on commit 225e3d4

Please sign in to comment.