9. #Servo pin. Set as the pin of the servo on your product that can control the left
and right rotation of the ultrasonic module.
10. servoPin = 1
11.
12. GPIO.setmode(GPIO.BCM)
13. GPIO.setup(UPin1, GPIO.OUT,initial=GPIO.LOW)
14. GPIO.setup(Upin2, GPIO.IN)
15.
16. pwm = Adafruit_PCA9685.PCA9685()
17. pwm.set_pwm_freq(50)
18.
19. pwm0_init = 300
20. pwm0_max = 450 # Set the maximum scanning angle
21. pwm0_min = 150 # Set the minimum scanning angle
22. pwm0_pos = pwm0_init
Define scan function.
1. def checkdist():
2. ''' Refer to the basic module to control ultra.Py '''
3.
4. def radarScan(): # Main function of ultrasonic scanning function
5. global pwm0_pos
6. scan_speed = 3 # Scanning speed
7.
8. # Move the servo to the maximum angle
9. pwm0_pos = pwm0_max
10. pwm.set_pwm(servoPin, 0, pwm0_pos)
11. time.sleep(0.5)
12.
13. # Rotate the servo and continuously measure the distance during the period
until it reaches the minimum angle.
14. print(str(checkdist()), 'm')
15. while pwm0_pos>pwm0_min:
16. pwm0_pos-=scan_speed
17. pwm.set_pwm(servoPin, 0, pwm0_pos)
18. print(str(checkdist()), 'm')
19.
20. # Reply
21. pwm.set_pwm(servoPin, 0, pwm0_init)