[Tutor] terminating driver instance prior to completion

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed Aug 25 12:35:19 EDT 2021


On Tue, 24 Aug 2021 10:48:52 -0600, David Haan <dghmerch at comcast.net>
declaimed the following:

>I have a driver written in Python by others that is used to move a 
>stepper motor.  The parameters of the driver calls for, among other 
>things, the number of steps the motor needs to be moved.  It can be from 
>1 to infinity.
>
>I also have a hardware interrupt available that will indicate when the 
>device the motor is moving has reached its home position.  I need to 
>stop the driver at that point regardless of the number of steps it has 
>processed.
>
>I create an instance of the driver and pass it appropriate values for 
>its variables.  Once the driver instance starts, it will not return to 
>the main program until it has processed all the steps I passed in one of 
>the instance variables.
>
>When the hardware interrupt fires, I want to terminate the driver code 
>execution so it will return control immediately to the main program flow.
>
>My question is if there is a method to terminate the Python driver code 
>instance.  I really don't want to copy the driver code and modify it 
>specifically for my own program if I don't have to.

	Given you are tallking stepper motors, this doesn't sound like a
desktop Python -- Circuit Python or microPython, perhaps?

	For the most part, Python does not have means for true hardware
interrupts. One has to use something like asyncio (I have no experience
with that -- my mind understands threading, though CircuitPython doesn't)
or threads. In the case of threads, one would have a thread that is blocked
(or, ugh, polls) on the interrupt signal. When the signal triggers, the
thread would set a global flag, which all other threads have to
periodically test, and exit if they find the flag set.

	So -- yes, you likely WILL need to tweak this driver. Somewhere in it
you need a loop:

while True:
	if intFlag: break
	#step the motor by one unit
	#doing whatever current checks exist for non-infinite movement
	#maybe end with a sleep(0) to trigger thread swap so monitor 
	#thread can check the interrupt.
	#
	#note: if ONLY the motor driver cares about the interrupt, you
	#don't need a separate thread, and can replace
	#		if intFlag:
	#with whatever is needed to actually check the interrupt



-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list