-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFSM_w_target_flr.go
179 lines (151 loc) · 4.3 KB
/
FSM_w_target_flr.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package FSM
import (
"../driver"
"../structs"
"fmt"
"time"
"../localState"
"sync"
)
const stuck_period = 5 * time.Second
type FSM_state int
const (
idle = 0
//door_open = 1
moving = 1
stuck = -1
)
//Not necessary
var mu sync.Mutex
// Need to send state every time it changes
func FSM_init(floor_event <-chan int,
new_target_floor <-chan int,
floor_completed chan<- int,
elev_send_state chan<- structs.Elev_state) {
target_floor := -1
FSM_state := idle
stuck_timer := time.NewTimer(stuck_period)
stuck_timer.Stop()
for {
select {
case <-stuck_timer.C:
switch FSM_state {
case idle:
case moving:
fmt.Printf("FSM: Elevator timed out. State = stuck\n")
driver.SetMotorDirection(driver.DirnStop)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnStop)
elev_send_state <- ReadLocalState()
mu.Unlock()
FSM_state = stuck
case stuck:
}
case floor := <-new_target_floor:
if floor == target_floor {
break
}
target_floor = floor
fmt.Printf("FSM: New target floor is %d\n", target_floor+1)
switch FSM_state {
case idle:
if target_floor == -1 {
//fmt.Printf("FSM: No target floor\n")
} else {
stuck_timer.Reset(stuck_period)
if target_floor < localState.ReadLocalState().Last_passed_floor{
FSM_state = moving
driver.SetMotorDirection(driver.DirnDown)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnDown)
elev_send_state <- ReadLocalState()
mu.Unlock()
} else if target_floor > localState.ReadLocalState().Last_passed_floor {
FSM_state = moving
driver.SetMotorDirection(driver.DirnUp)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnUp)
elev_send_state <- ReadLocalState()
mu.Unlock()
} else {
driver.SetMotorDirection(driver.DirnStop)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnStop)
elev_send_state <- ReadLocalState()
mu.Unlock()
FSM_state = idle
floor_completed <- localState.ReadLocalState().Last_passed_floor
driver.OpenCloseDoor()
fmt.Print("FSM: [IDLE] Reached target floor\n")
}
}
case moving:
if target_floor == localState.ReadLocalState().Last_passed_floor {
driver.SetMotorDirection(driver.DirnStop)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnStop)
elev_send_state <- ReadLocalState()
mu.Unlock()
FSM_state = idle
floor_completed <- localState.ReadLocalState().Last_passed_floor
driver.OpenCloseDoor()
fmt.Print("FSM: [MOVING] Reached target floor\n")
}
case stuck:
fmt.Printf("FSM: [STUCK] \n")
}
case floor := <-floor_event:
driver.SetFloorIndicator(floor)
if (floor == 0) || (floor == driver.NumFloors-1) {
driver.SetMotorDirection(driver.DirnStop)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnStop)
elev_send_state <- ReadLocalState()
mu.Unlock()
}
mu.Lock()
localState.ChangeLocalState_flr(floor)
elev_send_state <- ReadLocalState()
mu.Unlock()
switch FSM_state {
case idle:
fmt.Printf("FSM: CASE [floor event]: STATE [idle], floor: %d\n", floor+1)
case moving:
fmt.Printf("FSM: CASE [floor event]: STATE [moving], floor: %d\n", floor+1)
stuck_timer.Reset(stuck_period)
mu.Lock() //Read current floor under mutex
state_flr := localState.ReadLocalState().Last_passed_floor
mu.Unlock()
if target_floor == -1 {
break
} else if target_floor < state_flr {
driver.SetMotorDirection(driver.DirnDown)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnDown)
elev_send_state <- ReadLocalState()
mu.Unlock()
} else if target_floor > state_flr {
driver.SetMotorDirection(driver.DirnUp)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnUp)
elev_send_state <- ReadLocalState()
mu.Unlock()
} else {
driver.SetMotorDirection(driver.DirnStop)
mu.Lock()
localState.ChangeLocalState_dir(driver.DirnStop)
elev_send_state <- ReadLocalState()
mu.Unlock()
stuck_timer.Stop()
FSM_state = idle
mu.Lock()
floor_completed <- localState.ReadLocalState().Last_passed_floor
mu.Unlock()
driver.OpenCloseDoor()
}
case stuck:
fmt.Printf("FSM: STATE [stuck]; cannot reach floor %d\n", floor+1)
}
}
}
}