@@ -1,220 +1,327 @@
package com .qualcomm .ftcrobotcontroller .opmodes ;
//import android.hardware.Sensor;
import com .qualcomm .robotcore .hardware .DcMotorController ;
import com .qualcomm .robotcore .hardware .LegacyModule ;
import com .qualcomm .robotcore .eventloop .opmode .LinearOpMode ;
import com .qualcomm .robotcore .hardware .DcMotor ;
import com .qualcomm .robotcore .hardware .LegacyModule ;
import com .qualcomm .robotcore .hardware .Servo ;
import com .qualcomm .robotcore .hardware .UltrasonicSensor ;
//import android.content.Context;
//import android.hardware.Sensor;
//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;
//import android.hardware.SensorManager;
import com .qualcomm .robotcore .eventloop .opmode .OpMode ;
import com .qualcomm .robotcore .hardware .DcMotor ;
import com .qualcomm .robotcore .hardware .DcMotorController ;
import com .qualcomm .robotcore .util .ElapsedTime ;
import com .qualcomm .robotcore .util .Range ;
import java .text .SimpleDateFormat ;
import java .util .Date ;
import java .util .UnknownFormatConversionException ;
/**
* Start backwards and align with climber thing
* Created by bk on 9/21/2015.
*/
public class EncoderTest extends LinearOpMode {
public class EncoderTest extends OpMode {
private String startDate ;
private ElapsedTime runTime = new ElapsedTime ();
DcMotor backLeftDrive ;
DcMotor backRightDrive ;
DcMotor frontLeftDrive ;
DcMotor frontRightDrive ;
DcMotor tape ;
Servo adjust ;
Servo deposit ;
DcMotor backLeftDrive ;
DcMotor backRightDrive ;
int ANDYMARK_TICKS_PER_REV = 1680 ;
double Inches_per_Rotation = 13.125 ;
public EncoderTest () {
@ Override public void runOpMode () throws InterruptedException {
//Initialize hardware
backLeftDrive = hardwareMap .dcMotor .get ("backLeftDrive" );
backRightDrive = hardwareMap .dcMotor .get ("backRightDrive" );
frontLeftDrive = hardwareMap .dcMotor .get ("frontLeftDrive" );
frontRightDrive = hardwareMap .dcMotor .get ("frontRightDrive" );
//tape = hardwareMap.dcMotor.get("tape");
//adjust = hardwareMap.servo.get("adjust");
//deposit = hardwareMap.servo.get("deposit");
}
backLeftDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
//frontLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//frontRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//tape.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
@ Override public void init () {
startDate = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" ).format (new Date ());
backRightDrive = hardwareMap .dcMotor .get ("backRightDrive" );
backLeftDrive = hardwareMap .dcMotor .get ("backLeftDrive" );
backLeftDrive .setDirection (DcMotor .Direction .REVERSE );
frontLeftDrive .setDirection (DcMotor .Direction .REVERSE );
//Wait for the game to start
waitForStart ();
frontRightDrive = hardwareMap .dcMotor .get ("frontRightDrive" );
frontLeftDrive = hardwareMap .dcMotor .get ("frontLeftDrive" );
frontLeftDrive .setDirection (DcMotor .Direction .REVERSE );
//deposit.setPosition(Servo.MAX_POSITION);
// The strings must match names given in Settings->Configure Robot
// backwards(.5);
// sleep(2500);
// stopDrive();
// sleep(250);
//
// forward(.5);
// sleep(700);
// stopDrive();
// sleep(250);
//
// turnLeft(.5);
// sleep(800);
// stopDrive();
// sleep(250);
// forward(.75);
// sleep(1500);
// stopDrive();
telemetry .addData ("ticks" , "" + findTicks (24 ));
driveForwardDistance (1 ,findTicks (24 ));
stop ();
}
// Do not do RESET_ENCODERS and RUN_WITHOUT_ENCODERS
// If you do - it will not run.
// setDriveMode(DcMotorController.RunMode.RESET_ENCODERS);
setDriveMode (DcMotorController .RunMode .RUN_WITHOUT_ENCODERS );
setDrivePower (0.0 , 0.0 );
public int findTicks (double distance ){
return (int ) (2 * ANDYMARK_TICKS_PER_REV * distance / Inches_per_Rotation );
runTime .reset ();
}
public void forward (double val ) {
@ Override public void loop () {
// Negative is up on the joystick,
// Positive is down on the joystick,
// Send the "negative" of the joystick so up is now positive
setDrivePower (-gamepad1 .left_stick_y ,-gamepad1 .right_stick_y );
frontLeftDrive .setPower (val );
frontRightDrive .setPower (val );
backLeftDrive .setPower (val );
backRightDrive .setPower (val );
telemetry .addData ("1 Motor 1" , backRightDrive .getCurrentPosition ());
telemetry .addData ("2 Motor 2" , backLeftDrive .getCurrentPosition ());
}
public void driveForwardDistance (double power , int distance )
{
//frontRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
//frontLeftDrive.setMode(DcMotorController. RunMode.RESET_ENCODERS);
backLeftDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
//frontLeftDrive.setTargetPosition(distance);
//frontRightDrive.setTargetPosition(distance);
backLeftDrive .setTargetPosition (distance );
backRightDrive .setTargetPosition (distance );
//frontLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
//frontRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
backLeftDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
backRightDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
forward (power );
while (backLeftDrive .isBusy () && backRightDrive .isBusy ()){
}
stopDrive ();
backLeftDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
//frontLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//frontRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
@ Override public void stop () {
setDrivePower (0.0 , 0.0 );
}
public void turnLeftDistance (double power , int distance )
{
frontRightDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
frontLeftDrive .setMode (DcMotorController . RunMode .RESET_ENCODERS );
backLeftDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
frontLeftDrive .setTargetPosition (-distance );
frontRightDrive .setTargetPosition (distance );
backLeftDrive .setTargetPosition (-distance );
backRightDrive .setTargetPosition (distance );
frontLeftDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
frontRightDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
backLeftDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
backRightDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
turnLeft (power );
while (backLeftDrive .isBusy () && backRightDrive .isBusy () && frontLeftDrive .isBusy () && frontRightDrive .isBusy ()){
}
stopDrive ();
backLeftDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
frontLeftDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
frontRightDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
/**
* Set the power to left and right motors, the values must range
* between -1 and 1.
* @param left
* @param right
*/
public void setDrivePower (double left , double right ) {
// This assumes power is given as -1 to 1
// The range clip will make sure it is between -1 and 1
// An incorrect value can cause the program to exception
backLeftDrive .setPower (Range .clip (left , -1.0 , 1.0 ));
backRightDrive .setPower (Range .clip (right , -1.0 , 1.0 ));
}
public void turnRightDistance (double power , int distance )
{
frontRightDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
frontLeftDrive .setMode (DcMotorController . RunMode .RESET_ENCODERS );
backLeftDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RESET_ENCODERS );
frontLeftDrive .setTargetPosition (distance );
frontRightDrive .setTargetPosition (-distance );
backLeftDrive .setTargetPosition (distance );
backRightDrive .setTargetPosition (-distance );
frontLeftDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
frontRightDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
backLeftDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
backRightDrive .setMode (DcMotorController .RunMode .RUN_TO_POSITION );
turnRight (power );
while (backLeftDrive .isBusy () && backRightDrive .isBusy () && frontLeftDrive .isBusy () && frontRightDrive .isBusy ()){
/**
* Sets the drive mode for each motor.
* The types of Run Modes are
* DcMotorController.RunMode.RESET_ENCODERS
* Resets the Encoder Values to 0
* DcMotorController.RunMode.RUN_TO_POSITION
* Runs until the encoders are equal to the target position
* DcMotorController.RunMode.RUN_USING_ENCODERS
* Attempts to keep the robot running straight utilizing
* the PID the reduces the maximum power by about 15%
* DcMotorController.RunMode.RUN_WITHOUT_ENCODERS
* Applies the power directly
* @param mode
*/
public void setDriveMode (DcMotorController .RunMode mode ) {
if (backLeftDrive .getChannelMode () != mode ) {
backLeftDrive .setChannelMode (mode );
}
stopDrive ();
backLeftDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
backRightDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
frontLeftDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
frontRightDrive .setMode (DcMotorController .RunMode .RUN_USING_ENCODERS );
}
public void backwards (double val ) {
frontLeftDrive .setPower (-val );
frontRightDrive .setPower (-val );
backLeftDrive .setPower (-val );
backRightDrive .setPower (-val );
}
public void turnLeft (double val ) {
frontLeftDrive .setPower (-val );
frontRightDrive .setPower (val );
backLeftDrive .setPower (-val );
backRightDrive .setPower (val );
}
public void turnRight (double val ) {
frontLeftDrive .setPower (val );
frontRightDrive .setPower (-val );
backLeftDrive .setPower (val );
backRightDrive .setPower (-val );
if (backRightDrive .getChannelMode () != mode ) {
backRightDrive .setChannelMode (mode );
}
}
}
public void stopDrive (){
forward (0 );
}
}
//
//
////import android.hardware.Sensor;
//
//import com.qualcomm.robotcore.hardware.DcMotorController;
//import com.qualcomm.robotcore.hardware.LegacyModule;
//import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
//import com.qualcomm.robotcore.hardware.DcMotor;
//import com.qualcomm.robotcore.hardware.LegacyModule;
//import com.qualcomm.robotcore.hardware.Servo;
//import com.qualcomm.robotcore.hardware.UltrasonicSensor;
//
//import android.content.Context;
//import android.hardware.Sensor;
//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;
//import android.hardware.SensorManager;
//
//import com.qualcomm.robotcore.eventloop.opmode.OpMode;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
//
//import java.util.UnknownFormatConversionException;
//
///**
// * Start backwards and align with climber thing
// */
//public class EncoderTest extends LinearOpMode {
//
// DcMotor backLeftDrive;
// DcMotor backRightDrive;
// DcMotor frontLeftDrive;
// DcMotor frontRightDrive;
// DcMotor tape;
// Servo adjust;
// Servo deposit;
//
// int ANDYMARK_TICKS_PER_REV = 1680;
// double Inches_per_Rotation = 13.125;
//
// @Override public void runOpMode() throws InterruptedException{
// //Initialize hardware
// backLeftDrive = hardwareMap.dcMotor.get("backLeftDrive");
// backRightDrive = hardwareMap.dcMotor.get("backRightDrive");
// frontLeftDrive = hardwareMap.dcMotor.get("frontLeftDrive");
// frontRightDrive = hardwareMap.dcMotor.get("frontRightDrive");
// //tape = hardwareMap.dcMotor.get("tape");
//
// //adjust = hardwareMap.servo.get("adjust");
// //deposit = hardwareMap.servo.get("deposit");
//
//// backLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// backRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// //frontLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// //frontRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// //tape.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
////
//// backLeftDrive.setDirection(DcMotor.Direction.REVERSE);
//// frontLeftDrive.setDirection(DcMotor.Direction.REVERSE);
//
// //Wait for the game to start
// waitForStart();
//
// //deposit.setPosition(Servo.MAX_POSITION);
//
//// backwards(.5);
//// sleep(2500);
//// stopDrive();
//// sleep(250);
////
//// forward(.5);
//// sleep(700);
//// stopDrive();
//// sleep(250);
////
//// turnLeft(.5);
//// sleep(800);
//// stopDrive();
//// sleep(250);
//// forward(.75);
//// sleep(1500);
//// stopDrive();
//
// //telemetry.addData("ticks", "" + findTicks(24));
// //driveForwardDistance(0.95,findTicks(24));
// stop();
//
// }
//
// public int findTicks(double distance){
// return (int) (2 * ANDYMARK_TICKS_PER_REV * distance / Inches_per_Rotation);
// }
//
// public void forward(double val) {
//
// frontLeftDrive.setPower(val);
// frontRightDrive.setPower(val);
// backLeftDrive.setPower(val);
// backRightDrive.setPower(val);
// }
//
// public void driveForwardDistance(double power, int distance)
// {
// //frontRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
// //frontLeftDrive.setMode(DcMotorController. RunMode.RESET_ENCODERS);
// backLeftDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
// backRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
////
//// //frontLeftDrive.setTargetPosition(distance);
//// //frontRightDrive.setTargetPosition(distance);
//// backLeftDrive.setTargetPosition(distance);
//// backRightDrive.setTargetPosition(distance);
////
//// //frontLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
//// //frontRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
//// backLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
//// backRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
////
//// forward(power);
////
//// while(backLeftDrive.isBusy() && backRightDrive.isBusy()){
////
//// }
////
//// stopDrive();
//// backLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// backRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// //frontLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//// //frontRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
//
//
// }
//
// public void turnLeftDistance(double power, int distance)
// {
// frontRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
// frontLeftDrive.setMode(DcMotorController. RunMode.RESET_ENCODERS);
// backLeftDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
// backRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
//
// frontLeftDrive.setTargetPosition(-distance);
// frontRightDrive.setTargetPosition(distance);
// backLeftDrive.setTargetPosition(-distance);
// backRightDrive.setTargetPosition(distance);
//
// frontLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
// frontRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
// backLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
// backRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
//
// turnLeft(power);
//
// while(backLeftDrive.isBusy() && backRightDrive.isBusy() && frontLeftDrive.isBusy() && frontRightDrive.isBusy()){
//
// }
//
// stopDrive();
// backLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// backRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// frontLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// frontRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// }
//
// public void turnRightDistance(double power, int distance)
// {
// frontRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
// frontLeftDrive.setMode(DcMotorController. RunMode.RESET_ENCODERS);
// backLeftDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
// backRightDrive.setMode(DcMotorController.RunMode.RESET_ENCODERS);
//
// frontLeftDrive.setTargetPosition(distance);
// frontRightDrive.setTargetPosition(-distance);
// backLeftDrive.setTargetPosition(distance);
// backRightDrive.setTargetPosition(-distance);
//
// frontLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
// frontRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
// backLeftDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
// backRightDrive.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
//
// turnRight(power);
//
// while(backLeftDrive.isBusy() && backRightDrive.isBusy() && frontLeftDrive.isBusy() && frontRightDrive.isBusy()){
//
// }
//
// stopDrive();
// backLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// backRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// frontLeftDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// frontRightDrive.setMode(DcMotorController.RunMode.RUN_USING_ENCODERS);
// }
//
// public void backwards(double val) {
// frontLeftDrive.setPower(-val);
// frontRightDrive.setPower(-val);
// backLeftDrive.setPower(-val);
// backRightDrive.setPower(-val);
// }
//
// public void turnLeft(double val) {
// frontLeftDrive.setPower(-val);
// frontRightDrive.setPower(val);
// backLeftDrive.setPower(-val);
// backRightDrive.setPower(val);
// }
//
// public void turnRight(double val) {
// frontLeftDrive.setPower(val);
// frontRightDrive.setPower(-val);
// backLeftDrive.setPower(val);
// backRightDrive.setPower(-val);
// }
//
// public void stopDrive(){
// forward(0);
// }
//
//}