how do I wire and configure a servo and then control it from an opmode
Wiring the Servo
- Connect the standard 3-wire servo cable to one of the 6 built-in servo ports on the Hub, such as the port labeled "0" .
- Ensure that the black ground wire of the servo cable aligns with the ground pin, which is located on the left side of the servo port .
- If using extension wires, heavy-duty wires are recommended . Secure the connections between the extension wires and the servo wires with electrical tape to prevent them from loosening over time .
Configuring the Servo
- Before programming, you must create a configuration file on the Robot Controller .
- On the Driver Station app, open the pop-up menu by touching the three vertical dots, select Configure Robot, and tap New .
- Select your Expansion Hub Portal and then select the Expansion Hub to display its available ports .
- Touch Servos to open the Servo Configuration screen .
- Use the dropdown control to select "Servo" as the servo type for port #0 . To utilize the full 500-2500 microsecond range of your servo instead of the default 600-2400 microsecond range, select "Full Range Servo" instead .
- Specify a name for the servo (for example, "Release Servo") and press Done to complete and save the configuration .
Controlling the Servo from an Op Mode
Using Blocks
In the Blocks Programming Tool, you can control the servo by specifying a target position ranging from 0 to 1 . A target position of 0 corresponds to 0 degrees of rotation, and a target position of 1 corresponds to 180 degrees of rotation for a typical servo . You can map gamepad buttons to send specific position values (such as 0, 0.5, or 1) to the servo .
Using Java (OnBot Java or Android Studio)
To control the servo in Java, instantiate the Servo class and use setPosition() to command the servo to a target position between 0 and 1 .
Servo relicServo = hardwareMap.get(Servo.class, "Release Servo");
releaseServo.setPosition(0.75);
telemetry.addData("Release Servo Target", releaseServo.getPosition());
The getPosition() method returns the current target position of the servo, not its physical position . For advanced control, such as setting a custom PWM range or disabling the PWM signal to let the servo de-energize, cast the Servo object to ServoImplEx .