超声波传感器,直流电机和Python中的sw420

时间:2018-03-22 15:30:01

标签: python python-3.x raspberry-pi3 sensor

我正在开发一种系统,我可以通过超声波传感器提供的距离减慢电机速度来避免事故。此外,如果发生事故,通过sw420必须有意义,并且应显示事故发生的结果。 问题是,我有两个电机管理的代码。 - 距离感应和冲击检测。但是因为我希望它们并行运行,但两者都在同时运行。怎么做? 例如。如果汽车运行平稳,突然有另一辆车撞到你,让我们说,从后面.....然后自动,sw420传感器应该感知它并警告事故发生的消息。 换句话说,我需要一些我不知道的多处理概念。

这是我迄今为止所做的事情 - 1.当超声波传感器检测到较短的距离时,电机完全减速。

import RPi.GPIO as GPIO
import time

# Define GPIO For Driver motors
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(7,gpio.OUT)


pwm=GPIO.PWM(18,100)

# Define GPIO for ultrasonic central
GPIO_TRIGGER_CENTRAL = 23
GPIO_ECHO_CENTRAL = 24
GPIO.setup(GPIO_TRIGGER_CENTRAL, GPIO.OUT)  # Trigger > Out
GPIO.setup(GPIO_ECHO_CENTRAL, GPIO.IN)      # Echo < In




# Functions for driving


def goforward():
    pwm.start(100)
    GPIO.output(12, True)
    GPIO.output(16, False)
    GPIO.output(18, True)



def goforwardslow():

    GPIO.output(12, True)
    GPIO.output(16, False)
    GPIO.output(18, True)

def stopmotors():
    GPIO.output(12, False)
    GPIO.output(16, False)
    GPIO.output(18, False)


def buzzer():
try:
    while True:
            gpio.output(7,0)
            time.sleep(0.2)
            gpio.output(7,1)
            time.sleep(0.2)

except KeyboardInterrupt:
            gpio.cleanup()
            exit


#Detect front obstacle
def frontobstacle():

    # Set trigger to False (Low)
    GPIO.output(GPIO_TRIGGER_CENTRAL, False)
    # Allow module to settle
    time.sleep(0.2)
    # Send 10us pulse to trigger
    GPIO.output(GPIO_TRIGGER_CENTRAL, True)
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER_CENTRAL, False)
    start = time.time()
    while GPIO.input(GPIO_ECHO_CENTRAL) == 0:
        start = time.time()
    while GPIO.input(GPIO_ECHO_CENTRAL) == 1:
        stop = time.time()
    # Calculate pulse length
    elapsed = stop - start
    # Distance pulse travelled in that time is time
    # Multiplied by the speed of sound (cm/s)
    distance = elapsed * 17150 # distance of both directions so divide by 2
    print "Front Distance : %.1f" % distance
    return distance



# Check front obstacle and stop  if there is an obstacle
def checkanddrivefront():
    while frontobstacle() < 15:
        pwm.ChangeDutyCycle(25)
       buzzer()
        goforwardslow()
time.sleep(3)
goforward()



# Avoid obstacles and drive forward
def obstacleavoiddrive():
    goforward()
    start = time.time()
    # Drive 5 minutes
    while start > time.time() - 300:  # 300 = 60 seconds * 5
        if frontobstacle() < 15:
            pwm.ChangeDutyCycle(25)
            goforwardslow()
            checkanddrivefront()
        #elif rightobstacle() < 30:
          #  stopmotors() 
           # checkanddriveright()
        #elif leftobstacle() < 30:
          #  stopmotors()
            #checkanddriveleft()


# Clear GPIOs, it will stop motors       
    #cleargpios()


#def cleargpios():
    #print "clearing GPIO"
    #GPIO.output(12, False)
    #GPIO.output(13, False)
    #print "All GPIOs CLEARED"


def main():
    # First clear GPIOs
    #cleargpios()
    print "start driving: "
    # Start obstacle avoid driving
    obstacleavoiddrive()

if __name__ == "__main__":
    main()
  1. sw420也检测到振动

    将RPi.GPIO导入为GPIO     从时间导入睡眠

    channel = 17
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(channel, GPIO.IN)
    sleep(0.1)
    
    while True:
            result = GPIO.input(channel)
            if result == 1:
                print ("Vibration")
    
  2. 现在的问题是,我应该在哪里放置或如何在MAIN CODE中使用振动/ sw420代码,以便两者并行运行,并且每当发生事故时,sw420&#39;代码比电机代码占优先级并发生意外发生了消息。

    请分享您的建议。

    现在这是我现在已经完成的事情 -

    import RPi.GPIO as GPIO
    import time
    from multiprocessing import Process
    
    # Define GPIO For Driver motors
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)
    GPIO.setup(16, GPIO.OUT)
    GPIO.setup(18, GPIO.OUT)
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    
    GPIO.setup(7,GPIO.OUT)
    pwm=GPIO.PWM(18,100)
    
    
    channel = 11
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(channel, GPIO.IN)
    sleep(0.1)
    
    
    
    
    # Define GPIO for ultrasonic central
    GPIO_TRIGGER_CENTRAL = 23
    GPIO_ECHO_CENTRAL = 24
    GPIO.setup(GPIO_TRIGGER_CENTRAL, GPIO.OUT)  # Trigger > Out
    GPIO.setup(GPIO_ECHO_CENTRAL, GPIO.IN)      # Echo < In
    
    
    
    
    # Functions for driving
    
    
    def goforward():
        pwm.start(100)
        GPIO.output(12, True)
        GPIO.output(16, False)
        GPIO.output(18, True)
    
    
    
    def goforwardslow():
    
        GPIO.output(12, True)
        GPIO.output(16, False)
        GPIO.output(18, True)
    
    def stopmotors():
        GPIO.output(12, False)
        GPIO.output(16, False)
        GPIO.output(18, False)
    
    
    def buzzer():
        while True:
                GPIO.output(7,0)
                time.sleep(0.2)
                GPIO.output(7,1)
                time.sleep(0.2)
    
    def vib():
    
    while True:
            result = GPIO.input(channel)
            if result == 1:
                print ("Vibration")
    
    
    
    
    #Detect front obstacle
    def frontobstacle():
    
        # Set trigger to False (Low)
        GPIO.output(GPIO_TRIGGER_CENTRAL, False)
        # Allow module to settle
        time.sleep(0.2)
        # Send 10us pulse to trigger
        GPIO.output(GPIO_TRIGGER_CENTRAL, True)
        time.sleep(0.00001)
        GPIO.output(GPIO_TRIGGER_CENTRAL, False)
        start = time.time()
        while GPIO.input(GPIO_ECHO_CENTRAL) == 0:
            start = time.time()
        while GPIO.input(GPIO_ECHO_CENTRAL) == 1:
            stop = time.time()
        # Calculate pulse length
        elapsed = stop - start
        # Distance pulse travelled in that time is time
        # Multiplied by the speed of sound (cm/s)
        distance = elapsed * 17150 # distance of both directions so divide by 2
        print "Front Distance : %.1f" % distance
        return distance
    
    # Check front obstacle and stop  if there is an obstacle
    def checkanddrivefront():
        while frontobstacle() < 15:
            pwm.ChangeDutyCycle(25)
            buzzer()
            goforwardslow()
    time.sleep(3)
    goforward()
    
    
    
    # Avoid obstacles and drive forward
    def obstacleavoiddrive():
        goforward()
        start = time.time()
        # Drive 5 minutes
        while start > time.time() - 300:  # 300 = 60 seconds * 5
            if frontobstacle() < 15:
                pwm.ChangeDutyCycle(25)
                buzzer()
                goforwardslow()
                checkanddrivefront()
            #elif rightobstacle() < 30:
              #  stopmotors() 
               # checkanddriveright()
            #elif leftobstacle() < 30:
              #  stopmotors()
                #checkanddriveleft()
    
    
    # Clear GPIOs, it will stop motors       
        #cleargpios()
    #def cleargpios():
        #print "clearing GPIO"
    
    
    def main():    
    print "start driving: "
        # Start obstacle avoid driving
        obstacleavoiddrive()
    
    
    
    if __name__ == "__main__":
    
        p1 = multiprocess.Process(target = main)
    
        p2 = multiprocess.Process(target = vib)
        p1.start()
        p2.start()
    

    我已经使用了多处理....但是,只有电机驱动功能正在执行,振动代码在代码中不活动....意味着没有输出。

0 个答案:

没有答案