Solved||Self driving car to stop every tym obstacle

#1

Please bare me have least knowledge on programming.
Trying to replicate and want to stop on every time obstacle.

def GoToTurn():
        while my_Distance_portI2C.read() < 50:
                gpg.stop()
        gpg.forward()
        while not  my_linefollower.read_position() == "black" and my_Distance_portI2C.read() > 51:
                if my_linefollower.read_position() == 'center' and my_Distance_portI2C.read() > 51:
                        gpg.forward()
                if my_linefollower.read_position() == 'left' and my_Distance_portI2C.read() > 51:
                        gpg.right()
                if my_linefollower.read_position() == 'right' and my_Distance_portI2C.read() > 51:
                        gpg.left()
        gpg.stop()
        gpg.drive_cm(7,blocking=True)


tried some basic and but seems not working.

original code attached.Python_Self_Driving_Car-Drive_from_John_to_Nicoles-2.py (1.4 KB)

/Chetuk

#2

It would help knowing what you’re trying to achieve. My guess is you want to have the robot stop even when an obstacle appears out of nowhere, is that it ?

I haven’t tested this code but I would try it this way:

def GoToTurn():
    while my_Distance_portI2C.read() < 20:
        gpg.stop()
    gpg.forward()
    while not  my_linefollower.read_position() == "black":
        if my_Distance_portI2C.read() < 20:
            gpg.stop()
        elif my_linefollower.read_position() == 'center':
            gpg.forward()
        elif my_linefollower.read_position() == 'left':
            gpg.right()
        elif my_linefollower.read_position() == 'right':
            gpg.left()
    gpg.stop()
    gpg.drive_cm(7,blocking=True)

Cleo

#3

@cleoqc will try this

Yes Exactly!!

#4

@cleoqc thanks for the recommendation tried but not stable as original.
Movement is not precise with line follow.
Some time leaves the line or does not detect T intersections.

below video with original code only if statements

https://www.youtube.com/watch?v=XlSCMwmsTRI

#5

@chetuk
this sounds more like the lighting may have changed in the room, and you would need to calibrate the line follower again.
Have you tried recalibrating the line follower?

Cleo

#6

@cleoqc Yes I did the calibration seems elif statements take tym in loops and decide.

Where If statements are working seamless.
Anything with threading ?? or mutex can help

#7

Did you try only adding the obstacle detecting “if”, without changing the three line following “if” statements to “elif”? (BTW, this is the “two line solution” I was hoping you would come up with yourself in the very beginning of your adventure.)

(Perhaps the sequence of commanding right, immediately followed by a test for position right is critical, such that the change to elif means only one adjustment per cycle?

def GoToTurn():
    while my_Distance_portI2C.read() < 20:       # test and wait for no obstacle
        gpg.stop()
    gpg.forward()                                                    # start moving
 
    while not  my_linefollower.read_position() == "black":     # loop until reach T or +
        while my_Distance_portI2C.read() < 20:                        # test and wait for no obstacle
            gpg.stop()
        if my_linefollower.read_position() == 'center':      # continue (in dir pointed) if on line
            gpg.forward()
        if my_linefollower.read_position() == 'left':           # adjust if wandering left
            gpg.right()
        if my_linefollower.read_position() == 'right':        # adjust if wandering right
            gpg.left()

    # continue here if found T or +
    gpg.stop()
    gpg.drive_cm(7,blocking=True)

Moving the obstacle checks to a separate thread is a very advanced topic that may or may not result in tighter directional control.

As it is, you do not know how often the loop is being executed, and do not know if multiple corrections in a single loop are occurring. Doctoral students have spent many years on this very subject of control (forward, left, right commands) with feedback (sensor readings).

Another thing to try might be to test for obstacles only if the position is center, not every loop. If the test for obstacles has introduced a control delay, it might be acceptable when centered, but not acceptable when making a correction. (This means moving the two line distance check. Where is left to the reader.)

There is even an optimization you could try by only reading position once each loop (save reading to a variable), and testing the variable. Since you don’t have any timings, this might improve control, or might not. Perhaps this is too advanced coding, so try the other two ideas first.

#8

@cyclicalobsessive appreciate the code with explanation.

I tried above code early but not working as expected it don’t stop at all even obstacle present.

#9

right, the distance if needs to be a while when the following are if.

#10

yes that’s a challenge for me … looking for threading option if can be applicable.

#11
  1. Could you explain “that” in “that’s a challenge for me”
  2. You certainly will not get a threaded solution from me.
#12

I mean line follow n distance that’s a challenge
And will manage solution.