8.8. Example serial code in Python

<p>The example Python code below uses the <a href="http://pyserial.readthedocs.io">pySerial</a> library to communicate with the Simple Motor Controller G2 via serial. It demonstrates how to read variables from the controller and set its target speed.</p> <p>For this example to work, the Simple Motor Controller G2&#8217;s input mode must be <strong>Serial/USB</strong>, the serial mode must be <strong>Binary</strong>, and the CRC must be disabled. These are the default settings that the controller is shipped with. If you are using TTL serial, you should set the controller to use a fixed baud rate of 9600, or change the baud rate in the code below to match whatever fixed baud rate you choose.</p> <p>If you run the code and get the error &#8220;ImportError: No module named serial&#8221; or &#8220;ModuleNotFoundError: No module named &#8216;serial&#8217;&#8221;, it means that the pySerial library is not installed, and you should follow the instructions in the <a href="http://pyserial.readthedocs.io">pySerial documentation</a> to install it.</p> <pre name="code" class="python">&#x000A;# Uses the pySerial library to send and receive data from a&#x000A;# Simple Motor Controller G2.&#x000A;#&#x000A;# NOTE: The Simple Motor Controller's input mode must be "Serial/USB".&#x000A;# NOTE: You might need to change the "port_name =" line below to specify the&#x000A;# right serial port.&#x000A;&#x000A;import serial&#x000A;&#x000A;class SmcG2Serial(object):&#x000A; def __init__(self, port, device_number=None):&#x000A; self.port = port&#x000A; self.device_number = device_number&#x000A;&#x000A; def send_command(self, cmd, *data_bytes):&#x000A; if self.device_number == None:&#x000A; header = [cmd] # Compact protocol&#x000A; else:&#x000A; header = [0xAA, device_number, cmd &amp; 0x7F] # Pololu protocol&#x000A; self.port.write(bytes(header + list(data_bytes)))&#x000A;&#x000A; # Sends the Exit Safe Start command, which is required to drive the motor.&#x000A; def exit_safe_start(self):&#x000A; self.send_command(0x83)&#x000A;&#x000A; # Sets the SMC's target speed (-3200 to 3200).&#x000A; def set_target_speed(self, speed):&#x000A; cmd = 0x85 # Motor forward&#x000A; if speed &lt; 0:&#x000A; cmd = 0x86 # Motor reverse&#x000A; speed = -speed&#x000A; self.send_command(cmd, speed &amp; 0x1F, speed &gt;&gt; 5 &amp; 0x7F)&#x000A;&#x000A; # Gets the specified variable as an unsigned value.&#x000A; def get_variable(self, id):&#x000A; self.send_command(0xA1, id)&#x000A; result = self.port.read(2)&#x000A; if len(result) != 2:&#x000A; raise RuntimeError("Expected to read 2 bytes, got {}."&#x000A; .format(len(result)))&#x000A; b = bytearray(result)&#x000A; return b[0] + 256 * b[1]&#x000A;&#x000A; # Gets the specified variable as a signed value.&#x000A; def get_variable_signed(self, id):&#x000A; value = self.get_variable(id)&#x000A; if value &gt;= 0x8000:&#x000A; value -= 0x10000&#x000A; return value&#x000A;&#x000A; # Gets the target speed (-3200 to 3200).&#x000A; def get_target_speed(self):&#x000A; return self.get_variable_signed(20)&#x000A;&#x000A; # Gets a number where each bit represents a different error, and the&#x000A; # bit is 1 if the error is currently active.&#x000A; # See the user's guide for definitions of the different error bits.&#x000A; def get_error_status(self):&#x000A; return self.get_variable(0)&#x000A;&#x000A;# Choose the serial port name.&#x000A;# Linux USB example: "/dev/ttyACM0" (see also: /dev/serial/by-id)&#x000A;# macOS USB example: "/dev/cu.usbmodem001234562"&#x000A;# Windows example: "COM6"&#x000A;port_name = "/dev/ttyACM0"&#x000A;&#x000A;# Choose the baud rate (bits per second). This does not matter if you are&#x000A;# connecting to the SMC over USB. If you are connecting via the TX and RX&#x000A;# lines, this should match the baud rate in the SMC's serial settings.&#x000A;baud_rate = 9600&#x000A;&#x000A;# Change this to a number between 0 and 127 that matches the device number of&#x000A;# your SMC if there are multiple serial devices on the line and you want to&#x000A;# use the Pololu Protocol.&#x000A;device_number = None&#x000A;&#x000A;port = serial.Serial(port_name, baud_rate, timeout=0.1, write_timeout=0.1)&#x000A;&#x000A;smc = SmcG2Serial(port, device_number)&#x000A;&#x000A;smc.exit_safe_start()&#x000A;&#x000A;error_status = smc.get_error_status()&#x000A;print("Error status: 0x{:04X}".format(error_status))&#x000A;&#x000A;target_speed = smc.get_target_speed()&#x000A;print("Target speed is {}.".format(target_speed))&#x000A;&#x000A;new_speed = 3200 if target_speed &lt;= 0 else -3200&#x000A;print("Setting target speed to {}.\n".format(new_speed));&#x000A;smc.set_target_speed(new_speed)</pre>

Related Products

High-Power Simple Motor Controller G2 18v15 (Connectors Soldered)
High-Power Simple Motor Controller G2 18v15
High-Power Simple Motor Controller G2 24v12 (Connectors Soldered)
High-Power Simple Motor Controller G2 24v12
High-Power Simple Motor Controller G2 18v25
High-Power Simple Motor Controller G2 24v19
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