Improve the Control App Part 2

Here is the second code drop for you. We now add a second function which will navigate the robot around an obstacle.

  • It use the distance sensor to navigate around obstacles

  • Puts the code to drive around obstacles in the function bypass_obstacle()

  • Creates some more parameters to test, optimize and tune different values in your code

Add the new "bypass_obstacle()" function

Below the new function def search_for_hat: add this function (as usual check you identation)

# Check for an obstacle directly in front and bypass it, if existing
def bypass_obstacle():
    # Determine if an obstacle is in sight
    obstacle_in_sight = distance_int() <= min_distance_to_obstacle

    # Only continue if an obstacle is ahead
    if not obstacle_in_sight:
        return

    # For debugging only
    take_picture('static/current_view.jpg')

    # Determine distance to obstacle
    distance_to_object = distance_int()

    print('### Bypass Obstacle Mode - START ###')

    # Turn left
    turn_left(angle_delta)

    # Determine if there is another obstacle is in sight
    obstacle_in_sight = distance_int() <= min_distance_to_obstacle
    print ('Got distance -> ', distance())

    # If no other obstacle is in the bypass direction, move a bit forward
    # and then go back again on course
    if not obstacle_in_sight:
        move_forward(20)
        turn_right(angle_delta)

    # Determine if original obsctacle is still in sight (after having turned back in original direction)
    obstacle_in_sight = distance_int() <= min_distance_to_obstacle
    print ('Got distance -> ', distance())

    # If original obstacle is not in sight anymore, move forward to bypass it
    if not obstacle_in_sight:
        # Move forward using the original distance to the obstacle and a buffer
        move_forward(math.ceil(distance_to_object / 10) + 40)

    print('### Bypass Obstacle Mode - END: SUCCESS! ###')

Now first test the new obstacle avoidance code by commenting out the function search_for_hat()) from the startRobot() function and adding the bypass_obstacle() function like so:

    # Drop your code here
    # Initialize switch for identifying found and intercepted hats across functions
    global hat_found_and_intercepted
    hat_found_and_intercepted = False

    # Main loop running until one hat is properly identified and intercepted (or the app ist stopped)
    while thread_event.is_set() and not hat_found_and_intercepted:
        # Check for an obstacle directly in front and bypass it, if existing
        bypass_obstacle()

        # Search for the hat and intercrept it
        #search_for_hat()

    print('Done')

Give the code a try. Place a barrier in front of the robot and test the object avoidance code.

Try to change some of the value parameters and see if you can tune the performance of your robot.

Putting it together

You should by now have tested both functions, it’s time to combine both to enable the robot to do challenge two as shown in the next chapter. The robot has to:

  • navigate around a barrier

  • find the hat and drive towards it

You have worked enough with the code to tackle this without a lot of instructions. Go on and try it out!

Exercise

Your mission: Your robot will be positioned in front of a barrier. It needs to drive around it, indentify the red fedora behind it and drive towards it, stopping before.

The playing field will look like in the first challenge with a barrier added:

challenge2