Search in sources :

Example 1 with LynxSetMotorChannelModeCommand

use of com.qualcomm.hardware.lynx.commands.core.LynxSetMotorChannelModeCommand in project robotcode by OutoftheBoxFTC.

the class LynxDcMotorController method internalSetZeroPowerBehavior.

void internalSetZeroPowerBehavior(int motorZ, DcMotor.ZeroPowerBehavior behavior) {
    if (motors[motorZ].lastKnownZeroPowerBehavior.updateValue(behavior)) {
        DcMotor.RunMode runMode = internalGetMotorChannelMode(motorZ);
        LynxCommand command = new LynxSetMotorChannelModeCommand(this.getModule(), motorZ, runMode, behavior);
        try {
            if (DEBUG) {
                RobotLog.vv(TAG, "setZeroBehavior mod=%d motor=%d zero=%s", getModuleAddress(), motorZ, behavior.toString());
            }
            command.send();
        } catch (InterruptedException | RuntimeException | LynxNackException e) {
            handleException(e);
        }
    }
}
Also used : LynxCommand(com.qualcomm.hardware.lynx.commands.LynxCommand) DcMotor(com.qualcomm.robotcore.hardware.DcMotor) LynxSetMotorChannelModeCommand(com.qualcomm.hardware.lynx.commands.core.LynxSetMotorChannelModeCommand)

Example 2 with LynxSetMotorChannelModeCommand

use of com.qualcomm.hardware.lynx.commands.core.LynxSetMotorChannelModeCommand in project robotcode by OutoftheBoxFTC.

the class LynxDcMotorController method setMotorMode.

@Override
public synchronized void setMotorMode(int motor, DcMotor.RunMode mode) {
    this.validateMotor(motor);
    motor -= apiMotorFirst;
    // and power levels will be interpreted correctly.
    if (!motors[motor].lastKnownMode.isValue(mode)) {
        // Get the current power so we can preserve across the change.
        Double prevPower = motors[motor].lastKnownPower.getNonTimedValue();
        if (prevPower == null) {
            prevPower = internalGetMotorPower(motor);
        }
        LynxCommand command;
        DcMotor.ZeroPowerBehavior zeroPowerBehavior = DcMotor.ZeroPowerBehavior.UNKNOWN;
        if (mode == DcMotor.RunMode.STOP_AND_RESET_ENCODER) {
            // Stop the motor, but not in such a way that we disrupt the last known
            // power, since we need to restore same when we come out of this mode.
            internalSetMotorPower(motor, 0);
            command = new LynxResetMotorEncoderCommand(this.getModule(), motor);
        } else {
            zeroPowerBehavior = internalGetZeroPowerBehavior(motor);
            command = new LynxSetMotorChannelModeCommand(this.getModule(), motor, mode, zeroPowerBehavior);
        }
        try {
            if (DEBUG) {
                RobotLog.vv(TAG, "setMotorChannelMode: mod=%d motor=%d mode=%s power=%f zero=%s", getModuleAddress(), motor, mode.toString(), prevPower, zeroPowerBehavior.toString());
            }
            command.send();
            // Ok, remember that mode. Note we need to set it before we call internalSetMotorPower()
            motors[motor].lastKnownMode.setValue(mode);
            // re-issue current motor power to ensure it's correct for this mode
            internalSetMotorPower(motor, prevPower, true);
        } catch (InterruptedException | RuntimeException | LynxNackException e) {
            handleException(e);
        }
    }
}
Also used : LynxResetMotorEncoderCommand(com.qualcomm.hardware.lynx.commands.core.LynxResetMotorEncoderCommand) LynxCommand(com.qualcomm.hardware.lynx.commands.LynxCommand) DcMotor(com.qualcomm.robotcore.hardware.DcMotor) LynxSetMotorChannelModeCommand(com.qualcomm.hardware.lynx.commands.core.LynxSetMotorChannelModeCommand)

Aggregations

LynxCommand (com.qualcomm.hardware.lynx.commands.LynxCommand)2 LynxSetMotorChannelModeCommand (com.qualcomm.hardware.lynx.commands.core.LynxSetMotorChannelModeCommand)2 DcMotor (com.qualcomm.robotcore.hardware.DcMotor)2 LynxResetMotorEncoderCommand (com.qualcomm.hardware.lynx.commands.core.LynxResetMotorEncoderCommand)1