Advertisement
matt1987

Untitled

Nov 9th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. from time import sleep
  3. from time import time
  4. import os
  5. import sys, tty, termios
  6.  
  7.  
  8. GPIO.setmode(GPIO.BCM)
  9.  
  10. GPIO.setup(9,GPIO.OUT)
  11. GPIO.setup(10,GPIO.OUT)
  12. GPIO.setup(11,GPIO.OUT)
  13.  
  14. Motor1 = GPIO.PWM(11, 50)
  15. Motor1.start(0)
  16.  
  17. Echo = 17
  18. Steer = 4
  19.  
  20. def forward(speed):
  21.   GPIO.output(9,GPIO.HIGH)
  22.   GPIO.output(10,GPIO.LOW)
  23.   Motor1.ChangeDutyCycle(speed)
  24.  
  25.  
  26. def backward(speed):
  27.   GPIO.output(9,GPIO.LOW)
  28.   GPIO.output(10,GPIO.HIGH)
  29.   Motor1.ChangeDutyCycle(speed)
  30.  
  31.  
  32. def left(speed):
  33.  
  34.   string = "echo 0=110 > /dev/servoblaster"
  35.   os.system(string)
  36.   sleep(1)
  37.   GPIO.output(9,GPIO.LOW)
  38.   GPIO.output(10,GPIO.HIGH)
  39.   Motor1.ChangeDutyCycle(speed)
  40.  
  41.  
  42. def right(speed):
  43.  
  44.   string = "echo 0=190 > /dev/servoblaster"
  45.   os.system(string)
  46.   sleep(1)
  47.   GPIO.output(9,GPIO.LOW)
  48.   GPIO.output(10,GPIO.HIGH)
  49.   Motor1.ChangeDutyCycle(speed)
  50.  
  51.  
  52. def stop():
  53.   Motor1.ChangeDutyCycle(0)
  54.  
  55.  
  56.  
  57. # Infinite loop that will not end until the user presses the
  58. # exit key
  59. while True:
  60.     # Keyboard character retrieval method is called and saved
  61.     # into variable
  62.     char = getch()
  63.  
  64.     # The car will drive forward when the "w" key is pressed
  65.     if(char == "w"):
  66.      print "forward"
  67.      forward
  68.  
  69.     # The car will reverse when the "s" key is pressed
  70.     if(char == "s"):
  71.       print "backward"
  72.        backward
  73.  
  74.     # The "a" key will toggle the steering left
  75.     if(char == "a"):
  76.       print "left"
  77.       left
  78.  
  79.     # The "d" key will toggle the steering right
  80.     if(char == "d"):
  81.       print "right"
  82.       right
  83.        
  84.      # The "x" key will break the loop and exit the program
  85.     if(char == "x"):
  86.         print("Program Ended")
  87.         break
  88.  
  89.  
  90.    # At the end of each loop the acceleration motor will stop
  91.     # and wait for its next command
  92.     motor1.ChangeDutyCycle(0)
  93.  
  94.     # The keyboard character variable will be set to blank, ready
  95.     # to save the next key that is pressed
  96.     char = ""
  97.  
  98. # Program will cease all GPIO activity before terminating
  99. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement