IF Statement Quiz 3 Answers



IF Statement Quiz 3 Answers



Exercise: Using a PID Controller

With the test robot used to develop this course, there is a problem with the previous examples of turning under gyro or IMU control. When turning at a constant power setting and setting the power to zero when the target angle is reached, depending on motor configuration, gear ratio, robot weight and the turn power, the robot will most likely not stop quick enough to be on the desired angle. This is called overshoot. On our test robot, a 90 degree turn would end up being 110-120 degrees. Fixing this can be tricky to do manually but there is an automated way to better control the turn.

Exercise: Using the REV IMU

The REV Expansion Hub has a built-in IMU, or Inertial Measurement Unit. This is a sensor that can measure acceleration (movement) in several axes. It can be used in place of an external gyro. The IMU is not used in quite the same way as the gyro but is similar. Note: you must configure the IMU on I2C channel 0, port 0. Here is the DriveAvoid example converted to use the IMU in place of the MR gyro.



We are now going to take a look at logging (also called tracing) as a tool to debug our robot programs. Logging is recording useful information from the robot program to a disk file on the RoboRio controller. You can then download that file to your PC and examine it. It can be very useful to record information while your robot is running during a match so you can look at it afterwards and see what took place. If you have not read the general lesson on Logging, you should do that now.


1. An encoder measures the rotations of a motor shaft either optically or magnetically and makes a count of shaft movements available to your program on the DcMotor class. There are typically a number of counts per revolution of the motor shaft.


2. motor.setmode(DcMotor.RunMode.RUN_TO_POSITION);

Runs motor to the position (encoder count) you set with setTargetPosition() at the power level set with setPower() and stops the motor when the target position is reached.


Runs the motor at a constant speed set as a percentage of motor maximum speed with the setPower() method. Does not stop the motor based on the current target position.


Runs the motor without any automatic action based on encoder counts but does count and report encoder counts.


3. Using the number of encoder counts returned for one revolution of the motor shaft, set the target position to that number and run mode RUN_TO_POSITION. Apply power.


4. Set the run mode to RUN_WITH_ENCODER, set the speed to run with setPower(). Enter a wait for wait loop for wo seconds then set the power to zero.


5. The code sets the wrong run mode for using setTargetPosition(). The code does not set the power level of the motor to start movement.




1. What is an encoder? How does it work?



2. There are three main settings for encoders. Briefly explain what each does (which is running at a set speed, which is running at a set power?)








3. Describe the process for setting a motor to turn one full rotation and then stop.



4. Describe the process for setting a motor to run at a consistent speed for two seconds and then stop.




5. This code is supposed to make the motor turn half a rotation. It never moves. There are two errors, what are they? (This is not a configuration problem)

        motor = hardwareMap.dcMotor.get("ml");


        startPosition = motor.getCurrentPosition();


        motor.setTargetPosition(startPosition + 500);

Here are the answers.



Unit 1 Review

1. Draw a picture/diagram showing the flow of steps from a programming idea all the way to running code.

2. What is the difference between source code and binary object code?

3. What does an API do for us?

4. What is the difference between an SDK and API?


1. We have not declared variable a. Line 1 would generate a compiler error.

1a. Declaration creates a variable with it's data type and allocates appropriate memory space based on the data type. A variable must be declared before it's first use. Initialization is assigning a starting value to a newly created variable.

2. Only a single variable can appear left of the assignment ( = ) operator.

3. a = 7, b = 9.

4. a = 7, b = 9, c = 63.

5. a = 7, b = 63, c= 63.

6. a = 63, b = 63, c = 63.

7. a = 63, b = 63, c = 5.

8. The student thinks the definition of c has been permanently changed by line 8 to be b * a. c is a simple integer variable and only contains the numeric value assigned to it. The result of line 8 is c = 63.

9. A compiler error due to the fact we are trying to assign a floating point number to an integer. The Java compiler can't convert the float to an integer without loss of accuracy.

10. The same compiler error for the same reason.




Given the following statements, answer the questions below:

1) a = 15;

2) int b;

3) b = 12;

4) a = a + 5;

5) b - 2 = 10;

6) int a = b - 5;

7) b = 9;

8) int c = b * a;

9) b = c;

10) a = b;

11) c = 5;

12) c = 3.2;

13) float f = 121.23;

14) c = f;

1. After the code up to line 4 has run, b will indeed be equal to 12, but a will NOT be equal to 20. There actually is an error. What is wrong with what we have done with a up to line 4?

1a. What is the difference between declaration and initialization? (When do you need to say the type of variable, like int)

2. Line 5 also will make java unhappy and won’t do anything (and will actually prevent us from compiling). Why?

3. After line 7, what will the variables a and b have stored in them?

4. After line 8, what will the values of a, b and c be?

5. After line 9, what will the values of a, b and c be?

6. After line 10, what will the values of a, b and c be?

7. After line 11, what will the values of a, b and c be?

8. A student thinks that at the end of line 10, a= 63, b = 63, and c = 3969 (which is 63*63). What mistake are they making?

9. What is the result of line 12?

10. What is the result of line 14.

The answers are on the next page.