Lab: Conditionals

The purpose of this lab is to introduce different types of conditional statements in C, practice simple control flow, and gain experience using the Scribbler 2 sensors.

Preparation

When using the Scribbler 2 robots, activities may depend upon the immediate environment of the robot. To learn about this environment, Scribbler 2 robots contain several sensors. This lab utilizes four types of sensors to provide experience with conditional statements.

Be sure to review the description of Sensors for the Scribbler 2 Robot before proceeding in this lab. Some sensors are located on the robot itself, and some are located on the Fluke that plugs into the robot. A common error for beginners is to program based on sensors located in a different place than expected.

When working with the Scribbler robots, conditionals are how the robot can be programmed to make decisions based on data gathered from its sensors.

As electrical instruments, all sensors are subject to experimental error. That is, multiple queries to a sensor may yield different results. For some sensors, this variability may be modest, but for other sensors, successive queries may produce substantially different results.

To manage some variability, the MyroC package takes several readings and averages. In particular, most sensor functions include a second parameter that specifies how many values from the sensor should be averaged. For example, the following call takes 3 readings and returns their average:

int avgValue = rGetLightTxt ("left", 3);

Of course, with this syntax, getting a single reading corresponds to giving the second parameter the value 1.

Using Light Sensors

According to the documentation on Sensors for the Scribbler 2 Robot, the light sensors on the front of the Scribbler 2 robots are reasonably consistent, so we start there. Note that the light sensors return values near 0 for bright lights and large values (about 65,000) when the area is quite dark.

  1. Copy the program light-sensor-example.c to your account. Recall you should be able to use the make command to compile it. Compile the program and run it several times to determine how it works.
    • For some runs, do not move the robots, and examine the output to determine what variability is present in the readings printed.
    • For some runs, move the robot so it is facing a relatively bright light or a relatively dim area. Again, compare the results obtained.
  2. Add statements to determine the light levels in front of the robot and on the right. Update your variable names so all remain clear.
    • To what extent do the light levels seem to vary from the left to the center and to the right?
    • How might you change the robot's environment (e.g., cover some sensors, or create a shadow over part of the robot) to impact the light readings?

Using the Obstacle Sensors

One set of obstacle sensors utilize the IR sensors on the Scribbler 2 body, and another set of obstacle sensors utilize sensors on the fluke. Again, review Sensors for the Scribbler 2 Robot for details.

  1. Use the IR sensors on the Scribbler 2 body to determine whether there is an obstacle in front of the robot. This can be done using the the rGetIRTxt function. Unlike the light sensors, the function returns only a one (true), or zero (false) depending on whether there is an obstacle close to the scribbler's IR sensors.

    As with the light-sensor-example.c program, report several readings, and comment upon the variability that you observe.

  2. Change the robot's behavior, depending upon whether or not an obstacle is present. If an obstacle is present, the program should beep three times at a high pitch. If there are not obstacles in front of any of the sensors, it should move forward for 1 second.

Avoiding Obstacles

Testing one sensor is great, but the robot has two IR sensors. It would be more interesting if we tested both the sensors and acted according to the resulting values. To do this you can employ an "else if" test sequence.

  1. Rather than beeping, instruct the robot to turn if it senses an obstacle in front of one of its sensors. That is, if there is an obstacle in front of the left sensor, turn right, and if there is an obstacle in front of the right sensor, turn left. This is a simple, but effective, way to avoid an obstacle.

    Change your code so the robot behaves as described. One way to proceed would be to use the following form:

    if ( left sensor )
        turn right;
    else if ( right sensor )
        turn left;

    Compile, run, and verify your code works before moving on.

    For future references (i.e., a later exercise), test what happens in the case where there is an obstacle in front of both sensors.

Another Conditional: the switch

  1. Program light-sensor-switch.c is modified from the earlier light-sensing programs. In the original program, categories of light intensities were identified through nested if ... else if statements. Since the categories were organized in 10,000 groupings of light intensities, division of the light intensity by 10000 provides a simple way to identify the relevant category. This gives rise to the use of a switch statement.

    Compile and run light-sensor-switch.c under different light conditions. Then review the code to determine how it works.

  2. Remove the break statements from the program, and run them again. Explain in your own words what purpose the break statements address.

The && and || Operators

To combine two tests, use the && (AND), and || (OR) operators. If you want certain code to execute only if both tests are true, use the AND operator. If your want to do something if either test is true use the OR operator.

  1. Copy the program combine-tests.c to your working directory and open it in an editor. This program is similar to parts of what you may have done in Steps 3 and 4 of this lab.
    1. Modify the program so that the robot moves backwards if both sensors are blocked. That is,
      • if both sensors are blocked, the robot should move backward
      • if only the left sensor is blocked, the robot should turn right
      • if only the right sensor is blocked, the robot should turn left
    2. As a further variation, insert the following code within your program.
      if ( leftIR || rightIR )
        rBeep (1, 550);
      What happens when you put an obstacle in front of any of the sensors?

Switch to avoid obstacles

Steps 3 and 4 used the IR sensors to infer whether or not an obstacle was located in front of the Scribbler 2 body. In this Step, you are asked to use the obstacle sensors on the Fluke that plugs into the Scribbler 2. Values returned from the rGetObstacleTxt function range from 0 to 6000.

  1. Write a program that behaves in different ways, according to the data obtained from the rGetObstacleTxt function.
    1. Query the robot for the value of its obstacle sensors (these are the IR sensors located on the fluke dongle).
    2. Convert the result to a range from 0 to 5 (or 1 to 6).
    3. Use a switch statement that instructs the robot to perform one of 6 actions based on the range of values from rGetObstacleTxt.

    Some ideas might include moving forward for longer amounts of time the farther away the robot is from an obstacle, or beeping longer or multiply times if the robot is closer to an object.

    Hint: Remember integer division truncates any decimal in C.