Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time, os
- from time import sleep
- import getch
- global val
- val = 30
- run_once = 0
- #servo
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(18, GPIO.OUT)
- #Turn L/R DC motor
- GPIO.setup(26, GPIO.OUT)
- GPIO.setup(19, GPIO.OUT)
- GPIO.output(26, 0) # set port/pin value to 1/GPIO.HIGH/True
- GPIO.output(19, 0) # set port/pin value to 1/GPIO.HIGH/True
- #Walking DC motor
- GPIO.setup(24, GPIO.OUT)
- GPIO.setup(13, GPIO.OUT)
- GPIO.output(24, 0) # set port/pin value to 1/GPIO.HIGH/True
- GPIO.output(13, 0) # set port/pin value to 1/GPIO.HIGH/True
- pwm=GPIO.PWM(18, 50)
- pwm.start(0)
- GPIO.setwarnings(False)
- def temperature_of_raspberry_pi():
- cpu_temp = os.popen("vcgencmd measure_temp").readline()
- return cpu_temp.replace("temp=", "")
- def help_function():
- print("_________________Help function_________________")
- print("W key: Move forward")
- print("S key: Move backward")
- print("A key: Steer head left")
- print("D key: Steer head right")
- print("E key: Move head up")
- print("Q key: Move head down")
- print("Z key: Move head to the lowest position")
- print("X key: Move head to the highest position")
- print("C key: Move head to middle position")
- print("T key: To show CPU temperature")
- print("P key: To quit program and clear gpio outputs")
- print("H key: For help_function")
- print("Project created by IGI 2022/23. Basic functions")
- print("are implemented.")
- print("I/O pins definitions:")
- print("Servo for the head is atteched in pin 18")
- print("DC motor for F/B is attached in pins 24 and 13")
- print("DC motor for L/R is attached in pins 26 and 19")
- def StartUp():
- SetAngle(70)
- sleep(1)
- SetAngle(16)
- sleep(1)
- SetAngle(30)
- GPIO.output(26, 1)
- GPIO.output(19, 0)
- sleep(0.5)
- GPIO.output(26, 0)
- GPIO.output(19, 1)
- sleep(1)
- GPIO.output(26, 1)
- GPIO.output(19, 0)
- sleep(0.5)
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- GPIO.output(24, 0)
- GPIO.output(13, 1)
- sleep(0.5)
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- sleep(0.5)
- GPIO.output(24, 1)
- GPIO.output(13, 0)
- sleep(0.5)
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- def SetAngle(angle):
- duty = angle / 18 + 2
- GPIO.output(18, True)
- pwm.ChangeDutyCycle(duty)
- sleep(0.25)
- GPIO.output(18, False)
- pwm.ChangeDutyCycle(0)
- def neck_up(): #moves neck servo up
- global val
- val = val + 2
- print(val)
- SetAngle(val)
- def neck_down(): #moves neck servo down
- global val
- val = val - 2
- print(val)
- SetAngle(val)
- t0 = time.time()
- while True:
- # Keyboard character retrieval method is called and saved
- # into variable
- if run_once == 0:
- print("Start-up sequence.")
- StartUp()
- print("Motor functionality checked motors perform as they should.")
- sleep(2)
- print("Show help function.")
- sleep(2)
- help_function()
- run_once = 1
- char = getch.getche()
- if (char == "t"):
- print(temperature_of_raspberry_pi())
- if(char == "q"):
- print("Neck up")
- neck_up()
- if(char == "e"):
- print("Neck down")
- neck_down()
- if(char == "z"):
- print("Neck upper position")
- SetAngle(70)
- if(char == "x"):
- print("Neck in place (middle position)")
- SetAngle(30)
- if(char == "c"):
- print("Neck lower position")
- SetAngle(16)
- if(char == "a"):
- print("Neck left")
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- GPIO.output(26, 1)
- GPIO.output(19, 0)
- sleep(0.01)
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- if(char == "d"):
- print("Neck right")
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- GPIO.output(26, 0)
- GPIO.output(19, 1)
- sleep(0.01)
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- if(char == "w"):
- print("Move Forward")
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- GPIO.output(24, 0)
- GPIO.output(13, 1)
- sleep(0.02)
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- if(char == "s"):
- print("Move Backward")
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- GPIO.output(24, 1)
- GPIO.output(13, 0)
- sleep(0.02)
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- if(char == "k"):
- print("Dancing")
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- GPIO.output(26, 0)
- GPIO.output(19, 1)
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- GPIO.output(24, 1)
- GPIO.output(13, 0)
- sleep(0.02)
- GPIO.output(26, 0)
- GPIO.output(19, 0)
- GPIO.output(24, 0)
- GPIO.output(13, 0)
- if(char == "h"):
- help_function()
- # The "p" key will break the loop and exit the program
- if(char == "p"):
- print("Program Ended")
- break
- # At the end of each loop
- # The keyboard character variable will be set to blank, ready
- # to save the next key that is pressed
- char = ""
- # Program will cease all GPIO activity before terminating
- pwm.stop()
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement