-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.go
93 lines (61 loc) · 1.41 KB
/
fsm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import "./elevio"
import "sync"
import "net"
import "fmt"
import "time"
func fsm_run_elev(newOrder <-chan Button, floorReached chan int, orderDone chan<- Button) {
doorTime := time.NewTimer(3*time.Second)
var e ElevState
var dir MotorDirection := ChooseDirection(e)
var state STATES
var order[4][3]int
for{
select{
case newOrder := <- newOrder:
switch state {
case IDLE:
if dir == 0 {
state = DOOR_OPEN
elevio.SetDoorOpenLamp(1)
time.Sleep(3*time.Second)
elevio.SetDoorOpenLamp(0)
state = IDLE
}
else{
SetMotorDirection(dir)
state = MOVING
}
case MOVING:
case DOOR_OPEN:
if e.Floor == newOrder.Floor {
elevio.SetDoorOpenLamp(1)
time.Sleep(3*time.Second)
elevio.SetDoorOpenLamp(0)
state = IDLE
}
case MOTOR_STOP:
}
case floorArrival := <- floorReached:
switch state {
case IDLE:
case MOVING:
if ShouldStop(e)
e = ClearAtCurrentFloor(e, func(btn int){ orderDone <- Button{e.Floor, btn} })
state = DOOR_OPEN
SetMotorDirection(0)
doorTime.Reset(3*time.Second)
elevio.SetDoorOpenLamp(1)
// some door shit goes here
// maybe a timer idk
case INIT:
SetMotorDirection(0)
}
case doorTimeOut := <- doorTime.C:
switch state {
case DOOR_OPEN:
elevio.SetDoorOpenLamp(0)
orderDone <- true
}
}
}
}