With the simplified data structure, the control algorithm boils down to answering three critical questions: When does the train communicate with the CTC? How much space does it need to reserve to avoid collisions? And how long does the CTC take to set the switches?
1. The Inexact Communication Interval (The Telemetry Window)
Communication between the train and the CTC does not take place at an exact moment. A train reaches the end of a block at a given speed and, due to its mass, cannot brake abruptly. It needs space. In our algorithm, for simplicity, the train requires 1/2 of the length of a complete block to stop.
The process is triggered at the instant the train reaches the end of its current block and enters the next one. However, the CTC's response is neither instantaneous nor synchronous; it will arrive at some point later, while the train is travelling through the new block, and always before it reaches the next block boundary. If the response does not arrive in time, the train simply will not have permission to continue and will stop.
To realistically emulate this process occurring at any intermediate point, we postpone the transmission using a timeout and send it to the CTC through an asynchronous signal:
timeout sendTelemetry,2,D$N
...
...
procedure sendTelemetry
assign train,P$PARAM_A
assign status,{
train:P$train,
state:P$(state,P$train),
route:P$(route,P$train),
occupiedBlock:P$(block,P$train),
releasedBlock:P$(releasedBlock,P$train)
}
signal processTelemetry,X$ctc,V$status
terminate_ve
endprocedure
2. The N+2 Reservation Rule and the Return of Permissions
The CTC, for its part, must ensure that the train has that safety block available for braking if the track is blocked further ahead. Therefore, the algorithm looks two blocks ahead (N+2): it evaluates the subsequent block (nextNext), reserves any additional blocks required by the database constraints (such as the protective flanks "bloq"), and releases the blocks the train has already left.
After processing the request and assigning the available blocks to the train's requirements, the CTC generates a summary of that train's permissions and sends it back through an asynchronous signal:
call getTrainPermissions,P$train
signal trainReceiveOrders,P$train,V$getTrainPermissions
...
...
procedure getTrainPermissions
assign permissions,[]
FOREACH nblock, IN_OBJECT, V$(blocks)
assign occupiedBy,X$(blocks.P$nblock.data.occupiedBy)
if (P$occupiedBy == P$PARAM_A)
assign.push permissions, P$nblock
endif
ENDFOREACH
return V$permissions
endprocedure
3. Automated Switch Interlocking
In this asynchronous flow, infrastructure movement is directly linked to space authorization. At the very moment an occupancy permission is granted, the CTC will initiate the switching of the points required for the train to which it has just granted the route.
The switch moves as a logical consequence of the reservation being registered by the CTC. Once the train receives its array of authorized blocks through trainRecibeOrdenes, the switch will already be aligned in the correct position (position: 0 or 1), ensuring that the train performs its physical (trainUpdate) and visual updates without jumps or geometric misalignment.
This is the main reason why communication is asynchronous. It is not known how long the CTC will take to move the switches after receiving the request.