Avoiding obstacles
Planning paths without obstacles, as has been shown, is quite easy. However, it becomes a bit more challenging when your robot needs to walk around the obstacles. Let's look at the case where there is an obstacle in the path that you calculated previously. It might look similar to the following diagram:
You can still use the same path planning algorithm to find the starting angle; however, you'll now need to use your sonar sensor to detect the obstacle. When your sonar sensor detects the obstacle, you'll need to stop and recalculate the path to avoid the barrier, and also recalculate the desired path to the goal. One very simple way to do this is, when your robot senses a barrier, to turn right at 90 degrees, move a fixed distance, and then recalculate the optimum path. When you turn back to move towards the target, if you sense no barrier, you will move along the optimum path.
However, if your robot encounters the obstacle again, it will repeat the process until it reaches...