Wiki source code of Basic design

Version 16.1 by sna on 2014/08/13 02:41

Hide last authors
nbw 4.1 1
2
3 {{toc/}}
4
nbw 1.1 5 We compose our railway controller from several train controllers, combined with controllers for mutual exclusion.
6
7 == Mutual Exclusion ==
8
9 === Normal segments ===
10
nbw 3.1 11 **For each block** we generate one mutex controller, similar to the following pattern.
nbw 1.1 12
13 [[image:attach:Mutex.png]]
14
nbw 3.1 15 **For each controlled train** one state and the corresponding transitions are added.
nbw 1.1 16
nbw 6.1 17 A train **must** signal the wish to enter a segment by setting the variable **bool <segment>_req[trainNum] **to true.
18 The right to enter a segement is given to the train by setting the variable **int <segment>_perm** to the train number.
csp 9.1 19 After leaving a segment the train **must** remove its request by setting <segment>_req[trainNum] to false again.
nbw 6.1 20
csp 9.1 21 For a free track the *_perm variable is set to -1 and all tracks, apart from starting positions, are initialized with -1. For the starting positions the *_perm variables are set to the corresponding train number and the *_req variable for the train is set to true.
nbw 1.1 22
nbw 3.1 23 **A train must not enter a segment or alter the settings of a segment without holding the lock for the segment. No exceptions from this rule are allowed.**
nbw 1.1 24
25 === Kicking Horse Pass ===
26
nbw 3.1 27 To prevent collisions on the track, a special controller manages the rights to enter or leave the Kicking Horse Pass.
nbw 1.1 28
29 [[image:attach:Mutex_KH.png]]
30
nbw 3.1 31 The controller splits the pass into two parts, //left// and //right//, corresponding to the track layout chart. [[(Simplified track layout)>>url:http://www.informatik.uni-kiel.de/~~railway/Downloads/kscheme.pdf||shape="rect"]]
nbw 1.1 32
csp 9.1 33 Each part has two operational modes, //in// and //out//, being active when trains are allowed to either enter or leave the pass. When entering the pass the controller counts the entering trains and only lets two trains enter (one after the other). When leaving the pass the trains are removed from the counter, freeing space for the next train.
nbw 3.1 34
35 == Train controller ==
36
krat 14.1 37 Each train controller is set in a separate region with a referenced state. This state has the following interface:
nbw 3.1 38
39
40 \\\\\\\\
41
42 {{{    input int *_perm;         # All permission variables (Tracks) input bool *_perm # KH permission variables   input int trainNum;    input bool cleanup; input bool debug; output bool *_req[]; # All request variables}}}
43
krat 14.1 44 The train controller is composed of several //Station-2-Station// controllers. These are combined to form a complete schedule. Additionally, the cleanup signal has to be watched to abort the schedule and return back to the initial position.
nbw 3.1 45
46 == Station-2-Station controller ==
47
krat 14.1 48 Each Station-2-Station controller realizes the movement from one of the stations (IC,OC,KH) to another station. All controllers using IC or OC parts have to respect the traveling directions. For the Kicking Horse Pass two separate controllers, forwards and backwards, are used.
nbw 3.1 49
krat 14.1 50 The controllers starting from Kicking Horse Pass Station make an assumption of the direction of the train. These are dependent on the directions of the inner or outer circle, e.g. the KHIC controller starts backwards because this is the only valid direction to travel this path. To drive a train from the Kicking Horse Station (facing forward) to the Inner Circle we have to combine the KHOC and OCIC controllers.
nbw 4.1 51
52 [[image:attach:ICIC.png]]
53
nbw 5.1 54 When arriving on a station the train controller **must** first call the function //void railArrival(int train, int station)//. This starts the waiting timer for the train.
55 Next the train **must** wait for// int railDeparture(int train)// to return 1.
56 After the waiting has finished the controller can reach a final state and pass the control back to the train controller.
sna 10.1 57
sna 15.1 58 === Structure  ===
sna 10.1 59
sna 15.1 60 {{code linenumbers="true" language="sct"}}
61 //
62 // Structure of a Station-2-Station controller from Station * to Station *
63 //
64 scchart STST " * to * Controller " {
sna 10.1 65
sna 15.1 66 // Set of permission variables for required tracks
67 input int *_perm;
sna 10.1 68
sna 15.1 69 // Set of request variables for required tracks for 11 trains
70 output bool *_req[11];
sna 10.1 71
sna 15.1 72 // Train number
73 input int trainNum;
sna 10.1 74
sna 15.1 75 // Number of the departure track in a station
76 input int depTrack;
sna 10.1 77
sna 15.1 78 // Number of the destination track in a station
79 input int destTrack;
sna 10.1 80
sna 15.1 81 // Cleanup flag for selecting the destination track
82 input bool cleanup;
sna 10.1 83
sna 15.1 84 // Debug flag for additional output
85 input bool debug;
sna 10.1 86
sna 15.1 87 // Arrival track
88 output int arrTrack;
sna 10.1 89
sna 15.1 90 // Variable with value for arrTrack for selecting correct station elements
91 int i_arrOnTrack;
sna 10.1 92
sna 15.1 93 // Handles departing from a station *
94 initial state *_ST {
95 // hostcode call for additional output when debug
96 entry debug / 'println([trainNum][ST-ST] ... )';
sna 10.1 97
sna 15.1 98 // State, which sets requests for needed tracks
99 initial state waitForPerm {
100 entry / *_ST_4_req[trainNum] = true;
101 entry / *_LN_0_req[trainNum] = true;
102 }
103 // Transition is taken, if all permissions are received
104 --> gotPerm with (*_ST_4_perm == trainNum) & (*_LN_0_perm == trainNum)
105 // Transition is taken, if some (not all) permissions are received
106 --> backOff with (*_ST_4_perm == trainNum) | (*_LN_0_perm == trainNum);
sna 10.1 107
sna 15.1 108 // State for waiting an additional tick when not all permissions are received
109 state backOff
110 --> backOff1;
sna 10.1 111
sna 15.1 112 // State, which releases the requests for needed tracks
113 state backOff1 {
114 entry / *_ST_4_req[trainNum] = false;
115 entry / *_LN_0_req[trainNum] = false;
116 }
117 // Transition to repeat requesting permissions procedure
118 --> waitForPerm;
sna 10.1 119
sna 15.1 120 final state gotPerm;
121 }
122 // Transition to the departure state
123 >-> Dep_*_ST;
sna 10.1 124
sna 15.1 125 // State, which handles the departure of a train
126 state Dep_*_ST {
127 // Set of entry-Actions with hostcode calls to set tracks, points and signals according to depTrack
128 entry / 'railPoint(*,STRAIGHT)';
129 entry / 'railSignal(*_LN_0, FWD, RED)';
130 entry / 'railTrack(*_LN_0,FWD,trainNum,NORMAL)';
131 entry / 'railTrack(*_ST_4,FWD,trainNum,NORMAL)';
132 entry depTrack == 1 / 'railSignal(*_ST_1, FWD, GREEN)';
133 entry depTrack == 2 / 'railSignal(*_ST_2, FWD, GREEN)';
134 entry depTrack == 3 / 'railSignal(*_ST_3, FWD, GREEN)';
135 //...
136 // Transition to next track segment, if contact is triggered
137 } --> *_LN_0 with 'railContact(*_LN_0,0)';
sna 10.1 138
139
140
sna 15.1 141 // .....................................................................................
142 // Set of track segment controlling states such as follows
143 // .....................................................................................
sna 10.1 144
sna 15.1 145 // Transition to next track segment, if contact is triggered
146 state *_LN_0 {
147 // Hostcode calls for outputs
148 entry / 'println("[trainNum][ST-ST] Entering *_LN_0")';
149 entry debug / 'println("[trainNum][ST-ST] Requesting permission for *_LN_1")';
150 // Entry-Actions with hostcode calls to set previous signal according to depTrack to RED
151 entry depTrack == 1 / 'railSignal(*_ST_1, FWD, RED)';
152 entry depTrack == 2 / 'railSignal(*_ST_2, FWD, RED)';
153 entry depTrack == 3 / 'railSignal(*_ST_3, FWD, RED)';
154 // Requesting the next track segment
155 entry / *_LN_1_req[trainNum] = true;
sna 10.1 156
sna 15.1 157 // Region for handling train driving
158 region Travel:
159 initial state Entry
160 // Transition to continuing state, if permitted
161 --> Continue with 'railContact(*_LN_0,0)' & (*_LN_1_perm == trainNum)
162 // Transition to slowing down else
163 --> Slowdown with 'railContact(*_LN_0,0)';
sna 10.1 164
sna 15.1 165 // State for slowing down the train
166 state Slowdown {
167 entry debug / 'println("[trainNum][ST-ST] Slowing down on *_LN_0")';
168 // Entry-Action with hostcode calls for slowing down the train
169 entry / 'railTrack(*_LN_0,FWD,trainNum,CAUTION)';
170 }
171 // Transition to waiting state
172 --> Waiting with 'railContact(*_LN_0,1)'
173 // Transition to continuing state, if permitted
174 --> Continue with *_LN_1_perm == trainNum;
sna 10.1 175
sna 15.1 176 // State for train waiting on permission
177 state Waiting {
178 entry debug / 'println("[trainNum][ST-ST] Stopping on *_LN_0")';
179 // Entry-Action with hostcode call for stopping the train
180 entry / 'railTrackBrake(*_LN_0)';
181 }
182 --> Continue with *_LN_1_perm == trainNum;
sna 10.1 183
sna 15.1 184 // State to continuing driving on the track
185 final state Continue {
186 entry debug / 'println("[trainNum][ST-ST] Continuing on *_LN_0")';
187 // Entry-Actions with hostcode calls to set tracks and signals for driving
188 entry / 'railSignal(*_LN_0,FWD,GREEN)';
189 entry / 'railTrack(*_LN_0,FWD,trainNum,NORMAL)';
190 entry / 'railTrack(*_LN_1,FWD,trainNum,NORMAL)';
191 entry / 'railSignal(*_LN_1, FWD, RED)';
192 };
sna 10.1 193
sna 15.1 194 // Region for handling cleanup functionalities
195 region Cleanup:
196 initial state Entry
197 // Transition to cleanup state
198 --> cleanup with 'railContact(*_LN_0,0)';
sna 10.1 199
sna 15.1 200 // State for cleaning up the previous track segments
201 final state cleanup {
202 entry debug / 'println("[trainNum][ST-ST] Entered *_LN_0 completely")';
203 // Entry-Action with hostcode call to switching off the previous track
204 entry / 'railTrackOff(*_ST_4)';
205 // Entry-Action to release the previous track
206 entry / *_ST_4_req[trainNum] = false;
207 };
208 // Transition to transitional state
209 }>-> *_LN_0_*_LN_1;
sna 10.1 210
sna 15.1 211 state *_LN_0_*_LN_1
212 // Transition to next track segment, if contact is triggered
213 --> *_LN_1 with 'railContact(*_LN_1,0)';
sna 10.1 214
sna 15.1 215 // ..................................................................................
sna 10.1 216
217
218
sna 15.1 219 // State for entering a station
220 state *_LN_5 {
221 // Variable for checking all needed permissions
222 int perm_all_next_segments = false;
223 entry / 'println("[trainNum][ST-ST] Entering *_LN_5")';
224 entry / 'railSignal(*_LN_4, FWD, RED)';
sna 10.1 225
sna 15.1 226 // Region for handling train driving such as above,
227 // only with perm_all_next_segments for permitting more than one track
228 region Travel:
229 initial state Entry
230 --> Continue with 'railContact(*_LN_5,0)' & perm_all_next_segments
231 --> Slowdown with 'railContact(*_LN_5,0)';
sna 10.1 232
sna 15.1 233 state Slowdown {
234 entry debug / 'println("[trainNum][ST-ST] Slowing down on *_LN_5")';
235 entry / 'railTrack(*_LN_5,FWD,trainNum,CAUTION)';
236 }
237 --> Waiting with 'railContact(*_LN_5,1)'
238 --> Continue with perm_all_next_segments;
sna 10.1 239
sna 15.1 240 state Waiting {
241 entry debug / 'println("[trainNum][ST-ST] Stopping on *_LN_5")';
242 entry / 'railTrackBrake(*_LN_5)';
243 }
244 --> Continue with perm_all_next_segments;
sna 10.1 245
sna 15.1 246 final state Continue {
247 entry debug / 'println("[trainNum][ST-ST] Continuing on *_LN_5")';
248 entry i_arrOnTrack == 1 / 'railTrack(*_ST_1,FWD,trainNum,NORMAL)';
249 entry i_arrOnTrack == 2 / 'railTrack(*_ST_2,FWD,trainNum,NORMAL)';
250 entry i_arrOnTrack == 3 / 'railTrack(*_ST_3,FWD,trainNum,NORMAL)';
251 //...
252 entry / arrTrack = i_arrOnTrack;
253 };
sna 10.1 254
sna 15.1 255 // Region for handling cleanup-functionalities such as above
256 region Cleanup:
257 initial state Entry
258 --> cleanup with 'railContact(*_LN_5,0)';
sna 10.1 259
sna 15.1 260 final state cleanup {
261 entry debug / 'println("[trainNum][ST-ST] Entered *_LN_5 completely")';
262 entry / 'railTrackOff(*_LN_4)';
263 entry / *_LN_4_req[trainNum] = false;
264 };
sna 10.1 265
sna 15.1 266 // Region for handling permissions of all needed tracks
267 region Permissions:
268 // State for requesting all needed tracks according to destination track and cleanup-Flag
269 initial state checking {
270 entry / *_ST_0_req[trainNum] = true;
271 entry destTrack == 1 | !cleanup / *_ST_1_req[trainNum] = true;
272 entry destTrack == 2 | !cleanup / *_ST_2_req[trainNum] = true;
273 entry destTrack == 3 | !cleanup / *_ST_3_req[trainNum] = true;
274 }
275 // Transitions for permitted tracks match wished tracks
276 --> success with destTrack == 1 & *_ST_0_perm == trainNum & *_ST_1_perm == trainNum / i_arrOnTrack = 1
277 --> success with destTrack == 2 & *_ST_0_perm == trainNum & *_ST_2_perm == trainNum / i_arrOnTrack = 2
278 --> success with destTrack == 3 & *_ST_0_perm == trainNum & *_ST_3_perm == trainNum / i_arrOnTrack = 3
279 // Transitions for permitted tracks don't match wished tracks
280 --> success with *_ST_0_perm == trainNum & *_ST_1_perm == trainNum / i_arrOnTrack = 1
281 --> success with *_ST_0_perm == trainNum & *_ST_2_perm == trainNum / i_arrOnTrack = 2
282 --> success with *_ST_0_perm == trainNum & *_ST_3_perm == trainNum / i_arrOnTrack = 3
283 // Transition for not all tracks permitted
284 --> resolving with *_ST_0_perm == trainNum | *_ST_3_perm == trainNum | *_ST_2_perm == trainNum | *_ST_1_perm == trainNum;
sna 10.1 285
sna 15.1 286 // State for waiting an additional tick
287 state resolving
288 --> resolving1;
289
290 // State for releasing track requests
291 state resolving1 {
292 entry / *_ST_0_req[trainNum] = false;
293 entry / *_ST_1_req[trainNum] = false;
294 entry / *_ST_2_req[trainNum] = false;
295 entry / *_ST_3_req[trainNum] = false;
296 }
297 // Transition for trying the requesting again
298 --> checking;
sna 10.1 299
sna 15.1 300 // State for waiting an additional tick
301 state success
302 --> success1;
sna 10.1 303
sna 15.1 304 // State for releasing not used track requests
305 final state success1 {
306 entry !(i_arrOnTrack == 1) / *_ST_1_req[trainNum] = false;
307 entry !(i_arrOnTrack == 2) / *_ST_2_req[trainNum] = false;
308 entry !(i_arrOnTrack == 3) / *_ST_3_req[trainNum] = false;
309 // Settting perm_all_next_segments to true
310 entry / perm_all_next_segments = true;
311 };
312 // Transition to station entry states
313 }>-> *_LN_5_*_ST;
sna 10.1 314
sna 15.1 315 // State waiting for station entry
316 state *_LN_5_*_ST
317 --> Arr_*_ST with i_arrOnTrack == 1 & 'railContact(*_ST_1,0)'
sna 16.1 318  --> Arr_*_ST with i_arrOnTrack == 2 & 'railContact(*_ST_2,0)'
sna 15.1 319 --> Arr_*_ST with i_arrOnTrack == 3 & 'railContact(*_ST_3,0)';
sna 10.1 320
sna 15.1 321 // State for setting tracks, points and signals according to i_arrOnTrack
322 // and releasing previous track request
323 state Arr_*_ST {
324 entry / 'railSignal(*_LN_5, FWD, RED)';
325 entry / 'railTrackOff(*_LN_5)';
326 entry / 'railTrack(*_ST_0,FWD,trainNum,SLOW)';
327 entry i_arrOnTrack == 1 / 'railTrack(*_ST_1,FWD,trainNum,SLOW)';
328 entry i_arrOnTrack == 2 / 'railTrack(*_ST_2,FWD,trainNum,SLOW)';
329 entry i_arrOnTrack == 3 / 'railTrack(*_ST_3,FWD,trainNum,SLOW)';
330 entry / *_LN_5_req[trainNum] = false;
sna 10.1 331
sna 15.1 332 initial state SlowEntry
333 --> Slow with i_arrOnTrack == 1 & 'railContact(*_ST_1,0)'
334 --> Slow with i_arrOnTrack == 2 & 'railContact(*_ST_2,0)'
335 --> Slow with i_arrOnTrack == 3 & 'railContact(*_ST_3,0)';
sna 10.1 336
sna 15.1 337 // State for switching off previous track and releasing the request
338 state Slow {
339 entry / 'railTrackOff(*_ST_0)';
340 entry / *_ST_0_req[trainNum] = false;
341 }
342 // Transitions to halt state, when train is at second contact of a track segment
343 --> Halt with i_arrOnTrack == 1 & 'railContact(*_ST_1,1)'
344 --> Halt with i_arrOnTrack == 2 & 'railContact(*_ST_2,1)'
345 --> Halt with i_arrOnTrack == 3 & 'railContact(*_ST_3,1)';
sna 10.1 346
sna 15.1 347
348 final state Halt {
349 // Entry-Actions for braking the train on correct track
350 entry i_arrOnTrack == 1 / 'railTrackBrake(*_ST_1)';
351 entry i_arrOnTrack == 2 / 'railTrackBrake(*_ST_2)';
352 entry i_arrOnTrack == 3 / 'railTrackBrake(*_ST_3)';
353 // Entry-Actions for waiting for timer on correct track
354 entry i_arrOnTrack == 1 / 'railArrival(trainNum, *_ST_1)';
355 entry i_arrOnTrack == 2 / 'railArrival(trainNum, *_ST_2)';
356 entry i_arrOnTrack == 3 / 'railArrival(trainNum, *_ST_3)';
357 };
358 }
359 >-> done;
sna 10.1 360
sna 15.1 361 state done
362 // Transition to final state, if timer is ready
363 --> reallyDone with 'railDeparture(trainNum)';
sna 10.1 364
sna 15.1 365 final state reallyDone;
366 }
367 {{/code}}