12.2. Example code to run ticcmd in Ruby

The example Ruby 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 YAML parser that comes with Ruby to parse the output of ticcmd when reading data from the Tic. This code should work on any system with ticcmd and Ruby.

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 Ruby process, so you will see any error messages printed by ticcmd if you run the Ruby program in a terminal. If you want to instead capture the standard error output so that you can use it in your Ruby program, you can change the code to use Ruby’s Open3.capture3 method.

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".

require 'open3'
require 'yaml'

def ticcmd(*args)
  command = 'ticcmd ' + args.join(' ')
  stdout, process_status = Open3.capture2(command)
  if !process_status.success?
    raise "Command failed with code #{process_status.exitstatus}: #{command}"
  end
  stdout
end

status = YAML.safe_load(ticcmd('-s', '--full'))

position = status.fetch('Current position')
puts "Current position is #{position}."

new_target = position > 0 ? -200 : 200
puts "Setting target position to #{new_target}."
ticcmd('--exit-safe-start', '--position', new_target)

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