Stap 7: Controle van de servo 's
Servo's zijn belangrijk in de ontwikkeling van de robot, hier zou je over het bepalen van de servo's van 0 - 180 graden in 3 stappen (dwz 0, 90 en 180 graden)
Hier heb ik gebruikt Futuba s3003 servo - de verbinding is zeer eenvoudig de "+" & "-" gaat naar de onze batterij (in mijn geval dezelfde als voor mijn 4wd dwz instellen van 4 AA cellen).
Het is belangrijk om te kijken naar de bedrijfsspanning van servo (in mijn geval zijn 4.8-6 V zie afbeelding hierboven), de signaal draad te worden aangesloten op de GPIO output is (Zie de pin niet in python programma hieronder)
Goed beheersen servomotoren in de raspberry pi kan lastig zijn maar dan is er zeer krachtige bibliotheek gehost @ RPIO. PWM, om het te installeren op de pi gebruik de volgende code
import RPi.GPIO as GPIO import time from time import sleep from subprocess import call GPIO.setmode(GPIO.BCM) GPIO.setup(21,GPIO.OUT) def takestillpic(num): # function block which accepts user defined photo character print" please enter photo character" num = raw_input() call ( ["raspistill -vf -hf -o " + str(num) + ".jpg" ],shell=True ) while True: inp = raw_input() if inp == "1": call ( ["raspistill -vf -hf -o image.51jpg" ],shell=True ) print" phot done please" elif inp == "2": num = raw_input() takestillpic(num) print " photo please" GPIO.output(21,True) # connect this output to buzzer sleep(1) GPIO.output(21,False)<br>
en hier is de python code voor het draaien van de servo's (om meer te weten over RPIO. PWM en de DMA gebruikt, verwijzen wij u naar de link: https://pythonhosted.org/RPIO/pwm_py.html )
# ultrasonic distance measurement import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 13 ECHO = 26 print" Distance measurement in progress" GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG,False) print" waiting for sensor to settle please" time.sleep(2) GPIO.output(TRIG,True) time.sleep(0.00001) GPIO.output(TRIG,False) while GPIO.input(ECHO)==0: pulse_start=time.time() while GPIO.input(ECHO)==1: pulse_end=time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance,2) print " Distance ", distance, "cm" if distance > 50: print " GOOD one " # these are just some reference of distance you can change it else: print " move on" GPIO.cleanup() # You can now integrate the above code with robotics control as mentioned in earlier steps to make more complex application, the same is now left as exercise :)