Page 1 of 1

STC1002 running without setTargetPosition

Posted: Mon Jun 26, 2023 12:31 am
by Raphisfa
Hello everyone,
so i got this code:

Code: Select all

#! /usr/env/python

from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
from Phidget22.Devices.Stepper import *
import time

#Stepper moving up
def motorHoch():

	mot4 = Stepper()

	mot4.setHubPort(0)

	mot4.setDeviceSerialNumber(682067)

	mot4.openWaitForAttachment(5000)

	mot4.setAcceleration(1000000)
	mot4.setVelocityLimit(15000)
	mot4.setTargetPosition(10000)

	mot4.setEngaged(True)

	while mot4.getIsMoving():
		time.sleep(0.1)  # Eine kurze Verzögerung, um unnötige CPU-Auslastung zu vermeiden

	mot4.close()

pass

#Stepper moving down
def motorRunter():

	mot4 = Stepper()
	# print("1")
	mot4.setHubPort(0)
	# print("2")
	mot4.setDeviceSerialNumber(682067)
	# print("3")
	mot4.openWaitForAttachment(5000)
	# print("4")
	mot4.setAcceleration(1000000)
	mot4.setVelocityLimit(15000)
	mot4.setTargetPosition(-10000)
	# print("5")
	mot4.setEngaged(True)
	# print("6")
	while mot4.getIsMoving():
		time.sleep(0.1)  # Eine kurze Verzögerung, um unnötige CPU-Auslastung zu vermeiden
	# print("7")
	mot4.close()
	# print("8")



pass

#stepper stopp
def motorStopp():
	mot4 = Stepper()
	# print("1")
	mot4.setHubPort(0)
	# print("2")
	mot4.setDeviceSerialNumber(682067)
	# print("3")
	mot4.openWaitForAttachment(5000)
	# print("4")
	mot4.setTargetPosition(0)
	# print("5")
	mot4.setEngaged(False)
	# print("6")
	while mot4.getIsMoving():
		time.sleep(0.1)  # Eine kurze Verzögerung, um unnötige CPU-Auslastung zu vermeiden
	# print("7")
	mot4.close()

pass

#status of digital input
def onStateChangeTop(self, state):
	print("State [" + str(self.getHubPort()) + "]: " + str(state))

pass



def main():
	# Initialisierung Endschalter oben
	digitalInputTop = DigitalInput()
	digitalInputBot = DigitalInput()

	digitalInputTop.setIsHubPortDevice(True)
	digitalInputTop.setHubPort(3)
	digitalInputTop.setDeviceSerialNumber(682056)

	digitalInputBot.setIsHubPortDevice(True)
	digitalInputBot.setHubPort(3)
	digitalInputBot.setDeviceSerialNumber(682057)

	digitalInputTop.setOnStateChangeHandler(onStateChangeTop)
	digitalInputBot.setOnStateChangeHandler(onStateChangeTop)
	digitalInputTop.openWaitForAttachment(5000)
	digitalInputBot.openWaitForAttachment(5000)

	digitalInputTop.getState()
	digitalInputBot.getState()

	#try:
	#	input("Press Enter to Stop\n")
	#except (Exception, KeyboardInterrupt):
	#	pass
	
	#loop for stepper moving up until the upper input is 0 and then moving down until the lower input is 0

	bool1 = False
	while True:
		if digitalInputTop.getState() and digitalInputBot.getState() and bool1 == False:
			motorHoch()
			print("oben =", digitalInputTop.getState())
			continue
		bool1 = True


		if digitalInputBot.getState() and bool1 == True:
			motorRunter()
			print("oben =", digitalInputTop.getState())
			continue





		else:
			motorStopp()
			print("unten =", digitalInputBot.getState())
			break



	digitalInputTop.close()
	digitalInputBot.close()


My stepper motor moves upward until the digital input is set to 0, indicating it has reached its maximum height. Then, it moves downward until the other digital input is set to 0, indicating it has reached its lowest position, and then it stops. The digital inputs are buttons that are pressed when the stepper motor reaches its maximum or minimum position.

The problem is that the stepper motor moves in small increments of 10,000 steps. How can I make it move smoothly and consistently without these small increments?

Thank you for your help :)

Re: STC1002 running without setTargetPosition

Posted: Mon Jun 26, 2023 7:07 am
by Raphisfa
currently using 'mot4.setControlMode(1)' which works, but I can't let it go in the other direction. Any ideas how to make it run in the other direction :?: