Skip to content

Commit

Permalink
加入一些 Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
6-BennyLi-9 committed Aug 24, 2024
1 parent 9cc713b commit f9d2459
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.acmerobotics.roadrunner.Pose2d;

import org.firstinspires.ftc.teamcode.utils.Exceptions.UnKnownErrorsException;

public abstract class SubassemblyLocalizer implements Localizer{
public final LocalizerPlugin[] plugins;
public Pose2d RobotPosition;
Expand Down Expand Up @@ -45,7 +47,7 @@ public void update(){
++VectorFactor;
++HeadingFactor;
}else{
throw new RuntimeException("unknown localizer plugin");
throw new UnKnownErrorsException("Unknown localizer plugin");
}
}
RobotPosition=new Pose2d(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.firstinspires.ftc.teamcode.Hardwares.basic.Servos;
import org.firstinspires.ftc.teamcode.RuntimeOption;
import org.firstinspires.ftc.teamcode.utils.Enums.ClipPosition;
import org.firstinspires.ftc.teamcode.utils.Exceptions.UnKnownErrorsException;

public class Structure {
Motors motors;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void ClipOption(@NonNull ClipPosition clipPosition){
closeClips();
break;
default:
throw new RuntimeException("UnKnown ClipPosition");
throw new UnKnownErrorsException("UnKnown ClipPosition");
}
}

Expand All @@ -79,7 +80,7 @@ public void operateThroughGamePad(@NonNull Gamepad gamepad){
clipPosition=ClipPosition.Open;
break;
default:
throw new RuntimeException("UnKnown ClipPosition");
throw new UnKnownErrorsException("UnKnown ClipPosition");
}
}
}else gamePadButtonBHolding=false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.firstinspires.ftc.teamcode.Hardwares.namespace.HardwareDevices;
import org.firstinspires.ftc.teamcode.utils.Enums.HardwareState;
import org.firstinspires.ftc.teamcode.utils.Exceptions.DeviceDisabledException;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,15 +38,19 @@ public DeviceMap(HardwareMap hardwareMap){
}

public HardwareDevice getDevice(@NonNull HardwareDevices hardwareDevices){
if(hardwareDevices.state==HardwareState.Disabled)throw new RuntimeException("Hardware "+hardwareDevices.name()+" Disabled.");
if(hardwareDevices.state==HardwareState.Disabled) {
throw new DeviceDisabledException(hardwareDevices.name());
}
if(devices.containsKey(hardwareDevices)){
return devices.get(hardwareDevices);
}else{
throw new NullPointerException("Device Not Found:"+ hardwareDevices.toString());
}
}
public void setDirection(@NonNull HardwareDevices hardwareDevices, DcMotorSimple.Direction direction){
if(hardwareDevices.state==HardwareState.Disabled)throw new RuntimeException("Hardware "+hardwareDevices.name()+" Disabled.");
if(hardwareDevices.state==HardwareState.Disabled) {
throw new DeviceDisabledException(hardwareDevices.name());
}
HardwareDevice device=getDevice(hardwareDevices);
if(device instanceof DcMotorEx){
((DcMotorEx) device).setDirection(direction);
Expand All @@ -54,7 +59,9 @@ public void setDirection(@NonNull HardwareDevices hardwareDevices, DcMotorSimple
}
}
public void setPower(@NonNull HardwareDevices hardwareDevices, double power){
if(hardwareDevices.state==HardwareState.Disabled)throw new RuntimeException("Hardware "+hardwareDevices.name()+" Disabled.");
if(hardwareDevices.state==HardwareState.Disabled) {
throw new DeviceDisabledException(hardwareDevices.name());
}
HardwareDevice device=getDevice(hardwareDevices);
if(device instanceof DcMotorEx){
((DcMotorEx) device).setPower(power);
Expand All @@ -65,7 +72,9 @@ public void setPower(@NonNull HardwareDevices hardwareDevices, double power){
}
}
public void setPosition(@NonNull HardwareDevices hardwareDevices, double position){
if(hardwareDevices.state==HardwareState.Disabled)throw new RuntimeException("Hardware "+hardwareDevices.name()+" Disabled.");
if(hardwareDevices.state==HardwareState.Disabled) {
throw new DeviceDisabledException(hardwareDevices.name());
}
HardwareDevice device=getDevice(hardwareDevices);
if(device instanceof Servo){
((Servo) device).setPosition(position);
Expand All @@ -74,7 +83,9 @@ public void setPosition(@NonNull HardwareDevices hardwareDevices, double positio
}
}
public double getPosition(@NonNull HardwareDevices hardwareDevices){
if(hardwareDevices.state==HardwareState.Disabled)throw new RuntimeException("Hardware "+hardwareDevices.name()+" Disabled.");
if(hardwareDevices.state==HardwareState.Disabled) {
throw new DeviceDisabledException(hardwareDevices.name());
}
HardwareDevice device=getDevice(hardwareDevices);
if (device instanceof Servo) {
return ((Servo) device).getPosition();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.firstinspires.ftc.teamcode.utils.Exceptions;

public class DeviceDisabledException extends NullPointerException{
public DeviceDisabledException(String s){
super("Device "+s+" is/are Disabled!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.firstinspires.ftc.teamcode.utils.Exceptions;

public class UnKnownErrorsException extends RuntimeException{
public UnKnownErrorsException(String s){
super("UnKnown Enum Type"+s);
}
}

0 comments on commit f9d2459

Please sign in to comment.