Planning paths without obstacles is, as has been shown, quite easy. However, it becomes a bit more challenging when your robot needs to walk around obstacles. Let's look at a case where there is an obstacle in the path you calculated previously. It might look like this:
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. To keep this all very simple, you'll do this in steps. You'll turn and move toward the obstacle for a one-time distance. Then, you'll check whether there is a barrier. When your sonar sensor detects the obstacle, ...