# Tic36v4 Homing Python

Hello,

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 = TicI2C(tic_S1_backend)

#tic_S1.reset()
tic_S1.energize()
tic_S1.halt_and_set_position(0)
tic_S1.exit_safe_start()
sleep(0.1)

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 getEnergized():
return getVar8(0x01) >> 0 & 1

# Uncertain position Check Var  =======
return getVar8(address,0x01) >> 1 & 1
def PrintPosUncertain(var):
print("Posisition Uncertain = ", var)

return getVar8(address,0x01) >> 2 & 1
def PrintForHomingActive(var):
print("For Homing Active = ", var)

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

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

global minPos
global maxPos
homedrev = 0
homedfor = 0
breaker = False

print("Calibration for motor ", address, " starting now ")
while(True):
tic.go_home(0)  #-- Home Reverse
#print("Waiting for release")
sleep(0.1)
tic.halt_and_set_position(0)
sleep(0.1)
sleep(1)
homedrev = 1
break
breaker = True
if breaker: # the interesting part!
break   # <--- !
sleep(0.8)
breaker = False
while(True):
tic.go_home(1)  #-- Home Reverse
#print("Waiting for release")
sleep(0.1)
maxPos = tic.get_current_position()
tic.halt_and_set_position(maxPos)
sleep(1)
homedrev = 1

break
breaker = True
if breaker: # the interesting part!
break   # <--- !
print("Min Pos: ",minPos)
print("Max Pos: ",maxPos)

``````

Hello.

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.

Brandon

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.

Brandon