Exemple #1
0
func main() {
	eventCh := def.EventChan{
		FloorReached: make(chan int),
		DoorTimeout:  make(chan bool),
		DeadElevator: make(chan int, 10),
	}
	hwCh := def.HardwareChan{
		MotorDir:       make(chan int, 10),
		FloorLamp:      make(chan int, 10),
		DoorLamp:       make(chan bool, 10),
		BtnPressed:     make(chan def.BtnPress, 10),
		DoorTimerReset: make(chan bool, 10),
	}
	msgCh := def.MessageChan{
		Outgoing:  make(chan def.Message, 10),
		Incoming:  make(chan def.Message, 10),
		CostReply: make(chan def.Message, 10),
		NumOnline: make(chan int),
	}

	//Initialization
	startFloor := hw.Init()
	fsm.Init(eventCh, hwCh, startFloor)
	network.Init(msgCh.Outgoing, msgCh.Incoming)
	q.RunBackup(msgCh.Outgoing)

	go EventHandler(eventCh, msgCh, hwCh)
	go assigner.CollectCosts(msgCh.CostReply, msgCh.NumOnline)
	go safeKill()

	hold := make(chan bool)
	<-hold
}
Exemple #2
0
func main() {
	if _, err := os.Open(config.QUEUE_FILENAME); err == nil {
		BackupHold()
		queue.FileRead(config.QUEUE_FILENAME)
		network.Init(ch_outgoing_msg, ch_incoming_msg, ch_new_elev, ch_main_alive)
	} else {
		network.Init(ch_outgoing_msg, ch_incoming_msg, ch_new_elev, ch_main_alive)
		time.Sleep(time.Millisecond)
		if _, err := os.Create(config.QUEUE_FILENAME); err != nil {
			log.Fatal("FATAL: Could not create queue file!")
		}
	}
	backup := exec.Command("gnome-terminal", "-x", "sh", "-c", "go run main/main.go")
	backup.Run()

	if !hardware.Init() {
		log.Fatal("Unable to initialize elevator hardware!")
	}
	hardware.SetMotorDirection(fsm.ChooseNewDirection())
	go MessageServer()
	go hardware.ReadButtons(ch_button_pressed)
	go hardware.SetLights()
	go hardware.FloorPoller(ch_floor_poll)
	go StateSpammer()
	go fsm.EventOrderReceived()
	fsm.Init(ch_outgoing_msg, ch_new_order)

	log.Printf("Elev addr: %s", config.Laddr)

	for {
		select {
		case button := <-ch_button_pressed:
			ch_new_order <- button
			if button.Button_type != config.BUTTON_COMMAND {
				ch_outgoing_msg <- config.Message{Msg_type: config.ADD_ORDER, Button: button}
			}
		case floor := <-ch_floor_poll:
			fsm.EventReachedFloor(floor)
		}
	}
}
func main() {
	var myStateV ElevatorState
	myState := &myStateV
	for _, order := range myState.ExternalOrders {
		order[0] = false
		order[1] = false
	}
	for n, _ := range myState.InternalOrders {
		myState.InternalOrders[n] = false
	}
	myState.printState()
	// Initialize the hardware module and the channel to message with it.
	buttonChannel := make(chan hardware.ButtonEvent, 1) // Channel to receive buttonEvents
	lightChannel := make(chan hardware.LightEvent, 3)   // Channel to send driver.LightEvents
	motorChannel := make(chan int, 1)                   // Channel to send MotorEvents
	floorChannel := make(chan hardware.FloorEvent, 1)   // Channel to receive floorEvents
	doorTimer := time.NewTimer(5 * time.Second)         // Door timer
	doorTimer.Stop()
	polldelay := time.Duration(10 * time.Millisecond)
	fmt.Println("ONEELEVATOR:\t Polling delay set to: %d", polldelay)

	defer func() {
		motorChannel <- typedef.DIR_STOP
	}()

	err := hardware.Init(buttonChannel, lightChannel, motorChannel, floorChannel, polldelay) // Starts the hardware polling loop.
	if err != nil {
		fmt.Println("Error initializing hardware..")
		return
	}

	// ----------------------  WAIT FOR EVENTS! -------------------------
	for {
		select {
		case buttonEvent := <-buttonChannel:
			// A event has been sendt to us on the button channel.
			bType := buttonEvent.ButtonType
			if bType == typedef.BUTTON_CALL_UP || bType == typedef.BUTTON_CALL_DOWN || bType == typedef.BUTTON_COMMAND {
				fmt.Printf("ONEELEVATOR:\t New Order, floor: %d, buttonType: %d\n", buttonEvent.Floor, buttonEvent.ButtonType)
				runNow := !myState.haveOrders()
				fmt.Printf("ONEELEVATOR:\t Do we have other orders? \t %t\n", !runNow)
				lightChannel <- hardware.LightEvent{LightType: bType, Floor: buttonEvent.Floor, Value: true}
				if bType == typedef.BUTTON_COMMAND {
					myState.InternalOrders[buttonEvent.Floor] = true
				} else {
					myState.ExternalOrders[buttonEvent.Floor][bType] = true
				}
				if runNow {
					fmt.Println("RUNNOW: value - true")
					// We have no orders, exequte this one immediately.
					if buttonEvent.Floor > myState.Lastfloor {
						// We are going up!
						myState.Direction = typedef.DIR_UP
						myState.Moving = true
						motorChannel <- typedef.DIR_UP
					} else if buttonEvent.Floor < myState.Lastfloor {
						// We are going down.
						myState.Direction = typedef.DIR_DOWN
						myState.Moving = true
						motorChannel <- typedef.DIR_DOWN
					} else {
						// Ordered at current floor.
						motorChannel <- typedef.DIR_STOP
						lightChannel <- hardware.LightEvent{LightType: typedef.DOOR_LAMP, Value: true}
						doorTimer.Reset(5 * time.Second)
						myState.Moving = false
						myState.setOpenDoor(true)
					}

				}

			} else if bType == typedef.BUTTON_STOP {
				fmt.Printf("ONEELEVATOR:\t Received stop button event, value:\t%t\n", buttonEvent.Value)
				if buttonEvent.Value {
					motorChannel <- typedef.DIR_STOP
					lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_STOP, Value: true}
					myState.Moving = false
				} else {
					motorChannel <- myState.Direction
					lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_STOP, Value: false}
					myState.Moving = true
				}

			}
			myState.printState()
		case floorEvent := <-floorChannel:
			fmt.Printf("ONEELEVATOR:\t At floor: %d, direction: %d\n", floorEvent.Floor, myState.Direction)
			myState.setLastFloor(floorEvent.Floor)
			// Initialization between floors.
			if myState.Direction == typedef.DIR_STOP {
				fmt.Printf("ONEELEVATOR:\t Stopping\n")
				motorChannel <- typedef.DIR_STOP
				myState.Moving = false
			}

			if myState.shouldStop() {
				fmt.Printf("ONEELEVATOR:\t Stopping at this floor, direction: %d opening door for 3 sec\n", myState.Direction)
				// Turn off lights and clear orders.
				if myState.Direction == typedef.DIR_UP {
					lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_CALL_UP, Floor: floorEvent.Floor, Value: false}
					lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_COMMAND, Floor: floorEvent.Floor, Value: false}
					myState.InternalOrders[floorEvent.Floor] = false
					myState.ExternalOrders[floorEvent.Floor][1] = false

					// Check if we are turning around.
					if b := myState.nextDirection(); b == typedef.DIR_DOWN || b == typedef.DIR_STOP {
						lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_CALL_DOWN, Floor: floorEvent.Floor, Value: false}
						myState.ExternalOrders[floorEvent.Floor][0] = false
					}

				} else if myState.Direction == typedef.DIR_DOWN {
					lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_CALL_DOWN, Value: false}
					lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_COMMAND, Value: false}
					myState.InternalOrders[floorEvent.Floor] = false
					myState.ExternalOrders[floorEvent.Floor][0] = false

					// Check if we are turning around.
					if b := myState.nextDirection(); b == typedef.DIR_UP || b == typedef.DIR_STOP {
						lightChannel <- hardware.LightEvent{LightType: typedef.BUTTON_CALL_UP, Floor: floorEvent.Floor, Value: false}
						myState.ExternalOrders[floorEvent.Floor][1] = false
					}
				}
				motorChannel <- typedef.DIR_STOP
				doorTimer.Reset(3 * time.Second)
				lightChannel <- hardware.LightEvent{LightType: typedef.DOOR_LAMP, Value: true}
				myState.Moving = false
				myState.setOpenDoor(true)
			}
			myState.printState()

		case <-doorTimer.C:
			myState.setOpenDoor(false)
			lightChannel <- hardware.LightEvent{LightType: typedef.DOOR_LAMP, Value: false}
			fmt.Printf("ONEELEVATOR:\t Door timeout.\n")
			if b := myState.haveOrders(); !b {
				fmt.Printf("ONEELEVATOR:\t No orders, staying at floor.\n")
				myState.setDirection(typedef.DIR_STOP)
			} else if myState.Direction == typedef.DIR_UP && myState.haveOrderAbove() {
				fmt.Println("ONEELEVATOR:\t Have order above, and direction upwards.. Continuing.")
				myState.setMoving(true)
				motorChannel <- typedef.DIR_UP
			} else if myState.Direction == typedef.DIR_DOWN && myState.haveOrderBelow() {
				fmt.Println("ONEELEVATOR:\t Have order above, and direction downwards.. Continuing.")
				myState.setMoving(true)
				motorChannel <- typedef.DIR_DOWN
			} else if myState.haveOrderBelow() {
				fmt.Println("ONEELEVATOR:\t Have order below.. Go down.")
				myState.setMoving(true)
				motorChannel <- typedef.DIR_DOWN
				myState.setDirection(typedef.DIR_DOWN)

			} else if myState.haveOrderAbove() {
				fmt.Println("ONEELEVATOR:\t Have order above.. Go up.")
				myState.setMoving(true)
				motorChannel <- typedef.DIR_UP
				myState.setDirection(typedef.DIR_UP)
			}
		}
		myState.printState()
	}
}