so I have made a code where it detects an obstacle it brakes elif it runs the trackok.py code
the problem is when it starts the trackok.py code it stays in it and doesn't stop even if an obstacle is in front of it.
this is the piece of code that has this algorithm
try:
init()
while True:
GPIO.output(TrigPin, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(TrigPin, GPIO.LOW)
while GPIO.input(EchoPin)==0:
pulse_start_time = time.time()
GPIO.output(TrigPin,1)
time.sleep(0.00001)
GPIO.output(TrigPin,0)
while GPIO.input(EchoPin)==1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
if distance > 5:
os.system("python /home/pi/trackok.py")
elif distance < 5:
brake()
except KeyboardInterrupt:
pass
pwm_ENA.stop()
pwm_ENB.stop()
GPIO.cleanup()
for the full code and the trackok.py code, I will leave a link in here trackok.py Full code
edit1: Now I am sure the problem is in the trackok.py I did this code and it keeps on printing the distance until it gets to the distance where it should start the trackok.py code it then runs that code and keeps looping in it without calculating any new distances here is the code new code
edit2: Ok so I combined both codes in 1 algorithm and I get this error now the same thing happens tracking the line is working but ultrasonic runs for until tracking is on and then no more distance reading here is the code newest code the thing is I get this error now
Traceback (most recent call last):
File "/home/pi/Trackerultrasonic.py", line 49, in <module>
pwm_ENA = GPIO.PWM(ENA, 2000)
RuntimeError: A PWM object already exists for this GPIO channel
edit3: This is the nearest I have reached now it runs the tracker and should light the blue led and brake() when it detects an obstacle the issue is it detects the obstacle and the blue led lights up but doesn't brake() and it continues on the track here is the newest code I have reached newest code