Introduction to Robotics/Control Flow/Lab/Students

From Wikiversity
Jump to navigation Jump to search


Note[edit]

This lab will use the same constant values for "Left", "Right", "LeftStop", "RightStop", "EndLoop1", "Turn", and so on. In each program you write in this lab, you should define all these values.

FOR / NEXT Loops[edit]

To move our BoeBot in a square, we could write a nested FOR / NEXT loop. Remember to define your constants from the previous lab!

FOR MyTurns = 0 TO 4
   FOR MyCount = 0 TO EndLoop6
       PULSOUT Left, LeftStop + 50
       PULSOUT Right, RightStop – 50
       PAUSE 20
   NEXT
   FOR MyCount = 0 TO Turn
       PULSOUT Left, LeftStop + 50
       PULSOUT Right, RightStop + 50
       PAUSE 20
   NEXT
NEXT

This code uses a loop to move the BoeBot forward, and a loop to turn the BoeBot to the right. The outer loop repeats this process 4 times. When nesting loops, you must use different counter variables.

Problems[edit]

Do these exercises and demonstrate them for your teacher.

  1. Use Nested FOR / NEXT loops to move the Robot along a square 3 times.
  2. Use nested FOR / NEXT loops to move the Robot in a zig-zag pattern (forward, right, forward, left, forward...)

Branches[edit]

Do these exercises and demonstrate them for your teacher.

  1. Use branches, labels, and GOTOS to make the robot move forward for 6 inches without using any FOR / NEXT loops. Is this easier or harder then using loops?
  2. Our BoeBot is lazy and needs to take a break. Write a program using both FOR / NEXT loops and IF / THEN branches that lets the BoeBot take a 1 second break every inch for 6 inches.

Subroutines[edit]

Write subroutines for each of the following manuevers:

  • Move forward 1 inch
  • Move forward 6 inches
  • Move forward 12 inches
  • Turn 90° to the right
  • Turn 90° to the left
  • Move in a 6 inch right-handed square
  • move in a 6 inch left-handed square

Use your subroutines from above to make your BoeBot move in the following patters:

  1. 3 laps of a square (either direction)
  2. A figure 8
  3. A 4-leaf clover shape, or a 2 × 2 checkerboard.

Demonstrate these patterns to your teacher.

Putting it Together[edit]

Our BoeBots don't always move in a straight line, and sometimes we need to correct them. The Instructor will draw a 12-foot straight line on the ground. Your job is to write a program that tries to follow this line, even if your robot does not move straight. Write the following program:

FOR MyCount = 0 TO EndLoop12 * 12
   PULSOUT Left, LeftStop + 65
   PULSOUT Right, RightStop + 50
   PAUSE 20
NEXT
RETURN

Notice the "+ 65" on the left wheel, which means the BoeBot will not be moving in a straight line. Using Branches, loops, or subroutines (or a combination of all 3), try to find a way to follow the straight line that the Instructor has drawn. You may only add new code to this program, you may not delete or change the code that is given. You may add code inside the loop, after the loop or before the loop, but you may not change or delete these lines.

Demonstrate this for your instructor. The instructor may want to check your code to ensure that you have not changed or deleted any of the above code.