imptube.pistepper

An example implementation of the Stepper class for movable termination. In this case, implemented for the original impedance tube operated solely by Raspberry Pi 4 Model B. Because of some sound stability issues, newer implementation relies on FT232H breakout board from Adafruit.

This module provides a PiStepper class that contains methods for controlling a stepper motor using a Raspberry Pi and a DRV8825 driver. The class allows for setting the microstepping resolution, defining the ramping of the motor speed, initiating GPIO settings, enabling/disabling the motor, and performing the actual spinning of the motor.

Constants:

  • DIR: Direction GPIO Pin
  • STEP: Step GPIO Pin
  • MODE: Microstep Resolution GPIO Pins
  • ENABLE: Enable GPIO Pin
  • CW: Clockwise Rotation
  • CCW: Counterclockwise Rotation
  • SPR: Steps per Revolution
  • FREQ: Frequency
  • RESOLUTION: Dictionary mapping microstepping resolution names to GPIO pin values
  • RESOLUTION_M: Dictionary mapping microstepping resolution names to step multipliers
  1""" An example implementation of the Stepper class for movable termination.
  2In this case, implemented for the original impedance tube operated solely
  3by Raspberry Pi 4 Model B. Because of some sound stability issues,
  4newer implementation relies on FT232H breakout board from Adafruit.
  5
  6This module provides a PiStepper class that contains methods for controlling
  7a stepper motor using a Raspberry Pi and a DRV8825 driver. The class allows
  8for setting the microstepping resolution, defining the ramping of the motor
  9speed, initiating GPIO settings, enabling/disabling the motor, and performing
 10the actual spinning of the motor.
 11
 12Constants:
 13- DIR: Direction GPIO Pin
 14- STEP: Step GPIO Pin
 15- MODE: Microstep Resolution GPIO Pins
 16- ENABLE: Enable GPIO Pin
 17- CW: Clockwise Rotation
 18- CCW: Counterclockwise Rotation
 19- SPR: Steps per Revolution
 20- FREQ: Frequency
 21- RESOLUTION: Dictionary mapping microstepping resolution names to GPIO pin values
 22- RESOLUTION_M: Dictionary mapping microstepping resolution names to step multipliers
 23
 24"""
 25from time import sleep
 26# import RPi.GPIO as GPIO
 27import numpy as np
 28
 29DIR = 20   # Direction GPIO Pin
 30STEP = 21 # Step GPIO Pin
 31MODE = (14, 15, 18)   # Microstep Resolution GPIO Pins
 32ENABLE = 16
 33
 34CW = 1     # Clockwise Rotation
 35CCW = 0    # Counterclockwise Rotation
 36SPR = 200   # Steps per Revolution
 37FREQ = 2
 38RESOLUTION = {'Full': (0, 0, 0),
 39              'Half': (1, 0, 0),
 40              '1/4': (0, 1, 0),
 41              '1/8': (1, 1, 0),
 42              '1/16': (0, 0, 1),
 43              '1/32': (1, 0, 1)}
 44RESOLUTION_M = {'Full': 1,
 45                'Half': 2,
 46                '1/4': 4,
 47                '1/8': 8,
 48                '1/16': 16,
 49                '1/32': 32}
 50
 51class PiStepper:
 52    """
 53    Contains methods for stepper motor operation using Pi and DRV8825 driver
 54    
 55    Attributes:
 56    ----------
 57    res : str
 58        The microstepping resolution of the stepper motor.
 59    delay : numpy.ndarray
 60        The delay values for each step in the stepper motor movement.
 61    
 62    Methods:
 63    --------
 64    __init__(res="Half"):
 65        Initializes a PiStepper object with the specified microstepping resolution.
 66        
 67        Parameters:
 68        ----------
 69        res : str, optional
 70            The microstepping resolution of the stepper motor. Default is "Half".
 71    
 72    set_delay(step_count, fade=1, sin_begin=4):
 73        Sets the delay values for each step in the stepper motor movement.
 74        
 75        Parameters:
 76        ----------
 77        step_count : int
 78            The total number of steps in the stepper motor movement.
 79        fade : float, optional
 80            The duration of the ramping effect at the beginning and end of the movement. Default is 1.
 81        sin_begin : int, optional
 82            The number of steps over which the ramping effect is applied. Default is 4.
 83    
 84    on():
 85        Initializes the GPIO settings for the stepper motor operation.
 86    
 87    off():
 88        Cleans up the GPIO settings after the stepper motor operation.
 89    
 90    enable():
 91        Enables the stepper motor by operating the ENABLE pin on the driver.
 92    
 93    disable():
 94        Disables the stepper motor by operating the ENABLE pin on the driver.
 95    
 96    turn(revolutions=1, clockwise=True):
 97        Performs the actual spinning of the stepper motor.
 98        
 99        Parameters:
100        ----------
101        revolutions : float, optional
102            The number of full revolutions to be performed by the stepper motor. Default is 1.
103        clockwise : bool, optional
104            Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.
105    """
106
107    def __init__(self, res="Half"):
108        """
109        Initializes a PiStepper object with the specified microstepping resolution.
110        
111        Parameters:
112        ----------
113        res : str, optional
114            The microstepping resolution of the stepper motor. Default is "Half".
115        """
116        self.res = res
117
118    def set_delay(self, step_count, fade=1, sin_begin=4):
119        """
120        Sets the delay values for each step in the stepper motor movement.
121        
122        Parameters:
123        ----------
124        step_count : int
125            The total number of steps in the stepper motor movement.
126        fade : float, optional
127            The duration of the ramping effect at the beginning and end of the movement. Default is 1.
128        sin_begin : int, optional
129            The number of steps over which the ramping effect is applied. Default is 4.
130        """
131        self.delay = np.full(
132            step_count,
133            1 / FREQ / SPR / RESOLUTION_M[self.res]
134            )
135        len_fade = int(fade * SPR)
136        ramp_in = 1/np.sin(np.linspace(
137            np.pi/sin_begin,
138            np.pi/2,
139            len_fade
140            ))
141        ramp_out = 1/np.sin(np.linspace(
142            np.pi/2,
143            np.pi - np.pi/sin_begin,
144            len_fade
145            ))
146        ramp_shape = ramp_in.shape[0]
147        self.delay[0:ramp_shape] = self.delay[0:ramp_shape] * ramp_in
148        self.delay[-ramp_shape:] = self.delay[-ramp_shape:] * ramp_out
149
150    def on(self):
151        """
152        Initializes the GPIO settings for the stepper motor operation.
153        """
154        GPIO.setmode(GPIO.BCM)
155        GPIO.setup(DIR, GPIO.OUT)
156        GPIO.setup(STEP, GPIO.OUT)
157        GPIO.setup(ENABLE, GPIO.OUT)
158        GPIO.output(DIR, CW)
159        GPIO.setup(MODE, GPIO.OUT)
160
161        GPIO.output(MODE, RESOLUTION[self.res])
162
163    def off(self):
164        """
165        Cleans up the GPIO settings after the stepper motor operation.
166        """
167        GPIO.cleanup()
168
169    def enable(self):
170        """
171        Enables the stepper motor by operating the ENABLE pin on the driver.
172        """
173        GPIO.output(ENABLE, 0)
174
175    def disable(self):
176        """
177        Disables the stepper motor by operating the ENABLE pin on the driver.
178        """
179        GPIO.output(ENABLE, 1)
180
181
182    def turn(self, revolutions=1, clockwise=True):
183        """
184        Performs the actual spinning of the stepper motor.
185        
186        Parameters:
187        ----------
188        revolutions : float, optional
189            The number of full revolutions to be performed by the stepper motor. Default is 1.
190        clockwise : bool, optional
191            Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.
192        """
193        if clockwise:
194            GPIO.output(DIR, CW)
195        else:
196            GPIO.output(DIR, CCW)
197
198        step_count = int(revolutions * SPR * RESOLUTION_M[self.res])
199        
200        self.set_delay(step_count)
201
202        for d in self.delay:
203            GPIO.output(STEP, GPIO.HIGH)
204            sleep(d)
205            GPIO.output(STEP, GPIO.LOW)
206            sleep(d)
207
208
209
210if __name__ == "__main__":
211    p = PiStepper()
212    p.on()
213    p.turn(0.75)
214    p.off()
215
216
217    
class PiStepper:
 52class PiStepper:
 53    """
 54    Contains methods for stepper motor operation using Pi and DRV8825 driver
 55    
 56    Attributes:
 57    ----------
 58    res : str
 59        The microstepping resolution of the stepper motor.
 60    delay : numpy.ndarray
 61        The delay values for each step in the stepper motor movement.
 62    
 63    Methods:
 64    --------
 65    __init__(res="Half"):
 66        Initializes a PiStepper object with the specified microstepping resolution.
 67        
 68        Parameters:
 69        ----------
 70        res : str, optional
 71            The microstepping resolution of the stepper motor. Default is "Half".
 72    
 73    set_delay(step_count, fade=1, sin_begin=4):
 74        Sets the delay values for each step in the stepper motor movement.
 75        
 76        Parameters:
 77        ----------
 78        step_count : int
 79            The total number of steps in the stepper motor movement.
 80        fade : float, optional
 81            The duration of the ramping effect at the beginning and end of the movement. Default is 1.
 82        sin_begin : int, optional
 83            The number of steps over which the ramping effect is applied. Default is 4.
 84    
 85    on():
 86        Initializes the GPIO settings for the stepper motor operation.
 87    
 88    off():
 89        Cleans up the GPIO settings after the stepper motor operation.
 90    
 91    enable():
 92        Enables the stepper motor by operating the ENABLE pin on the driver.
 93    
 94    disable():
 95        Disables the stepper motor by operating the ENABLE pin on the driver.
 96    
 97    turn(revolutions=1, clockwise=True):
 98        Performs the actual spinning of the stepper motor.
 99        
100        Parameters:
101        ----------
102        revolutions : float, optional
103            The number of full revolutions to be performed by the stepper motor. Default is 1.
104        clockwise : bool, optional
105            Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.
106    """
107
108    def __init__(self, res="Half"):
109        """
110        Initializes a PiStepper object with the specified microstepping resolution.
111        
112        Parameters:
113        ----------
114        res : str, optional
115            The microstepping resolution of the stepper motor. Default is "Half".
116        """
117        self.res = res
118
119    def set_delay(self, step_count, fade=1, sin_begin=4):
120        """
121        Sets the delay values for each step in the stepper motor movement.
122        
123        Parameters:
124        ----------
125        step_count : int
126            The total number of steps in the stepper motor movement.
127        fade : float, optional
128            The duration of the ramping effect at the beginning and end of the movement. Default is 1.
129        sin_begin : int, optional
130            The number of steps over which the ramping effect is applied. Default is 4.
131        """
132        self.delay = np.full(
133            step_count,
134            1 / FREQ / SPR / RESOLUTION_M[self.res]
135            )
136        len_fade = int(fade * SPR)
137        ramp_in = 1/np.sin(np.linspace(
138            np.pi/sin_begin,
139            np.pi/2,
140            len_fade
141            ))
142        ramp_out = 1/np.sin(np.linspace(
143            np.pi/2,
144            np.pi - np.pi/sin_begin,
145            len_fade
146            ))
147        ramp_shape = ramp_in.shape[0]
148        self.delay[0:ramp_shape] = self.delay[0:ramp_shape] * ramp_in
149        self.delay[-ramp_shape:] = self.delay[-ramp_shape:] * ramp_out
150
151    def on(self):
152        """
153        Initializes the GPIO settings for the stepper motor operation.
154        """
155        GPIO.setmode(GPIO.BCM)
156        GPIO.setup(DIR, GPIO.OUT)
157        GPIO.setup(STEP, GPIO.OUT)
158        GPIO.setup(ENABLE, GPIO.OUT)
159        GPIO.output(DIR, CW)
160        GPIO.setup(MODE, GPIO.OUT)
161
162        GPIO.output(MODE, RESOLUTION[self.res])
163
164    def off(self):
165        """
166        Cleans up the GPIO settings after the stepper motor operation.
167        """
168        GPIO.cleanup()
169
170    def enable(self):
171        """
172        Enables the stepper motor by operating the ENABLE pin on the driver.
173        """
174        GPIO.output(ENABLE, 0)
175
176    def disable(self):
177        """
178        Disables the stepper motor by operating the ENABLE pin on the driver.
179        """
180        GPIO.output(ENABLE, 1)
181
182
183    def turn(self, revolutions=1, clockwise=True):
184        """
185        Performs the actual spinning of the stepper motor.
186        
187        Parameters:
188        ----------
189        revolutions : float, optional
190            The number of full revolutions to be performed by the stepper motor. Default is 1.
191        clockwise : bool, optional
192            Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.
193        """
194        if clockwise:
195            GPIO.output(DIR, CW)
196        else:
197            GPIO.output(DIR, CCW)
198
199        step_count = int(revolutions * SPR * RESOLUTION_M[self.res])
200        
201        self.set_delay(step_count)
202
203        for d in self.delay:
204            GPIO.output(STEP, GPIO.HIGH)
205            sleep(d)
206            GPIO.output(STEP, GPIO.LOW)
207            sleep(d)

Contains methods for stepper motor operation using Pi and DRV8825 driver

Attributes:

res : str The microstepping resolution of the stepper motor. delay : numpy.ndarray The delay values for each step in the stepper motor movement.

Methods:

__init__(res="Half"): Initializes a PiStepper object with the specified microstepping resolution.

Parameters:
----------
res : str, optional
    The microstepping resolution of the stepper motor. Default is "Half".

set_delay(step_count, fade=1, sin_begin=4): Sets the delay values for each step in the stepper motor movement.

Parameters:
----------
step_count : int
    The total number of steps in the stepper motor movement.
fade : float, optional
    The duration of the ramping effect at the beginning and end of the movement. Default is 1.
sin_begin : int, optional
    The number of steps over which the ramping effect is applied. Default is 4.

on(): Initializes the GPIO settings for the stepper motor operation.

off(): Cleans up the GPIO settings after the stepper motor operation.

enable(): Enables the stepper motor by operating the ENABLE pin on the driver.

disable(): Disables the stepper motor by operating the ENABLE pin on the driver.

turn(revolutions=1, clockwise=True): Performs the actual spinning of the stepper motor.

Parameters:
----------
revolutions : float, optional
    The number of full revolutions to be performed by the stepper motor. Default is 1.
clockwise : bool, optional
    Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.
PiStepper(res='Half')
108    def __init__(self, res="Half"):
109        """
110        Initializes a PiStepper object with the specified microstepping resolution.
111        
112        Parameters:
113        ----------
114        res : str, optional
115            The microstepping resolution of the stepper motor. Default is "Half".
116        """
117        self.res = res

Initializes a PiStepper object with the specified microstepping resolution.

Parameters:

res : str, optional The microstepping resolution of the stepper motor. Default is "Half".

def set_delay(self, step_count, fade=1, sin_begin=4):
119    def set_delay(self, step_count, fade=1, sin_begin=4):
120        """
121        Sets the delay values for each step in the stepper motor movement.
122        
123        Parameters:
124        ----------
125        step_count : int
126            The total number of steps in the stepper motor movement.
127        fade : float, optional
128            The duration of the ramping effect at the beginning and end of the movement. Default is 1.
129        sin_begin : int, optional
130            The number of steps over which the ramping effect is applied. Default is 4.
131        """
132        self.delay = np.full(
133            step_count,
134            1 / FREQ / SPR / RESOLUTION_M[self.res]
135            )
136        len_fade = int(fade * SPR)
137        ramp_in = 1/np.sin(np.linspace(
138            np.pi/sin_begin,
139            np.pi/2,
140            len_fade
141            ))
142        ramp_out = 1/np.sin(np.linspace(
143            np.pi/2,
144            np.pi - np.pi/sin_begin,
145            len_fade
146            ))
147        ramp_shape = ramp_in.shape[0]
148        self.delay[0:ramp_shape] = self.delay[0:ramp_shape] * ramp_in
149        self.delay[-ramp_shape:] = self.delay[-ramp_shape:] * ramp_out

Sets the delay values for each step in the stepper motor movement.

Parameters:

step_count : int The total number of steps in the stepper motor movement. fade : float, optional The duration of the ramping effect at the beginning and end of the movement. Default is 1. sin_begin : int, optional The number of steps over which the ramping effect is applied. Default is 4.

def on(self):
151    def on(self):
152        """
153        Initializes the GPIO settings for the stepper motor operation.
154        """
155        GPIO.setmode(GPIO.BCM)
156        GPIO.setup(DIR, GPIO.OUT)
157        GPIO.setup(STEP, GPIO.OUT)
158        GPIO.setup(ENABLE, GPIO.OUT)
159        GPIO.output(DIR, CW)
160        GPIO.setup(MODE, GPIO.OUT)
161
162        GPIO.output(MODE, RESOLUTION[self.res])

Initializes the GPIO settings for the stepper motor operation.

def off(self):
164    def off(self):
165        """
166        Cleans up the GPIO settings after the stepper motor operation.
167        """
168        GPIO.cleanup()

Cleans up the GPIO settings after the stepper motor operation.

def enable(self):
170    def enable(self):
171        """
172        Enables the stepper motor by operating the ENABLE pin on the driver.
173        """
174        GPIO.output(ENABLE, 0)

Enables the stepper motor by operating the ENABLE pin on the driver.

def disable(self):
176    def disable(self):
177        """
178        Disables the stepper motor by operating the ENABLE pin on the driver.
179        """
180        GPIO.output(ENABLE, 1)

Disables the stepper motor by operating the ENABLE pin on the driver.

def turn(self, revolutions=1, clockwise=True):
183    def turn(self, revolutions=1, clockwise=True):
184        """
185        Performs the actual spinning of the stepper motor.
186        
187        Parameters:
188        ----------
189        revolutions : float, optional
190            The number of full revolutions to be performed by the stepper motor. Default is 1.
191        clockwise : bool, optional
192            Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.
193        """
194        if clockwise:
195            GPIO.output(DIR, CW)
196        else:
197            GPIO.output(DIR, CCW)
198
199        step_count = int(revolutions * SPR * RESOLUTION_M[self.res])
200        
201        self.set_delay(step_count)
202
203        for d in self.delay:
204            GPIO.output(STEP, GPIO.HIGH)
205            sleep(d)
206            GPIO.output(STEP, GPIO.LOW)
207            sleep(d)

Performs the actual spinning of the stepper motor.

Parameters:

revolutions : float, optional The number of full revolutions to be performed by the stepper motor. Default is 1. clockwise : bool, optional Specifies the direction of rotation. True for clockwise, False for counterclockwise. Default is True.