Advertisement
ioannisIGI

Robotic_spider_simple_control_code

Feb 1st, 2023
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.51 KB | Source Code | 0 0
  1. import RPi.GPIO as GPIO
  2. import time, os
  3. from time import sleep
  4. import getch
  5. global val
  6. val = 30
  7. run_once = 0
  8. #servo
  9. GPIO.setmode(GPIO.BCM)
  10. GPIO.setup(18, GPIO.OUT)
  11. #Turn L/R DC motor
  12. GPIO.setup(26, GPIO.OUT)
  13. GPIO.setup(19, GPIO.OUT)
  14. GPIO.output(26, 0)       # set port/pin value to 1/GPIO.HIGH/True
  15. GPIO.output(19, 0)       # set port/pin value to 1/GPIO.HIGH/True
  16. #Walking DC motor
  17. GPIO.setup(24, GPIO.OUT)
  18. GPIO.setup(13, GPIO.OUT)
  19. GPIO.output(24, 0)       # set port/pin value to 1/GPIO.HIGH/True
  20. GPIO.output(13, 0)       # set port/pin value to 1/GPIO.HIGH/True
  21.  
  22. pwm=GPIO.PWM(18, 50)
  23. pwm.start(0)
  24. GPIO.setwarnings(False)
  25.  
  26. def temperature_of_raspberry_pi():
  27.     cpu_temp = os.popen("vcgencmd measure_temp").readline()
  28.     return cpu_temp.replace("temp=", "")
  29.  
  30. def help_function():
  31.     print("_________________Help function_________________")
  32.     print("W key: Move forward")
  33.     print("S key: Move backward")
  34.     print("A key: Steer head left")
  35.     print("D key: Steer head right")
  36.     print("E key: Move head up")
  37.     print("Q key: Move head down")
  38.     print("Z key: Move head to the lowest position")
  39.     print("X key: Move head to the highest position")
  40.     print("C key: Move head to middle position")
  41.     print("T key: To show CPU temperature")
  42.     print("P key: To quit program and clear gpio outputs")
  43.     print("H key: For help_function")
  44.     print("Project created by IGI 2022/23. Basic functions")
  45.     print("are implemented.")
  46.     print("I/O pins definitions:")
  47.     print("Servo for the head is atteched in pin 18")
  48.     print("DC motor for F/B is attached in pins 24 and 13")
  49.     print("DC motor for L/R is attached in pins 26 and 19")
  50.    
  51.            
  52. def StartUp():
  53.     SetAngle(70)
  54.     sleep(1)
  55.     SetAngle(16)
  56.     sleep(1)
  57.     SetAngle(30)
  58.     GPIO.output(26, 1)    
  59.     GPIO.output(19, 0)
  60.     sleep(0.5)
  61.     GPIO.output(26, 0)    
  62.     GPIO.output(19, 1)
  63.     sleep(1)
  64.     GPIO.output(26, 1)    
  65.     GPIO.output(19, 0)
  66.     sleep(0.5)
  67.     GPIO.output(26, 0)    
  68.     GPIO.output(19, 0)
  69.     GPIO.output(24, 0)
  70.     GPIO.output(13, 1)
  71.     sleep(0.5)
  72.     GPIO.output(24, 0)
  73.     GPIO.output(13, 0)
  74.     sleep(0.5)
  75.     GPIO.output(24, 1)
  76.     GPIO.output(13, 0)
  77.     sleep(0.5)
  78.     GPIO.output(24, 0)
  79.     GPIO.output(13, 0)
  80.  
  81. def SetAngle(angle):
  82.     duty = angle / 18 + 2
  83.     GPIO.output(18, True)
  84.     pwm.ChangeDutyCycle(duty)
  85.     sleep(0.25)
  86.     GPIO.output(18, False)
  87.     pwm.ChangeDutyCycle(0)
  88.  
  89. def neck_up():      #moves neck servo up
  90.     global val
  91.     val = val + 2
  92.     print(val)
  93.     SetAngle(val)
  94.    
  95. def neck_down():      #moves neck servo down
  96.     global val
  97.     val = val - 2
  98.     print(val)
  99.     SetAngle(val)
  100.  
  101. t0 = time.time()
  102.  
  103. while True:
  104.     # Keyboard character retrieval method is called and saved
  105.     # into variable
  106.        
  107.     if run_once == 0:
  108.         print("Start-up sequence.")
  109.         StartUp()
  110.         print("Motor functionality checked motors perform as they should.")
  111.         sleep(2)
  112.         print("Show help function.")
  113.         sleep(2)
  114.         help_function()
  115.         run_once = 1
  116.        
  117.        
  118.     char = getch.getche()
  119.    
  120.     if (char == "t"):
  121.         print(temperature_of_raspberry_pi())
  122.  
  123.     if(char == "q"):
  124.         print("Neck up")
  125.         neck_up()
  126.  
  127.     if(char == "e"):
  128.         print("Neck down")
  129.         neck_down()
  130.    
  131.     if(char == "z"):
  132.         print("Neck upper position")
  133.         SetAngle(70)
  134.  
  135.     if(char == "x"):
  136.         print("Neck in place (middle position)")
  137.         SetAngle(30)
  138.    
  139.     if(char == "c"):
  140.         print("Neck lower position")
  141.         SetAngle(16)
  142.  
  143.     if(char == "a"):
  144.         print("Neck left")
  145.         GPIO.output(26, 0)    
  146.         GPIO.output(19, 0)
  147.         GPIO.output(26, 1)    
  148.         GPIO.output(19, 0)
  149.         sleep(0.01)
  150.         GPIO.output(26, 0)    
  151.         GPIO.output(19, 0)
  152.  
  153.     if(char == "d"):
  154.         print("Neck right")
  155.         GPIO.output(26, 0)    
  156.         GPIO.output(19, 0)
  157.         GPIO.output(26, 0)    
  158.         GPIO.output(19, 1)
  159.         sleep(0.01)
  160.         GPIO.output(26, 0)    
  161.         GPIO.output(19, 0)
  162.        
  163.     if(char == "w"):
  164.         print("Move Forward")
  165.         GPIO.output(24, 0)    
  166.         GPIO.output(13, 0)
  167.         GPIO.output(24, 0)    
  168.         GPIO.output(13, 1)
  169.         sleep(0.02)
  170.         GPIO.output(24, 0)    
  171.         GPIO.output(13, 0)
  172.        
  173.     if(char == "s"):
  174.         print("Move Backward")
  175.         GPIO.output(24, 0)    
  176.         GPIO.output(13, 0)
  177.         GPIO.output(24, 1)    
  178.         GPIO.output(13, 0)
  179.         sleep(0.02)
  180.         GPIO.output(24, 0)    
  181.         GPIO.output(13, 0)
  182.        
  183.     if(char == "k"):
  184.         print("Dancing")
  185.         GPIO.output(26, 0)    
  186.         GPIO.output(19, 0)
  187.         GPIO.output(26, 0)    
  188.         GPIO.output(19, 1)
  189.         GPIO.output(24, 0)    
  190.         GPIO.output(13, 0)
  191.         GPIO.output(24, 1)    
  192.         GPIO.output(13, 0)
  193.         sleep(0.02)
  194.         GPIO.output(26, 0)    
  195.         GPIO.output(19, 0)
  196.         GPIO.output(24, 0)    
  197.         GPIO.output(13, 0)
  198.                  
  199.     if(char == "h"):
  200.         help_function()
  201.        
  202.        
  203.     # The "p" key will break the loop and exit the program
  204.     if(char == "p"):
  205.         print("Program Ended")
  206.         break
  207.  
  208.     # At the end of each loop
  209.  
  210.     # The keyboard character variable will be set to blank, ready
  211.     # to save the next key that is pressed
  212.     char = ""
  213.  
  214. # Program will cease all GPIO activity before terminating
  215. pwm.stop()
  216. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement