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)
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)
@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.
@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?
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.