Exercise: Using a Regulated Motor as a Servo

A servo is a motor where we control its movements in finer detail than just turn on or off. Typically we want to move the motor in a range less than one revolution. This allows us to use the servo (motor) to control things like arms (raise/lower) and grippers (open/close). A Regulated Motor allows us to move the motor to a specific angle or by a specific angle. This allows us to have finer control over the movement of the motor and that allows us to control more complex mechanisms than things that just rotate.

What's Next

If you are using the EV3 platform, it is simple enough that you have learned enough Java to do some actual programming. You can skip to Unit 9 and start working on some EV3 example programs. As you proceed in the examples you may see Java concepts and constructs that are covered in Unit 8. After doing some of the examples you should return to Unit 8 and complete it as it covers topics that, while not needed to get started, likely will be needed as you write more serious robot programs.

Packages and Imports

Classes within a project or in a library are organized into packages. A package is simply an identifier specified at the top of a class with the package statement. In a project, all classes with the same package name are grouped together under that name. Packages are important when we want to use libraries of classes published by other programmers, such as the Java Class Library or one of the robot specific libraries.




Line Number: x y z k
5 0 0 0 0
8 0 0 9 0
14 0 0 9 0
24 0 0 3 0
27 0 0 0 0
36 0 0 7 0
43 0 0 0 0
56 0 0 99 33
65 0 0 2 0
74 0 0 0 0
87 0 0 9 0



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 Intertial Measurement Unit. This is a sensor that can measure acceleration (movement) in several axis. 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.