Hello, I am using a Tic T825 to control a stepper motor. The stepper accepts my write commands through the smbus and the I2C address for my device. The trouble I am having is getting any useful data returned when I want to read from a variable.
Here is a sample of my code with small GUI for user-control.
from tkinter import *
import tkinter
from smbus2 import SMBus, i2c_msg
# Reset
stepper_reset = 0xB0
# Set velocity
stepper_velocity = 0xE3
# Energize
energize = 0x85
# Denergize
de_energize = 0x86
# Class of variables
class Vars():
position = 0
control = 0
v = Vars()
# Establishes class for I2C commmunication
class TicI2C(object):
def __init__(self, bus, address):
self.bus = bus
self.address = address
# Sets the target position.
def set_target_position(self, target):
command = [0xE0,
target >> 0 & 0xFF,
target >> 8 & 0xFF,
target >> 16 & 0xFF,
target >> 24 & 0xFF]
write = i2c_msg.write(self.address, command)
self.bus.i2c_rdwr(write)
# Gets one or more variables from the Tic.
def get_variables(self, offset, length):
write = i2c_msg.write(self.address, [0xA1, offset])
read = i2c_msg.read(self.address, length)
self.bus.i2c_rdwr(write, read)
return list(read)
# Gets the "Current position" variable from the Tic.
def get_current_position(self):
b = self.get_variables(0x22, 4)
position = b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24)
if position >= (1 << 31):
position -= (1 << 32)
return position
# Declaration and intialization of smbus variable
bus = SMBus(1)
# Declaration of TicI2C instance
tic = TicI2C(bus, 0x11)
# Function to print position
def print_position():
print(tic.get_variables(0x22,4))
# Function to reset stepper
def reset_stepper():
tic.bus.write_byte(tic.address,stepper_reset)
v.position=0
def exit_stepper():
tic.bus.write_byte(tic.address,de_energize)
v.control = 1
def increase_position():
tic.set_target_position(v.position+100)
v.position+=100
def decrease_position():
tic.set_target_position(v.position-100)
v.position-=100
# Initially energizes the motor
tic.bus.write_byte(tic.address,energize)
# Creates frame for GUI
root = Tk()
root.title('Stepper Motor Variable Retrival')
root.geometry('300x300')
btt_reset = Button(root,text='reset',command=reset_stepper)
btt_reset.place(x=200,y=20)
btt_exit = Button(root,text='exit',command=exit_stepper)
btt_exit.place(x=200,y=50)
lbl_position = Label(root,text=v.position)
lbl_position.place(x=20,y=50)
btt_inc_position = Button(root, text='+100',command=increase_position)
btt_inc_position.place(x=60,y=30)
btt_dec_position = Button(root, text='-100',command=decrease_position)
btt_dec_position.place(x=60,y=70)
# Button to print position
btt_print_position = Button(root,text='print position', command=print_position)
btt_print_position.place(x=60,y=100)
# Button to print current position returned from TicI2C function
btt_current_position = Button(root,text='Current Position from TicI2C',
command=lambda:print(tic.get_current_position()))
btt_current_position.place(x=60,y=130)
# Update loop dependant on v.control
while v.control == 0:
root.update_idletasks()
root.update()
lbl_position.config(text=v.position)
# Destroys the frame of the gui
root.destroy()