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 --position 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.
#
# NOTE: The Tic's control mode must be "Serial / I2C / USB"
# in order to set the target position over USB.

import subprocess
import yaml

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

status = yaml.safe_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 and Python 3 by running this command:

sudo apt install python3 python3-yaml

In an MSYS2 MINGW environment, run:

pacman --needed -S $MINGW_PACKAGE_PREFIX-python3{,-pip}
pip3 install pyyaml

For other systems, you should make sure you have Python 3 and pip installed, and then try running:

pip3 install pyyaml

If your system does not have pip3, you should try pip instead, but be aware that it might install packages for the Python 2 instead of Python 3. If your system does not have pip or pip3, 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
Tic 36v4 USB Multi-Interface High-Power Stepper Motor Controller (Connectors Soldered)
Tic 36v4 USB Multi-Interface High-Power Stepper Motor Controller

Related Categories

Tic Stepper Motor Controllers
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors