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_address = 32
tic_S1_backend = SMBus2Backend(bus,tic_S1_address)
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)
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:
while(True):
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:
sleep(0.1)
tic.halt_and_set_position(0)
sleep(0.1)
minPos = get_current_position(address)
sleep(1)
homedrev = 1
break
breaker = True
if breaker: # the interesting part!
break # <--- !
sleep(0.8)
breaker = False
if getHomingActive(address) == False:
while(True):
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:
sleep(0.1)
#maxPos = get_current_position(address)
maxPos = tic.get_current_position()
tic.halt_and_set_position(maxPos)
sleep(1)
homedrev = 1
break
breaker = True
if breaker: # the interesting part!
break # <--- !
#maxPos = get_current_position(address)
print("Min Pos: ",minPos)
print("Max Pos: ",maxPos)
calibrate(tic_S1,tic_S1_address)