Search in sources :

Example 1 with ElapsedTime

use of com.qualcomm.robotcore.util.ElapsedTime in project robotcode by OutoftheBoxFTC.

the class RobotUsbDeviceTty method read.

@Override
public int read(byte[] data, final int ibFirst, final int cbToRead, final long msTimeout, @Nullable TimeWindow timeWindow) throws RobotUsbException, InterruptedException {
    // Only one reader at a time, thank you very much
    synchronized (this.readLock) {
        try {
            ElapsedTime timer = new ElapsedTime();
            int cbRead = 0;
            // Use up any readahead that we have from when we might have timed out previously
            while (cbRead < cbToRead && this.readAhead.size() > 0) {
                data[cbRead++] = this.readAhead.remove();
            }
            // Get the remainder by actually reading
            while (// isOpen() call may not actually be needed: read() call might (?) prematurely return
            isOpen() && cbRead < cbToRead) {
                // Attempt to read all that we still need
                int cbReadOnce = this.serialPort.getInputStream().read(data, ibFirst + cbRead, cbToRead - cbRead);
                // Adjust for Java's odd EOF behavior
                if (cbReadOnce == -1) {
                    cbReadOnce = 0;
                }
                // Do our bookkeeping based on what we've got; return if we're done
                Assert.assertTrue(cbReadOnce >= 0);
                cbRead += cbReadOnce;
                if (cbRead == cbToRead) {
                    break;
                }
                // Did we hit the timeout?
                if (timer.milliseconds() > msTimeout) {
                    break;
                }
                // Behave ourselves, then go around the loop again
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
                Thread.yield();
            }
            if (cbRead == cbToRead) {
                // All is well: we got what we came for
                if (DEBUG) {
                    dumpBytesReceived(data, ibFirst, cbRead);
                }
                // We don't support timestamps (we wish we could)!
                if (timeWindow != null) {
                    timeWindow.clear();
                }
                // Return data to our caller
                return cbRead;
            } else {
                // Timeout or close case. Push back data for next time.
                for (int i = 0; i < cbRead; i++) {
                    this.readAhead.add(data[i]);
                }
                RobotLog.ee(TAG, "didn't read enough data cbToRead=%d cbRead=%d", cbToRead, cbRead);
                // maybe an odd semantic to indicate timeout, but that's what we've got
                return 0;
            }
        } catch (InterruptedIOException e) {
            // Our callers don't know about the IO flavor of InterruptedException well, so turn it into what they're expecting
            throw (e.getCause() instanceof InterruptedException) ? (InterruptedException) e.getCause() : new InterruptedException(e.getMessage());
        } catch (IOException e) {
            // Wrap anything else in what they're expecting
            throw RobotUsbUnspecifiedException.createChained(e, "exception in %s.read()", TAG);
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 2 with ElapsedTime

use of com.qualcomm.robotcore.util.ElapsedTime in project robotcode by OutoftheBoxFTC.

the class PushbotAutoDriveByGyro_Linear method gyroHold.

/**
 * Method to obtain & hold a heading for a finite amount of time
 * Move will stop once the requested time has elapsed
 *
 * @param speed    Desired speed of turn.
 * @param angle    Absolute Angle (in Degrees) relative to last gyro reset.
 *                 0 = fwd. +ve is CCW from fwd. -ve is CW from forward.
 *                 If a relative angle is required, add/subtract from current heading.
 * @param holdTime Length of time (in seconds) to hold the specified heading.
 */
public void gyroHold(double speed, double angle, double holdTime) {
    ElapsedTime holdTimer = new ElapsedTime();
    // keep looping while we have time remaining.
    holdTimer.reset();
    while (opModeIsActive() && (holdTimer.time() < holdTime)) {
        // Update telemetry & Allow time for other processes to run.
        onHeading(speed, angle, P_TURN_COEFF);
        telemetry.update();
    }
    // Stop all motion;
    robot.leftDrive.setPower(0);
    robot.rightDrive.setPower(0);
}
Also used : ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime)

Example 3 with ElapsedTime

use of com.qualcomm.robotcore.util.ElapsedTime in project Relic_Main by TeamOverdrive.

the class ConceptTelemetry method runOpMode.

@Override
public void runOpMode() {
    /* we keep track of how long it's been since the OpMode was started, just
         * to have some interesting data to show */
    ElapsedTime opmodeRunTime = new ElapsedTime();
    // We show the log in oldest-to-newest order, as that's better for poetry
    telemetry.log().setDisplayOrder(Telemetry.Log.DisplayOrder.OLDEST_FIRST);
    // We can control the number of lines shown in the log
    telemetry.log().setCapacity(6);
    // The interval between lines of poetry, in seconds
    double sPoemInterval = 0.6;
    /**
     * Wait until we've been given the ok to go. For something to do, we emit the
     * elapsed time as we sit here and wait. If we didn't want to do anything while
     * we waited, we would just call {@link #waitForStart()}.
     */
    while (!isStarted()) {
        telemetry.addData("time", "%.1f seconds", opmodeRunTime.seconds());
        telemetry.update();
        idle();
    }
    // Ok, we've been given the ok to go
    /**
     * As an illustration, the first line on our telemetry display will display the battery voltage.
     * The idea here is that it's expensive to compute the voltage (at least for purposes of illustration)
     * so you don't want to do it unless the data is <em>actually</em> going to make it to the
     * driver station (recall that telemetry transmission is throttled to reduce bandwidth use.
     * Note that getBatteryVoltage() below returns 'Infinity' if there's no voltage sensor attached.
     *
     * @see Telemetry#getMsTransmissionInterval()
     */
    telemetry.addData("voltage", "%.1f volts", new Func<Double>() {

        @Override
        public Double value() {
            return getBatteryVoltage();
        }
    });
    // Reset to keep some timing stats for the post-'start' part of the opmode
    opmodeRunTime.reset();
    int loopCount = 1;
    // Go go gadget robot!
    while (opModeIsActive()) {
        // Emit poetry if it's been a while
        if (poemElapsed.seconds() > sPoemInterval) {
            emitPoemLine();
        }
        // As an illustration, show some loop timing information
        telemetry.addData("loop count", loopCount);
        telemetry.addData("ms/loop", "%.3f ms", opmodeRunTime.milliseconds() / loopCount);
        // Show joystick information as some other illustrative data
        telemetry.addLine("left joystick | ").addData("x", gamepad1.left_stick_x).addData("y", gamepad1.left_stick_y);
        telemetry.addLine("right joystick | ").addData("x", gamepad1.right_stick_x).addData("y", gamepad1.right_stick_y);
        /**
         * Transmit the telemetry to the driver station, subject to throttling.
         * @see Telemetry#getMsTransmissionInterval()
         */
        telemetry.update();
        /**
         * Update loop info and play nice with the rest of the {@link Thread}s in the system
         */
        loopCount++;
    }
}
Also used : ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime)

Example 4 with ElapsedTime

use of com.qualcomm.robotcore.util.ElapsedTime in project Relic_Main by TeamOverdrive.

the class PushbotAutoDriveByGyro_Linear method gyroHold.

/**
 *  Method to obtain & hold a heading for a finite amount of time
 *  Move will stop once the requested time has elapsed
 *
 * @param speed      Desired speed of turn.
 * @param angle      Absolute Angle (in Degrees) relative to last gyro reset.
 *                   0 = fwd. +ve is CCW from fwd. -ve is CW from forward.
 *                   If a relative angle is required, add/subtract from current heading.
 * @param holdTime   Length of time (in seconds) to hold the specified heading.
 */
public void gyroHold(double speed, double angle, double holdTime) {
    ElapsedTime holdTimer = new ElapsedTime();
    // keep looping while we have time remaining.
    holdTimer.reset();
    while (opModeIsActive() && (holdTimer.time() < holdTime)) {
        // Update telemetry & Allow time for other processes to run.
        onHeading(speed, angle, P_TURN_COEFF);
        telemetry.update();
    }
    // Stop all motion;
    robot.leftDrive.setPower(0);
    robot.rightDrive.setPower(0);
}
Also used : ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime)

Example 5 with ElapsedTime

use of com.qualcomm.robotcore.util.ElapsedTime in project Relic_Main by TeamOverdrive.

the class B_Far_Multi method runOpMode.

@Override
public void runOpMode() throws InterruptedException {
    waitForStart("BFar", AutoParams.AUTO, true);
    Robot.getDrive().zeroSensors();
    ElapsedTime t = new ElapsedTime();
    Robot.getJewel().hit(this.jewel_Color, Jewel_Color.Blue);
    switch(WhatColumnToScoreIn()) {
        case LEFT:
            Robot.getDrive().encoderDrive(0.6, -24, -24, 5);
            // -90
            Robot.getDrive().encoderDrive(0.6, -(WHEEL_BASE * PI * 90) / 180, 0, 3);
            Robot.getDrive().encoderDrive(0.5, -6, -6, 3);
            // -90-52
            Robot.getDrive().turnCW(54, 0.3, 3);
            Robot.getDrive().encoderDrive(0.2, 1, 1, 3);
            Robot.getSlammer().stopperUp();
            sleep(400);
            Robot.getDrive().encoderDrive(0.2, -3, -3, 3);
            Robot.getIntake().releaseIntake();
            Robot.getDrive().encoderDrive(0.5, -10, -10, 2);
            Robot.getRelic().setAngles(0, 0);
            Robot.getRelic().setWristPostion(50);
            sleep(400);
            Robot.getSlammer().stopperDown();
            // -150
            Robot.getDrive().turnCW(8, 1.0, 3);
            Robot.getIntake().reverse();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    sleep(1000);
                    Robot.getIntake().intake();
                }
            }).start();
            Robot.getDrive().encoderDrive(1.0, -35, -35, 6);
            boolean left = false;
            if (!(Robot.getIntake().backDetected() && Robot.getIntake().frontDetected())) {
                left = true;
                Robot.getIntake().reverse();
                sleep(500);
                Robot.getIntake().intake();
                Robot.getDrive().encoderDrive(0.4, -10, -10, 3);
            } else {
                Robot.getIntake().reverse();
            }
            while (opModeIsActive() && t.seconds() > 24 && !(Robot.getIntake().frontDetected() || Robot.getIntake().backDetected())) {
                Thread.yield();
                telemetry.clearAll();
                telemetry.addData("Front", Robot.getIntake().frontDetected());
                telemetry.addData("Back", Robot.getIntake().backDetected());
                telemetry.update();
            }
            sleep(500);
            Robot.getIntake().reverse();
            if (left) {
                Robot.getDrive().encoderDrive(1.0, 0, (WHEEL_BASE * PI * 10) / 180, 3);
                Robot.getIntake().intake();
                Robot.getDrive().encoderDrive(0.8, 53, 53, 3);
            } else {
                Robot.getDrive().encoderDrive(1.0, 0, (WHEEL_BASE * PI * 8) / 180, 3);
                Robot.getIntake().intake();
                Robot.getDrive().encoderDrive(0.8, 49, 49, 3);
            }
            Robot.getSlammer().autoSlam();
            Robot.getDrive().encoderDrive(0.2, -4, -4, 1);
            if (t.seconds() < 26) {
                Robot.getDrive().encoderDrive(0.3, 3, 3, 2);
                Robot.getDrive().encoderDrive(0.3, -4, -4, 2);
            }
            Robot.getSlammer().setPower(-1);
            sleep(100);
            Robot.getSlammer().stop();
            break;
        case CENTER:
            Robot.getDrive().encoderDrive(0.6, -24, -24, 5);
            // -90
            Robot.getDrive().encoderDrive(0.6, -(WHEEL_BASE * PI * 90) / 180, 0, 3);
            Robot.getDrive().encoderDrive(0.5, -13, -13, 3);
            // -90-52
            Robot.getDrive().turnCW(54, 0.3, 3);
            Robot.getDrive().encoderDrive(0.2, 1, 1, 3);
            Robot.getSlammer().stopperUp();
            sleep(400);
            Robot.getDrive().encoderDrive(0.2, -3, -3, 3);
            Robot.getIntake().releaseIntake();
            Robot.getDrive().encoderDrive(0.5, -10, -10, 2);
            Robot.getRelic().setAngles(0, 0);
            Robot.getRelic().setWristPostion(50);
            sleep(400);
            Robot.getSlammer().stopperDown();
            // -150
            Robot.getDrive().turnCW(20, 1.0, 3);
            Robot.getIntake().reverse();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    sleep(1000);
                    Robot.getIntake().intake();
                }
            }).start();
            Robot.getDrive().encoderDrive(1.0, -35, -35, 6);
            Robot.getIntake().reverse();
            sleep(500);
            Robot.getIntake().intake();
            Robot.getDrive().encoderDrive(0.4, -10, -10, 3);
            while (opModeIsActive() && t.seconds() > 24 && !(Robot.getIntake().frontDetected() || Robot.getIntake().backDetected())) {
                Thread.yield();
                telemetry.clearAll();
                telemetry.addData("Front", Robot.getIntake().frontDetected());
                telemetry.addData("Back", Robot.getIntake().backDetected());
                telemetry.update();
            }
            sleep(500);
            Robot.getIntake().reverse();
            Robot.getDrive().encoderDrive(0.8, 52, 52, 3);
            Robot.getSlammer().autoSlam();
            Robot.getDrive().encoderDrive(0.2, -2, -2, 1);
            if (t.seconds() < 26) {
                Robot.getDrive().encoderDrive(0.3, 2, 2, 2);
                Robot.getDrive().encoderDrive(0.3, -4, -4, 2);
            }
            Robot.getSlammer().setPower(-1);
            sleep(100);
            Robot.getSlammer().stop();
            break;
        case RIGHT:
            // Drive off
            Robot.getDrive().encoderDrive(0.4, -30, -30, 3);
            // Turn 90
            Robot.getDrive().turnCW(90, 0.3, 3);
            // Drive 17 in
            Robot.getDrive().encoderDrive(0.5, -19, -19, 3);
            // Turn 90
            Robot.getDrive().turnCW(90, 0.4, 3);
            // Release Preloaded Glyph
            Robot.getSlammer().stopperUp();
            sleep(800);
            // Release Intake
            Robot.getIntake().releaseIntake();
            // Drive Backward
            Robot.getDrive().encoderDrive(0.6, -10, -10, 3);
            // Relic
            Robot.getRelic().setAngles(0, 0);
            Robot.getRelic().setWristPostion(64);
            Robot.getRelic().close();
            Robot.getSlammer().stopperDown();
            // Start intaking
            Robot.getIntake().intake();
            Robot.getDrive().turnCCW(28, 0.5, 2);
            Robot.getDrive().encoderDrive(0.5, -25, -25, 5);
            Robot.getRelic().lock();
            sleep(200);
            boolean farther = false;
            if (!(Robot.getIntake().frontDetected() && Robot.getIntake().backDetected())) {
                Robot.getIntake().reverse();
                sleep(400);
                Robot.getIntake().intake();
                Robot.getDrive().encoderDrive(0.2, -7, -7, 3);
                farther = true;
            }
            while (opModeIsActive() && t.seconds() > 24 && !(Robot.getIntake().frontDetected() && Robot.getIntake().backDetected())) {
                Thread.yield();
                telemetry.clearAll();
                telemetry.addData("Front", Robot.getIntake().frontDetected());
                telemetry.addData("Back", Robot.getIntake().backDetected());
                telemetry.update();
            }
            Robot.getIntake().reverse();
            boolean farther3 = false;
            if (!Robot.getIntake().frontDetected() && !Robot.getIntake().backDetected()) {
                sleep(600);
                Robot.getIntake().intake();
                Robot.getDrive().encoderDrive(0.2, -4, -4, 2);
                Robot.getIntake().reverse();
                farther3 = true;
            }
            Robot.getDrive().turnCW(6, 0.3, 2);
            if (farther3) {
                Robot.getDrive().encoderDrive(0.6, 46.5, 46.5, 3);
            } else {
                Robot.getDrive().encoderDrive(0.6, 45, 45, 3);
            }
            Robot.getSlammer().autoSlam();
            Robot.getDrive().encoderDrive(0.3, -4, -4, 3);
            if (t.seconds() < 26) {
                Robot.getDrive().encoderDrive(0.3, 3, 3, 2);
                Robot.getDrive().encoderDrive(0.3, -4, -4, 2);
            }
            break;
    }
}
Also used : ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime)

Aggregations

ElapsedTime (com.qualcomm.robotcore.util.ElapsedTime)16 IOException (java.io.IOException)2 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)1 ReadWriteFile (com.qualcomm.robotcore.util.ReadWriteFile)1 File (java.io.File)1 InterruptedIOException (java.io.InterruptedIOException)1 CancellationException (java.util.concurrent.CancellationException)1 RecvLoopRunnable (org.firstinspires.ftc.robotcore.internal.network.RecvLoopRunnable)1 SendOnceRunnable (org.firstinspires.ftc.robotcore.internal.network.SendOnceRunnable)1 OpModeManagerImpl (org.firstinspires.ftc.robotcore.internal.opmode.OpModeManagerImpl)1 RobotUsbDeviceClosedException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException)1 RobotUsbStuckUsbWriteException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException)1 RobotUsbTimeoutException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbTimeoutException)1