🔧 Chapter 3: Servos

Visual Blocks Examples
Set servo to position 0 (one end)
set servo1 .position to 0
Set servo to center position
set servo1 .position to 0.5
Set servo to position 1 (other end)
set servo1 .position to 1
Complete servo OpMode
Initialize servo to start position
set servo1 .position to 0
repeat while opModeIsActive
if gamepad1.a
set servo1 .position to 1
else
set servo1 .position to 0
Blocks Guide

Programming Servos

Programming Servos

This section addresses the Smart Robot Servo in its default mode. If your servo has been modified to function in continuous mode or with angular limits, it will not behave identically using the provided code examples.

What is a Servo?

A servo functions as an actuator or device designed for moving. With a typical servo, you can specify a target position. The servo will turn its motor shaft to move to the target position, and then maintain that position, even if moderate forces are applied to try and disturb its position.

For Hello Robot, the Smart Robot Servo is used, which can switch between a continuous and angular mode.

  • Continuous mode allows the servo to rotate a full 360 degrees, either direction, indefinitely similar to a standard motor.
  • Angular mode sets the servo to move to specified positions within a 270 degree range of motion.
  • Understanding Servo Positions

    While most common servos have a range of 180 degrees for motion, the Smart Robot Servo has a range of 270 degrees due to its ability to switch between modes. When programming, the 0 and 1 positions may differ from typical expectations.

    When the servo is at default position 0, it will be at -135 degrees. At position 1, the servo moves to +135 degrees. Therefore, to return to 0 degrees, you would need to program it to move to position 0.5.

    A servo horn attachment connected to your Smart Robot Servo may affect where 0 degrees appears. Using a SRS programmer to set the servo to zero before adding attachments is recommended.

    Quick Check - Basic Positions

    | Programmed Position | Degrees |

    | ------------------- | ------- |

    | 0 | -135 |

    | 0.5 | 0 |

    | 1 | 135 |

    Consider these two questions:

    1. If you wanted your servo to move to -67.5 degrees, what position would you program it to move to?

    2. If you have programmed your servo to move to position 0.7, what would that equal in degrees?

    Answers

    1. Breaking it down, -67.5 degrees is halfway of the movement between 0 degrees and -135 degrees. Therefore, set the position to halfway between 0 and 0.5, equaling 0.25.

    2. This requires additional calculation. Each 0.1 of position equals 27 degrees (270 degrees divided by 10). Starting from the 0 degrees at position 0.5, moving to position 0.7 equals 54 degrees.

    For Hello Robot programming, only positions are used. Understanding their translation to degrees remains important for designing a mechanism.

    Let's get Programming!

    In the following sections, you will learn to program your servo to first move automatically to different requested positions, then in response to your gamepad's input.

    The next sections include:

  • Programming Servo Basics
  • Using a Gamepad with a Servo
  • Programming Servo Telemetry
  • Level Up: OnBot Java versions

    Programming Servos (Java)

    Programming Servos - OnBot Java

    What is a Servo?

    This section is considering the Smart Robot Servo in its default mode. If your servo has been changed to function in continuous mode or with angular limits it will not behave the same using the code examples below.

    A servo functions as a device designed for moving where users can specify target positions. The shaft maintains that position despite moderate external forces.

    The Hello Robot implementation uses the Smart Robot Servo with two operational modes:

  • Continuous mode: Full 360 degree rotation in either direction, indefinitely
  • Angular mode: Movement to specified positions within 270 degree range
  • Position-to-Degree Mapping

    | Position | Degrees |

    |----------|---------|

    | 0 | -135 |

    | 0.5 | 0 |

    | 1 | 135 |

    When programming this means our 0 and 1 position might be a little different than what you'd expect.

    Quick Check Practice Problems

    1. Position needed for -67.5 degree movement: 0.25 (halfway between 0 and 0.5)

    2. Degree equivalent of position 0.7: 54 degrees (calculated using 0.1 position = 27 degrees formula)

    A servo horn attachment connected to your Smart Robot Servo may affect where 0 degrees appears. We recommend using a SRS programmer to set the servo to zero before adding attachments.
    For Hello Robot we will only be programming using positions. Understanding their translation to degrees is still important, however, to help think through designing a mechanism.

    Sample Code

    @TeleOp
    public class HelloRobot_TeleOp extends LinearOpMode {
        private Blinker control_Hub;
        private DcMotor test_motor;
        private Servo test_servo;
        private TouchSensor test_touch;
    
        @Override
        public void runOpMode() {
            control_Hub = hardwareMap.get(Blinker.class, "Control Hub");
            test_motor = hardwareMap.get(DcMotor.class, "test_motor");
            test_servo = hardwareMap.get(Servo.class, "test_servo");
            test_touch = hardwareMap.get(TouchSensor.class, "test_touch");
    
            test_servo.setPosition(0);
    
            telemetry.addData("Status", "Initialized");
            telemetry.update();
            // Wait for the game to start (driver presses PLAY)
            waitForStart();
    
            // run until the end of the match (driver presses STOP)
            while (opModeIsActive()) {
                if (gamepad1.y){
                    //move to position 0
                    test_servo.setPosition(0);
                } else if (gamepad1.x || gamepad1.b) {
                    //move to position 0.5
                    test_servo.setPosition(0.5);
                } else if (gamepad1.a) {
                    //move to position 1
                    test_servo.setPosition(1);
                }
                telemetry.addData("Servo Position", test_servo.getPosition());
                telemetry.addData("Status", "Running");
                telemetry.update();
            }
        }
    }
    ← 📝 Your First OpMode⚡ Motors →