-1

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 doesn't run, but it runs, but it doesn't run "properly"... what does any of that mean in terms of *actual observable behavior*? – hobbs Jul 22 '22 at 14:30
  • I hear a very faint sound from the sensor but it does not work – OsamaAlhajjaj Jul 22 '22 at 14:43
  • Is it possible that issue is related to indent before while loop? – ZabielskiGabriel Jul 22 '22 at 15:03
  • Please [edit] your question and describe better what the problem is. "Doesn't work" is not helpful. What happens when you run the code, and what did you expect to happen instead? Any errors? See [ask]. – Robert Jul 22 '22 at 15:30

1 Answers1

1

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.

jprebys
  • 2,469
  • 1
  • 11
  • 16