diff --git a/stepper.py b/stepper.py index 0c43682..d0e28f3 100644 --- a/stepper.py +++ b/stepper.py @@ -1,9 +1,20 @@ # Stepper Motors on Raspberry Pi -from gpiozero import OutputDevice -from time import time +from time import time +from gpiozero import OutputDevice class Stepper: + """ + Class to control a stepper motor connected to the GPIO pins. + + Args: + number_of_steps: The number of steps per revolution for the motor. If + the value is given as the number of degrees per step, + then divide 360 by that number to get the number of + steps (for example, 360 / 1.8 gives 200 steps). + motor_pins: The GPIO pins to use to control the motor. + """ + def __init__(self, number_of_steps, *motor_pins): self.step_number = 0 self.direction = 0 @@ -29,9 +40,31 @@ def __init__(self, number_of_steps, *motor_pins): self.pins.append(self.motor_pin_5) def set_speed(self, speed): + """ + Sets the speed of the motor. This does not move the motor, just + specifies how fast it moves when you call `step()`. + + Args: + speed: The motor speed in rotations per minute (RPM). + """ + + # Convert the speed to delay in seconds self.step_delay = 60.0 / (self.number_of_steps * speed) def step(self, steps_to_move): + """ + Make the motor turn the specified number of steps. The speed of the + steps is determined by the most recent call of `speed()`. + + Note: this function does not return until all the steps have been + completed, which could be a long time for a large number of steps (or a + low speed). + + Args: + steps_to_move: The number of steps to turn. A negative value turns + in the opposite direction. + """ + self.direction = 1 if steps_to_move > 0 else -1 steps_left = abs(steps_to_move) while steps_left > 0: @@ -46,20 +79,24 @@ def step(self, steps_to_move): self.step_motor(self.step_number % 4) steps_left -= 1 - def step_motor(self, cur_step): + def step_motor(self, cur_step: int): + """ + Internal method to implement a single step by toggling the GPIO pins. + """ + if self.pin_count == 2: if cur_step == 0: # 01 - self.motor_pins[0].off() - self.motor_pins[1].on() + self.motor_pin_1.off() + self.motor_pin_2.on() elif cur_step == 1: # 11 - self.motor_pins[0].on() - self.motor_pins[1].on() + self.motor_pin_1.on() + self.motor_pin_2.on() elif cur_step == 2: # 10 - self.motor_pins[0].on() - self.motor_pins[1].off() + self.motor_pin_1.on() + self.motor_pin_2.off() elif cur_step == 3: # 00 - self.motor_pins[0].off() - self.motor_pins[1].off() + self.motor_pin_1.off() + self.motor_pin_2.off() elif self.pin_count == 4: if cur_step == 0: # 1010 self.motor_pin_1.on() @@ -144,5 +181,9 @@ def step_motor(self, cur_step): self.motor_pin_5.on() def stop(self): + """ + Stop the motor by setting all the GPIO pins low. + """ + for pin in self.pins: pin.off()