2020-09-21 11:09:43 -05:00
|
|
|
/* Copyright (c) 2017 FIRST. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted (subject to the limitations in the disclaimer below) provided that
|
|
|
|
* the following conditions are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
* of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of FIRST nor the names of its contributors may be used to endorse or
|
|
|
|
* promote products derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
|
|
|
|
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.firstinspires.ftc.robotcontroller.external.samples;
|
|
|
|
|
|
|
|
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
|
|
|
|
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
|
|
|
|
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
|
|
|
|
import com.qualcomm.robotcore.hardware.DcMotor;
|
2022-07-22 23:50:42 -05:00
|
|
|
import com.qualcomm.robotcore.hardware.Servo;
|
2020-09-21 11:09:43 -05:00
|
|
|
import com.qualcomm.robotcore.util.ElapsedTime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file illustrates the concept of driving a path based on encoder counts.
|
|
|
|
* The code is structured as a LinearOpMode
|
|
|
|
*
|
|
|
|
* The code REQUIRES that you DO have encoders on the wheels,
|
2022-07-22 23:50:42 -05:00
|
|
|
* otherwise you would use: RobotAutoDriveByTime;
|
2020-09-21 11:09:43 -05:00
|
|
|
*
|
|
|
|
* This code ALSO requires that the drive Motors have been configured such that a positive
|
2022-07-22 23:50:42 -05:00
|
|
|
* power command moves them forward, and causes the encoders to count UP.
|
2020-09-21 11:09:43 -05:00
|
|
|
*
|
|
|
|
* The desired path in this example is:
|
|
|
|
* - Drive forward for 48 inches
|
|
|
|
* - Spin right for 12 Inches
|
2022-07-22 23:50:42 -05:00
|
|
|
* - Drive Backward for 24 inches
|
2020-09-21 11:09:43 -05:00
|
|
|
* - Stop and close the claw.
|
|
|
|
*
|
|
|
|
* The code is written using a method called: encoderDrive(speed, leftInches, rightInches, timeoutS)
|
|
|
|
* that performs the actual movement.
|
2022-07-22 23:50:42 -05:00
|
|
|
* This method assumes that each movement is relative to the last stopping place.
|
2020-09-21 11:09:43 -05:00
|
|
|
* There are other ways to perform encoder based moves, but this method is probably the simplest.
|
|
|
|
* This code uses the RUN_TO_POSITION mode to enable the Motor controllers to generate the run profile
|
|
|
|
*
|
2022-07-22 23:50:42 -05:00
|
|
|
* Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
|
2020-09-21 11:09:43 -05:00
|
|
|
* Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
|
|
|
|
*/
|
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
@Autonomous(name="Robot: Auto Drive By Encoder", group="Robot")
|
2020-09-21 11:09:43 -05:00
|
|
|
@Disabled
|
2022-07-22 23:50:42 -05:00
|
|
|
public class RobotAutoDriveByEncoder_Linear extends LinearOpMode {
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
/* Declare OpMode members. */
|
2022-07-22 23:50:42 -05:00
|
|
|
private DcMotor leftDrive = null;
|
|
|
|
private DcMotor rightDrive = null;
|
|
|
|
|
2020-09-21 11:09:43 -05:00
|
|
|
private ElapsedTime runtime = new ElapsedTime();
|
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
// Calculate the COUNTS_PER_INCH for your specific drive train.
|
|
|
|
// Go to your motor vendor website to determine your motor's COUNTS_PER_MOTOR_REV
|
|
|
|
// For external drive gearing, set DRIVE_GEAR_REDUCTION as needed.
|
|
|
|
// For example, use a value of 2.0 for a 12-tooth spur gear driving a 24-tooth spur gear.
|
|
|
|
// This is gearing DOWN for less speed and more torque.
|
|
|
|
// For gearing UP, use a gear ratio less than 1.0. Note this will affect the direction of wheel rotation.
|
2020-09-21 11:09:43 -05:00
|
|
|
static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: TETRIX Motor Encoder
|
2022-07-22 23:50:42 -05:00
|
|
|
static final double DRIVE_GEAR_REDUCTION = 1.0 ; // No External Gearing.
|
2020-09-21 11:09:43 -05:00
|
|
|
static final double WHEEL_DIAMETER_INCHES = 4.0 ; // For figuring circumference
|
|
|
|
static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
|
|
|
|
(WHEEL_DIAMETER_INCHES * 3.1415);
|
|
|
|
static final double DRIVE_SPEED = 0.6;
|
|
|
|
static final double TURN_SPEED = 0.5;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void runOpMode() {
|
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
// Initialize the drive system variables.
|
|
|
|
leftDrive = hardwareMap.get(DcMotor.class, "left_drive");
|
|
|
|
rightDrive = hardwareMap.get(DcMotor.class, "right_drive");
|
2020-09-21 11:09:43 -05:00
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
// To drive forward, most robots need the motor on one side to be reversed, because the axles point in opposite directions.
|
|
|
|
// When run, this OpMode should start both motors driving forward. So adjust these two lines based on your first test drive.
|
|
|
|
// Note: The settings here assume direct drive on left and right wheels. Gear Reduction or 90 Deg drives may require direction flips
|
|
|
|
leftDrive.setDirection(DcMotor.Direction.REVERSE);
|
|
|
|
rightDrive.setDirection(DcMotor.Direction.FORWARD);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
leftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
|
|
|
|
rightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
leftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
|
|
|
|
rightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
// Send telemetry message to indicate successful Encoder reset
|
2022-07-22 23:50:42 -05:00
|
|
|
telemetry.addData("Starting at", "%7d :%7d",
|
|
|
|
leftDrive.getCurrentPosition(),
|
|
|
|
rightDrive.getCurrentPosition());
|
2020-09-21 11:09:43 -05:00
|
|
|
telemetry.update();
|
|
|
|
|
|
|
|
// Wait for the game to start (driver presses PLAY)
|
|
|
|
waitForStart();
|
|
|
|
|
|
|
|
// Step through each leg of the path,
|
|
|
|
// Note: Reverse movement is obtained by setting a negative distance (not speed)
|
|
|
|
encoderDrive(DRIVE_SPEED, 48, 48, 5.0); // S1: Forward 47 Inches with 5 Sec timeout
|
|
|
|
encoderDrive(TURN_SPEED, 12, -12, 4.0); // S2: Turn Right 12 Inches with 4 Sec timeout
|
|
|
|
encoderDrive(DRIVE_SPEED, -24, -24, 4.0); // S3: Reverse 24 Inches with 4 Sec timeout
|
|
|
|
|
|
|
|
telemetry.addData("Path", "Complete");
|
|
|
|
telemetry.update();
|
2022-07-22 23:50:42 -05:00
|
|
|
sleep(1000); // pause to display final telemetry message.
|
2020-09-21 11:09:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Method to perform a relative move, based on encoder counts.
|
|
|
|
* Encoders are not reset as the move is based on the current position.
|
|
|
|
* Move will stop if any of three conditions occur:
|
|
|
|
* 1) Move gets to the desired position
|
|
|
|
* 2) Move runs out of time
|
|
|
|
* 3) Driver stops the opmode running.
|
|
|
|
*/
|
|
|
|
public void encoderDrive(double speed,
|
|
|
|
double leftInches, double rightInches,
|
|
|
|
double timeoutS) {
|
|
|
|
int newLeftTarget;
|
|
|
|
int newRightTarget;
|
|
|
|
|
|
|
|
// Ensure that the opmode is still active
|
|
|
|
if (opModeIsActive()) {
|
|
|
|
|
|
|
|
// Determine new target position, and pass to motor controller
|
2022-07-22 23:50:42 -05:00
|
|
|
newLeftTarget = leftDrive.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
|
|
|
|
newRightTarget = rightDrive.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
|
|
|
|
leftDrive.setTargetPosition(newLeftTarget);
|
|
|
|
rightDrive.setTargetPosition(newRightTarget);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
// Turn On RUN_TO_POSITION
|
2022-07-22 23:50:42 -05:00
|
|
|
leftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
|
|
|
|
rightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
// reset the timeout time and start motion.
|
|
|
|
runtime.reset();
|
2022-07-22 23:50:42 -05:00
|
|
|
leftDrive.setPower(Math.abs(speed));
|
|
|
|
rightDrive.setPower(Math.abs(speed));
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
// keep looping while we are still active, and there is time left, and both motors are running.
|
|
|
|
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
|
|
|
|
// its target position, the motion will stop. This is "safer" in the event that the robot will
|
|
|
|
// always end the motion as soon as possible.
|
|
|
|
// However, if you require that BOTH motors have finished their moves before the robot continues
|
|
|
|
// onto the next step, use (isBusy() || isBusy()) in the loop test.
|
|
|
|
while (opModeIsActive() &&
|
|
|
|
(runtime.seconds() < timeoutS) &&
|
2022-07-22 23:50:42 -05:00
|
|
|
(leftDrive.isBusy() && rightDrive.isBusy())) {
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
// Display it for the driver.
|
2022-07-22 23:50:42 -05:00
|
|
|
telemetry.addData("Running to", " %7d :%7d", newLeftTarget, newRightTarget);
|
|
|
|
telemetry.addData("Currently at", " at %7d :%7d",
|
|
|
|
leftDrive.getCurrentPosition(), rightDrive.getCurrentPosition());
|
2020-09-21 11:09:43 -05:00
|
|
|
telemetry.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop all motion;
|
2022-07-22 23:50:42 -05:00
|
|
|
leftDrive.setPower(0);
|
|
|
|
rightDrive.setPower(0);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
|
|
|
// Turn off RUN_TO_POSITION
|
2022-07-22 23:50:42 -05:00
|
|
|
leftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
|
|
|
|
rightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
|
2020-09-21 11:09:43 -05:00
|
|
|
|
2022-07-22 23:50:42 -05:00
|
|
|
sleep(250); // optional pause after each move.
|
2020-09-21 11:09:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|