forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use annotations for hardware! great idea
- Loading branch information
1 parent
20478ae
commit a9b08ac
Showing
8 changed files
with
136 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/HardwareMapper.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/AutoClearEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Specifies that an encoder should be cleared when the robot is initialized. | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface AutoClearEncoder { } |
74 changes: 74 additions & 0 deletions
74
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/HardwareMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import com.qualcomm.robotcore.hardware.DcMotor; | ||
import com.qualcomm.robotcore.hardware.DcMotorSimple; | ||
import com.qualcomm.robotcore.hardware.HardwareMap; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Annotation processor for hardware map things. | ||
*/ | ||
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; | ||
Object result = map.tryGet(targetType, field.getName()); | ||
|
||
if (result == null) { | ||
throw new RuntimeException( | ||
"Hardware: '" + | ||
field.getName() + "' not found, expected type " + | ||
targetType.getName() + " for field " + | ||
field.getName() + " in " + this.getClass().getName() | ||
); | ||
} | ||
try { | ||
field.set(this, result); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException("Field " + field.getName() + " assign failed: " + result.getClass().getName() + " to " + field.getType().getName()); | ||
} | ||
|
||
if (field.isAnnotationPresent(Reversed.class)) { | ||
if (targetType.isAssignableFrom(DcMotorSimple.class)) { | ||
DcMotorSimple motor = (DcMotorSimple) result; | ||
motor.setDirection(DcMotorSimple.Direction.REVERSE); | ||
} else { | ||
throw new RuntimeException("@Reversed annotation can only be used on DcMotorSimple (or subclasses)"); | ||
} | ||
} | ||
if (field.isAnnotationPresent(ZeroPower.class)) { | ||
if (targetType.isAssignableFrom(DcMotor.class)) { | ||
DcMotor motor = (DcMotor) result; | ||
ZeroPower zeroPower = field.getAnnotation(ZeroPower.class); | ||
if (zeroPower == null) throw new RuntimeException("ZeroPower annotation is null?!"); | ||
motor.setZeroPowerBehavior(zeroPower.value()); | ||
} else { | ||
throw new RuntimeException("@ZeroPower annotation can only be used on DcMotor (or subclasses)"); | ||
} | ||
} | ||
if (field.isAnnotationPresent(AutoClearEncoder.class)) { | ||
if (targetType.isAssignableFrom(DcMotor.class)) { | ||
DcMotor motor = (DcMotor) result; | ||
DcMotor.RunMode current = motor.getMode(); | ||
motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); | ||
motor.setMode(current); | ||
} else { | ||
throw new RuntimeException("@AutoClearEncoder annotation can only be used on DcMotor (or subclasses)"); | ||
} | ||
} | ||
} catch (IllegalArgumentException e) { | ||
throw new RuntimeException("Field " + field.getName() + " typecast failed"); | ||
} finally { | ||
// lock the field back if it was locked | ||
field.setAccessible(accessible); | ||
} | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/HardwareName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import java.lang.annotation.Documented; | ||
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) | ||
@Documented | ||
public @interface HardwareName { | ||
String value(); | ||
} |
6 changes: 6 additions & 0 deletions
6
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/Reversed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Specifies that a motor should start with its direction reversed. | ||
* The annotated field must be a DcMotorSimple or subclass. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
@Documented | ||
public @interface Reversed {} |
19 changes: 19 additions & 0 deletions
19
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/ZeroPower.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.firstinspires.ftc.teamcode.hardware; | ||
|
||
import com.qualcomm.robotcore.hardware.DcMotor; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Defiles the zero power behavior of a motor. | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface ZeroPower { | ||
DcMotor.ZeroPowerBehavior value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters