Guest User

Robot RC car mode 1st draft

a guest
May 12th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. from time import sleep
  4.  
  5. # global vars
  6.  
  7. pwmpinL = 18
  8. pwmpinR = 13
  9. directionL = 22
  10. directionR = 23
  11. pwmfreq=1000 # set PWM frequency to 10Khz, compatible with slow slew mode on the motor controller
  12. dc = 25 # percent duty cycle to test with
  13.  
  14. GPIO.setmode(GPIO.BCM) # set pin selection mode
  15. GPIO.setup(pwmpinL, GPIO.OUT) #set left motor's pin to output mode
  16. GPIO.setup(pwmpinR, GPIO.OUT) #set right motor's pin to output mode
  17. GPIO.setup(directionL, GPIO.OUT) #set right motor's pin to output mode
  18. GPIO.setup(directionR, GPIO.OUT) #set right motor's pin to output mode
  19.  
  20. pwmL = GPIO.PWM(pwmpinL, pwmfreq)
  21. pwmR = GPIO.PWM(pwmpinR, pwmfreq)
  22. #dirL = GPIO.OUT(directionL, 1)
  23. #dirR = GPIO.OUT(directionR, 1)
  24.  
  25. #GPIO.output(pwmpinL, GPIO.LOW)
  26.  
  27. # Some simple predefined motor control functions
  28.  
  29. def stopmotors():
  30.     global dc
  31.     pwmL.stop()
  32.     pwmR.stop()
  33.     return
  34.    
  35. def turnright():
  36.     global dc
  37.     pwmR.stop()
  38.     pwmL.start(dc)
  39.     return
  40.    
  41. def turnleft():
  42.     global dc
  43.     pwmL.stop()
  44.     pwmR.start(dc)
  45.     return
  46.  
  47. def gostraight():
  48.     global dc
  49.     pwmL.start(dc) #start running left motor at test duty cycle
  50.     pwmR.start(dc) #start running left motor at test duty cycle
  51.     return
  52.  
  53. def changespeed(newspeed):
  54.     global dc
  55.     dc=newspeed*25
  56.     return
  57.    
  58. # Code to get keypresses
  59.  
  60. class _Getch:
  61.     """Gets a single character from standard input.  Does not echo to the
  62. screen."""
  63.     def __init__(self):
  64.         try:
  65.             self.impl = _GetchWindows()
  66.         except ImportError:
  67.             self.impl = _GetchUnix()
  68.  
  69.     def __call__(self): return self.impl()
  70.  
  71. class _GetchUnix:
  72.     def __init__(self):
  73.         import tty, sys
  74.  
  75.     def __call__(self):
  76.         import sys, tty, termios
  77.         fd = sys.stdin.fileno()
  78.         old_settings = termios.tcgetattr(fd)
  79.         try:
  80.             tty.setraw(sys.stdin.fileno())
  81.             ch = sys.stdin.read(1)
  82.         finally:
  83.             termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  84.         return ch
  85.  
  86.  
  87. class _GetchWindows:
  88.     def __init__(self):
  89.         import msvcrt
  90.  
  91.     def __call__(self):
  92.         import msvcrt
  93.         return msvcrt.getch()
  94.  
  95.  
  96. print("Use W/A/S/D to move, numbers 1~4 to adjust movement speed. Press C to exit")
  97.  
  98. # Keypress polling loop
  99. while True:
  100.     getch = _Getch()
  101.     if (getch=="w"):
  102.         gostraight()
  103.     if (getch=="a"):
  104.         turnleft()
  105.     if (getch=="d"):
  106.         turnright()
  107.     if (getch=="s"):
  108.         stopmotors()
  109.     if (getch=="1"):
  110.         changespeed(1)
  111.     if (getch=="2"):
  112.         changespeed(2)
  113.     if (getch=="3"):
  114.         changespeed(3)
  115.     if (getch=="4"):
  116.         changespeed(4)
  117.     if (getch=="c"):
  118.         stopmotors()
  119.         break
  120.     sleep(0.05)
  121.  
  122.  
  123. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment