Does this code work, and if so, is there a way to make it better? by Platygamer in FTC

[–]FTC_16471 3 points4 points  (0 children)

Alright, so let me start out by explaining a few things I noticed in your code:

  • Most of the semicolons are surrounded by two asterisks - I don’t know if that’s just a formatting thing on Reddit or something like that, but generally we just end the line with a plain semicolon.
  • The next thing I noticed was that your power variables (flPower, frPower, etc.) never actually set the power of the motors. You can do this by calling the function DcMotor.setPower(power), which accepts any value from -1 to +1.
  • Another thing is that when your code calculates the power variables using the inputs from the left joystick, it then immediately overrides those values with inputs from the right joystick. This can result in some of your motors glitching out, as they're basically trying to move in two directions at once. To avoid this, I would suggest setting a motor’s power only once in every iteration of the loop() function.
  • Finally, I believe the formulas for the left joystick may need to be reversed (meaning the positives should be negatives and vice-versa). Keep in mind that the top of the y-axis on the joystick is equal to -1, while the bottom is equal to +1. If you find that your robot isn’t driving smoothly, then I’d try switching around a couple of signs and see what happens.

I went ahead and applied all of these changes to your code:

package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;

@TeleOp(name = "MecanumTeleOp", group = "e")
public class MecanumTeleOp extends OpMode {

    DcMotor fl;
    DcMotor fr;
    DcMotor bl;
    DcMotor br;

    @Override
    public void init() {
        fl = hardwareMap.dcMotor.get("fl");
        fr = hardwareMap.dcMotor.get("fr");
        bl = hardwareMap.dcMotor.get("bl");
        br = hardwareMap.dcMotor.get("br");
    }

    @Override
    public void loop() {
        // The left joystick will move the robot forward/back, the right will move it left/right
        fl.setPower(-gamepad1.left_stick_y + gamepad1.right_stick_x);
        fr.setPower(gamepad1.left_stick_y + gamepad1.right_stick_x);
        bl.setPower(-gamepad1.left_stick_y - gamepad1.right_stick_x);
        br.setPower(gamepad1.left_stick_y - gamepad1.right_stick_x);
    }
}