func (elevinf *Elevatorinfo) SetEvent() { currentFloor := elevdriver.GetFloor() switch { case elevdriver.GetStopButton() && elevinf.state != EMERGENCY: elevinf.event = STOP case elevdriver.GetObs(): elevinf.event = OBSTRUCTION case elevinf.DetermineDirection() != 2 && elevinf.state != ASCENDING && elevinf.state != DECENDING: elevinf.event = ORDER case currentFloor != -1: elevinf.event = SENSOR elevinf.last_floor = currentFloor default: elevinf.event = NO_EVENT } fmt.Printf("updated event\n") }
func (elevinf *Elevatorinfo) statemachineEmergency() { // for elevinf.state == EMERGENCY { switch elevinf.event { case ORDER: for i := 0; i < 4; i++ { if elevinf.internal_orders[i][2] == 1 { elevdriver.ClearStopButton() if elevinf.DetermineDirection() != 2 && elevdriver.GetFloor() == -1 { for elevdriver.GetFloor() == -1 { StartMotor(-1) elevinf.ReceiveOrders() elevinf.CheckLights() if elevinf.StopAtCurrentFloor() == 2 { elevinf.StopMotor() elevinf.state = OPEN_DOOR elevinf.DeleteOrders() break } if elevdriver.GetStopButton() || elevdriver.GetObs() { elevinf.StopButtonPushed() elevinf.state = EMERGENCY break } } break } if elevinf.DetermineDirection() == -2 { elevinf.state = OPEN_DOOR } else if elevinf.DetermineDirection() == -1 { elevinf.state = DECENDING StartMotor(-1) } else if elevinf.DetermineDirection() == 1 { elevinf.state = ASCENDING StartMotor(1) } } } case STOP: case OBSTRUCTION: case SENSOR: case NO_EVENT: } time.Sleep(1 * time.Millisecond) // } }
func (elevinf *Elevatorinfo) statemachineOpendoor() { switch elevinf.event { case ORDER: fmt.Printf("The door is open\n") elevdriver.SetDoor() elevinf.DeleteOrders() for i := 0; i < 300; i++ { if elevdriver.GetFloor() == -1 && elevdriver.GetObs() == false { elevdriver.ClearDoor() elevinf.state = IDLE break } else if elevdriver.GetStopButton() == true { elevinf.StopButtonPushed() elevinf.state = EMERGENCY break } elevinf.ReceiveOrders() elevinf.CheckLights() time.Sleep(10 * time.Millisecond) if elevdriver.GetObs() == true { fmt.Printf("Obstruction detected, door staying open\n") i = 0 } } elevdriver.ClearDoor() elevinf.DeleteOrders() fmt.Printf("Door cleared\n") if elevinf.DetermineDirection() == -2 { elevinf.state = OPEN_DOOR } else if elevinf.DetermineDirection() == -1 { elevinf.state = DECENDING StartMotor(-1) } else if elevinf.DetermineDirection() == 1 { elevinf.state = ASCENDING StartMotor(1) } else if elevinf.DetermineDirection() == 2 { elevinf.state = IDLE } case STOP: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case OBSTRUCTION: case SENSOR: fmt.Printf("Elevator reached floor %d\n", elevdriver.GetFloor()) elevdriver.SetDoor() for i := 0; i < 300; i++ { if elevdriver.GetFloor() == -1 && elevdriver.GetObs() == false { elevdriver.ClearDoor() elevinf.state = IDLE break } else if elevdriver.GetStopButton() == true { elevinf.StopButtonPushed() elevinf.state = EMERGENCY break } elevinf.ReceiveOrders() elevinf.CheckLights() if elevdriver.GetObs() == true { i = 0 } } elevinf.DeleteOrders() elevdriver.ClearDoor() if elevinf.DetermineDirection() == -2 { elevinf.state = OPEN_DOOR } else if elevinf.DetermineDirection() == -1 { elevinf.state = DECENDING StartMotor(-1) } else if elevinf.DetermineDirection() == 1 { elevinf.state = ASCENDING StartMotor(1) } else if elevinf.DetermineDirection() == 2 { elevinf.state = IDLE } case NO_EVENT: } }