Skip to content

Commit

Permalink
WIP: magic
Browse files Browse the repository at this point in the history
  • Loading branch information
penguinencounter committed Jul 3, 2024
1 parent d0f8f3d commit 20478ae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;

public class Hardware {
import org.firstinspires.ftc.teamcode.hardware.HardwareName;
import org.firstinspires.ftc.teamcode.hardware.Reversed;

public class Hardware extends HardwareMapper {
// left = left motor = exp 0 frontLeft
// right = right motor = ctr 0 frontRight
// center = ctr 3 intake

public final DcMotor frontLeft;
public final DcMotor frontRight;
public final DcMotor backLeft;
public final DcMotor backRight;

public final DcMotor encoderLeft;
public final DcMotor encoderCenter;
public final DcMotor encoderRight;
@HardwareName("frontLeft")
@Reversed
public DcMotor frontLeft;
@HardwareName("frontRight")
public DcMotor frontRight;
@HardwareName("backLeft")
@Reversed
public DcMotor backLeft;
@HardwareName("backRight")
public DcMotor backRight;

@HardwareName("frontLeft")
public DcMotor encoderLeft;
@HardwareName("intake")
public DcMotor encoderCenter;
@HardwareName("frontRight")
public DcMotor encoderRight;

private void resetEncoder(DcMotor target) {
DcMotor.RunMode mode = target.getMode();
Expand All @@ -24,6 +36,7 @@ private void resetEncoder(DcMotor target) {
}

public Hardware(HardwareMap hwMap) {
super(hwMap);
frontLeft = hwMap.dcMotor.get("frontLeft");
frontRight = hwMap.dcMotor.get("frontRight");
backLeft = hwMap.dcMotor.get("backLeft");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.hardware.HardwareMap;

import org.firstinspires.ftc.teamcode.hardware.HardwareName;

import java.lang.reflect.Field;

public abstract class HardwareMapper {
public HardwareMapper(HardwareMap map) {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
boolean accessible = field.isAccessible();
if (!accessible) field.setAccessible(true);
try {
Class<?> targetType = field.getType();
HardwareName annotation = field.getAnnotation(HardwareName.class);
if (annotation == null) continue;
field.set(this, map.get(targetType, field.getName()));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Field " + field.getName() + " typecast failed");
} finally {
// lock the field back if it was locked
field.setAccessible(accessible);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.firstinspires.ftc.teamcode;
package org.firstinspires.ftc.teamcode.hardware;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface HardwareName {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.firstinspires.ftc.teamcode.hardware;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Reversed {}

0 comments on commit 20478ae

Please sign in to comment.