Sunday 30 April 2017

TrafficControl: Refactoring the Train::move() function - Train State Model

To handle the logic for the train movements, I've defined some states that the trains will always be in. The trains will typicallly move clockwise in the state model:
The transitions that are reprosented by green arrows are more complex and may require their own methods.
The states are:
  • Opening: Train is at a station and is opening the doors.
  • Loading: Train is at a station with doors open and letting passengers enter and leave the train.
  • Closing:  Train is at a station and is closing the doors.
  • Waiting: Train is at a station with closed doors and is waiting for the departure time.
  • Ready: Train is at a track or station and is waiting for permission to leave.
  • Running: Train is moving on a track.
  • Passing: Train is moving on a track and has permission to enter the next track
  • Emergency: Train must stop immediately (or at next station, if in a tunnel).
  • Stopped: Train is not allowed to move anymore (end of TravelPlan or after Emergency state).
Each state will have a corresponding method.


n is a counter that each train is assigned every tick from the trafficClock class. It counts down and is returned from the functions. Once it reaches zero, the train will wait for the next tick from the trafficClock. 

I'll describe the trafficClock in a late blog post.

For the transitions between the states, I'll define specific methods if they are needed. In the picture above, the arrows are representing the transitions.

Tuesday 25 April 2017

TrafficControl: Live Traffic Websites

Two web sites are presenting real-time information of public transportation:

Tågkartan has an impressive map showing the current train status, including delays for trains in Sweden. To do this, Tågkartan is combining data sources such as
  • train info and train plan from Trafikverket (The Swedish Transport Administration), 
  • user GPS data from the app TågTavlan, 
  • a railway database
The other website, Livemap covers large parts of the world, and not only trains but also buses, trams, ferrys, subways and more. That map seems to calculate positions of the vehicles based on time tables and not consider delays. Also, it is interpolating straight lines between the stops. Given the huge amount of vehicles it covers, it would be heavier to follow the busses on more accurate trajectories.

The blog Cornucopia? discussed that app in a blog post covering the poor public transit services in smaller towns close to Göteborg.

Where will my program be in relation to Livemap and Tågkartan? As stated before, the purpose of TrafficControl is for me to learn programming. As a simulation tool, TrafficControl will not consider actual train scenarios.

There are also some other differences from the applications:
  • TrafficControl is currently a desktop application. It will maybe be migrated to a web service in the future.
  • TrafficControl will simulate the number of passengers on trains and stations.
  • TrafficControl will allow the user to create non-existing tracks and stations.
  • With TrafficControl, it will be possible to simulate different rules when scheduling trains.
Edit: LiveMap actually considers delays, but it depends on the transport operator. Also, some lines contain more detailed trajectories.

Thursday 20 April 2017

TrafficControl: Refactoring the Train::move() function

There are some issues with the code in the tcTrain part of the program. The code needs to be refactored and when the train has reached the end of the TravelPlan, it is still looking for the next station.

Since the bug was easy to spot and fix, I've solved that first before refactoring. If I would have considered the bug to be difficult to solve, I would have considered refactoring first and debugging later.

The tcTrain::move() method covers some 170 lines of code that needs to be broken up to different methods.

First, I'll review the existing methods for tcTrain:


The tcTrain:move() method is based on some different states:


That method will call some new methods:

  • tcTrain;:waitingState
  • tcTrain::readyState
  • tcTrain::runningState
  • tcTrain::openingState
  • tcTrain::loadingState
  • tcTrain::closingState
  • and also some other helper methods/functions.

I'll remove the method tcTrain::getRouteVectorTracks().

The first step will be porting the code to the new methods without breaking anything...

Monday 10 April 2017

Trains: Walking the Tracks

First of all: Don't ever walk on a railway track where there are active trains. Looking at a time table isn't enough - trains can be late and several trains don't follow time tables.

No trains will pass some time.
Over a hundred and twenty years ago, a railway company, MöToJ (Malmö-Tomelilla Järnväg) was founded. They operated passenger traffic until 1970 and freight traffic some more years. 

Formally, the line is still in use but in order to use it, track needs to be upgraded and renovated. Trafikverket (the Swedish Ministry for Transportation) has a paused project for that work. http://www.trafikverket.se/nara-dig/skane/projekt-i-skane-lan/simrishamn_malmo-simrishamnsbanan/

The railway track that goes from Östervärn, Malmö to Staffanstorp is deactivated. Most of the tracks are remaining but the tracks are removed at road intersections so it will take some time until trains are seen on that track.
My walk started from Lund and ended in Malmö. Five kilometers along non-functional tracks.
One afternoon after work, I walked home from my work, a distance of some 20 km. Five of them along Staffanstorpsbanan.
Nordanå railway station.
Along the trail, I saw some stations that I've missed adding. Nordanå is one of those stations.




It will take lots of work to make the track useful again, and I can understand that it is hard to motivate renovating the line from an economical viewpoint. Raising power connectors, groundworks and replacing tracks will be too expensive.
Neither passengers nor trains will accept the gaps between the rails.

Sege Station, another station that I forgot adding.

The railway enters the eastern parts of Malmö through a golf course.

Sunday 2 April 2017

TrafficControl: Debugging Train::move Method (part 2)

After re-testing with more debug prints, I've made an observation:

INFO : TRAIN SET MODEL recognised with Model= "X41" . To be implemented!
INFO : TRAIN SET STATION recognised with Station= "Lund_C" . Check that the station exists.
INFO : TRAIN SET DESIRED SPEED recognised with DesiredSpeed= "145"
IMPORT : "Gunnesbo" found. Adding to travelplan
ERROR : Train::addTrackToTrainRoute Error: negative number specified. 0 : "Gunnesbo"<<The stationID 0 is a valid ID.
INFO : "Marie_Curie" has 100 seats. 0 are taken.
: Train is at station "Lund_C"
: TrainRoute, index: 0 :

The program shall check whether the stationID is smaller than zero (invalid value). The bug is that the program is checking whether the stationID is smaller or equal to zero. The new condition is (stationID < 0)


Another issue has been seen: The check for end of travel plans doesn't seem to work:

INFO : "Ada_Lovelace" has reached end of travelplan. tp_s : 2 pos: 1<<For this particular event, the train hasn't reached the end of the travel plan.
INFO : "Marie_Curie" has reached end of travelplan. tp_s : 2 pos: 1
INFO : "Marie_Curie" has reached end of travelplan. tp_s : 2 pos: 1

This new issue will be debugged in a later blog post.