Page 1 of 1

stepper moving with getState

Posted: Mon May 22, 2023 3:09 am
by Raphisfa
Hi
I am trying to let my stepper run as long as my Digital Input which is a Button is unpressed.
When its unpressed i get:
State [3]:1
ok
but my stepper is not moving only a little "clack" to hear.

Code: Select all

#! /usr/env/python

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


def onStateChange(self, state):
	print("State [" + str(self.getHubPort()) + "]: " + str(state))
	if state == 1:
		print("ok")
		mot4 = Stepper()
		mot4.setHubPort(0)
		mot4.setDeviceSerialNumber(682067)
		mot4.openWaitForAttachment(4000)
		mot4.setTargetPosition(100000)
		mot4.setEngaged(True)
		mot4.close()
	else:
		print("nope")

def main():

	digitalInput3 = DigitalInput()
	digitalInput3.setIsHubPortDevice(True)
	digitalInput3.setHubPort(3)
	digitalInput3.setDeviceSerialNumber(682056)
	digitalInput3.setOnStateChangeHandler(onStateChange)
	digitalInput3.openWaitForAttachment(5000)

	try:
		input("Press Enter to Stop\n")
	except (Exception, KeyboardInterrupt):
		pass

	digitalInput3.close()


main()


Btw: my stepper is able to move since i got other programs making it move

Thanks for the help :D

Re: stepper moving with getState

Posted: Tue May 23, 2023 10:07 am
by fraser
calling mot4.close() will cause the motor to shutdown and stop doing whatever you just asked it to do. So it is "clacking" because it engages, and tries to move, then gets shut down immediately.

It seems more likely that you want to call close after the "else:"

Re: stepper moving with getState

Posted: Mon Jun 12, 2023 6:54 am
by Raphisfa
Thanks.

edited the code a bit and it works now.

now i want to loop this:

Code: Select all

def onStateChangeTop(self, state):
   print("State [" + str(self.getHubPort()) + "]: " + str(state))

   if state == 1:
      print("Top: OK")
      motorStart()
      print("gestartet")


   else:
      print("Top: Stopp")
      motorStopp()
      print("gestoppt")
i want it to do motorStart as long as it's state 1 and then when it presses the button and state changes to 0 i want it to get in the else.
Do you have any idea how i can make it happen? tried it with whiles and fors but it wont work :/

thanks for the help!