小车接线图:
主机光敏传感器接线图:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from gpiozero import Robot from time import sleep from gpiozero.pins.pigpio import PiGPIOFactory from gpiozero import Button from signal import pause from gpiozero import LightSensor sensor = LightSensor(26,charge_time_limit=0.002) factory = PiGPIOFactory(host='raspberry00.local') robot = Robot(left=(15, 14), right=(18, 23),pin_factory=factory) Bup = Button(21) def moveup(): robot.forward(1) Bup.when_pressed = moveup Bup.when_released = robot.stop sensor.when_dark = robot.stop sensor.when_light = moveup pause() |
通过LDR调速程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
from gpiozero import Robot from time import sleep from gpiozero.pins.pigpio import PiGPIOFactory from gpiozero import Button from signal import pause from gpiozero import LightSensor sensor = LightSensor(26,charge_time_limit=0.003) factory = PiGPIOFactory(host='raspberry00.local') robot = Robot(left=(15, 14), right=(18, 23),pin_factory=factory) Bup = Button(21) def moveup(): robot.forward(1) Bup.when_pressed = moveup Bup.when_released = robot.stop #sensor.when_dark = robot.stop #sensor.when_light = moveup #while True: # print('val:',sensor.value) # sleep(0.1) # if sensor.value > 0.25: # robot.forward(sensor.value) # else : # robot.stop() def drv(): while True: a = (sensor.value) a = a if a>0.25 else 0 sleep(0.1) yield (a,a) robot.source = drv() pause() |
转载请注明:徐自远的乱七八糟小站 » 20181012 LDR控制ROBOT