func (elev *ElevatorState) goToStateIdle() { if DEBUG_FSM { fmt.Println("fsm: Going idle in floor = ", elev.floor) } driver.RunStop() elev.motorErrorTimer.Stop() elev.setState(STATE_IDLE) }
func motorError() { driver.RunStop() fmt.Printf("\n\n================== ERROR, ELEVATOR MOTOR NOT WORKING PROPERLY ==========\n\n") if floor := driver.GetFloorSignal(); floor != -1 { fmt.Printf("Elevator at floor %d, door opened\n", floor) } fmt.Printf("took %d seconds to reach new floor when running, try fixing the problem and restart\n", MOTOR_ERROR_SEC) fmt.Printf("Program exiting now!\n") fmt.Printf("\n===============================================================================\n\n\n") os.Exit(1) }
func (elev *ElevatorState) goToStateDoorOpen() { if DEBUG_FSM { fmt.Printf("fsm: Door Opening in floor = %d\n", elev.floor) } if elev.State() == STATE_DOOR_OPEN { if DEBUG_FSM { fmt.Println("fsm: Door alredy open") } elev.doorTimerMutex.Lock() elev.doorTimer.Reset(time.Second * 3) elev.doorTimerMutex.Unlock() elev.OrderDone <- int(elev.floor) return } driver.RunStop() elev.motorErrorTimer.Stop() elev.setState(STATE_DOOR_OPEN) elev.OrderDone <- int(elev.floor) driver.SetDoorOpen(true) doorClose := func() { if DEBUG_FSM { fmt.Printf("fsm: Door closing, currentDestination = %d\n", elev.Destination()) } driver.SetDoorOpen(false) if elev.Destination() == NO_DESTINATION { elev.goToStateIdle() } else { elev.goToStateMoving(calculateDir(elev.Destination(), elev.Floor())) } } elev.doorTimerMutex.Lock() elev.doorTimer = time.AfterFunc(time.Second*3, doorClose) elev.doorTimerMutex.Unlock() }
func NewElevator() *ElevatorState { var elev ElevatorState elev.setDestination(NO_DESTINATION) elev.OrderDone = make(chan int, 1) floorEvent := eventmgr.CheckFloorSignal() driver.RunDown() elev.motorErrorTimer = time.AfterFunc(time.Second*MOTOR_ERROR_SEC, motorError) elev.NewFloorReached(<-floorEvent) driver.RunStop() elev.motorErrorTimer.Stop() elev.goToStateIdle() go func() { for { newFloor := <-floorEvent elev.NewFloorReached(newFloor) } }() fmt.Printf("fsm: init done at floor %d, NewElevator returned\n\n", elev.Floor()) return &elev }