Please find attached file for block mode.
OpMode converted Java code:
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DistanceSensor;
import com.qualcomm.robotcore.hardware.Servo;
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
@Autonomous(name = “AutonoumousRobot (Blocks to Java)”)
public class AutonoumousRobot extends LinearOpMode {
private DistanceSensor DistanceSensor_DistanceSensor;
private DcMotor LeftMotor;
private DcMotor RightMotor;
private Servo GrabberServo;
private DcMotor ArmMotor;
boolean l_robot_distance;
double INCHES_TO_MM;
double COUNTS_PER_MM;
double WHEEL_CIRCUMFERENCE_MM;
String COUNTS_PER_ARMMOTOR_DEGREE;
double ROBOT_LENGTH_MM;
double SENSOR_ROBOT_DISTANCE_MM;
double SUBSTRC_DISTANCE_TO_ROBOT_MM;
/**
- Describe this function…
*/
private void moveRobotForwardFacingSubForSpecimenFitUsingDS(double l_chasismotor_power) {
double ROBOT_DISTANCE_FROM_OBJECT;
double l_robot_distance_count_mm;
ROBOT_DISTANCE_FROM_OBJECT = DistanceSensor_DistanceSensor.getDistance(DistanceUnit.MM);
telemetry2("Measured distance by sensor was", (int) ROBOT_DISTANCE_FROM_OBJECT);
l_robot_distance_count_mm = (ROBOT_DISTANCE_FROM_OBJECT - (SENSOR_ROBOT_DISTANCE_MM + SUBSTRC_DISTANCE_TO_ROBOT_MM)) * COUNTS_PER_MM;
LeftMotor.setTargetPosition((int) (LeftMotor.getCurrentPosition() - l_robot_distance_count_mm));
RightMotor.setTargetPosition((int) (RightMotor.getCurrentPosition() + l_robot_distance_count_mm));
LeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
RightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
LeftMotor.setPower(0 - l_chasismotor_power);
RightMotor.setPower(l_chasismotor_power);
while (LeftMotor.isBusy() && RightMotor.isBusy()) {
telemetry2("Measured distance by sensor was", (int) ROBOT_DISTANCE_FROM_OBJECT);
}
}
/**
- This sample contains the bare minimum Blocks for any regular OpMode. The 3 blue
- Comment Blocks show where to place Initialization code (runs once, after touching the
- DS INIT button, and before touching the DS Start arrow), Run code (runs once, after
- touching Start), and Loop code (runs repeatedly while the OpMode is active, namely not
- Stopped).
*/
@Override
public void runOpMode() {
int COUNTS_PER_MOTOR_REV;
double COUNTS_PER_WHEEL_REV;
int DRIVE_GEAR_REDUCTION;
int ARMGEAR_REDUCTION;
int COUNTS_PER_ARMMOTOR_REV;
int COUNTS_PER_ARMGEAR_REV;
int OriginalArmPos;
double TPS;
DistanceSensor_DistanceSensor = hardwareMap.get(DistanceSensor.class, "DistanceSensor");
LeftMotor = hardwareMap.get(DcMotor.class, "LeftMotor");
RightMotor = hardwareMap.get(DcMotor.class, "RightMotor");
GrabberServo = hardwareMap.get(Servo.class, "GrabberServo");
ArmMotor = hardwareMap.get(DcMotor.class, "ArmMotor");
// Put initialization blocks here.
COUNTS_PER_MOTOR_REV = 28;
DRIVE_GEAR_REDUCTION = 15;
WHEEL_CIRCUMFERENCE_MM = 90 * Math.PI;
COUNTS_PER_WHEEL_REV = COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION;
COUNTS_PER_MM = COUNTS_PER_WHEEL_REV / WHEEL_CIRCUMFERENCE_MM;
INCHES_TO_MM = 25.4;
ROBOT_LENGTH_MM = 425.5;
ARMGEAR_REDUCTION = 2 * (125 / 45);
COUNTS_PER_ARMMOTOR_REV = 28 * 60;
COUNTS_PER_ARMGEAR_REV = COUNTS_PER_ARMMOTOR_REV * ARMGEAR_REDUCTION;
COUNTS_PER_ARMMOTOR_DEGREE = COUNTS_PER_ARMGEAR_REV / 360;
SENSOR_ROBOT_DISTANCE_MM = INCHES_TO_MM * 8.6;
SUBSTRC_DISTANCE_TO_ROBOT_MM = INCHES_TO_MM * 4.5;
GrabberServo.setPosition(1);
resetMotorEncoder();
resetArmMotorEncoder();
OriginalArmPos = ArmMotor.getCurrentPosition();
waitForStart();
if (opModeIsActive()) {
telemetry.addLine(OriginalArmPos);
telemetry.update();
moveArmMotor(1, "UP", 90, 1);
telemetry.addLine(OriginalArmPos);
telemetry.update();
telemetry.addLine("Code Started");
TPS = (75 / 60) * COUNTS_PER_WHEEL_REV;
moveRobotToDistanceFD(2, 43, "F", 0.6);
moveArmMotor(3, "UP", 65, 0.3);
telemetry.addLine("Specimen scored");
telemetry.update();
operateGrabberAndWait(-1);
telemetry2("Moving Robot Back", LeftMotor.getCurrentPosition());
moveRobotToDistanceFD(4, 23, "B", 0.3);
rotateDegree(5, "L", 0.5, 125);
resetMotorEncoder();
moveRobotToDistanceFD(6, 58, "B", 0.6);
telemetry2("Ready to reset motor encoder", RightMotor.getCurrentPosition());
resetMotorEncoder();
telemetry2("Reset motor encoder is done", RightMotor.getCurrentPosition());
telemetry2("Reseting Motor Encoder", LeftMotor.getCurrentPosition());
LeftMotor.setTargetPosition(0);
RightMotor.setTargetPosition(0);
LeftMotor.setPower(0);
RightMotor.setPower(0);
LeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
RightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
for (int count = 0; count < 2000; count++) {
telemetry2("Reseting Motor Encoder Done", LeftMotor.getCurrentPosition());
}
LeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
RightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
telemetry2("Reseting Motor Encoder Done", LeftMotor.getCurrentPosition());
LeftMotor.setTargetPosition(0);
RightMotor.setTargetPosition(0);
moveRobotToDistanceFD(7, 10, "F", 0.1);
telemetry2("FInal moved robot forward", LeftMotor.getCurrentPosition());
}
}
/**
- Describe this function…
*/
private void resetArmMotorEncoder() {
ArmMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
ArmMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
/**
- Describe this function…
*/
private void moveRobotChasisBackward(double l_chasismot_power) {
RightMotor.setPower(0 - l_chasismot_power);
LeftMotor.setPower(l_chasismot_power);
}
/**
- Describe this function…
*/
private void telemetry2(String l_key, int l_text) {
telemetry.addData(l_key, l_text);
telemetry.update();
}
/**
- Describe this function…
*/
private void moveRobotToDistanceFD(int l_stepNo, int l_desireDistance, String l_operator, double l_motorPower) {
telemetry2(“Started moving robot: dir, step no, left mot pos, right mot pos:”, “” + l_operator + l_stepNo + LeftMotor.getCurrentPosition() + RightMotor.getCurrentPosition());
l_robot_distance = (INCHES_TO_MM * l_desireDistance - ROBOT_LENGTH_MM) * COUNTS_PER_MM;
if (l_operator.equals(“F”)) {
LeftMotor.setTargetPosition(LeftMotor.getCurrentPosition() - l_robot_distance);
RightMotor.setTargetPosition(RightMotor.getCurrentPosition() + l_robot_distance);
} else {
LeftMotor.setTargetPosition(LeftMotor.getCurrentPosition() + l_robot_distance);
RightMotor.setTargetPosition(RightMotor.getCurrentPosition() - l_robot_distance);
}
LeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
RightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
LeftMotor.setPower(l_motorPower);
RightMotor.setPower(l_motorPower);
while (LeftMotor.isBusy() && RightMotor.isBusy()) {
}
telemetry2(“Done Moving out of wait”, LeftMotor.getCurrentPosition());
}
/**
- Describe this function…
*/
private void resetMotorEncoder() {
telemetry2(“Reseting Motor Encoder”, LeftMotor.getCurrentPosition());
LeftMotor.setPower(0);
RightMotor.setPower(0);
LeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
RightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
for (int count2 = 0; count2 < 2000; count2++) {
telemetry2(“resetting wait”, LeftMotor.getCurrentPosition());
}
LeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
RightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
telemetry2(“Reseting Motor Encoder Done”, LeftMotor.getCurrentPosition());
}
/**
- Describe this function…
*/
private void operateGrabberAndWait(int l_grabberPos) {
double GRABBER_POS;
GrabberServo.setPosition(l_grabberPos);
GRABBER_POS = GrabberServo.getPosition();
while (l_robot_distance) {
GRABBER_POS = GrabberServo.getPosition();
telemetry2("Grabber Pos", (int) GRABBER_POS);
telemetry.addLine(GRABBER_POS);
telemetry.addLine("Grabber should open");
telemetry.update();
if (GRABBER_POS == l_grabberPos || GRABBER_POS == 0) {
break;
}
}
}
/**
- Describe this function…
*/
private void rotateDegree(int l_stepNo, String l_rot_direction, double l_rotatepower, int l_deg) {
double DistanceTraveledPerWheel;
double OutputShaftRotations;
double EncodersTicksForDegree;
telemetry2("Started rotating robot: step no, direction, degree, left mot pos, right mot pos:", "" + l_stepNo + l_rot_direction + l_deg + LeftMotor.getCurrentPosition() + RightMotor.getCurrentPosition());
DistanceTraveledPerWheel = INCHES_TO_MM * Math.PI * (14.1732 / (360 / l_deg));
OutputShaftRotations = DistanceTraveledPerWheel / WHEEL_CIRCUMFERENCE_MM;
EncodersTicksForDegree = OutputShaftRotations * 420;
if (l_rot_direction.equals("L")) {
LeftMotor.setTargetPosition((int) (LeftMotor.getCurrentPosition() + EncodersTicksForDegree));
RightMotor.setTargetPosition((int) (RightMotor.getCurrentPosition() + EncodersTicksForDegree));
} else {
LeftMotor.setTargetPosition((int) (LeftMotor.getCurrentPosition() - EncodersTicksForDegree));
RightMotor.setTargetPosition((int) (RightMotor.getCurrentPosition() - EncodersTicksForDegree));
}
LeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
RightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
LeftMotor.setPower(l_rotatepower);
RightMotor.setPower(l_rotatepower);
telemetry.addLine("Robot turnig");
telemetry.update();
while (LeftMotor.isBusy() && RightMotor.isBusy()) {
}
telemetry2("Done rotating", LeftMotor.getCurrentPosition());
}
/**
- This function moves arm motor up/down. It takes three inputs a)
- direction UP/DOWN, position arm degree, power: how fast between 1 and 0.
*/
private void moveArmMotor(int l_stepNo, String l_arm_direction, int l_arm_position, double l_arm_m_power) {
telemetry2(“step no”, l_stepNo);
if (l_arm_direction.equals(“UP”)) {
ArmMotor.setTargetPosition(COUNTS_PER_ARMMOTOR_DEGREE * (0 - l_arm_position));
} else {
ArmMotor.setTargetPosition(COUNTS_PER_ARMMOTOR_DEGREE * (0 + l_arm_position));
}
ArmMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
ArmMotor.setPower(l_arm_m_power);
while (opModeIsActive() && ArmMotor.isBusy()) {
telemetry.addLine(COUNTS_PER_ARMMOTOR_DEGREE);
}
}
/**
- Describe this function…
*/
private void moveRobotChasisForward(double l_chasismot_power) {
LeftMotor.setPower(0 - l_chasismot_power);
RightMotor.setPower(l_chasismot_power);
}
}