12.11. Example code using the C API

<p>The code below uses the C API provided by the <a href="https://github.com/pololu/pololu-tic-software">Tic software</a> to send and receive data from a Tic over USB. This code is written in C and works on Windows, Linux, and macOS. For a very similar example using the C++ API, see <a href="/docs/0J71/12.12">Section 12.12</a>.</p> <p>If you have compiled the Tic software from source and installed it on a Unix-like system (including MSYS2 on Windows), and copied the code below to a file named <code>code.c</code>, you should be able to compile the code below by running this command in a shell:</p> <pre>&#x000A;gcc code.c $(pkg-config libpololu-tic-1 --cflags --libs)</pre> <p>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&#8217;s command timeout feature: by default, the Tic&#8217;s &#8220;Command timeout&#8221; error will happen if it does not receive certain commands periodically (see <a href="/docs/0J71/5.4">Section 5.4</a> for details), causing the motor to stop. You can send a &#8220;Reset command timeout&#8221; command every second to get around this, or you can disable the command timeout feature using the Tic Control Center: uncheck the &#8220;Enable command timeout&#8221; checkbox in the &#8220;Serial&#8221; box.</p> <pre name="code" class="c">&#x000A;// Uses the Tic's C API to send and receive data from a Tic.&#x000A;// NOTE: The Tic's control mode must be "Serial / I2C / USB".&#x000A;&#x000A;#include &lt;stdio.h&gt;&#x000A;#include &lt;stdint.h&gt;&#x000A;#include &lt;string.h&gt;&#x000A;#include &lt;tic.h&gt;&#x000A;&#x000A;bool handle_error(tic_error * error)&#x000A;{&#x000A; if (error == NULL) { return false; }&#x000A; fprintf(stderr, "Error: %s\n", tic_error_get_message(error));&#x000A; tic_error_free(error);&#x000A; return true;&#x000A;}&#x000A;&#x000A;// Opens a handle to a Tic that can be used for communication.&#x000A;//&#x000A;// To open a handle to any Tic:&#x000A;// tic_handle * handle = open_handle(NULL);&#x000A;// To open a handle to the Tic with serial number 01234567:&#x000A;// tic_handle * handle = open_handle("01234567");&#x000A;tic_handle * open_handle(const char * desired_serial_number)&#x000A;{&#x000A; tic_handle * handle = NULL;&#x000A;&#x000A; // Get a list of Tic devices connected via USB.&#x000A; tic_device ** list = NULL;&#x000A; size_t count = 0;&#x000A; tic_error * error = tic_list_connected_devices(&amp;list, &amp;count);&#x000A; if (handle_error(error)) { goto cleanup; }&#x000A;&#x000A; // Iterate through the list and select one device.&#x000A; tic_device * device = NULL;&#x000A; for (size_t i = 0; i &lt; count; i++)&#x000A; {&#x000A; tic_device * candidate = list[i];&#x000A;&#x000A; if (desired_serial_number)&#x000A; {&#x000A; const char * serial_number = tic_device_get_serial_number(candidate);&#x000A; if (strcmp(serial_number, desired_serial_number))&#x000A; {&#x000A; // Found a device with the wrong serial number, so continue on to&#x000A; // the next device in the list.&#x000A; continue;&#x000A; }&#x000A; }&#x000A;&#x000A; // Select this device as the one we want to connect to, and break&#x000A; // out of the loop.&#x000A; device = candidate;&#x000A; break;&#x000A; }&#x000A;&#x000A; if (device == NULL)&#x000A; {&#x000A; fprintf(stderr, "Error: No device found.\n");&#x000A; goto cleanup;&#x000A; }&#x000A;&#x000A; error = tic_handle_open(device, &amp;handle);&#x000A; if (handle_error(error)) { goto cleanup; }&#x000A;&#x000A;cleanup:&#x000A; for (size_t i = 0; i &lt; count; i++)&#x000A; {&#x000A; tic_device_free(list[i]);&#x000A; }&#x000A; tic_list_free(list);&#x000A; return handle;&#x000A;}&#x000A;&#x000A;int main()&#x000A;{&#x000A; int exit_code = 1;&#x000A; tic_handle * handle = NULL;&#x000A; tic_variables * variables = NULL;&#x000A;&#x000A; handle = open_handle(NULL);&#x000A; if (handle == NULL) { goto cleanup; }&#x000A;&#x000A; tic_error * error = tic_get_variables(handle, &amp;variables, false);&#x000A; if (handle_error(error)) { goto cleanup; }&#x000A;&#x000A; int32_t position = tic_variables_get_current_position(variables);&#x000A; printf("Current position is %d.\n", position);&#x000A;&#x000A; int32_t new_target = position &gt; 0 ? -200 : 200;&#x000A; printf("Setting target position to %d.\n", new_target);&#x000A;&#x000A; error = tic_exit_safe_start(handle);&#x000A; if (handle_error(error)) { goto cleanup; }&#x000A;&#x000A; error = tic_set_target_position(handle, new_target);&#x000A; if (handle_error(error)) { goto cleanup; }&#x000A;&#x000A; exit_code = 0; // This program ran successfully.&#x000A;&#x000A;cleanup:&#x000A; // Free the resources used by the variables.&#x000A; tic_variables_free(variables);&#x000A;&#x000A; // Call tic_handle_close() to free its resources and because Windows only&#x000A; // allows one open handle per device.&#x000A; // (Though, in this program, we are about to return from main, so the program&#x000A; // will exit and the operating system will do this for us very soon.)&#x000A; tic_handle_close(handle);&#x000A;&#x000A; return exit_code;&#x000A;}</pre>

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