Tic36v4 Homing Python


My apologies if i have done this wrong as this is my first forum post.
I am trying to write a stepper motor homing function where the motor will drive in the reverse direction, where a limit switch is activated and the position set to 0, then drives in the forward direction to where the forward limit switch is activated then saving the current position to a variable.

But this is not the case, I have managed to do this from the Arduino libraries but not able to in python. .As soon as the program enters the forward limit switch loop the position is set to 0 again on the driver, and this can be monitored through the ticgui.

Any idea what could be happening.

Also this program is being run on an Nvidia xavier nx.

from time import sleep
from smbus2 import SMBus, i2c_msg
from ticlib import TicI2C, SMBus2Backend

global minPos
global maxPos
minPos = 0
maxPos = 0

bus = SMBus(8)

tic_S1_address = 32 
tic_S1_backend = SMBus2Backend(bus,tic_S1_address)

tic_S1 = TicI2C(tic_S1_backend)


def get_current_position(address):
    b = get_variables(address,0x22, 4)
    position = b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24)
    if position >= (1 << 31):
      position -= (1 << 32)
    return position

 # Gets one or more variables from the Tic.
def get_variables(address, offset, length):
  write = i2c_msg.write(address, [0xA1, offset])
  read = i2c_msg.read(address, length)
  bus.i2c_rdwr(write, read)
  return list(read)

def getVar8(address, offset):
    return get_variables(address, offset, 1)[0]
def getEnergized():
    return getVar8(0x01) >> 0 & 1

# Uncertain position Check Var  =======
def getPosUncertain(address):
    return getVar8(address,0x01) >> 1 & 1
def PrintPosUncertain(var):
    print("Posisition Uncertain = ", var)
def getForwardLimitActive(address):
    return getVar8(address,0x01) >> 2 & 1
def PrintForHomingActive(var):
    print("For Homing Active = ", var)    

def getReverseLimitActive(address):
    return getVar8(address,0x01) >> 3 & 1
def PrintRevHomingActive(var):
    print("Rev Homing Active = ", var)   
def getHomingActive(address):
    return getVar8(address,0x01) >> 4 & 1
def PrintHomingActive(var):
    print("Homing Active = ", var)

def calibrate(tic,address):
    global minPos
    global maxPos
    homedrev = 0
    homedfor = 0
    breaker = False
    print("Calibration for motor ", address, " starting now ")
    if getHomingActive(address) == False:
            tic.go_home(0)  #-- Home Reverse
            #if getHomingActive(address) == 1 & getReverseLimitActive(address) == 0:
            while getReverseLimitActive(address) == 1:
                 #print("Waiting for release")
                 if getReverseLimitActive(address) == 0:
                     minPos = get_current_position(address)
                     homedrev = 1
                 breaker = True
            if breaker: # the interesting part!
                break   # <--- !
    breaker = False  
    if getHomingActive(address) == False:
            tic.go_home(1)  #-- Home Reverse
            #if getHomingActive(address) == 1 & getReverseLimitActive(address) == 0:
            while getForwardLimitActive(address) == 1:
                 #print("Waiting for release")
                 if getForwardLimitActive(address) == 0:
                     #maxPos = get_current_position(address)
                     maxPos = tic.get_current_position()
                     homedrev = 1
                 breaker = True
            if breaker: # the interesting part!
                break   # <--- !     
    #maxPos = get_current_position(address)         
    print("Min Pos: ",minPos)
    print("Max Pos: ",maxPos)



It sounds like you are not familiar with how the Tic’s homing procedure works. The Tic’s homing procedure will drive the stepper motor in the specified direction until the associated switch is activated, then in the opposite direction until it is deactivated. At that point, it will set the current position to 0.

So, you should be able to do what you are describing by taking the following steps:

  1. Home in the reverse direction.
  2. Send a set velocity forward command.
  3. Wait until the forward limit switch is triggered.
  4. Read the current position and use set target position commands to slowly back away from the switch until it deactivates.
  5. Save the current position to a variable.

It is not clear to me how your method worked for you when you used the Arduino library, but the Tic’s homing procedure works the same way when triggered through any interface.


do you have an example of this in python please

We do not have a Python example that does the procedure I described, but if you try and run into problems, you can post the code you have so far and a description of what happens when you run it, and I would be happy to take a look.