Announcement

Collapse
No announcement yet.

Initialization problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Philbot
    replied
    Hi KinaK

    I scan the forums regularly to see if people need help, and it gets confusing to track issues if people post the same question on multiple sub-forums.
    I just answered this question on the main forum, and I now see it here again.

    Tip: If the question relates to Java programming in Android Studio, this sub-forum is the best place for it.
    If you post it on the other forum by mistake, rest assured, we'll still find it.
    Double posting will not get an answer faster . It's like hitting the elevator button twice to get it to come faster....

    Phil.

    Leave a comment:


  • KinaK
    started a topic Initialization problem

    Initialization problem

    I'm trying this program (below) and when I hit initialized it automatically starts...
    Also, I am trying to get the robot to do the following; go forward for 48 inches, turn left 90 degrees, drive forward another 54 inches, and then back up 58 inches and stop. I have commented on where it is supposed to complete these actions.

    Update on code:
    package org.firstinspires.ftc.robotcontroller.external.sam ples;

    import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.util.ElapsedTime;

    /**
    * This file illustrates the concept of driving a path based on encoder counts.
    * It uses the common Pushbot hardware class to define the drive on the robot.
    * The code is structured as a LinearOpMode
    *
    * The code REQUIRES that you DO have encoders on the wheels,
    * otherwise you would use: PushbotAutoDriveByTime;
    *
    * This code ALSO requires that the drive Motors have been configured such that a positive
    * power command moves them forwards, and causes the encoders to count UP.
    *
    * The desired path in this example is:
    * - Drive forward for 48 inches
    * - Spin right for 12 Inches
    * - Drive Backwards for 24 inches
    * - Stop and close the claw.
    *
    * The code is written using a method called: encoderDrive(speed, leftInches, rightInches, timeoutS)
    * that performs the actual movement.
    * This methods assumes that each movement is relative to the last stopping place.
    * 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
    *
    * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
    * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
    */

    @Autonomous(name="Auto_Encoder", group="Test Programs")
    public class Encoder_Auto_test extends LinearOpMode {

    /* Declare OpMode members. */
    HardwareDriveTrain robot = new HardwareDriveTrain(); // Use a Pushbot's hardware
    private ElapsedTime runtime = new ElapsedTime();

    static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: TETRIX Motor Encoder
    static final double DRIVE_GEAR_REDUCTION = 2.0 ; // This is < 1.0 if geared UP
    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.4;
    static final double TURN_SPEED = -0.8;

    @Override
    public void runOpMode() throws InterruptedException {

    /*
    * Initialize the drive system variables.
    * The init() method of the hardware class does all the work here
    */
    robot.init(hardwareMap);

    waitForStart();

    // Send telemetry message to signify robot waiting;
    telemetry.addData("Status", "Resetting Encoders"); //
    telemetry.update();

    robot.topL.setMode(DcMotor.RunMode.STOP_AND_RESET_ ENCODER);
    robot.topR.setMode(DcMotor.RunMode.STOP_AND_RESET_ ENCODER);
    robot.botL.setMode(DcMotor.RunMode.STOP_AND_RESET_ ENCODER);
    robot.botR.setMode(DcMotor.RunMode.STOP_AND_RESET_ ENCODER);
    idle();

    robot.topL.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    robot.topR.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    robot.botL.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    robot.botR.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);

    // Send telemetry message to indicate successful Encoder reset
    telemetry.addData("Path0", "Starting at %7d :%7d",
    robot.topL.getCurrentPosition(),
    robot.topR.getCurrentPosition(),
    robot.botL.getCurrentPosition(),
    robot.botR.getCurrentPosition());
    telemetry.update();

    // Wait for the game to start (driver presses PLAY)

    robot.init(hardwareMap);
    waitForStart();

    // Step through each leg of the path,
    // Note: Reverse movement is obtained by setting a negative distance (not speed)

    while (opModeIsActive()) {
    if (runtime.seconds() < 2.2) {
    encoderDrive(DRIVE_SPEED, 49, 49, 49, 49, 2.2); // S1: Forward 48 Inches with 5 Sec timeout
    }
    }

    while (opModeIsActive()) {
    if (runtime.seconds() > 2.2 && runtime.seconds() < 2.83) {
    encoderDrive(TURN_SPEED, 12, 12, 12, 12, .63);// S2: Turn Right 12 Inches with 4 Sec timeout
    }
    }

    while (opModeIsActive()) {
    if (runtime.seconds() > 2.83 && runtime.seconds() < 5.63)
    encoderDrive(DRIVE_SPEED, 55, 55, 55, 55, 2.8); //s3: Drive forward 54 Inches with 4 Sec timeout
    wait();
    }

    while (opModeIsActive()) {
    if (runtime.seconds() > 5.63 && runtime.seconds() < 8.43)
    encoderDrive(-DRIVE_SPEED, -59, -59, -59, -59, 2.8); // S4: Reverse 58 Inches with 4 Sec timeout
    }
    // robot.leftClaw.setPosition(1.0); // S4: Stop and close the claw.
    //robot.rightClaw.setPosition(0.0);
    //sleep(1000); // pause for servos to move

    telemetry.addData("Path", "Complete");
    telemetry.update();
    }

    /*
    * Method to perfmorm 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 TLInches, double TRInches, double BLInches, double BRInches,
    double timeoutS) throws InterruptedException {
    int newTopLeftTarget = 0;
    int newBotLeftTarget = 0;
    int newTopRightTarget = 0;
    int newBotRightTarget = 0;

    // Ensure that the opmode is still active
    if (opModeIsActive()) {

    // Determine new target position, and pass to motor controller
    newTopLeftTarget = robot.topL.getCurrentPosition() + (int) (TLInches * COUNTS_PER_INCH);
    newTopRightTarget = robot.topR.getCurrentPosition() + (int) (TRInches * COUNTS_PER_INCH);
    newBotLeftTarget = robot.botL.getCurrentPosition() + (int) (BLInches * COUNTS_PER_INCH);
    newBotRightTarget = robot.botR.getCurrentPosition() + (int) (BRInches * COUNTS_PER_INCH);

    robot.topL.setTargetPosition(newTopLeftTarget);
    robot.topR.setTargetPosition(newTopRightTarget);
    robot.botR.setTargetPosition(newBotRightTarget);
    robot.botL.setTargetPosition(newBotLeftTarget);


    // Turn On RUN_TO_POSITION
    robot.topL.setMode(DcMotor.RunMode.RUN_TO_POSITION );
    robot.topR.setMode(DcMotor.RunMode.RUN_TO_POSITION );
    robot.botL.setMode(DcMotor.RunMode.RUN_TO_POSITION );
    robot.botR.setMode(DcMotor.RunMode.RUN_TO_POSITION );

    // reset the timeout time and start motion.
    runtime.reset();
    robot.topL.setPower(Math.abs(speed));
    robot.topR.setPower(Math.abs(speed));
    robot.botL.setPower(Math.abs(speed));
    robot.botR.setPower(Math.abs(speed));

    // keep looping while we are still active, and there is time left, and both motors are running.
    while (opModeIsActive()) {
    final boolean path1 = (runtime.seconds() < timeoutS) &&

    (robot.topL.isBusy() && robot.topR.isBusy() && robot.botL.isBusy() && robot.botR.isBusy());


    // Display it for the driver.
    telemetry.addData("Path1", "Running to %7d :%7d", newTopLeftTarget, newTopRightTarget, newBotLeftTarget, newBotRightTarget);
    telemetry.addData("Path2", "Running at %7d :%7d",
    robot.topL.getCurrentPosition(),
    robot.topR.getCurrentPosition(),
    robot.botL.getCurrentPosition(),
    robot.botR.getCurrentPosition());
    telemetry.update();

    // Allow time for other processes to run.
    idle();
    }

    // Stop all motion;
    robot.topL.setPower(0);
    robot.topR.setPower(0);
    robot.botL.setPower(0);
    robot.botR.setPower(0);

    // Turn off RUN_TO_POSITION
    robot.topL.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    robot.topR.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    robot.botL.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    robot.botR.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);

    // sleep(250); // optional pause after each move
    }
    }

    }
Working...
X