From 65e2f97d7e6db80853e33fbb4d7c47c70c18a955 Mon Sep 17 00:00:00 2001 From: Alaa Agwa Date: Mon, 3 Mar 2014 01:18:19 +0200 Subject: [PATCH 1/3] adds stepper library with an example --- examples/Stepper_knob.py | 38 +++++++++ libraries/Stepper/README | 17 ++++ libraries/Stepper/Stepper.py | 141 ++++++++++++++++++++++++++++++++++ libraries/Stepper/__init__.py | 3 + 4 files changed, 199 insertions(+) create mode 100644 examples/Stepper_knob.py create mode 100644 libraries/Stepper/README create mode 100644 libraries/Stepper/Stepper.py create mode 100644 libraries/Stepper/__init__.py diff --git a/examples/Stepper_knob.py b/examples/Stepper_knob.py new file mode 100644 index 0000000..fe12135 --- /dev/null +++ b/examples/Stepper_knob.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +""" + Stepper_knob.py + Alaa Agwa - 3/3/2014 + + An example use of PyBBIO's stepper motor follows the turns of a potentiometer + (or other sensor) on analog input 0. + + Based on Arduino's Servo library example: + http://arduino.cc/en/Tutorial/MotorKnob + + + + This example is in the public domain. +""" + +# First we must import PyBBIO: +from bbio import * +# Then we can import Stepper: +from Stepper import * + +stepper = Stepper(100,GPIO01_17,GPIO03_21,GPIO03_19,GPIO03_15) + +def setup(): + stepper.setSpeed(30); + +def loop(): + #get the sensor value + val = analogRead(0) + + #move a number of steps equal to the change in the + #sensor reading + stepper.step(val - previous) + + #remember the previous value of the sensor + previous = val + +run(setup, loop) \ No newline at end of file diff --git a/libraries/Stepper/README b/libraries/Stepper/README new file mode 100644 index 0000000..086314d --- /dev/null +++ b/libraries/Stepper/README @@ -0,0 +1,17 @@ +Stepper - v0.1 + +Copyright 2012 Alexander Hiam +agwatic@gmail.com + +Library for controlling stepper motors with the BeagleBone's pins. + +See documentation here: + +As well as the included example programs: + PyBBIO/examples/Stepper_knob.py + +Based on the Arduino Servo library: + http://www.arduino.cc/en/Reference/Stepper + +Servo is released as part of PyBBIO under its Apache 2.0 license. +See PyBBIO/LICENSE.txt \ No newline at end of file diff --git a/libraries/Stepper/Stepper.py b/libraries/Stepper/Stepper.py new file mode 100644 index 0000000..7c611b8 --- /dev/null +++ b/libraries/Stepper/Stepper.py @@ -0,0 +1,141 @@ +""" + Stepper - v0.1 + Copyright 2014 Alaa Agwa + + Library for controlling stepper motors. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 +""" +from bbio import * + + +class Stepper(object): + def __init__(self,steps = 100 , pin_1, pin_2, pin_3=None, pin_4=None): + self.step_number = 0 # which step the motor is on + self.speed = 0 # the motor speed, in revolutions per minute + self.direction = 0 # motor direction + self.last_step_time = 0 # time stamp in ms of the last step taken + self.number_of_steps = setps # total number of steps for this motor + + # Pins for the motor control connection + self.motor_pin1 = pin_1 + self.motor_pin2 = pin_2 + self.motor_pin3 = pin_3 + self.motor_pin4 = pin_4 + + #setup the pins + pinMode(self.motor_pin1,OUTPUT) + pinMode(self.motor_pin2,OUTPUT) + pinMode(self.motor_pin3,OUTPUT) + pinMode(self.motor_pin4,OUTPUT) + + #pins count the stepMotor() method + if(self.motor_pin3 == None and self.motor_pin4 == None): + self.motor_pins = 2 + elif(self.motor_pin3 != None and self.motor_pin4 != None): + self.motor_pins = 4 + + + + #Sets the speed in revs per minute + def setSpeed(self , whatSpeed): + self.step_delay = 60L * 1000L / self.number_of_steps / whatSpeed + + + + """ + Moves the motor steps_to_move steps. If the number is negative, + the motor moves in the reverse direction. + """ + def step(self,steps_to_move,start_time): + steps_left = abs(steps_to_move) #how many steps to take + + #determine direction based on whether steps_to_mode is + or - + + if(steps_to_move>0): + self.direction = 1 + elif(steps_to_move<0): + self.direction=0 + + #decrement the number of steps, moving one step each time + while(steps_left >0): + #move only if the appropriate delay has passed + if(((start_time*1000) - self.last_step_time) >= self.step_delay): + self.last_step_time = start_time*1000 + + #increment or decrement the step number, depending on direction + if(self.direction == 1): + self.step_number +=1 + if(self.step_number == self.number_of_steps): + self.step_number = 0 + else: + if(self.step_number == 0): + self.step_number = self.number_of_steps + + self.step_number -=1 + #decrement the steps left + steps_left -=1 + #step the motor to step number 0, 1, 2, or 3 + stepMotor(self.step_number %4) + + """ + + Moves the motor forward or backwards. + + """ + def stepMotor(self,thisStep): + if(self.motor_pins == 2): + if(thisStep == 0): # 01 + digitalWrite(self.motor_pin1,LOW) + digitalWrite(self.motor_pin2,HIGH) + elif(thisStep == 1): # 11 + digitalWrite(self.motor_pin1,HIGH) + digitalWrite(self.motor_pin2,HIGH) + elif(thisStep == 2): # 10 + digitalWrite(self.motor_pin1,HIGH) + digitalWrite(self.motor_pin2,LOW) + elif(thisStep == 3): # 00 + digitalWrite(self.motor_pin1,LOW) + digitalWrite(self.motor_pin2,LOW) + + elif(self.motor_pins == 4): + if(thisStep == 0): # 1010 + digitalWrite(self.motor_pin1,HIGH) + digitalWrite(self.motor_pin2,LOW) + digitalWrite(self.motor_pin3,HIGH) + digitalWrite(self.motor_pin4,LOW) + elif(thisStep == 1): # 0110 + digitalWrite(self.motor_pin1,LOW) + digitalWrite(self.motor_pin2,HIGH) + digitalWrite(self.motor_pin3,HIGH) + digitalWrite(self.motor_pin4,LOW) + elif(thisStep == 2): # 0101 + digitalWrite(self.motor_pin1,LOW) + digitalWrite(self.motor_pin2,HIGH) + digitalWrite(self.motor_pin3,LOW) + digitalWrite(self.motor_pin4,HIGH) + elif(thisStep == 3): # 1001 + digitalWrite(self.motor_pin1,HIGH) + digitalWrite(self.motor_pin2,LOW) + digitalWrite(self.motor_pin3,LOW) + digitalWrite(self.motor_pin4,HIGH) + + + + + diff --git a/libraries/Stepper/__init__.py b/libraries/Stepper/__init__.py new file mode 100644 index 0000000..aabbdab --- /dev/null +++ b/libraries/Stepper/__init__.py @@ -0,0 +1,3 @@ +# __init__.py for Stepper + +from Stepper import * \ No newline at end of file From 7b463445b2896773015e9e45f363cbfd2c387ed8 Mon Sep 17 00:00:00 2001 From: Alaa Agwa Date: Mon, 3 Mar 2014 01:53:07 +0200 Subject: [PATCH 2/3] bug fix --- examples/Stepper_knob.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/Stepper_knob.py b/examples/Stepper_knob.py index fe12135..52423dc 100644 --- a/examples/Stepper_knob.py +++ b/examples/Stepper_knob.py @@ -19,6 +19,9 @@ # Then we can import Stepper: from Stepper import * +import time +start_time = time.time() + stepper = Stepper(100,GPIO01_17,GPIO03_21,GPIO03_19,GPIO03_15) def setup(): @@ -30,7 +33,7 @@ def loop(): #move a number of steps equal to the change in the #sensor reading - stepper.step(val - previous) + stepper.step(val - previous , start_time) #remember the previous value of the sensor previous = val From 7432496778a3e4c4eb946131cabe95b4e4214f3d Mon Sep 17 00:00:00 2001 From: Alaa Agwa Date: Mon, 10 Mar 2014 08:34:53 +0200 Subject: [PATCH 3/3] bug fix in the example --- examples/Stepper_knob.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Stepper_knob.py b/examples/Stepper_knob.py index 52423dc..fc36422 100644 --- a/examples/Stepper_knob.py +++ b/examples/Stepper_knob.py @@ -22,7 +22,7 @@ import time start_time = time.time() -stepper = Stepper(100,GPIO01_17,GPIO03_21,GPIO03_19,GPIO03_15) +stepper = Stepper(GPIO01_17,GPIO03_21,GPIO03_19,GPIO03_15,100) def setup(): stepper.setSpeed(30); @@ -38,4 +38,4 @@ def loop(): #remember the previous value of the sensor previous = val -run(setup, loop) \ No newline at end of file +run(setup, loop)