Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- from time import sleep
- from time import time
- import os
- import sys, tty, termios
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(9,GPIO.OUT)
- GPIO.setup(10,GPIO.OUT)
- GPIO.setup(11,GPIO.OUT)
- Motor1 = GPIO.PWM(11, 50)
- Motor1.start(0)
- Echo = 17
- Steer = 4
- def forward(speed):
- GPIO.output(9,GPIO.HIGH)
- GPIO.output(10,GPIO.LOW)
- Motor1.ChangeDutyCycle(speed)
- def backward(speed):
- GPIO.output(9,GPIO.LOW)
- GPIO.output(10,GPIO.HIGH)
- Motor1.ChangeDutyCycle(speed)
- def left(speed):
- string = "echo 0=110 > /dev/servoblaster"
- os.system(string)
- sleep(1)
- GPIO.output(9,GPIO.LOW)
- GPIO.output(10,GPIO.HIGH)
- Motor1.ChangeDutyCycle(speed)
- def right(speed):
- string = "echo 0=190 > /dev/servoblaster"
- os.system(string)
- sleep(1)
- GPIO.output(9,GPIO.LOW)
- GPIO.output(10,GPIO.HIGH)
- Motor1.ChangeDutyCycle(speed)
- def stop():
- Motor1.ChangeDutyCycle(0)
- # Infinite loop that will not end until the user presses the
- # exit key
- while True:
- # Keyboard character retrieval method is called and saved
- # into variable
- char = getch()
- # The car will drive forward when the "w" key is pressed
- if(char == "w"):
- print "forward"
- forward
- # The car will reverse when the "s" key is pressed
- if(char == "s"):
- print "backward"
- backward
- # The "a" key will toggle the steering left
- if(char == "a"):
- print "left"
- left
- # The "d" key will toggle the steering right
- if(char == "d"):
- print "right"
- right
- # The "x" key will break the loop and exit the program
- if(char == "x"):
- print("Program Ended")
- break
- # At the end of each loop the acceleration motor will stop
- # and wait for its next command
- motor1.ChangeDutyCycle(0)
- # The keyboard character variable will be set to blank, ready
- # to save the next key that is pressed
- char = ""
- # Program will cease all GPIO activity before terminating
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement