Advertisement
CJBurkey

moveitnew.py

Nov 14th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.35 KB | None | 0 0
  1. # Online at: https://pastebin.com/cTW3d6JL
  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 and display cool
  20. # stuff
  21. import curses
  22. import RPi.GPIO as GPIO
  23. import time, sys, tty, termios
  24.  
  25. # Pulses per second (Don't change)
  26. Frequency = 120
  27.  
  28. # Speed bounds (probably don't change)
  29. MinSpeed = 20
  30. MaxSpeed = 100
  31.  
  32. # Motor imperfection correction (Change this until your robot moves
  33. # straight enough)
  34. FineA = -3
  35.  
  36. # No movement (Don't change)
  37. Stop = 0
  38.  
  39. motorBBackwards = None;
  40. motorBForwards = None;
  41. motorABackwards = None;
  42. motorAForwards = None;
  43.  
  44. def StopAllMotors():
  45.     motorAForwards.ChangeDutyCycle(Stop)
  46.     motorABackwards.ChangeDutyCycle(Stop)
  47.     motorBForwards.ChangeDutyCycle(Stop)
  48.     motorBBackwards.ChangeDutyCycle(Stop)
  49.  
  50. # Initializes the pins and such on the robot
  51. def Init():
  52.     print("Initilizing movement");
  53.     GPIO.setmode(GPIO.BCM)
  54.     GPIO.setwarnings(False)
  55.     global motorBBackwards;
  56.     global motorBForwards;
  57.     global motorABackwards;
  58.     global motorAForwards;
  59.     GPIO.setup(7, GPIO.OUT)
  60.     GPIO.setup(8, GPIO.OUT)
  61.     GPIO.setup(9, GPIO.OUT)
  62.     GPIO.setup(10, GPIO.OUT)
  63.     motorBBackwards = GPIO.PWM(8, Frequency)
  64.     motorBForwards = GPIO.PWM(7, Frequency)
  65.     motorABackwards = GPIO.PWM(10, Frequency)
  66.     motorAForwards = GPIO.PWM(9, Frequency)
  67.     motorAForwards.start(Stop)
  68.     motorABackwards.start(Stop)
  69.     motorBForwards.start(Stop)
  70.     motorBBackwards.start(Stop)
  71.     print("Initilized movement");
  72.  
  73. # Cleans up
  74. def Exit():
  75.     StopAllMotors()
  76.     GPIO.cleanup()
  77.  
  78. # Time (t) is in seconds
  79. # Speed is in percent of maximum speed (20-100 by default)
  80. def MoveBackward(t, speed):
  81.     motorABackwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  82.     motorBBackwards.ChangeDutyCycle(ClampSpeed(speed))
  83.  
  84. # For the following move methods,
  85. #  if stop is 'True', then the motors will stop
  86. #  automatically after 't' seconds, otherwise,
  87. #  the robot will continue to move until 'StopAllMotors()'
  88. #  is called or another movement method is called.
  89. def MoveForward(t, speed):
  90.     motorAForwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  91.     motorBForwards.ChangeDutyCycle(ClampSpeed(speed))
  92.  
  93. def TurnLeft(t, speed):
  94.     motorABackwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  95.     motorBForwards.ChangeDutyCycle(ClampSpeed(speed))
  96.  
  97. def TurnRight(t, speed):
  98.     motorAForwards.ChangeDutyCycle(ClampSpeed(speed + FineA))
  99.     motorBBackwards.ChangeDutyCycle(ClampSpeed(speed))
  100.  
  101. # Returns a value that is no less than "mi" nor greater than "ma"
  102. def Clamp(val, mi, ma):
  103.     if (val > ma):
  104.         return ma
  105.     if (val < mi):
  106.         return mi
  107.     return val
  108.  
  109. # Makes sure the supplied speed cannot
  110. #  be less than the minimum speed or greater than
  111. #  the maximum speed
  112. def ClampSpeed(speed):
  113.     return Clamp(speed, MinSpeed, MaxSpeed)
  114.  
  115. Init()
  116.  
  117. # Movement code
  118. # This will detect keypesses on the keyboard
  119. #  and allow them to control the car. You can
  120. #  change the keys to what ever you like, just
  121. #  be sure the keys have an actual character associated
  122. #  with them, keys like 'l', 'a', 'w', 'u', etc, will work,
  123. #  but 'shift', 'control', 'up', 'right', etc will not.
  124. # Note:
  125. #  If the robot moves in the wrong direction (such as 'w' moves backwards, or 'd' turns right),
  126. #    Change the method calls to the necessary actions, such as 'MoveForward' to 'MoveBackward' and 'MoveBackward' to 'MoveForward'
  127. #  If only one of the wheels turns in the right direction, while the other turns in the incorrect direction,
  128. #    The only solution is to edit the specific function which turns the incorrect direction.
  129. #    For example, if pressing 'w' moves one wheel forward and the other wheel backwards,
  130. #      Change the code inside of the 'MoveForward()' method from 'motorBForwards.ChangeDutyCycle(ClampSpeed(speed))' to
  131. #        'motorBBackwards.ChangeDutyCycle(ClampSpeed(speed))'
  132. scr = curses.initscr()
  133. curses.noecho()
  134. curses.cbreak()
  135. scr.keypad(1)
  136. scr.nodelay(True)   # Don't freeze the program if the user presses nothing
  137.  
  138. move = None
  139. speed = 1
  140. itSpeed = 0
  141. lastKey = 0
  142.  
  143. def Move(m, s):
  144.     global move
  145.     global speed
  146.    
  147.     if not (s == None):
  148.         speed = s
  149.    
  150.     itSpeed = (speed / 9)
  151.    
  152.     scr.clear()
  153.     if m == None:
  154.         move = None
  155.     elif not (m == ''):
  156.         move = m
  157.    
  158.     if move == None:
  159.         scr.addstr(0, 0, "Stopped", curses.A_REVERSE)
  160.     else:
  161.         scr.addstr(0, 0, move, curses.A_REVERSE)
  162.    
  163.     scr.addstr(1, 0, "Speed: " + str(speed), curses.A_REVERSE)
  164.     scr.addstr(3, 0, "Press 'x' to exit", curses.A_BOLD)
  165.     scr.addstr(4, 0, "Press space to stop moving", curses.A_BOLD)
  166.     scr.addstr(5, 0, "Use arrow keys to move", curses.A_BOLD)
  167.     scr.refresh()
  168.  
  169. Move(None, None)
  170. firstKey = True
  171.  
  172. while True:         # This will keep executing until the program is exited, can be dangerous.
  173.                     # KEEP IN MIND THAT 'Control+C' DOES NOT WORK.
  174.     global speed
  175.     global lastKey
  176.     global firstKey
  177.    
  178.     key = None
  179.    
  180.     try:
  181.         key = scr.getch()           # Retrieve the most recent keypress
  182.     except:
  183.         key = None                  # No key pressed this frame
  184.    
  185.     if key == -1 or key == None:
  186.         scr.addstr(7, 0, str(lastKey), curses.A_REVERSE)
  187.     else:
  188.         if time.time() - lastKey <= 0.1:
  189.             firstKey = False
  190.         lastKey = time.time()
  191.    
  192.     diff = time.time() - lastKey
  193.    
  194.     if (not firstKey and diff > 0.1) or (firstKey and diff > 0.5):
  195.         StopAllMotors()
  196.         Move(None, None)
  197.         firstKey = True
  198.    
  199.     if key == ord(' '):             # Space will stop all movement
  200.         StopAllMotors()
  201.         Move(None, None)
  202.     elif key == ord('x'):           # 'x' will exit the program
  203.         break
  204.     elif key == curses.KEY_UP:
  205.         StopAllMotors()
  206.         Move("Moving: Forward", None);
  207.         MoveForward(0, 35)
  208.     elif key == curses.KEY_DOWN:
  209.         StopAllMotors()
  210.         Move("Moving: Backward", None);
  211.         MoveBackward(0, 35)
  212.     elif key == curses.KEY_LEFT:
  213.         StopAllMotors()
  214.         Move("Turning: Left", None);
  215.         TurnLeft(0, 35)
  216.     elif key == curses.KEY_RIGHT:
  217.         StopAllMotors()
  218.         Move("Turning: Right", None);
  219.         TurnRight(0, 35)
  220.     elif key == ord('1'):
  221.         Move('', 1);
  222.     elif key == ord('2'):
  223.         Move('', 2);
  224.     elif key == ord('3'):
  225.         Move('', 3);
  226.     elif key == ord('4'):
  227.         Move('', 4);
  228.     elif key == ord('5'):
  229.         Move('', 5);
  230.     elif key == ord('6'):
  231.         Move('', 6);
  232.     elif key == ord('7'):
  233.         Move('', 7);
  234.     elif key == ord('8'):
  235.         Move('', 8);
  236.     elif key == ord('9'):
  237.         Move('', 9);
  238.  
  239. # Cleanup curses
  240. curses.nocbreak()
  241. scr.keypad(0)
  242. curses.echo()
  243. curses.endwin()
  244.  
  245. StopAllMotors()    # Cleans up and exits the program
  246. Exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement