stepper moving with getState

Supporting 2.7 and 3.2+
Post Reply
Raphisfa
Phidgetsian
Posts: 5
Joined: Mon May 22, 2023 2:55 am
Contact:

stepper moving with getState

Post 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
fraser
Engineering
Posts: 324
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: stepper moving with getState

Post 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:"
Raphisfa
Phidgetsian
Posts: 5
Joined: Mon May 22, 2023 2:55 am
Contact:

Re: stepper moving with getState

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests