I need help with my autonomous mode

I am new to this programming and I am the only programmer at ftc, I am doing the automatic code, since I already did the mecanum, but I already did all the calculations but at the time of adding another step, for example the one that moves to the right does first move to the right, oh please if someone can help me, I program in adroid studio with java, if you can help me know how to make the code or the robot go in steps I would greatly appreciate it

`@TeleOp(name = “prueba_nose”)
public class prueba_nose extends LinearOpMode {
@Override
public void runOpMode() throws InterruptedException {
DcMotor delantederecha = hardwareMap.get(DcMotor.class, “frontRight”);
DcMotor delanteizquierda = hardwareMap.get(DcMotor.class, “frontLeft”);
DcMotor abajoizquierda = hardwareMap.get(DcMotor.class, “backLeft”);
DcMotor abajoderecha = hardwareMap.get(DcMotor.class, “backRight”);

    delanteizquierda.setDirection(DcMotor.Direction.REVERSE);
    abajoizquierda.setDirection(DcMotorSimple.Direction.REVERSE);
    delantederecha.setDirection(DcMotorSimple.Direction.FORWARD);
    abajoderecha.setDirection(DcMotorSimple.Direction.FORWARD);

    delanteizquierda.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    delanteizquierda.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    delantederecha.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    delantederecha.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    abajoizquierda.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    abajoizquierda.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    abajoderecha.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    abajoderecha.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    
    int opuesto = (int) -537.7;
    int encoder = (int) 537.7;
    int perimetro = (int) (3.1416*96);
    int distancia = 3;
    int dondeva = perimetro*3;
    int sensor = encoder*distancia;            
    int lapiz = opuesto*distancia;                 

    waitForStart();

    delantederecha.setTargetPosition(sensor);                   
    delantederecha.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    delantederecha.setPower(1);
    delanteizquierda.setTargetPosition(sensor);
    delanteizquierda.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    delanteizquierda.setPower(1);
    abajoderecha.setTargetPosition(sensor);
    abajoderecha.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    abajoderecha.setPower(1);
    abajoizquierda.setTargetPosition(sensor);
    abajoizquierda.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    abajoizquierda.setPower(1);                       
    
    delanteizquierda.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    delanteizquierda.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    delantederecha.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    delantederecha.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    abajoizquierda.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    abajoizquierda.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    abajoderecha.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
    abajoderecha.setMode(DcMotor.RunMode.RUN_USING_ENCODER);

    delantederecha.setTargetPosition(opuesto);
    delantederecha.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    delantederecha.setPower(-1);
    delanteizquierda.setTargetPosition(encoder);
    delanteizquierda.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    delanteizquierda.setPower(1);
    abajoderecha.setTargetPosition(encoder);
    abajoderecha.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    abajoderecha.setPower(1);
    abajoizquierda.setTargetPosition(opuesto);
    abajoizquierda.setMode(DcMotor.RunMode.RUN_TO_POSITION);
    abajoizquierda.setPower(-1);`

I believe all the code you have currently is correct in the syntax and intention. After setting the target positions, set modes, and motor powers, I believe you need some kind of loop to stall the robot code while the robot’s motors drive to the target positions. It would look something like this:

delantederecha.setTargetPosition(sensor);                   
delantederecha.setMode(DcMotor.RunMode.RUN_TO_POSITION);
delanteizquierda.setTargetPosition(sensor);
delanteizquierda.setMode(DcMotor.RunMode.RUN_TO_POSITION);
abajoderecha.setTargetPosition(sensor);
abajoderecha.setMode(DcMotor.RunMode.RUN_TO_POSITION);
abajoizquierda.setTargetPosition(sensor);
abajoizquierda.setMode(DcMotor.RunMode.RUN_TO_POSITION);

while (delantederecha.currentPosition() <= delantederecha.getTargetPosition()) { // Condition to check the position of a single motor in comparison to the target positions. Depending on the autonomous action, you may need to add in more conditions.
    delantederecha.setPower(1);
    delanteizquierda.setPower(1);
    abajoderecha.setPower(1);
    abajoizquierda.setPower(1);
}

delantederecha.setPower(0);
delanteizquierda.setPower(0);
abajoderecha.setPower(0);
abajoizquierda.setPower(0);

This code will set the target positions and the run modes of the motors and then constantly check the position of one motor in the whole loop (see the relevant comment for more information). Do this for every linear movement. In the example code, after the first motor reaches the target position, all motors will stop. I would recommend experimenting with this code to tune it to your liking.

Another thing to note is that the DcMotor.setTargetPosition(int position) method will take a number in ticks of the motor. You said you already have this in your code, but it’s always good to have a refresher. You can look at the documentation for the specific motor to find the amount of ticks in one revolution and then multiply that by the revolutions you want the motor to spin (Example 1: 20 revolutions * 5480 ticks). Another way is to divide the amount of ticks in a revolution by the circumference of the wheel (Example 2: 5480 ticks / 20 cm) to get the amount of ticks per unit of measurement. By multiplying this number with the number of measurements units you’d like to go (ex. 50 cm * 274 ticks/cm), you can calculate the amount of ticks in the distance you’d like the to go. Here’s some example code to make sense of my rambling:

Example 1

public void setTargetRevolutions(double revolutions) {
   motor1.setTargetPosition(Math.round(revolutions * 5480)); // revolutions multiplied by ticks
}

Example 2

public void setTargetDistance(double distance) {
    motor1.setTargetPosition(Math.round(distance * 5480 / 20)); // centimeters multiplied by ticks per revolution divided by circumference in centimeters
}
2 Likes

Thank you very much for the help. Now I have a better idea of ​​how to do it. I really appreciate it because I was looking but I couldn’t find it. I’m going to try what you said about putting it in some kind of loop and I’m going to look at the rest as well. Thank you very much for the help.

1 Like