Documentation > nControl > API Reference
Train Control Tile
nControl™ 2021.0
This tile controls a train.

Introduction
The train control tile communicates with the 4DBrix WiFi train controller. It does not support custom scripts but you can control the train by calling train control tile functions from the event scripts of other tiles, for example: as sensor tile can make a train stop when it detects it.
Event Scripts
Train control tiles support the following event scripts:
  • Activate tile event: is executed when the tile is activated by switching to simulation of operational mode.
  • Deactivate tile event: is executed when the tile is deactivated by switching to design of configuration mode.
  • Mouse click tile event: is executed when the user clicks on the tile.
  • MQTT message tile event: is executed when the tile receives an MQTT message.
API Functions
Tile Specific Functions
nTrainControlTile.clickTile(zone)
Simulates a mouse click on the tile; this executes the mouse click event script. The argument zone specifies the mouse click zone where the virtual click appeared:
  • zone = 2: simulates a click on the horn button
  • zone = 3: simulates a click on the light button
  • zone = 4: simulates a click on the backward button
  • zone = 5: simulates a click on the forward button
  • zone = 6: simulates a click on the stop button
  • zone = 7: simulates a click on the - button
  • zone = 8: simulates a click on the + button
nTrainControlTile.getDirection()
Returns direction in which the train is going:
  • nConst.TC_STOP = the train is standing still
  • nConst.TC_FORWARD = the train is going forward
  • nConst.TC_BACKWARD = the train is going backward
nTrainControlTile.getLastPower()
Returns the motor power before it was changed the last time; expressed in percentage [0-100]. This function can, for example, be used to get what the motor power was before you stopped the train.
nTrainControlTile.getLightState()
Returns the status of the lights:
  • True = the lights are on
  • False = the lights are off
nTrainControlTile.getMessageData()
Returns the contents of the MQTT message.
nTrainControlTile.getMessageTopic()
Returns the topic of the MQTT message.
nTrainControlTile.getPower()
Returns the current motor power, expressed in percentage [0-100].
nTrainControlTile.goBackward(power)
Makes the train go backward using the specified motor power. The power is expressed in percentage: [0-100].
nTrainControlTile.goForward(power)
Makes the train go forward using the specified motor power. The power is expressed in percentage: [0-100].
nTrainControlTile.goRandom(power, forward=1, backward=1)
Makes the train go in a random direction using the specified motor power.
  • power specifies the motor power expressed as a percentage: [0-100].
  • forward specifies the weighting coefficient of the chance the train goes forward
  • backward specifies the weighting coefficient of the chance the train goes backward
The chance that the train goes in a specific direction can be fine-tuned with the forward and backward arguments. For example, forward = 3 and backward = 1 means that, on average, 3 times out of four the train will go forward and 1 time of four the train will reverse.
nTrainControlTile.sendCommand(portId, command)
Sends a serial command to an auxiliary device.
  • portId specifies the port.
  • command specifies the command.
nTrainControlTile.setPort(portId, mode, baud=38400)
Configures the auxiliary devices ports.
  • portId specifies the port.
  • mode specifies the mode: nConst.PM_SERIALWRITE to configure the port as an serial output port, nConst.PM_DIGITALOUT to configure the port as a digital output port
  • baud specifies the baud rate in case of a serial output port
nTrainControlTile.setPower(power)
Sets the current motor power, expressed in percentage [0-100].
nTrainControlTile.setPowerRandom(lower=0, upper=100, step=1)
Sets the current motor power to a random value:
  • lower specifies the lower bound of the acceptable random power value
  • upper specifies the upper bound of the acceptable random power value
  • step specifies the resolution of the random power values
This command does not affect the direction (forward or backward) of the train, it just adjust the motor power.
nTrainControlTile.soundHorn()
Plays the tile's horn sound.
nTrainControlTile.stop(reversePower = 0, duration = 0)
Makes the train stop.
  • You simply switch off the motor using stop()
  • You use a reverse power pulse with the specified reversePower and duration: stop(reversePower, duration)
    reversePower is expressed in percentage [0-100] while duration is expressed in milliseconds [0-1000].
Inherited from nTile
nTrainControlTile.clearScreen()
Clear all the text in the console window.
nTrainControlTile.getTile(label)
Returns the tile object specified by label. If there is no tile with the specified label, the function returns a None value.
nTrainControlTile.print(text)
Prints text in the console window. If the console window it not yet visible, printing text will make it visible.
nTrainControlTile.showConsole(mode)
Shows (mode = True) or hides (mode = False) the console window.
nTrainControlTile.sleep(duration)
Stops the execution of the script for the the given number of seconds. Note that duration can be a floating point number, e.g. to suspend the script for half a seconds use self.sleep(0.5).
nTrainControlTile.timestamp()
Returns the number of seconds that passed since nControl™ was launched. It returns a floating point value with the highest possible time resolution supported by your system. The difference between two time stamps provides the number of seconds elapsed between those two events.
nTrainControlTile.localVars
Base property to which you can add local variables that retain their value after the event script finishes. For example self.localVars.myVar = 5 adds a new property myVar to self.localVars and gives it the value 5. The localVars property is local to the current tile meaning that each tile has its own independent localVars property.
nTrainControlTile.globalVars
Base property to which you can add global variables that retain their value after the event script finishes. For example self.globalVars.myVar = 5 adds a new property myVar to self.globalVars and gives it the value 5. The globalVars property is global to nControl meaning that all tiles use the same globalVars property. As such globalVars can be used to exchange information between the tiles.