CSC 161 Grinnell College Fall, 2013 Imperative Problem Solving and Data Structures

# Laboratory Exercise on Scribbler 2 Motion and Loops

## Goals

The goal of this lab is to continue practice with C loops while also gaining some experience with moving the robot around.

## Exercises

Before beginning the following exercises, move to your labs directory and create a file named `loops-motion-lab.c` for this lab. Remember to set the forwardness in every program with motion using either the command rSetForwardnessTxt("fluke-forward"); or rSetForwardnessTxt("scribbler-forward");. For this lab, you will want to set the forwardness to "fluke-forward".

### Simple Motion Commands

Often, you will want to write programs that have the Scribbler 2 move for varying amounts of time. In `loops-motion-lab.c`, write commands that perform the following actions.

1. Write a loop which moves the scribbler forward 5 times.

2. Write a loop which moves the Scribbler in some direction for increasing amounts of time.

3. Write a loop which moves the Scribbler some number of times at changing speeds.

4. Try changing both speed and time in the same loop.

### Motion with Beeping

Here you will combine motion and beeping in both simultaneous and sequential ways.

1. Tell the robot to move forward for one second, then beep. Run your code when it is finished

Pseudocode:

```            connect to the robot
move forward for one second
beep for one second
disconnect from the robot
```
2. Now tell the robot to move forward (without specifying a time), then then beep, then stop. Try out your program when it is ready.

Pseudocode:

```            connect to the robot
move forward
beep for one second
stop
disconnect from the robot
```

Use a nonblocking command such as ```rMotors (double leftSpeed, double rightSpeed)``` or `rMove (double translate, double rotate)`. You can stop the robot with `rStop()`.

3. Now, switch the move and beep commands. What happens?

Pseudocode:

```            connect to the robot
beep for one second
move forward
stop
disconnect from the robot
```
4. Now add in the sleep command!

Pseudocode:

```            connect to the robot
beep for one second
move forward
sleep
stop
disconnect from the robot
```

Use the command sleep (int seconds) to make your program sleep.

5. Why do you need the sleep command to see any motion when you beep first and then move?

### Spiral Motion

Lets make the robot move spirally! Spirals begin from a center point, with the line moving in a circular motion, with a gradually greater distance from the origin. There are two straightforward ways, each using only one loop, that moves the Scribbler 2 in a spiral motion for at least ten seconds.

1. Write a program that makes the Scribbler 2 robot behave like turtles in CSC 151. So, the robot would move forward, then turn, then move forward a little further, then turn, and so on in a spiral shape.

2. Write the program that makes the Scribbler 2 robot move in a spiral pattern using a single command. So, the robot would be turning while moving forward.

Hint: use nonblocking motion calls together with the sleep function.

### Motion and Looped Sound

You now know how to make the robot beep while moving. You also learned in a previous lab how to use a loop to make rising pitches. Now, you will combine the two actions.

1. Write a program that moves the Scribbler 2 while it beeps fifteen times in a row, with every beep rising in pitch, then stops the Scribbler 2.

### Motion With Obstacles

While blind motion can be interesting, sensing obstacles is where motion really gets awesome!

1. Write a simple program which moves the robot forward until it sees an obstacle. Use the rGetObstacleNum(int sensor) function.

2. Write a simple program which moves the robot forward until it sees an obstacle, then turns right, then moves forward again until it sees an obstacle.

3. Now generalize your program so that your robot moves forward until it sees an obstacle, then turns right, moves forward until it sees an obstacle, turns right, moves forward until obstacle, turns right, etc. It should do this until the program is terminated.

## Feedback Welcome

Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.