Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Function to Return Current Motor Speed #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions PiMotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, motor, config):
GPIO.output(self.pins['e'],GPIO.HIGH)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)
self.currentSpeed = 0

def test(self, state):
''' Puts the motor into test mode
Expand All @@ -57,6 +58,9 @@ def forward(self, speed):
0 - stop and 100 - maximum speed
'''
print("Forward")

self.currentSpeed = speed

if self.testMode:
self.arrow.on()
else:
Expand All @@ -72,6 +76,9 @@ def reverse(self,speed):
0 - stop and 100 - maximum speed
'''
print("Reverse")

self.currentSpeed = speed * (-1)

if self.testMode:
self.arrow.off()
else:
Expand All @@ -87,6 +94,12 @@ def stop(self):
self.PWM.ChangeDutyCycle(0)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)

def getSpeed(self):
''' Returns current speed of the motor
positive numbers are forward, negative numbers are reverse '''

return self.currentSpeed

def speed(self):
''' Control Speed of Motor,
Expand All @@ -104,6 +117,7 @@ class LinkedMotors:
'''
def __init__(self, *motors):
self.motor = []
self.currentSpeed = [0, 0, 0, 0]
for i in motors:
print(i.pins)
self.motor.append(i)
Expand All @@ -115,7 +129,9 @@ def forward(self,speed):
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''

for i in range(len(self.motor)):
self.currentSpeed[i] = speed
self.motor[i].forward(speed)

def reverse(self,speed):
Expand All @@ -125,8 +141,16 @@ def reverse(self,speed):
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''

for i in range(len(self.motor)):
self.currentSpeed[i] = speed
self.motor[i].reverse(speed)

def getSpeed(self):
''' Returns array with current speed of the motors
positive numbers are forward, negative numbers are reverse '''

return self.currentSpeed

def stop(self):
''' Stops power to the motor,
Expand Down Expand Up @@ -237,7 +261,7 @@ def iRCheck(self):
self.Triggered = False

def sonicCheck(self):
print("SonicCheck has been triggered")
# print("SonicCheck has been triggered")
time.sleep(0.333)
GPIO.output(self.config["trigger"], True)
time.sleep(0.00001)
Expand All @@ -253,7 +277,7 @@ def sonicCheck(self):
if self.boundary > measure:
print("Boundary breached")
print(self.boundary)
print(measure)
print(measure / 2.54)
self.Triggered = True
else:
self.Triggered = False
Expand All @@ -267,7 +291,7 @@ def trigger(self):
If the specified "boundary" has been breached the Sensor's Triggered attribute gets set to True.
'''
self.config["check"](self)
print("Trigger Called")
# print("Trigger Called")

def __init__(self, sensortype, boundary):
self.config = self.sensorpins[sensortype]
Expand All @@ -293,9 +317,15 @@ def __init__(self, which):
self.pin = self.arrowpins[which]
GPIO.setup(self.pin,GPIO.OUT)
GPIO.output(self.pin, GPIO.LOW)
self.isOn = False

def on(self):
GPIO.output(self.pin,GPIO.HIGH)
self.isOn = True

def off(self):
GPIO.output(self.pin,GPIO.LOW)
self.isOn = False

def getStatus(self):
return self.isOn
17 changes: 12 additions & 5 deletions Stepper_Test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import RPi.GPIO as GPIO #Import GPIO library
import PiMotor
import time

m1 = PiMotor.Stepper("STEPPER1")

# Rotate Stepper 1 Contiously in forward/backward direction
while True:
m1.forward(0.1,10) # Delay and rotations
time.sleep(2)
m1.backward(0.1,10)
time.sleep(2)

try:
while True:
m1.forward(0.1,10) # Delay and rotations
time.sleep(2)
m1.backward(0.1,10)
time.sleep(2)


except KeyboardInterrupt:
GPIO.cleanup()
1 change: 1 addition & 0 deletions Test_Motor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python

import RPi.GPIO as GPIO #Import GPIO library
import PiMotor
import time

Expand Down