12.3. Example code to run ticcmd in Python

The example Python code below shows how to invoke the Tic Command-line Utility (ticcmd) to send and receive data from a Tic via USB. It demonstrates how to set the target position of the Tic using the --position option and how to read variables using the -s option. This code uses the PyYAML library to parse the output of ticcmd when reading data from the Tic. This code should work on any system with ticcmd, Python, and PyYAML.

The Tic’s control mode should be set to “Serial / I²C / USB”.

If you have multiple Tic devices connected to your computer via USB, you will need to use the -d option to specify the serial number of the device you want to use. For example, to set the target position to 200 on a Tic with serial number 00123456, you can run the command ticcmd -d 00123456 --target 200. You can run ticcmd --list in a shell to get the serial numbers of all the connected Tic devices.

If you want to set the target velocity instead of the target position, you can change the --position argument to --velocity. For a list of other options supported by the Tic Command-line Utility, run ticcmd with no arguments.

In the example below, the child ticcmd process uses the same error pipe as the Python process, so you will see any error messages printed by ticcmd if you run the Python program in a terminal. Additionally, if there is an error, Python’s subprocess.check_output method will detect it (by checking the ticcmd process exit status) and raise an exception.

You might notice that the Tic only performs the desired movement for about a second before it stops moving and the red LED turns on, indicating an error. This is because of the Tic’s command timeout feature: by default, the Tic’s “Command timeout” error will happen if it does not receive certain commands periodically (see Section 5.4 for details), causing the motor to stop. You can run ticcmd --reset-command-timeout every second to get around this, or you can disable the command timeout feature using the Tic Control Center: uncheck the “Enable command timeout” checkbox in the “Serial” box.

# Uses ticcmd to send and receive data from the Tic over USB.
# Works with either Python 2 or Python 3.
# NOTE: The Tic's control mode must be "Serial / I2C / USB".

import subprocess
import yaml

def ticcmd(*args):
  return subprocess.check_output(['ticcmd'] + list(args))

status = yaml.load(ticcmd('-s', '--full'))

position = status['Current position']
print("Current position is {}.".format(position))

new_target = -200 if position > 0 else 200
print("Setting target position to {}.".format(new_target))
ticcmd('--exit-safe-start', '--position', str(new_target))

PyYAML installation tips

If you run the code above and get the error “ImportError: No module named yaml” or “ModuleNotFoundError: No module named ‘yaml’”, it means that the PyYAML library is not installed.

On Raspbian, Ubuntu, and other Debian-based operating systems, you can install PyYAML for both Python 2 and Python 3 by running this command:

sudo apt-get install python-yaml python3-yaml

Alternatively, if your system has the pip, pip2, or pip3 command, you can use that to install the library. The correct command to use depends on how your system is set up and what version of Python you want to use, but it will most likely be one of the commands below:

pip install pyyaml
pip2 install pyyaml
pip3 install pyyaml

If your system does not have pip, pip2, or pip3 (like macOS and MSYS2), you can install PyYAML from source by following the download and installation instructions on the PyYAML web page.

Please note that PyYAML is only used for parsing the output of ticcmd in order to read data from the Tic. If you have trouble installing PyYAML but you do not actually need to read data from the Tic, you can simply remove the code that uses it. Also, the output of ticcmd is simple enough that you might consider writing your own functions to extract data from it instead of relying on a third-party library.

Related Products

Tic T825 USB Multi-Interface Stepper Motor Controller (Connectors Soldered)
Tic T825 USB Multi-Interface Stepper Motor Controller
Tic T834 USB Multi-Interface Stepper Motor Controller (Connectors Soldered)
Tic T834 USB Multi-Interface Stepper Motor Controller
Tic T500 USB Multi-Interface Stepper Motor Controller (Connectors Soldered)
Tic T500 USB Multi-Interface Stepper Motor Controller
Tic T249 USB Multi-Interface Stepper Motor Controller (Connectors Soldered)
Tic T249 USB Multi-Interface Stepper Motor Controller
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors