Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Touch Navigation for the cyber:bot

Touch Navigation for the cyber:bot

How the Navigating by Touch Code Works

How the script roaming_with_whiskers works

The first part of roaming_with_whiskers defines the four navigation functions: forward, left, right, and backwards.  Notice how the left, right, and backwards functions all have a sleep function. Every time each of these functions are called, the cyber:bot moves in that direction for a predetermined amount of time. 750 milliseconds is enough time to back up away from an obstacle, and 500 milliseconds makes about a 90 degree turn to the right or the left.  The forward function does not have a sleep function call inside of it. 

def forward():
    bot(18).servo_speed(75)
    bot(19).servo_speed(-75)

def backwards():
    bot(18).servo_speed(-75)
    bot(19).servo_speed(75)
    sleep(750)

def right():
    bot(18).servo_speed(75)
    bot(19).servo_speed(75)
    sleep(500)

def left():
    bot(18).servo_speed(-75)
    bot(19).servo_speed(-75)
    sleep(500)

 

After the four navigation functions have been defined, the script enters into the while True: loop.  Inside of this loop, we have a four-part if statement.  The first thing it checks is if both whiskers are touching, that is, if both whisker_left and whisker_right are equal to 0.  If this is true, then the program runs through the backwards and right functions making the cyber:bot backup and then turn right, presumably away from the obstacle.

    if whisker_left == 0 and whisker_right == 0:     
        backwards()                                 
        right()

 

If whisker_right and whisker_left are not touching, then the program goes to the next part of the if statement which says else if (elif) the left whisker is not touching (whisker_left == 1) and the right whisker is touching (whisker_right == 0), then the program executes the backwards and left functions.

    elif whisker_left == 1 and whisker_right == 0:   
        backwards()                                  
        left()

If the if statement has still not found a true condition, then it checks to see if the left whisker is touching (whisker_left == 0) and the right whisker is not touching (whisker_right == 1).  If this it true, the program executes the functions backwards and right.

    elif whisker_left == 0 and whisker_right == 1:  
        backwards()                                   
        right()

Finally, if all three of the above conditions are not met, the program executes the forward function and drives a little bit before once again checking to see if an obstacle is detected.

Compact Code — The script could have lastly checked:

elif whisker_left == 1 and whisker_right == 1:

…instead of using just else:, however, the only condition remaining at the end was that neither whisker was touching (whisker_right and whisker_left are both equal to 1).  Although both of these would have worked, else: is used because it shortens the code.

Compact it More — To make the script even more compact, shorten the variable names used to define each maneuver and to store the whisker I/O pin states.  This could help if want to combine whisker navigation with other sensor input or with piezospeaker sounds.


Printer-friendly version
Navigating by Touch
Prev
Escaping Corners
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress