简体   繁体   中英

Python code doesn't execute. No errors showed (raspberry pi)

I had a problem with the pigpio pin factory But with time I think I solved a problem , but basically my code runs, but it doesn't execute properlyI'm not sure what I did wrong it just ignores How would I fix it? just this!!!!

Python 3.7.3 (/usr/bin/python3)

%Run Robot.py

from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import PWMOutputDevice, DistanceSensor
import time
from time import sleep

factory = PiGPIOFactory()
sensor1 = DistanceSensor(echo=24,trigger=23, pin_factory=factory) 

#LEFT Motor
PWM_FORWARD_LEFT_PIN=13         #IN1-LEFT motor Forward input
PWM_REVERSE_LEFT_PIN=19         #IN2 LEFT motor Reverse input

forwardLeft=PWMOutputDevice(PWM_FORWARD_LEFT_PIN,True,0,1000)
reverseLeft=PWMOutputDevice(PWM_REVERSE_LEFT_PIN,True,0,1000)

#RIGHT Motor
PWM_FORWARD_RIGHT_PIN=5         #IN1-RIGHT motor Forward input
PWM_REVERSE_RIGHT_PIN=6         #IN2-RIGHT motor Reverse input
                     
forwardRight=PWMOutputDevice(PWM_FORWARD_RIGHT_PIN,True,0,1000)
reverseRight=PWMOutputDevice(PWM_REVERSE_RIGHT_PIN,True,0,1000)    
        
def stop():
    forwardLeft.value=0
    reverseLeft.value=0
    forwardRight.value=0
    reverseRight.value=0

def forward():                                                 
    forwardLeft.value=0.5
    reverseLeft.value=0
    forwardRight.value=0.4
    reverseRight.value=0

def reverse():
    forwardLeft.value=0
    reverseLeft.value=0.5
    forwardRight.value=0
    reverseRight.value=0.4   
                                                
def left():
    forwardLeft.value=0.2
    reverseLeft.value=0
    forwardRight.value=0.6
    reverseRight.value=0

def right():
    forwardLeft.value=.6
    reverseLeft.value=0
    forwardRight.value=0.2
    reverseRight.value=0

while True:
#distance is returned in meters
 distance_to_object = sensor1.distance * 100

#Avoid objects less then 40 cm away
 if distance_to_object <= 40:
    reverse()
    time.sleep(0.5)
    right() #right turn
    time.sleep(0.5) #0.25seconds
 else:
    forward() #keep moving forward
    time.sleep(0.1) #0.1 seconds

It appears from reading the docs , that you may be missing a call to PWMOutputDevice.on() . So maybe call .on() on each of the four instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM