Skip to content
Open
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
357 changes: 357 additions & 0 deletions Keyboard!! W,A,S,D,Q,E
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
#!/usr/bin/python
# Library for PiMotor Shield V2
# Developed by: SB Components
# Project: RPi Motor Shield 4wd car by STRIX :)(Dan Harding)

import RPi.GPIO as GPIO #Import GPIO library
import time
from time import sleep

GPIO.setmode(GPIO.BOARD) #Set GPIO pin numbering
GPIO.setwarnings(False)

class Motor:



''' Class to handle interaction with the motor pins
Supports redefinition of "forward" and "backward" depending on how motors are connected
Use the supplied Motorshieldtest module to test the correct configuration for your project.

Arguments:

motor = string motor pin label (i.e. "MOTOR1","MOTOR2","MOTOR3","MOTOR4") identifying the pins to which

the motor is connected.

config = int defining which pins control "forward" and "backward" movement.

'''

motorpins = {"MOTOR4":{"config":{1:{"e":32,"f":24,"r":26},2:{"e":32,"f":26,"r":24}},"arrow":1},
"MOTOR3":{"config":{1:{"e":19,"f":21,"r":23},2:{"e":19,"f":23,"r":21}}, "arrow":2},
"MOTOR2":{"config":{1:{"e":22,"f":16,"r":18},2:{"e":22,"f":18,"r":16}}, "arrow":3},
"MOTOR1":{"config":{1:{"e":11,"f":15,"r":13},2:{"e":11,"f":13,"r":15}},"arrow":4}}


def __init__(self, motor, config):


self.testMode = False
self.arrow = Arrow(self.motorpins[motor]["arrow"])
self.pins = self.motorpins[motor]["config"][config]
GPIO.setup(self.pins['e'],GPIO.OUT)
GPIO.setup(self.pins['f'],GPIO.OUT)
GPIO.setup(self.pins['r'],GPIO.OUT)
self.PWM = GPIO.PWM(self.pins['e'], 50) # 50Hz frequency
self.PWM.start(0)
GPIO.output(self.pins['e'],GPIO.HIGH)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)

def test(self, state):



''' Puts the motor into test mode
When in test mode the Arrow associated with the motor receives power on "forward"
rather than the motor. Useful when testing your code.

Arguments:

state = boolean

'''

self.testMode = state


def forward(self, speed):



''' Starts the motor turning in its configured "forward" direction.
Arguments:
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''

print("Forward")
if self.testMode:
self.arrow.on()

else:
self.PWM.ChangeDutyCycle(speed)
GPIO.output(self.pins['f'],GPIO.HIGH)
GPIO.output(self.pins['r'],GPIO.LOW)

def reverse(self,speed):



''' Starts the motor turning in its configured "reverse" direction.
Arguments:
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''

print("Reverse")
if self.testMode:
self.arrow.off()
else:
self.PWM.ChangeDutyCycle(speed)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.HIGH)

def stop(self):

''' Stops power to the motor,
'''
print("Stop")
self.arrow.off()
self.PWM.ChangeDutyCycle(0)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)

def speed(self):

''' Control Speed of Motor,

'''
class LinkedMotors:

''' Links 2 or more motors together as a set.

This allows a single command to be used to control a linked set of motors

e.g. For a 4x wheel vehicle this allows a single command to make all 4 wheels go forward.

Starts the motor turning in its configured "forward" direction.

Arguments:

*motors = a list of Motor objects

'''

def __init__(self, *motors):

self.motor = []
for i in motors:
print(i.pins)
self.motor.append(i)

def forward(self,speed):

''' Starts the motor turning in its configured "forward" direction.

Arguments:

speed = Duty Cycle Percentage from 0 to 100.

0 - stop and 100 - maximum speed

'''

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

def reverse(self,speed):

''' Starts the motor turning in its configured "reverse" direction.

Arguments:

speed = Duty Cycle Percentage from 0 to 100.

0 - stop and 100 - maximum speed

'''

for i in range(len(self.motor)):

self.motor[i].reverse(speed)

def stop(self):

''' Stops power to the motor,

'''

for i in range(len(self.motor)):

self.motor[i].stop()

class Arrow():

''' Defines an object for controlling one of the LED arrows on the Motorshield.
Arguments:
which = integer label for each arrow. The arrow number if arbitrary starting with:

1 = Arrow closest to the Motorshield's power pins and running clockwise round the board
...
4 = Arrow closest to the motor pins.

'''
arrowpins={1:33,2:35,3:37,4:36}


def __init__(self, which):
self.pin = self.arrowpins[which]
GPIO.setup(self.pin,GPIO.OUT)
GPIO.output(self.pin, GPIO.LOW)


def on(self):

GPIO.output(self.pin,GPIO.HIGH)

def off(self):

GPIO.output(self.pin,GPIO.LOW)

import RPi.GPIO as GPIO # Import the GPIO Library
import time # Import the Time library

# Set the GPIO modes

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# Set variables for the GPIO motor pins

motorpins = {"MOTOR4":{"config":{1:{"e":32,"f":24,"r":26},2:{"e":32,"f":26,"r":24}},"arrow":1},
"MOTOR3":{"config":{1:{"e":19,"f":21,"r":23},2:{"e":19,"f":23,"r":21}}, "arrow":2},
"MOTOR2":{"config":{1:{"e":22,"f":16,"r":18},2:{"e":22,"f":18,"r":16}}, "arrow":3},
"MOTOR1":{"config":{1:{"e":11,"f":15,"r":13},2:{"e":11,"f":13,"r":15}},"arrow":4}}

#Name of Individual MOTORS

m1 = Motor("MOTOR1",1)
m2 = Motor("MOTOR2",1)
m3 = Motor("MOTOR3",1)
m4 = Motor("MOTOR4",1)

# Set the GPIO Pin mode

def __init__(self, motor, config):
self.testMode = False
self.arrow = Arrow(self.motorpins[motor]["arrow"])
self.pins = self.motorpins[motor]["config"][config]
GPIO.setup(self.pins['e'],GPIO.OUT)
GPIO.setup(self.pins['f'],GPIO.OUT)
GPIO.setup(self.pins['r'],GPIO.OUT)
self.PWM = GPIO.PWM(self.pins['e'], 50) # 50Hz frequency
self.PWM.start(0)
GPIO.output(self.pins['e'],GPIO.HIGH)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)
def StartMotors():

m1.forward(50)
m2.forward(50)
m3.forward(50)
m4.forward(50)
time.sleep(0.1)

def StopMotors():

m1.forward(0)
m2.forward(0)
m3.forward(0)
m4.forward(0)
time.sleep(0.1)

def Forwards():

m1.forward(100)
m2.forward(100)
m3.forward(100)
m4.forward(100)
time.sleep(0.1)

def Backwards():

m1.reverse(100)
m2.reverse(100)
m3.reverse(100)
m4.reverse(100)
time.sleep(0.1)

def Left():

m1.reverse(100)
m2.reverse(100)
m3.forward(100)
m4.forward(100)
time.sleep(0.1)

def Right():

m1.forward(100)
m2.forward(100)
m3.reverse(100)
m4.reverse(100)
time.sleep(0.1)

import sys, termios, tty, os

def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)

try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)

finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch

PIN_LED = 29
GPIO.setup(PIN_LED, GPIO.OUT)
GPIO.output(PIN_LED, 0)
button_delay = 0.1

for x in range(0,3):

GPIO.output(PIN_LED, 1)
time.sleep(0.1)
GPIO.output(PIN_LED, 0)
time.sleep(0.1)

while True:

char = getch()

if (char == "q"):
StopMotors()
exit(0)

if (char == "e"):
StartMotors()
time.sleep(button_delay)

if (char == "a"):

print 'Left pressed'
Left()
time.sleep(button_delay)

if (char == "d"):

print 'Right pressed'
Right()
time.sleep(button_delay)

elif (char == "w"):

print 'Up pressed'
Forwards()
time.sleep(button_delay)

elif (char == "s"):

print 'Down pressed'
Backwards()
time.sleep(button_delay)