This is the code that we are using for teleop. We got the code from some other source, so I made the parts that I think are unneeded into comments.
Code: /* Copyright (c) 2014 Qualcomm Technologies Inc
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 Qualcomm Technologies Inc 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 com.qualcomm.ftcrobotcontroller.opmodes;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.Range;
public class TankDriveOp extends OpMode {
double servoPosition = 0.5;
DcMotor motorRight;
DcMotor motorLeft;
Servo servo;
public TankDriveOp() {
}
@Override
public void init() {
motorRight = hardwareMap.dcMotor.get("motor1");
motorLeft = hardwareMap.dcMotor.get("motor2");
motorLeft.setDirection(DcMotor.Direction.REVERSE);
servo = hardwareMap.servo.get("servo1");
}
@Override
public void loop() {
double left = -gamepad1.left_stick_y;
double right = -gamepad1.right_stick_y;
if (gamepad1.left_bumper) {
servoPosition = servoPosition - 0.01;
}
if (gamepad1.right_bumper) {
servoPosition = servoPosition + 0.01;
}
servoPosition = Range.clip(servoPosition, 0, 1);
// clip the right/left values so that the values never exceed +/- 1
//right = Range.clip(right, -1, 1);
//left = Range.clip(left, -1, 1);
// scale the joystick value to make it easier to control
// the robot more precisely at slower speeds.
//right = (float)scaleInput(right);
//left = (float)scaleInput(left);
//right = right/10;
//left = left/10;
// write the values to the motors
motorLeft.setPower(left);
motorRight.setPower(right);
servo.setPosition(servoPosition);
/*
* Send telemetry data back to driver station. Note that if we are using
* a legacy NXT-compatible motor controller, then the getPower() method
* will return a null value. The legacy NXT-compatible motor controllers
* are currently write only.
*/
telemetry.addData("Text", "*** Robot Data***");
telemetry.addData("left tgt pwr", "left pwr: " + String.format("%.2f", left));
telemetry.addData("right tgt pwr", "right pwr: " + String.format("%.2f", right));
telemetry.addData("servo", "servo: " + String.format("%.2f", servoPosition));
}
@Override
public void stop() {
}
/*
* This method scales the joystick input so for low joystick values, the
* scaled value is less than linear. This is to make it easier to drive
* the robot more precisely at slower speeds.
*/
/*double scaleInput(double dVal) {
double[] scaleArray = { 0.0, 0.05, 0.09, 0.10, 0.12, 0.15, 0.18, 0.24,
0.30, 0.36, 0.43, 0.50, 0.60, 0.72, 0.85, 1.00, 1.00 };
// get the corresponding index for the scaleInput array.
int index = (int) (dVal * 16.0);
if (index < 0) {
index = -index;
} else if (index > 16) {
index = 16;
}
double dScale = 0.0;
if (dVal < 0) {
dScale = -scaleArray[index];
} else {
dScale = scaleArray[index];
}
return dScale;
}
*/
}