make stuff not be static

This commit is contained in:
missing 2022-01-27 16:51:03 -06:00
parent c19a3668f3
commit aa18b17d29
17 changed files with 205 additions and 261 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
deploymentTargetDropDown.xml
# fork ds store
.DS_Store

View file

@ -13,41 +13,38 @@ import java.io.File;
import java.util.UUID;
public class API {
private static OpMode opMode;
public static HubIMU imu;
public static boolean useLogFile = false;
public static File logFile = null;
private OpMode opMode;
public HubIMU imu;
public boolean useLogFile = false;
public File logFile = null;
public Gamepad gamepad1;
public Gamepad gamepad2;
/**
* Initializes the API
*
* @param mode the opmode to initialize with
* @param useLogFile whether or not to log to a log file
*/
public static void init(final OpMode mode, boolean useLogFile) {
public API(final OpMode mode, boolean useLogFile) {
opMode = mode;
print("Initializing API for class " + opMode.getClass().getName() + "...");
HardwareMap map = mode.hardwareMap;
imu = new HubIMU("imu", map);
// for (Motor m : Motor.values()) m.init(mode.hardwareMap);
// for (Servo s : Servo.values()) s.init(mode.hardwareMap);
imu = new HubIMU("imu");
gamepad1 = new Gamepad(GamepadIndex.GAMEPAD1);
gamepad2 = new Gamepad(GamepadIndex.GAMEPAD2);
for (Motor m : Motor.values()) m.init(map);
for (Servo s : Servo.values()) s.init(map);
imu = new HubIMU("imu", map);
API.useLogFile = useLogFile;
this.useLogFile = useLogFile;
if (useLogFile)
logFile = AppUtil.getInstance().getSettingsFile("log-" + UUID.randomUUID().toString() + ".log");
}
/**
* Initializes the API
*
* @param mode the opmode to initialize with
*/
public static void init(final OpMode mode) {
API.init(mode, true);
public API(final OpMode mode) {
this(mode, true);
}
/**
@ -55,7 +52,7 @@ public class API {
*
* @param seconds the amount of seconds to pause for
*/
public static void pause(double seconds) {
public void pause(double seconds) {
double time = opMode.getRuntime() + seconds;
while (opMode.getRuntime() < time) ; /* no empty while loop here! */
}
@ -66,7 +63,7 @@ public class API {
* @param s the text to print
* @see API#print(String, String)
*/
public static void print(String s) {
public void print(String s) {
opMode.telemetry.addLine(s);
opMode.telemetry.update();
@ -81,7 +78,7 @@ public class API {
* @param value the value to print
* @see API#print(String)
*/
public static void print(String caption, String value) {
public void print(String caption, String value) {
opMode.telemetry.addData(caption, value);
opMode.telemetry.update();
@ -92,12 +89,12 @@ public class API {
/**
* Clears the telemetry
*/
public static void clear() {
public void clear() {
opMode.telemetry.clear();
}
public enum Motor {
M0("m0"), M1("m1"), M2("m2"), M3("m3"), M4("m4"), M5("m5"), M6("m6"), M7("m7");
public class Motor {
// M0("m0"), M1("m1"), M2("m2"), M3("m3"), M4("m4"), M5("m5"), M6("m6"), M7("m7");
private final String name;
private DcMotor rawMotor;
@ -106,10 +103,7 @@ public class API {
Motor(String name) {
this.name = name;
}
private void init(HardwareMap map) {
rawMotor = map.get(DcMotor.class, name);
rawMotor = opMode.hardwareMap.get(DcMotor.class, name);
rawMotor.setPower(0);
}
@ -261,8 +255,8 @@ public class API {
}
}
public enum Servo {
S0("s0"), S1("s1"), S2("s2"), S3("s3"), S4("s4"), S5("s5"), S6("s6"), S7("s7"), S8("s8"), S9("s9"), S10("s10"), S11("s11");
public class Servo {
// S0("s0"), S1("s1"), S2("s2"), S3("s3"), S4("s4"), S5("s5"), S6("s6"), S7("s7"), S8("s8"), S9("s9"), S10("s10"), S11("s11");
private final String name;
private com.qualcomm.robotcore.hardware.Servo servo;
private Direction direction = Direction.FORWARD;
@ -270,10 +264,7 @@ public class API {
Servo(String name) {
this.name = name;
}
void init(HardwareMap map) {
servo = map.get(com.qualcomm.robotcore.hardware.Servo.class, name);
servo = opMode.hardwareMap.get(com.qualcomm.robotcore.hardware.Servo.class, name);
}
/**
@ -327,12 +318,12 @@ public class API {
}
}
public static class HubIMU {
public class HubIMU {
private final BNO055IMU imu;
private double zeroPos;
public HubIMU(String name, HardwareMap hardwareMap) {
imu = hardwareMap.get(BNO055IMU.class, name);
public HubIMU(String name) {
imu = opMode.hardwareMap.get(BNO055IMU.class, name);
setParameters();
}
@ -414,17 +405,15 @@ public class API {
}
}
public enum Gamepad {
G1(1), G2(2);
private final int number;
public enum GamepadIndex {
GAMEPAD1, GAMEPAD2
}
public class Gamepad {
private com.qualcomm.robotcore.hardware.Gamepad gamepad;
Gamepad(int number) {
this.number = number;
}
void init(OpMode opMode) {
this.gamepad = this.number == 1 ? opMode.gamepad1 : opMode.gamepad2;
Gamepad(GamepadIndex index) {
this.gamepad = index == GamepadIndex.GAMEPAD1 ? opMode.gamepad1 : opMode.gamepad2;
}
// Sticks

View file

@ -1,24 +0,0 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
@Autonomous(name="Auton 5.0", preselectTeleOp = "AAAAAAAAA")
public class AutonFive extends LinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.print("Status", "Initializing, please wait");
MovementAPI.init();
GameMotor.LIFT.m.setDirection(API.Direction.REVERSE, false);
API.clear();
API.print("Press play to start");
waitForStart();
MovementAPI.move(0, 0.35, true);
API.pause(4);
MovementAPI.stop();
}
}

View file

@ -4,44 +4,44 @@ import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
@Autonomous(name="Auton 4.2 Blue", preselectTeleOp = "AAAAAAAAA")
public class AutonFourB extends LinearOpMode {
public class AutonFourB extends BaseLinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.print("Status", "Initializing, please wait");
MovementAPI.init();
GameMotor.LIFT.m.setDirection(API.Direction.REVERSE, false);
this.construct();
api.print("Status", "Initializing, please wait");
MovementAPI.init(api, motors);
motors.lift.setDirection(API.Direction.REVERSE, false);
API.clear();
API.print("Press play to start");
api.clear();
api.print("Press play to start");
waitForStart();
MovementAPI.move(-180, 0.4, true);
API.pause(0.8);
api.pause(0.8);
MovementAPI.stop();
GameMotor.LIFT.m.start(0.25);
API.pause(3);
GameMotor.LIFT.m.stop();
GameMotor.LIFT.m.start(-0.25);
API.pause(2.5);
GameMotor.LIFT.m.stop();
motors.lift.start(0.25);
api.pause(3);
motors.lift.stop();
motors.lift.start(-0.25);
api.pause(2.5);
motors.lift.stop();
MovementAPI.move(0, 0.4, true);
API.pause(1);
api.pause(1);
MovementAPI.move(90, 0.4, true);
API.pause(3);
api.pause(3);
MovementAPI.move(-180, 0.4, true);
API.pause(1);
api.pause(1);
MovementAPI.move(90, 0.4, true);
API.pause(1);
api.pause(1);
MovementAPI.move(0, 0.2, true);
API.pause(1.6);
api.pause(1.6);
MovementAPI.move(0, 0.1, true);
GameMotor.CAROUSEL.m.start(0.75);
API.pause(5);
GameMotor.CAROUSEL.m.stop();
motors.carousel.start(0.75);
api.pause(5);
motors.carousel.stop();
MovementAPI.move(180, 0.4, true);
API.pause(0.47);
api.pause(0.47);
MovementAPI.stop();
}
}

View file

@ -4,44 +4,44 @@ import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
@Autonomous(name="Auton 4.2 Red", preselectTeleOp = "AAAAAAAAA")
public class AutonFourR extends LinearOpMode {
public class AutonFourR extends BaseLinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.print("Status", "Initializing, please wait");
MovementAPI.init();
GameMotor.LIFT.m.setDirection(API.Direction.REVERSE, false);
this.construct();
api.print("Status", "Initializing, please wait");
MovementAPI.init(api, motors);
motors.lift.setDirection(API.Direction.REVERSE, false);
API.clear();
API.print("Press play to start");
api.clear();
api.print("Press play to start");
waitForStart();
MovementAPI.move(-180, 0.4, true);
API.pause(0.8);
api.pause(0.8);
MovementAPI.stop();
GameMotor.LIFT.m.start(0.25);
API.pause(3);
GameMotor.LIFT.m.stop();
GameMotor.LIFT.m.start(-0.25);
API.pause(2.5);
GameMotor.LIFT.m.stop();
motors.lift.start(0.25);
api.pause(3);
motors.lift.stop();
motors.lift.start(-0.25);
api.pause(2.5);
motors.lift.stop();
MovementAPI.move(0, 0.4, true);
API.pause(1);
api.pause(1);
MovementAPI.move(-90, 0.4, true);
API.pause(3);
api.pause(3);
MovementAPI.move(-180, 0.4, true);
API.pause(1);
api.pause(1);
MovementAPI.move(-90, 0.4, true);
API.pause(1);
api.pause(1);
MovementAPI.move(0, 0.2, true);
API.pause(1.6);
api.pause(1.6);
MovementAPI.move(0, 0.1, true);
GameMotor.CAROUSEL.m.start(-0.75);
API.pause(5);
GameMotor.CAROUSEL.m.stop();
motors.carousel.start(-0.75);
api.pause(5);
motors.carousel.stop();
MovementAPI.move(180, 0.4, true);
API.pause(0.6);
api.pause(0.6);
MovementAPI.stop();
}
}

View file

@ -1,24 +0,0 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
@Autonomous(name="Autonomous 0.00000000000000000000000002 WORKING????", preselectTeleOp="AAAAAAAAA")
public class AutonOne extends LinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.print("Status", "Initializing, please wait");
MovementAPI.init();
API.clear();
API.print("Press play to start");
waitForStart();
MovementAPI.move(0, 1, true);
API.pause(1);
MovementAPI.stop();
}
}

View file

@ -1,24 +0,0 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
@Autonomous(name="Autonomous 0.00000000000000000000000003 WORKING????", preselectTeleOp="AAAAAAAAA")
public class AutonThree extends LinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.print("Status", "Initializing, please wait");
MovementAPI.init();
API.clear();
API.print("Press play to start");
waitForStart();
MovementAPI.move(0, 1, true);
API.pause(1);
MovementAPI.stop();
GameMotor.CAROUSEL.m.start(0.6);
}
}

View file

@ -1,33 +0,0 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
@Autonomous(name="Auton 2.0 maybe working?????", preselectTeleOp = "AAAAAAAAA")
public class AutonTwo extends LinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.print("Status", "Initializing, please wait");
MovementAPI.init();
GameMotor.LIFT.m.setDirection(API.Direction.REVERSE, false);
API.clear();
API.print("Press play to start");
waitForStart();
MovementAPI.move(-180, 0.4, true);
API.pause(0.8);
MovementAPI.stop();
GameMotor.LIFT.m.start(0.25);
API.pause(5);
GameMotor.LIFT.m.stop();
GameMotor.LIFT.m.start(-0.25);
API.pause(2.5);
GameMotor.LIFT.m.stop();
MovementAPI.move(-180, -0.4, true);
API.pause(0.8);
MovementAPI.stop();
}
}

View file

@ -0,0 +1,17 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
public abstract class BaseLinearOpMode extends LinearOpMode {
protected API api;
protected GameMotors motors;
public void construct() {
api = new API(this, true);
motors = new GameMotors(api);
}
public void construct(boolean useLogFile) {
api = new API(this, useLogFile);
motors = new GameMotors(api);
}
}

View file

@ -0,0 +1,17 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
public abstract class BaseOpMode extends OpMode {
protected API api;
protected GameMotors motors;
public void construct() {
api = new API(this);
motors = new GameMotors(api);
}
public void construct(boolean useLogFile) {
api = new API(this, useLogFile);
motors = new GameMotors(api);
}
}

View file

@ -1,21 +0,0 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
@TeleOp
public class BasicOpMode extends LinearOpMode {
@Override
public void runOpMode() {
API.init(this);
API.Motor motor = API.Motor.M0;
motor.setDirection(API.Direction.REVERSE);
waitForStart();
while(opModeIsActive()) {
motor.start(1);
}
}
}

View file

@ -1,11 +1,21 @@
package org.firstinspires.ftc.teamcode;
//public enum GameMotor {
// FL(API.Motor.M0), FR(API.Motor.M1), BL(API.Motor.M2), BR(API.Motor.M3), INTAKE(API.Motor.M4), LIFT(API.Motor.M5), CAROUSEL(API.Motor.M6);
//
// public final API.Motor m;
//
// GameMotor(API.Motor _m) {
// m = _m;
// }
//}
public enum GameMotor {
FL(API.Motor.M0), FR(API.Motor.M1), BL(API.Motor.M2), BR(API.Motor.M3), INTAKE(API.Motor.M4), LIFT(API.Motor.M5), CAROUSEL(API.Motor.M6);
FL("m0"), FR("m1"), BL("m2"), BR("m3"),
INTAKE("m4"), LIFT("m5"), CAROUSEL("m6");
public final API.Motor m;
public final String name;
GameMotor(API.Motor _m) {
m = _m;
}
GameMotor(String name) { this.name = name; }
}

View file

@ -0,0 +1,20 @@
package org.firstinspires.ftc.teamcode;
public class GameMotors {
private API api;
public API.Motor fl, fr, bl, br, intake, lift, carousel;
public GameMotors(API api) {
this.api = api;
fl = api.new Motor(GameMotor.FL.name);
fr = api.new Motor(GameMotor.FR.name);
bl = api.new Motor(GameMotor.BL.name);
br = api.new Motor(GameMotor.BR.name);
intake = api.new Motor(GameMotor.INTAKE.name);
lift = api.new Motor(GameMotor.LIFT.name);
carousel = api.new Motor(GameMotor.CAROUSEL.name);
}
}

View file

@ -1,39 +1,38 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
public class Gamepad extends OpMode {
public abstract class Gamepad extends BaseOpMode {
protected double speed = 1;
@Override
public void init() {
API.init(this);
API.print("Status", "Initializing, please wait");
API.pause(1);
this.construct();
GameMotor.LIFT.m.setDirection(API.Direction.REVERSE);
MovementAPI.init();
api.print("Status", "Initializing, please wait");
api.pause(1);
API.clear();
API.print("Press play to start");
motors.lift.setDirection(API.Direction.REVERSE);
MovementAPI.init(api, motors);
api.clear();
api.print("Press play to start");
}
@Override
public void start() {
API.clear();
api.clear();
}
@Override
public void loop() {
MovementAPI.move(-gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x, speed, true);
API.print("Speed", speed + ""); // i dont know how to do this better
api.print("Speed", speed + ""); // i dont know how to do this better
if (gamepad1.right_bumper) speed = Math.min(speed + 0.01, 1);
else if (gamepad1.left_bumper) speed = Math.max(speed - 0.01, 0.2);
GameMotor.INTAKE.m.controlWithTwoButtons(gamepad2.a, gamepad2.b);
GameMotor.LIFT.m.controlWithTwoButtons(gamepad2.dpad_up, gamepad2.dpad_down, 0.25);
GameMotor.CAROUSEL.m.controlWithTwoButtons(gamepad2.x, gamepad2.y);
motors.intake.controlWithTwoButtons(gamepad2.a, gamepad2.b);
motors.lift.controlWithTwoButtons(gamepad2.dpad_up, gamepad2.dpad_down, 0.25);
motors.carousel.controlWithTwoButtons(gamepad2.x, gamepad2.y);
}
}

View file

@ -9,6 +9,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.HashMap;
@TeleOp(name="Gamepad Recorder v1.0.1")
public class GamepadRecord extends Gamepad {
@ -17,6 +18,7 @@ public class GamepadRecord extends Gamepad {
private JSONObject outputJSON;
private boolean prevGuide = false;
private int currentIndex = 0;
private HashMap<GameMotor, API.Motor> motors = new HashMap();
@Override
public void init() {
@ -27,11 +29,15 @@ public class GamepadRecord extends Gamepad {
outputJSON = new JSONObject();
outputJSON.put("recordedData", new JSONArray());
} catch (JSONException e) {
API.print("Why are we here? Just to suffer?");
api.print("Why are we here? Just to suffer?");
}
API.clear();
API.print("Press play to start");
for (GameMotor m : GameMotor.values()) {
motors.put(m, api.new Motor(m.name));
}
api.clear();
api.print("Press play to start");
}
@Override
@ -40,13 +46,13 @@ public class GamepadRecord extends Gamepad {
super.loop();
API.print("Time", this.time + "");
api.print("Time", this.time + "");
try {
JSONObject motorData = new JSONObject();
for (GameMotor m : GameMotor.values()) {
motorData.put(m.name().toLowerCase(), m.m.getRawPower());
motorData.put(m.name().toLowerCase(), motors.get(m).getRawPower());
}
JSONObject currentData = new JSONObject()
@ -61,19 +67,19 @@ public class GamepadRecord extends Gamepad {
)
outputJSON.getJSONArray("recordedData").put(currentIndex++, currentData);
} catch (JSONException e) {
API.print("Why are we here? Just to suffer?" + System.lineSeparator() + e.getMessage());
api.print("Why are we here? Just to suffer?" + System.lineSeparator() + e.getMessage());
}
ms -= System.currentTimeMillis();
if (ms > 5) try {
Thread.sleep(ms);
} catch (InterruptedException ie) {
API.print("Why are we here? Just to suffer? (Thread.sleep error)");
api.print("Why are we here? Just to suffer? (Thread.sleep error)");
}
if (gamepad1.guide && !prevGuide) {
ReadWriteFile.writeFile(outputFile, outputJSON.toString());
API.print("Wrote file to '" + outputFile.getName() + "'!");
api.print("Wrote file to '" + outputFile.getName() + "'!");
}
prevGuide = gamepad1.guide;

View file

@ -1,6 +1,8 @@
package org.firstinspires.ftc.teamcode;
public class MovementAPI {
private static API api;
private static API.Motor fl;
private static API.Motor fr;
private static API.Motor bl;
@ -19,8 +21,10 @@ public class MovementAPI {
* @param _bl the back-left wheel
* @param _br the back-right wheel
*/
public static void init(API.Motor _fl, API.Motor _fr, API.Motor _bl, API.Motor _br) {
API.print("Initializing MovementAPI...");
public static void init(API api, API.Motor _fl, API.Motor _fr, API.Motor _bl, API.Motor _br) {
MovementAPI.api = api;
api.print("Initializing Movementapi...");
fl = _fl;
fr = _fr;
@ -36,8 +40,8 @@ public class MovementAPI {
/**
* Initializes the API with the motors in the GameMotor enum
*/
public static void init() {
init(GameMotor.FL.m, GameMotor.FR.m, GameMotor.BL.m, GameMotor.BR.m);
public static void init(API api, GameMotors motors) {
init(api, motors.fl, motors.fr, motors.bl, motors.br);
}
/**
@ -66,7 +70,7 @@ public class MovementAPI {
bl.start(blPower);
br.start(brPower);
if (verbose) API.print(
if (verbose) api.print(
"Front Left: " + flPower + System.lineSeparator() +
"Back Left: " + blPower + System.lineSeparator() +
"Front Right: " + frPower + System.lineSeparator() +

View file

@ -1,6 +1,5 @@
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.util.ReadWriteFile;
import org.firstinspires.ftc.robotcore.internal.system.AppUtil;
@ -8,12 +7,14 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.HashMap;
public class Replayer extends OpMode {
public class Replayer extends BaseOpMode {
private final String recordingFileName;
private File recordingFile;
private JSONObject recordingJSON;
private int nextIndex = 0;
private HashMap<GameMotor, API.Motor> motors = new HashMap();
public Replayer(String fileName) {
this.recordingFileName = fileName;
@ -21,9 +22,10 @@ public class Replayer extends OpMode {
@Override
public void init() {
API.init(this);
API.print("Status", "Initializing, please wait");
API.pause(1);
this.construct();
api.print("Status", "Initializing, please wait");
api.pause(1);
recordingFile = AppUtil.getInstance().getSettingsFile(recordingFileName);
try {
@ -32,13 +34,17 @@ public class Replayer extends OpMode {
somethingWentWrong(e);
}
API.clear();
API.print("Press play to start");
for (GameMotor m : GameMotor.values()) {
motors.put(m, api.new Motor(m.name));
}
api.clear();
api.print("Press play to start");
}
@Override
public void start() {
API.clear();
api.clear();
this.resetStartTime();
}
@ -53,7 +59,7 @@ public class Replayer extends OpMode {
if (this.getRuntime() * 1000 < currentListElement.getDouble("time")) return;
for (GameMotor m : GameMotor.values()) {
m.m.start(currentData.has(m.name().toLowerCase()) ? currentData.getDouble(m.name().toLowerCase()) : 0);
motors.get(m).start(currentData.has(m.name().toLowerCase()) ? currentData.getDouble(m.name().toLowerCase()) : 0);
}
nextIndex++;
@ -68,7 +74,7 @@ public class Replayer extends OpMode {
}
public void somethingWentWrong(Exception e) {
API.print(
api.print(
"Why are we here? Just to suffer?" + System.lineSeparator() + e.getMessage()
);
this.requestOpModeStop();