func Run_Elevator(localStatusCh chan Elevator, sendBtnCallCh chan [2]int, receiveAllBtnCallsCh chan [Driver.NUMFLOORS][Driver.NUMBUTTONS - 1]bool, setLights_setExtBtnsCh chan [4][2]bool, errorCh chan int) { if Driver.ElevGetFloorSensorSignal() == -1 { fsm_onInitBetweenFloors() } fsm_elevatorUninitialized() fmt.Println("Elev ID: ", elevator.Elev_ID) running := true var prev_button [Driver.NUMFLOORS][Driver.NUMBUTTONS]int var prev_floor int prev_floor = Driver.ElevGetFloorSensorSignal() go Update_ExtBtnCallsInElevControl(setLights_setExtBtnsCh) go checkElevMoving(errorCh) update_status_count := 0 update_lights_count := 0 localStatusCh <- elevator for running { for f := 0; f < Driver.NUMFLOORS; f++ { for b := 0; b < Driver.NUMBUTTONS; b++ { v := Driver.ElevGetButtonSignal(b, f) if v&int(v) != prev_button[f][b] { if fsm_onRequestButtonPress(f, Button(b), sendBtnCallCh) { //Hvis true er det innvendig knappetrykk temp_Behaviour := elevator.Behaviour fsm_onNewActiveRequest(f, Button(b)) if temp_Behaviour != elevator.Behaviour { localStatusCh <- elevator } } else { fsm_SendNewOrderToMaster(f, Button(b), sendBtnCallCh) } } prev_button[f][b] = v } } f := Driver.ElevGetFloorSensorSignal() if f != -1 { if f != prev_floor { fsm_onFloorArrival(f) localStatusCh <- elevator } } prev_floor = f if timer_timedOut() { fsm_onDoorTimeout() timer_stop() localStatusCh <- elevator } if update_status_count == 50 { localStatusCh <- elevator update_status_count = 0 } if update_lights_count == 10 { setLights_setExtBtnsCh <- allExtBtns update_lights_count = 0 } time.Sleep(25 * time.Millisecond) update_status_count += 1 update_lights_count += 1 } }
func initMain() { driver.ElevInit() driver.ElevSetMotorDirection(1) var previousFloor = -1 for { if driver.ElevGetFloorSensorSignal() >= 0 { driver.ElevSetMotorDirection(0) previousFloor = driver.ElevGetFloorSensorSignal() fmt.Println("I've reached floor", previousFloor) break } } }
func RequestButtons() { for { var previousRequests = [driver.NUMFLOORS][3]bool{} for floor := 0; floor < driver.NUMFLOORS; floor++ { for btn := 0; btn < 3; btn++ { buttonPressed := driver.ElevGetButtonSignal(btn, floor) if buttonPressed && buttonPressed != previousRequests[floor][btn] { if btn != int(INSIDEBUTTON) { if driver.ElevGetFloorSensorSignal() == floor { FsmOnOrderReceived(floor, ButtonType(btn)) } else { E.LocalRequests[floor][btn] = true } } else { E.MasterOrders[floor][btn] = true FsmOnOrderReceived(floor, ButtonType(btn)) } //FsmOnOrderReceived(floor, ButtonType(btn)) //move to orderReceived } previousRequests[floor][btn] = buttonPressed } } time.Sleep(25 * time.Millisecond) } }
func fsm_onInitBetweenFloors() { Driver.ElevSetMotorDirection(int(D_Down)) elevator.Dir = D_Down elevator.Behaviour = EB_Moving lastFloorTime = time.Now().Unix() for Driver.ElevGetFloorSensorSignal() == -1 { } Driver.ElevSetMotorDirection(int(D_Idle)) }
func init() { driver.ElevInit() driver.ElevSetMotorDirection(1) for { if driver.ElevGetFloorSensorSignal() >= 0 { driver.ElevSetMotorDirection(0) break } } }
func fsm_elevatorUninitialized() { elevator.Dir = D_Idle elevator.Behaviour = EB_Idle elevator.Floor = Driver.ElevGetFloorSensorSignal() elevator.Elev_ID = GetActiveTime() for f := 0; f < Driver.NUMFLOORS; f++ { for b := 0; b < Driver.NUMBUTTONS; b++ { elevator.Requests[f][b] = false } } }
func FloorSensor() { for { var previousFloor = E.Floor floorSensor := driver.ElevGetFloorSensorSignal() if floorSensor != -1 && floorSensor != previousFloor { FsmOnFloorArrival(floorSensor) } previousFloor = floorSensor time.Sleep(25 * time.Millisecond) } }