2

my idea is that i want to make car with sensors for obstacle avoidance using raspberry pi and Python as programming language.

Thus i want to make a separate thread for sensor in way to keep monitoring the sensor and when it detect obstacle it should directly interrupt the main program( which give the movement command to the motor), and let the car stop

İf you can give me code example or just simulation with virtual sensor.

Or if there is a better practice please advice.

1 Answers1

2

since no one was able to help me, and I could not find tutorials that explain this concept. finally I figured a way to do that and I will share it so that others can make use of it hopefully, or suggest me a better practice if there is a one.

import time
from threading import Thread, current_thread



def monitorSensorThread(arg1):
    print "started sensor  montiring"

    while True:
        if(thereisobstacle):
            current_thread().interrupt()


def thereisobstacle():
    ## just virtual sensor to tell if there is an obstacles
    time.sleep(5)
    return True

def stopMovmentAndLookfornewdirection():
    ##the interrupt fuction
    ## in this function i will write somthing to stop the motor and look for another way
    print "stoped"


sensorThread = Thread(target=monitorSensorThread, args=(1,))
sensorThread.daemon=True
sensorThread.start()

try:

    moveforward()

except KeyboardInterrupt:
    print "the main thread interrupted"
  • Did this actually work? I have a similar issue, it’s just that instead of sensor, I have a speech command recognition module and the motor should keep running until I shout stop. The thing is I haven’t found any code in tutorials that dedicates a thread to running the motor. So I am not sure if it is a correct move or not. – Shilan Jul 10 '20 at 21:17