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

keyboard funtion for 4wd, 4 dc motor /wheels #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dr-scape
Copy link

@dr-scape dr-scape commented Jan 19, 2020

keyboard for 4wd things :) will be constantly working on this for improvements and more functionality

im very new to this and this took a lot of work and time I hope it helps as ive found nothing on this on the webby if you find a fault or upgrade please let me know thanks :)
@dr-scape dr-scape changed the title Create Keyboard!! W,A,S,D for 4wd robot keyboard funtion for 4wd, 4 dc motor /wheels Jan 21, 2020
keyboard function for 4wd forward W, backwards S, left A, right D, start motors 50% all forward E, StopMotors and exit promt Q, > Enter,..
Copy link
Author

@dr-scape dr-scape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update!

--------START------------

#!/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(10)
m2.forward(10)
m3.forward(10)
m4.forward(10)
time.sleep(0.1)

def StopMotors0():

m1.forward(0)
m2.forward(0)
m3.forward(0)
m4.forward(0)
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)

def Forwards1():

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

def Backwards1():

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

def Left1():

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

def Right1():

m1.forward(50)
m2.forward(50)
m3.reverse(50)
m4.reverse(50)
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 == "r"):
    StopMotors0()

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)
    
if (char == "f"):

    print 'Left pressed 50%'
    Left1()
    time.sleep(button_delay)

if (char == "h"):

    print 'Right pressed 50%'
    Right1()
    time.sleep(button_delay)          

elif (char == "t"):

    print 'Up pressed 50%' 
    Forwards1()       
    time.sleep(button_delay)
             
elif (char == "g"):

    print 'Down pressed 50%'      
    Backwards1()
    time.sleep(button_delay)

----------END-------------------
cleaned up script, changed e from 100% to 10% start function, added f left, h right, t forward, g backwards, @50% speed, added r function to stop motors without exiting,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant