Advertisement
CJBurkey

moveit.py

Feb 7th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.70 KB | None | 0 0
  1. # Online updated version at: https://pastebin.com/Wm30BmJE
  2. # Running:
  3. #  python <FILENAME>.py
  4. #  NOTE: IF THIS SCRIPT FAILS WITH AN ERROR SUCH AS:
  5. #   Failed to import "curses"
  6. #  Run "pip install curses" in Terminal (or in SSH)
  7. # Usage:
  8. #  Tap a key to perform an action, YOU DO NOT HAVE TO HOLD THEM.
  9. #  The default 'Control+C' CANNOT be used to exit the script, you must tap
  10. #  the 'X' key
  11. #  'w'    Begin moving foraward
  12. #  's'    Begin moving backward
  13. #  'd'    Begin turning right
  14. #  'a'    Begin turning left
  15. #  SPACE  Stop all movement
  16. #  'x'    Exit the python execution (exit the script)
  17.  
  18. # Import the required libraries
  19. # Curses is used to detect keypresses in the console
  20. import curses
  21. import RPi.GPIO as GPIO
  22. import time, sys, tty, termios
  23.  
  24. # Pulses per second (Don't change)
  25. Frequency = 120
  26.  
  27. # Speed bounds (probably don't change)
  28. MinSpeed = 20
  29. MaxSpeed = 100
  30.  
  31. # Motor imperfection correction (Change this until your robot moves straight enough)
  32. FineA = -0.5
  33.  
  34. # No movement (Don't change)
  35. Stop = 0
  36.  
  37. motorBBackwards = None;
  38. motorBForwards = None;
  39. motorABackwards = None;
  40. motorAForwards = None;
  41.  
  42. def StopAllMotors():
  43.     motorAForwards.ChangeDutyCycle(Stop)
  44.     motorABackwards.ChangeDutyCycle(Stop)
  45.     motorBForwards.ChangeDutyCycle(Stop)
  46.     motorBBackwards.ChangeDutyCycle(Stop)
  47.  
  48. # Initializes the pins and such on the robot
  49. def Init():
  50.     print("Initilizing movement");
  51.     GPIO.setmode(GPIO.BCM)
  52.     GPIO.setwarnings(False)
  53.     global motorBBackwards;
  54.     global motorBForwards;
  55.     global motorABackwards;
  56.     global motorAForwards;
  57.     GPIO.setup(7, GPIO.OUT)
  58.     GPIO.setup(8, GPIO.OUT)
  59.     GPIO.setup(9, GPIO.OUT)
  60.     GPIO.setup(10, GPIO.OUT)
  61.     motorBBackwards = GPIO.PWM(7, Frequency)
  62.     motorBForwards = GPIO.PWM(8, Frequency)
  63.     motorABackwards = GPIO.PWM(9, Frequency)
  64.     motorAForwards = GPIO.PWM(10, Frequency)
  65.     motorAForwards.start(Stop)
  66.     motorABackwards.start(Stop)
  67.     motorBForwards.start(Stop)
  68.     motorBBackwards.start(Stop)
  69.     print("Initilized movement");
  70.  
  71. # Cleans up
  72. def Exit():
  73.     StopAllMotors()
  74.     GPIO.cleanup()
  75.  
  76. # Time (t) is in seconds
  77. # Speed is in percent of maximum speed (20-100 by default)
  78. def MoveBackward(t, speed, stop):
  79.     motorABackwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  80.     motorBBackwards.ChangeDutyCycle(ClampSpeed(speed))
  81.     if stop:
  82.         time.sleep(t)
  83.         StopAllMotors()
  84.  
  85. # For the following move methods,
  86. #  if stop is 'True', then the motors will stop
  87. #  automatically after 't' seconds, otherwise,
  88. #  the robot will continue to move until 'StopAllMotors()'
  89. #  is called or another movement method is called.
  90. def MoveForward(t, speed, stop):
  91.     motorAForwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  92.     motorBForwards.ChangeDutyCycle(ClampSpeed(speed))
  93.     if stop:
  94.         time.sleep(t)
  95.         StopAllMotors()
  96.  
  97. def TurnLeft(t, speed, stop):
  98.     motorABackwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  99.     motorBForwards.ChangeDutyCycle(ClampSpeed(speed))
  100.     if stop:
  101.         time.sleep(t)
  102.         StopAllMotors()
  103.  
  104. def TurnRight(t, speed, stop):
  105.     motorAForwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  106.     motorBBackwards.ChangeDutyCycle(ClampSpeed(speed))
  107.     if stop:
  108.         time.sleep(t)
  109.         StopAllMotors()
  110.  
  111. # Returns a value that is no less than "mi" nor greater than "ma"
  112. def Clamp(val, mi, ma):
  113.     if (val > ma):
  114.         return ma
  115.     if (val < mi):
  116.         return mi
  117.     return val
  118.  
  119. # Makes sure the supplied speed cannot
  120. #  be less than the minimum speed or greater than
  121. #  the maximum speed
  122. def ClampSpeed(speed):
  123.     return Clamp(speed, MinSpeed, MaxSpeed)
  124.  
  125. Init()
  126.  
  127. # Movement code
  128. # This will detect keypesses on the keyboard
  129. #  and allow them to control the car. You can
  130. #  change the keys to what ever you like, just
  131. #  be sure the keys have an actual character associated
  132. #  with them, keys like 'l', 'a', 'w', 'u', etc, will work,
  133. #  but 'shift', 'control', 'up', 'right', etc will not.
  134. # Note:
  135. #  If the robot moves in the wrong direction (such as 'w' moves backwards, or 'd' turns right),
  136. #    Change the method calls to the necessary actions, such as 'MoveForward' to 'MoveBackward' and 'MoveBackward' to 'MoveForward'
  137. #  If only one of the wheels turns in the right direction, while the other turns in the incorrect direction,
  138. #    The only solution is to edit the specific function which turns the incorrect direction.
  139. #    For example, if pressing 'w' moves one wheel forward and the other wheel backwards,
  140. #      Change the code inside of the 'MoveForward()' method from 'motorBForwards.ChangeDutyCycle(ClampSpeed(speed))' to
  141. #        'motorBBackwards.ChangeDutyCycle(ClampSpeed(speed))'
  142. scr = curses.initscr()
  143. while True: # This will keep executing until the program is exited, can be dangerous. KEEP IN MIND THAT 'Control+C' DOES NOT WORK.
  144.     scr.nodelay(True) # Don't freeze the program if the user presses nothing
  145.     key = ''
  146.     try:
  147.     key = scr.getkey()  # Retrieve the most recent keypress
  148.     except:
  149.         key = ''        # No key pressed this frame
  150.     if key == ' ':      # Space will stop all movement
  151.         StopAllMotors()
  152.     elif key == 'x':        # 'x' will exit the program
  153.         break
  154.     elif key == 'w':        # 'w' will move forward until interrupted
  155.         StopAllMotors()
  156.         MoveForward(0, 75, False)
  157.     elif key == 's':        # 's' will move backward until interrupted
  158.         StopAllMotors()
  159.         MoveBackward(0, 75, False)
  160.     elif key == 'a':        # 'a' will turn left until interrupted
  161.         StopAllMotors()
  162.         TurnLeft(0, 100, False)
  163.     elif key == 'd':        # 'd' will turn right until interrupted
  164.         StopAllMotors()
  165.         TurnRight(0, 100, False)
  166.  
  167. StopAllMotors() # Cleans up and exits the program
  168. Exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement