FtcRobotController/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/MovementAPI.java

135 lines
4.3 KiB
Java
Raw Permalink Normal View History

2021-10-08 17:02:16 -05:00
package org.firstinspires.ftc.teamcode;
public class MovementAPI {
private final API api;
2022-01-27 16:51:03 -06:00
private final API.Motor fl;
private final API.Motor fr;
private final API.Motor bl;
private final API.Motor br;
2021-10-08 17:02:16 -05:00
private boolean flipped = false;
public API.Motor getFL() { return fl; }
public API.Motor getFR() { return fr; }
public API.Motor getBL() { return bl; }
public API.Motor getBR() { return br; }
2021-12-06 08:35:19 -06:00
2021-10-26 17:26:57 -05:00
/**
* Initializes the API
*
* @param _fl the front-left wheel
* @param _fr the front-right wheel
* @param _bl the back-left wheel
* @param _br the back-right wheel
*/
public MovementAPI(API api, API.Motor _fl, API.Motor _fr, API.Motor _bl, API.Motor _br) {
this.api = api;
2022-01-27 16:51:03 -06:00
2022-01-27 17:14:59 -06:00
api.print("Initializing MovementApi...");
2022-01-19 09:36:42 -06:00
2021-10-08 17:02:16 -05:00
fl = _fl;
fr = _fr;
bl = _bl;
br = _br;
2021-10-26 17:26:57 -05:00
fl.setDirection(API.Direction.REVERSE);
bl.setDirection(API.Direction.REVERSE);
fr.setDirection(API.Direction.FORWARD);
br.setDirection(API.Direction.FORWARD);
2021-10-08 17:02:16 -05:00
}
2022-01-21 09:44:40 -06:00
/**
* Initializes the API with the motors in the GameMotor enum
*/
public MovementAPI(API api, GameMotors motors) {
this(api, motors.fl, motors.fr, motors.bl, motors.br);
}
public void setFlipped(boolean flipped) {
this.flipped = flipped;
}
2021-10-26 17:26:57 -05:00
/**
* Moves the robot given the speed to move forward/back and left/right
2022-01-18 10:33:37 -06:00
*
* @param powerY the speed to move forward/back, -1 to 1, positive being forward
* @param powerX the speed to move left/right, -1 to 1, positive being to the right
* @param turn the speed to turn at, -1 to 1, positive being clockwise
2022-01-21 09:44:40 -06:00
* @param speed a multiplier on the final speed
2021-10-26 17:26:57 -05:00
* @param verbose whether or not to log extra data to telemetry
*/
public void move(double powerY, double powerX, double turn, double speed, boolean verbose) {
if (flipped) {
powerX *= -1;
turn *= -1;
}
2021-10-26 17:26:57 -05:00
double flPower = (powerY + turn + powerX) * speed;
double frPower = (powerY - turn - powerX) * speed;
double blPower = (powerY + turn - powerX) * speed;
double brPower = (powerY - turn + powerX) * speed;
2021-10-08 17:02:16 -05:00
2021-10-26 17:26:57 -05:00
double scale = Math.max(1, (Math.abs(powerY) + Math.abs(turn) + Math.abs(powerX)) * Math.abs(speed)); // shortcut for max(abs([fl,fr,bl,br]))
2021-10-08 17:02:16 -05:00
flPower /= scale;
frPower /= scale;
blPower /= scale;
brPower /= scale;
fl.start(flPower);
fr.start(frPower);
bl.start(blPower);
br.start(brPower);
2021-10-08 17:02:16 -05:00
2022-01-27 16:51:03 -06:00
if (verbose) api.print(
2021-10-08 17:02:16 -05:00
"Front Left: " + flPower + System.lineSeparator() +
"Back Left: " + blPower + System.lineSeparator() +
"Front Right: " + frPower + System.lineSeparator() +
"Back Right: " + brPower + System.lineSeparator()
);
}
2021-10-26 17:26:57 -05:00
/**
2022-01-21 09:44:40 -06:00
* Moves the robot given the speed to move forward/back and left/right
* @param powerY the speed to move forward/back, -1 to 1, positive being forward
2022-01-21 09:44:40 -06:00
* @param powerX the speed to move left/right, -1 to 1, positive being to the right
* @param turn the speed to turn at, -1 to 1, positive being clockwise
* @param speed a multiplier on the final speed
*
2021-10-26 17:26:57 -05:00
*/
public void move(double powerY, double powerX, double turn, double speed) {
2022-01-21 09:44:40 -06:00
move(powerY, powerX, turn, speed, false);
2021-10-08 17:02:16 -05:00
}
2021-10-26 17:26:57 -05:00
/**
2022-01-21 09:44:40 -06:00
* Moves the robot in a given direction with a given speed
2021-10-26 17:26:57 -05:00
*
2022-01-21 09:44:40 -06:00
* @param direction the direction to move in, in degrees, with positive being left of forward
2022-01-18 10:33:37 -06:00
* @param speed the speed to move at
* @param verbose whether or not to log extra data to telemetry
2021-10-26 17:26:57 -05:00
*/
public void move(double direction, double speed, boolean verbose) {
2021-10-26 17:26:57 -05:00
move(Math.cos(Math.toRadians(direction)), Math.sin(Math.toRadians(-direction)), 0, speed, verbose);
2021-10-08 17:02:16 -05:00
}
2021-10-26 17:26:57 -05:00
/**
2022-01-21 09:44:40 -06:00
* Moves the robot in a given direction with a given speed
*
* @param direction the direction to move in, in degrees, with positive being left of forward
* @param speed the speed to move at
2021-10-26 17:26:57 -05:00
*/
public void move(double direction, double speed) {
2021-10-26 17:26:57 -05:00
move(direction, speed, false);
}
/**
* Stops the robot
*/
public void stop() {
fl.stop();
2021-10-26 17:26:57 -05:00
fr.stop();
bl.stop();
br.stop();
}
2021-10-08 17:02:16 -05:00
}