[Tutor] terminating driver instance prior to completion

Alan Gauld alan.gauld at yahoo.co.uk
Wed Aug 25 14:33:43 EDT 2021


On 24/08/2021 17:48, David Haan wrote:
> 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.
> 

We can't really comment on this without knowing the API.
I would expect the API to provide a stop() method as a matter of
course - there are many reasons why you would want to stop the
motor immediately!

As for the hardware interrupt that all depends on the OS and how it
handles hardware interrupts. It might require some assembler
programming in the worst case! How do these interrupts manifest
themselves to your Python code? Is there a library?

> 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.

One (inefficient) possibility is always pass single steps and put
it in a loop (maybe even within a thread). That way control is
all within Python. But that may introduce speed issues. Or
even interface issues, we simply can't tell without a lot
more detail on the API.

> 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.  

We don't know. What does your API say?

> I really don't want to copy the driver code and modify it 
> specifically for my own program if I don't have to.

If its an OOP interface you can modify behaviour without
changing the original code simply by using inheritance to
create your own class and override the necessary methods
and add any needed new ones. The ability to change an existing
class's behaviour without modifying its code is one of the
strongest features of OOP.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list