Created
April 13, 2014 23:22
-
-
Save gezimbasha/10606341 to your computer and use it in GitHub Desktop.
DC Motor for Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import datetime as dt | |
class motor(): | |
x = 0 | |
v = 0.87654321 | |
def __init__(self): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(7, GPIO.OUT, initial=GPIO.HIGH) | |
GPIO.setup(11, GPIO.OUT, initial=GPIO.HIGH) | |
def cw(self): | |
GPIO.output(7, GPIO.LOW) | |
GPIO.output(11, GPIO.HIGH) | |
def ccw(self): | |
GPIO.output(7, GPIO.HIGH) | |
GPIO.output(11, GPIO.LOW) | |
def stopRot(self): | |
GPIO.output(7, GPIO.HIGH) | |
GPIO.output(11, GPIO.HIGH) | |
def forward(self, length): | |
#Naive check | |
if(length >= 25): | |
print("Can not exceed axis length") | |
#Calculate the time it takes to reach given distance | |
t = length/self.v | |
start = dt.datetime.now() | |
self.ccw() | |
while True: | |
if((dt.datetime.now()-start).total_seconds() >= t): | |
self.stopRot() | |
break | |
def goto(self, s): | |
#Smarter check | |
if( s >= 25): | |
print("Can not exceed 24 cm") | |
s = 24 | |
elif (s < 0): | |
print("Please give only positive values") | |
s = 0 | |
length = s - self.x | |
self.x = s | |
if(length > 0): | |
print("Going forward by", abs(length), " cm") | |
self.forward(length) | |
elif(length < 0): | |
print("Going backwards by", abs(length), " cm") | |
self.backwards(length) | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment