12. Writing PC software to control the Tic
- 12.1. Example code to run ticcmd in C
- 12.2. Example code to run ticcmd in Ruby
- 12.3. Example code to run ticcmd in Python
- 12.4. Running ticcmd with Windows shortcuts
- 12.5. Example serial code for Linux and macOS in C
- 12.6. Example serial code for Windows in C
- 12.7. Example serial code in Python
- 12.8. Example I²C code for Linux in C
- 12.9. Example I²C code for Linux in Python
- 12.10. Example code using the C API
- 12.11. Example code using the C++ API
This section is about writing computer software to control the Tic.
Picking an interface
First, you should decide which interface of the Tic your software will talk to: serial, I²C, or USB. The USB interface is the most natural interface to use for PC software since most computers have USB ports, and we provide Tic software that uses the USB interface. However, if your computer has a serial or I²C port, or you want to control the Tic over a radio link, you might decide not to use the USB interface. All three of these interfaces support nearly the same set of commands, which are documented in Section 8. Most of this section is about using the USB interface.
Picking a USB API
If you decide to use the USB interface to communicate with the Tic, the next thing to do is decide which API you want to use to access it.
Almost every operating system has its own API for accessing USB devices, so you could use the native USB API of your operating system. You would need to decide which commands you are going to send to the Tic by reading Section 8, then figure out how to encode those commands for the Tic’s USB interface by reading Section 11, and finally figure out how to send those USB commands using the API you have chosen by reading the documentation of that API. These APIs are typically meant for C programs, so you have to carefully manage pointers and memory allocation yourself. However, you might be able to find an API wrapper in the language of your choice that takes care of managing pointers for you.
Another option is to use a USB abstraction library like libusbp or libusb. These libraries abstract away the differences between USB APIs for the different operating systems they support, so you can write code that works on multiple operating systems. You would still have to read the documentation of the Tic’s commands and USB protocol, read the documentation of the API you have chosen, and carefully manage pointers. However, you might be able to find a library wrapper in the language of your choice that manages pointers for you.
In our Tic software package, we provide a C library called libpololu-tic that uses libusbp to talk to the Tic. You can compile it as a static or shared library, and it has a stable C API. The library takes care of the details of encoding the Tic’s USB commands so that you don’t have to know much about the Tic’s USB interface—you just have to call the appropriate function for each command you want to send. The library is designed to be used from C or C++, and you can access it from almost any other language if you write the appropriate foreign function interface (FFI) code. You would need to read the documentation of the library in the
include/tic.h file to understand how to use the library, and carefully manage pointers.
In general, the easiest way to write software to control the Tic over USB is to install the Tic software and then invoke the Tic Command-line Utility (ticcmd), which is built on top of libpololu-tic. You can run
ticcmd in a command prompt with no arguments to see what arguments the program supports. For example, to send an Exit Safe Start command and tell the Tic to travel to a particular position, you could run
ticcmd --exit-safe-start --position 1234. If ticcmd is installed correctly, then its folder should be listed in your PATH environment variable, meaning that other programs can find it and run it without knowing exactly where it is. The ticcmd utility takes care of all the details of finding the Tic you want to talk to, encoding your command properly for the Tic’s USB interface, sending the command, and then cleaning up after itself. If an error happens, ticcmd will print an error message to its standard error pipe and return a non-zero exit code. Your software can look at the non-zero exit code to detect if an error happened, or just ignore the error. For more information about getting started with
ticcmd, see Section 4.4.
Resources from the community
Please note that these resources are of varying quality and most are not tested or supported by Pololu.
- pytic: Python code that uses PyUSB to access the Tic’s USB interface. It was written by customer Dan Tyrrell.
- PyTic: A Python wrapper for libpololu-tic that provides access to most features of the C API. It was written by customer Daniel Castelli of the Allen Institute.
- TicDotNet: A C# .NET example that uses LibUsbDotNet to access the Tic’s USB interface. It was written by customer jigarciacortazar.