2

I am developing a system where I am avoiding an accident by slowing down the motor speed using distance provided by ultrasonic sensor. Also, if accident happens, it must be sense through sw420 and accident occurred result should be displayed. The problem is that, i have code for both motor mgmt. - distance sensing and impact detection. But as I want them to run parallel i.e.., both are running at same time. How to do it? Eg. If car is running smoothly, suddenly another car hits you from, let's say, from back.....then automatically, sw420 sensor should sense it and alert the accident occurred message. Or in other words, some type of multiprocessing concept is needed which i am unaware of.

NOW THIS IS WHAT LATEST I HAVE DONE TILL NOW -

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()

But only, process 1 is running. SW420 is not giving any response when i shake it. Please aware me that where I am wrong.

  • 2
    This is a general Python question. Most people would look at using threads. – joan Mar 29 '18 at 15:21
  • processes do not actually run at the same time (concurrently) .... one runs after another (sequentially) .... if you run them 100 times per second, then it appears that they run at the same time .... so all you have to do, check sensor then manage motor speed, and repeat .... using the threads framework (as @joan mentioned) makes it easier to manage the processes, and easier to add new processes, such as turning and communication – jsotola Mar 29 '18 at 17:02

Browse other questions tagged or ask your own question.