Documentation > nControl > Train Automation Course
Train Sensor - Reverse
For nControl™ 2019.2 or higher

What does this project do?
When the train passes in front of the sensor, it will stop the train for 3 seconds and then restart in the opposite direction.
What will you need to build this project?
  • 1x LEGO® PF train and a 4DBrix™ WiFi train controller   or   1x LEGO® PU train and a BLE112 dongle
  • a loop of LEGO® train tracks
  • 1x 4DBrix™ IR train detection sensor and USB controller for the sensor
  • nControl 2019.2 or higher
What will we focus on?
  • The if statement
  • The use of constants
Project File
Building the Set-up
This project uses the exact same set-up as the Train Sensor - Stop project.
Introduction
In this project we have to check the direction the train was going before it stopped and then restart the train in the opposite direction. This means that depending on the direction the train was going we will have to either call the .goForward() or the .goBackward() function. So the function we have to call depends on the value of a condition, i.e. the direction the train was going. This can be done with the if statement.
If Statement
The if statement enables you to execute different operations in function of a specific condition.
In Python™ the if statement has the following syntax1:
if condition:
   execute this code block when the condition is met
else:
   execute this code block when the condition is not met
There are two blocks of code, one to be execute when the condition is met and one for when the condition is not met. These blocks of code have to be indented, meaning that you have to add spaces in front of them so the code is moved to the right. Python™ is very strict with indentation because it's an integral part of the language syntax: it determines the end of the conditional code. Consider the following program:
if condition:
   ...
else:
   execute this line when the condition is not met
   execute this line when the condition is not met
   execute this line when the condition is not met
always execute this line
The first three lines that follow the else keyword are executed with the condition is not met. The forth line is always executed because it's not part of the else code block as it's not indented. The indentation thus defines the end of the conditional code and changing the indentation will change the behavior of your program.
When you start programming indenting your code might seem cumbersome. However, maintaining a proper indentation is fundamental in keeping your code structured and readable. The fact that Python™ forces you to use indentation makes it an excellent language for beginners, as it creates the habit of properly indenting you code. There is no fixed number of spaces you should use to indent your code. Most programmers use 2, 3 or 4 spaces; the nControl™ examples are all indented with 3 spaces.
To conclude, note that the else section is optional. The following code is perfectly fine:
if condition:
   executed when the condition is met
always executed
Writing the Code
Just like in the previous sensor projects, we start by creating an object to access the train control panel.
train = self.getTile('Train 1')
Besides the train power, we also need to store the direction in which the train is going:
trainPower = train.getPower()
trainDirection = train.getDirection()
Now that we have stored all the required information we can stop the train:
train.stop()
self.disable()
self.sleep(3)
We can use the following code to restart the train in the opposite direction:
if trainDirection == nConst.TC_FORWARD:
   train.goBackward(trainPower)
else:
   train.goForward(trainPower)
Let's analyze this block of code.
The first line specifies the condition; it compares the value of trainDirection with the value of nConst.TC_FORWARD. The former is the variable that contains the direction our train was going, the latter is a constant2 that has the value that represents 'going forward'.
So when trainDirection equals nConst.TC_FORWARD the train was going forward. That also means that when this condition is met, we have to restart the train going backward, which explains line 2. When the condition is not met (line 3), we have to restart the train going forward (line 4).
Note that we used two equality signs in the condition: trainDirection == nConst.ST_FORWARD. In Python™ == is the comparison operator, it compare two values. Single equality signs assign a value to a variable. So,
A == B
compares the values of A and B, while
A = B
assigns the value of B to A.
Using only one equality sign in a condition is a typical beginner's mistake. If you use A = B as condition it will always be met because it's not comparing the values, it's assigning the value of B to A. So if your else code block is not executed even when your condition is not met, check your code and make sure there are two equals signs in your condition.
The only thing we still have to do is re-enabling the sensor after the train is gone.
self.enabled()
This gives the following trigger event script:
#Get the train object
train = self.getTile('Train 1')

#Get the train power and direction
trainPower = train.getPower()
trainDirection = train.getDirection()

#Stop the train
train.stop()

#Disable the sensor
self.disable()

#Wait 3 seconds before restarting the train
self.sleep(3)
if trainDirection == nConst.TC_FORWARD:
   train.goBackward(trainPower)
else:
   train.goForward(trainPower)

#Re-enable the sensor
self.enable()
Validating the Script in Simulation Mode
Before running our script with an actual LEGO® train, we're going to test it in simulation mode.
Set the power of the train to a certain value, click the sensor tile and check if it restart the train in the opposite direction.

Running the Project with a LEGO® Train
Once the script works as expected in simulation mode you can run it with an actual train. Your train should stop for 3 seconds every time it passes in front of the sensor and then restart in the opposite direction. Change the speed while it's going and see if the sensor restarts the train with the modified power settings.

What Did We Learn?
In this project we learned:
  • We used an if statement to select which function had to be executed.
  • We compared the value of a variable with a constant.

1Python™ also has an elif: condition, but this is outside the scope of this example.
2A constant is similar to a variable with the main difference that the value of a constant does not change.
Documentation > nControl > Train Automation Course > Next Project: Traffic Light - Stop