15.3. Example code to run jrk2cmd in Python

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

If you have multiple Jrk G2 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 get the status of the Jrk G2 with serial number 00123456, you can run the Python code jrk2cmd('-d', '00123456', '-s', '--full'). You can run jrk2cmd --list in a shell to get the serial numbers of all the connected Jrk G2 devices.

In the example below, the child jrk2cmd process uses the same error pipe as the Python process, so you will see any error messages printed by jrk2cmd 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 jrk2cmd process exit status) and raise an exception.

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

import subprocess
import yaml

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

status = yaml.safe_load(jrk2cmd('-s', '--full'))

feedback = status['Feedback']
print("Feedback is {}.".format(feedback))

target = status['Target']
print("Target is {}.".format(target))

new_target = 2248 if target < 2048 else 1848
print("Setting target to {}.".format(new_target))

jrk2cmd('--target', 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 jrk2cmd in order to read data from the Jrk. If you have trouble installing PyYAML but you do not actually need to read data from the Jrk, you can simply remove the code that uses it. Also, the output of jrk2cmd 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

Jrk G2 18v19 USB Motor Controller with Feedback
Jrk G2 24v13 USB Motor Controller with Feedback
Jrk G2 18v27 USB Motor Controller with Feedback
Jrk G2 24v21 USB Motor Controller with Feedback
Jrk G2 21v3 USB Motor Controller with Feedback
Jrk G2 21v3 USB Motor Controller with Feedback (Connectors Soldered)
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