[car] # This is the parmaters that will control the car's wheels # The number is the interface number if GPIO(GPIO.BCM) #board 11 12 13 15 IN1 = 17 IN2 = 18 IN3 = 27 IN4 = 22
#!/usr/bin/env python # -*- coding: utf-8 -*- # 传感器探测障碍物类型 import RPi.GPIO as GPIO import time import configparser from infrared import Infrared from ultrasonic import Ultrasonic
#Define the number of all the GPIO taht used for 4wd car classSense(): def__init__(self): config = configparser.ConfigParser() config.read("config.ini") trigger_l=config.getint("ultrasonic","Trigger_l") trigger_r=config.getint("ultrasonic","Trigger_r") echo_l=config.getint("ultrasonic","Echo_l") echo_r=config.getint("ultrasonic","Echo_r") single_pin=config.getint("infrared","single_pin") self.Sonic_l = Ultrasonic(trigger_l,echo_l) self.Sonic_r = Ultrasonic(trigger_r,echo_r) self.Infrared = Infrared(single_pin) #get obstacles type defdetection(self): #NO Obstacles or 20cm safe distance type 0 if self.Infrared.check_distance() == "OK": return"Fgo" #Obstacles ahead #self.Infrared.check_distance() == "Warning" else : l_d = self.Sonic_l.get_distance() r_d = self.Sonic_r.get_distance() #Obstacles ahead&&right =safe type a if ((l_d>=30)and(r_d>=30)): return"Lgo" #Obstacles ahead&&right R>L type b elif ((l_d<=30) and(r_d>30)): return"Rgo" #Obstacles ahead&&right L>R type c elif ((l_d>30) and(r_d<=30)): return"Lgo" #Obstacles ahead&&right L&R<safe type d elif ((l_d<30) and(r_d<30)): return"Bgo" defask_distance_l(self): return self.Sonic_l.get_distance() defask_distance_r(self): return self.Sonic_r.get_distance() if __name__ == '__main__': t = Sense() whileTrue: s=t.detection() print(s) time.sleep(0.5)