func FloorIndicator() { for { if elevdriver.GetFloor() > 0 { elevdriver.SetFloor(elevdriver.GetFloor()) } time.Sleep(100 * time.Millisecond) } }
func (elevinf *Elevatorinfo) statemachineAscending() { time.Sleep(1 * time.Millisecond) switch elevinf.event { case ORDER: case STOP: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case OBSTRUCTION: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case SENSOR: fmt.Printf("FLOOR DETECTED\n") if elevinf.StopAtCurrentFloor() == 1 { fmt.Printf("GOING TO STOP HERE\n") elevinf.StopMotor() elevinf.state = OPEN_DOOR elevinf.DeleteOrders() break } else if elevdriver.GetFloor() == 4 { elevinf.StopMotor() elevinf.state = IDLE } case NO_EVENT: } }
func (elevinf *Elevatorinfo) statemachineIdle() { switch elevinf.event { case ORDER: if elevinf.DetermineDirection() != 2 && elevdriver.GetFloor() == -1 { elevinf.Initiate() elevinf.state = IDLE break } if elevinf.DetermineDirection() == -2 { elevinf.state = OPEN_DOOR } else if elevinf.DetermineDirection() == -1 { elevinf.state = DECENDING StartMotor(-1) } else if elevinf.DetermineDirection() == 1 { /*if elevdriver.GetFloor() == -1 { elevinf.Initiate() elevinf.state = IDLE break }*/ elevinf.state = ASCENDING StartMotor(1) } case STOP: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case OBSTRUCTION: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case SENSOR: case NO_EVENT: } }
func (elevinf *Elevatorinfo) statemachineAscending() { // for elevinf.state == ASCENDING { time.Sleep(1 * time.Millisecond) switch elevinf.event { case ORDER: case STOP: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case OBSTRUCTION: elevinf.StopButtonPushed() elevinf.state = EMERGENCY case SENSOR: // Destination reached || someone wants to go UP || no orders above DECENDING fmt.Printf("FLOOR DETECTED\n") if elevinf.StopAtCurrentFloor() == 1 { fmt.Printf("GOING TO STOP HERE\n") elevinf.StopMotor() elevinf.state = OPEN_DOOR elevinf.DeleteOrders() break } else if elevdriver.GetFloor() == 4 { elevinf.StopMotor() elevinf.state = IDLE } case NO_EVENT: } // } }
func (elevinf *Elevatorinfo) DetermineDirection() int { // The elevators "brain", choosing which way to go depending on direction and orders current_floor := elevdriver.GetFloor() orders_over := 0 orders_under := 0 orders_at_current := 0 for i := 0; i < 4; i++ { for j := 0; j < 3; j++ { if elevinf.internal_orders[i][j] == 1 && i < current_floor-1 { orders_under++ } else if elevinf.internal_orders[i][j] == 1 && i > current_floor-1 { orders_over++ } else if elevinf.internal_orders[i][j] == 1 && i == current_floor-1 { orders_at_current++ } } } if orders_at_current > 0 { return -2 //Stay at floor } else if (orders_under > 0 && elevinf.last_direction == 2) || (orders_under > 0 && orders_over == 0) { return -1 //Keep going down } else if (orders_over > 0 && elevinf.last_direction == 1) || (orders_over > 0 && orders_under == 0) { return 1 //Keep going up } else { return 2 //No orders, no direction } return 0 }
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 turnAround() { for { floor := elevdriver.GetFloor() go elevdriver.SetFloor(floor) switch floor { case 1: go elevdriver.MotorUp() case 4: go elevdriver.MotorDown() } } }
func (elevinf *Elevatorinfo) Initiate() { fmt.Printf("Halla\n") elevdriver.Init() fmt.Printf("Balle\n") StartMotor(-1) elevinf.last_direction = 2 for elevdriver.GetFloor() == -1 { } elevinf.StopMotor() fmt.Printf("Elevator initiation complete!\n") }
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) DetermineDirection() int { // The elevators "brain", choosing which way to go depending on direction and orders current_floor := elevdriver.GetFloor() last_dir := elevinf.last_direction - 1 orders_over := 0 orders_under := 0 orders_at_current := 0 for i := 0; i < 4; i++ { for j := 0; j < 3; j++ { if elevinf.internal_orders[i][j] == 1 && i < current_floor-1 { orders_under++ } else if elevinf.internal_orders[i][j] == 1 && i > current_floor-1 { orders_over++ } else if elevinf.internal_orders[i][j] == 1 && i == current_floor-1 { orders_at_current++ fmt.Printf("%dorders, %dfloor, %dbutton\n", orders_at_current, i, j) } } } if orders_at_current > 0 && (elevinf.internal_orders[current_floor-1][2] == 1 || elevinf.external_orders[current_floor-1][last_dir] == 1) { fmt.Printf("Stay at floor\n") return -2 //Stay at floor } else if (orders_under > 0 && elevinf.last_direction == 2) || (orders_under > 0 && orders_over == 0) { fmt.Printf("blabla1\n") return -1 //Keep going down } else if (orders_over > 0 && elevinf.last_direction == 1) || (orders_over > 0 && orders_under == 0) { fmt.Printf("blabla1\n") return 1 //Keep going up } else { fmt.Printf("two is returned, do nothing\n") return 2 //No orders, no direction } return 2 }
func (elevinf *Elevatorinfo) DeleteOrders() { if elevdriver.GetFloor() == 1 { for i := 0; i < 3; i++ { elevinf.internal_orders[0][i] = 0 elevinf.external_orders[0][0] = 0 go ExternalSendDelete(0, 0) } } else if elevdriver.GetFloor() == 2 && elevinf.last_direction == UP { for i := 0; i < 3; i = i + 2 { elevinf.internal_orders[1][i] = 0 elevinf.external_orders[1][0] = 0 go ExternalSendDelete(1, 0) } } else if elevdriver.GetFloor() == 2 && elevinf.last_direction == DOWN { for i := 1; i < 3; i++ { elevinf.internal_orders[1][i] = 0 elevinf.external_orders[1][1] = 0 go ExternalSendDelete(1, 1) } } else if elevdriver.GetFloor() == 3 && elevinf.last_direction == UP { for i := 0; i < 3; i = i + 2 { elevinf.internal_orders[2][i] = 0 elevinf.external_orders[2][0] = 0 go ExternalSendDelete(2, 0) } } else if elevdriver.GetFloor() == 3 && elevinf.last_direction == DOWN { for i := 1; i < 3; i++ { elevinf.internal_orders[2][i] = 0 elevinf.external_orders[2][1] = 0 go ExternalSendDelete(2, 1) } } else if elevdriver.GetFloor() == 4 { for i := 0; i < 3; i++ { elevinf.internal_orders[3][i] = 0 elevinf.external_orders[3][1] = 0 go ExternalSendDelete(3, 1) } } }
func (elevinf *Elevatorinfo) DeleteOrders() { fmt.Printf("gonna delete...\n") if elevdriver.GetFloor() == 1 { for i := 0; i < 3; i++ { elevinf.internal_orders[0][i] = 0 elevinf.external_orders[0][0] = 0 go ExternalSendDelete(0, 0) } } else if elevdriver.GetFloor() == 2 && elevinf.last_direction == UP { for i := 0; i < 3; i = i + 2 { elevinf.internal_orders[1][i] = 0 elevinf.external_orders[1][0] = 0 if elevinf.NoOrdersAbove() { elevinf.external_orders[1][1] = 0 elevinf.internal_orders[1][1] = 0 go ExternalSendDelete(1, 1) } go ExternalSendDelete(1, 0) } } else if elevdriver.GetFloor() == 2 && elevinf.last_direction == DOWN { for i := 1; i < 3; i++ { elevinf.internal_orders[1][i] = 0 elevinf.external_orders[1][1] = 0 if elevinf.NoOrdersBelow() { fmt.Printf("Deleting order KNUUUT\n") elevinf.internal_orders[1][0] = 0 elevinf.external_orders[1][0] = 0 go ExternalSendDelete(1, 0) } go ExternalSendDelete(1, 1) } } else if elevdriver.GetFloor() == 3 && elevinf.last_direction == UP { for i := 0; i < 3; i = i + 2 { elevinf.internal_orders[2][i] = 0 elevinf.external_orders[2][0] = 0 if elevinf.NoOrdersAbove() { elevinf.external_orders[2][1] = 0 elevinf.internal_orders[2][1] = 0 go ExternalSendDelete(2, 1) } go ExternalSendDelete(2, 0) } } else if elevdriver.GetFloor() == 3 && elevinf.last_direction == DOWN { for i := 1; i < 3; i++ { elevinf.internal_orders[2][i] = 0 elevinf.external_orders[2][1] = 0 if elevinf.NoOrdersBelow() { elevinf.external_orders[2][0] = 0 elevinf.internal_orders[2][0] = 0 go ExternalSendDelete(2, 0) } go ExternalSendDelete(2, 1) } } else if elevdriver.GetFloor() == 4 { for i := 0; i < 3; i++ { elevinf.internal_orders[3][i] = 0 elevinf.external_orders[3][1] = 0 go ExternalSendDelete(3, 1) } } }
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: } }