def servo_distance_check(): servo_appointed_direction(0) time.sleep(0.8) rightdistance = avoid_ultrasonic.Distance_test() servo_appointed_direction(180) time.sleep(0.8) leftdistance = avoid_ultrasonic.Distance_test() servo_appointed_direction(90) time.sleep(0.8) frontdistance = avoid_ultrasonic.Distance_test() global car_angle if leftdistance < 30 and rightdistance < 30 and frontdistance < 30: CarRun.spin_right(0.65, 35) CarRun.brake(0) car_angle -= 180 servo_appointed_direction(180) elif leftdistance >= rightdistance: CarRun.spin_left(0.4, 50) CarRun.brake(0) car_angle += 90 servo_appointed_direction(0) elif leftdistance <= rightdistance: CarRun.spin_right(0.4, 100) time.sleep(5) CarRun.brake(0) car_angle -= 90 servo_appointed_direction(180) time.sleep(0.8)
def servo_color_carstate(): servo_appointed_detection(0) time.sleep(0.8) rightdistance = avoid_ultrasonic.Distance_test() servo_appointed_detection(180) time.sleep(0.8) leftdistance = avoid_ultrasonic.Distance_test() servo_appointed_detection(90) time.sleep(0.8) frontdistance = avoid_ultrasonic.Distance_test() if leftdistance < 30 and rightdistance < 30 and frontdistance < 30: CarRun.spin_right(0.58, 35) elif leftdistance >= rightdistance: CarRun.spin_left(0.28, 35) elif leftdistance <= rightdistance: CarRun.spin_right(0.28, 35)
def look_for_opening(power): global car_angle distance = avoid_ultrasonic.Distance_test() while distance < 40: travelling_infra_avoid(power) distance = avoid_ultrasonic.Distance_test() CarRun.run(0.5, 30) CarRun.brake(0) if car_angle < 0: CarRun.spin_left(0.5, 50) car_angle += 90 else: CarRun.spin_right(0.5, 50) car_angle -= 90 CarRun.brake(0) servo_appointed_direction(90) time.sleep(0.8)
if leftdistance < 30 and rightdistance < 30 and frontdistance < 30: CarRun.spin_right(0.58, 35) elif leftdistance >= rightdistance: CarRun.spin_left(0.28, 35) elif leftdistance <= rightdistance: CarRun.spin_right(0.28, 35) #delay 2s time.sleep(2) #The try/except statement is used to detect errors in the try block. #the except statement catches the exception information and processes it. try: init() while True: distance = avoid_ultrasonic.Distance_test() if distance > 60: infrared_avoid.infr_avoid(50) elif 30 <= distance <= 60: infrared_avoid.infr_avoid(20) elif distance < 30: CarRun.brake(0) servo_color_carstate() except KeyboardInterrupt: pass pwm_ENA.stop() pwm_ENB.stop() GPIO.cleanup()