Hello! I am trying to program the XRP robot following the website’s curriculum. I am currently on the obstacle avoidance section and this is the code I have:
When I try this, the robot detects the obstacle (although at slightly less than 10 cm I believe) but after it detects and turns some angle, it drives in an arc and creates a sort of semicircle. I am not sure why it drives in an arc instead of directly turning around and driving straight; there are no other obstacles in other directions that could be confusing it.
I would really appreciate any guidance on how to fix this. Thank you!
The code snippet you posted calls for random angle for turn, so it picks a number of degrees randomly. If you want to turn around and drive straight again you need to specify it. (180 degrees).
You can alter your code by commenting (#)out turn degrees line:
#turnDegrees = random.randint(135,225)
and changing:
drivetrain.turn(turnDegrees) to drivetrain.turn(180)
Not sure what else is going on with your code from the snippet posted but this code shows drive straight, turn completely around, and drive straight:
from XRPLib.defaults import *
def test_drive():
print("Driving forward 25cm")
drivetrain.straight(25, 0.8)
time.sleep(1)
print("turn 180 degrees left")
drivetrain.turn(180,0.8)
time.sleep(1)
drivetrain.straight(25,0.8)
test_drive()
Thank you for responding. I think my question was not worded properly. Let me rephrase:
I want the robot to turn in place (at any random angle) once it detects an object nearby (within 10 cm).
What is currently happening is that after detecting an object, the robot turns while driving in an arc similar to a u-turn and returns to a similar direction.
For example, if it detects a wall, it turns around and drives in an arc to return to facing the wall - so it keeps moving along the wall basically.
Please let me know if you need any other details about it. Thank you for the help!
I think the problem is that you never stop the forward motion of the drivetrain before you turn. Try putting a drivetrain.stop() before you do your turn.