-
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.
Showing
12 changed files
with
100 additions
and
128 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
48 changes: 24 additions & 24 deletions
48
...teamcode/Hardwares/basic/HardwareSet.java → ...c/teamcode/Hardwares/basic/DeviceMap.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,72 +1,72 @@ | ||
package org.firstinspires.ftc.teamcode.Hardwares.basic; | ||
|
||
import android.util.Log; | ||
|
||
import com.qualcomm.robotcore.hardware.DcMotor; | ||
import com.qualcomm.robotcore.hardware.DcMotorEx; | ||
import com.qualcomm.robotcore.hardware.DcMotorSimple; | ||
import com.qualcomm.robotcore.hardware.HardwareDevice; | ||
import com.qualcomm.robotcore.hardware.HardwareMap; | ||
import com.qualcomm.robotcore.hardware.Servo; | ||
|
||
import org.firstinspires.ftc.teamcode.utils.enums.HardwareType; | ||
import org.firstinspires.ftc.teamcode.utils.enums.HardwareDevices; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @apiNote OpenCvCamera和BNU055IMU都不属于接口HardwareDevice | ||
*/ | ||
public class HardwareSet { | ||
public Map<HardwareType, HardwareDevice > devices; | ||
public HardwareSet(){ | ||
public class DeviceMap { | ||
public Map<HardwareDevices, HardwareDevice > devices; | ||
public DeviceMap(HardwareMap hardwareMap){ | ||
devices=new HashMap<>(); | ||
for(HardwareDevices device: HardwareDevices.values()){ | ||
devices.put(device,(HardwareDevice)hardwareMap.get(device.classType,device.deviceName)); | ||
} | ||
} | ||
|
||
public void addDevice(HardwareDevice device, HardwareType hardwareType){ | ||
devices.put(hardwareType,device); | ||
Log.i("HardwareSet",device.getDeviceName()+" added."); | ||
} | ||
public HardwareDevice getDevice(HardwareType hardwareType){ | ||
if(devices.containsKey(hardwareType)){ | ||
return devices.get(hardwareType); | ||
public HardwareDevice getDevice(HardwareDevices hardwareDevices){ | ||
if(devices.containsKey(hardwareDevices)){ | ||
return devices.get(hardwareDevices); | ||
}else{ | ||
throw new NullPointerException("Device Not Found:"+hardwareType.toString()); | ||
throw new NullPointerException("Device Not Found:"+ hardwareDevices.toString()); | ||
} | ||
} | ||
public void setDirection(HardwareType hardwareType, DcMotorSimple.Direction direction){ | ||
HardwareDevice device=getDevice(hardwareType); | ||
public void setDirection(HardwareDevices hardwareDevices, DcMotorSimple.Direction direction){ | ||
HardwareDevice device=getDevice(hardwareDevices); | ||
if(device instanceof DcMotorEx){ | ||
((DcMotorEx) device).setDirection(direction); | ||
}else{ | ||
throw new RuntimeException("DcMotor's Direction Only Matches To DcMotor"); | ||
} | ||
} | ||
public void setPower(HardwareType hardwareType, double power){ | ||
HardwareDevice device=getDevice(hardwareType); | ||
public void setPower(HardwareDevices hardwareDevices, double power){ | ||
HardwareDevice device=getDevice(hardwareDevices); | ||
if(device instanceof DcMotorEx){ | ||
((DcMotorEx) device).setPower(power); | ||
}else if(device instanceof DcMotor){ | ||
((DcMotor) device).setPower(power); | ||
}else if(device instanceof Servos){ | ||
throw new RuntimeException("Cannot set the power of a servo at HardwareSet.class"); | ||
throw new RuntimeException("Cannot set the power of a servo at DeviceMap.class"); | ||
} | ||
} | ||
public void setPosition(HardwareType hardwareType, double position){ | ||
HardwareDevice device=getDevice(hardwareType); | ||
public void setPosition(HardwareDevices hardwareDevices, double position){ | ||
HardwareDevice device=getDevice(hardwareDevices); | ||
if(device instanceof Servo){ | ||
((Servo) device).setPosition(position); | ||
}else{ | ||
throw new RuntimeException("Not allowed to set the position of a device witch isn't a Servo"); | ||
} | ||
} | ||
public double getPosition(HardwareType hardwareType){ | ||
HardwareDevice device=getDevice(hardwareType); | ||
public double getPosition(HardwareDevices hardwareDevices){ | ||
HardwareDevice device=getDevice(hardwareDevices); | ||
if (device instanceof Servo) { | ||
return ((Servo) device).getPosition(); | ||
}else if(device instanceof DcMotorEx){ | ||
return ((DcMotorEx) device).getCurrentPosition(); | ||
}else if(device instanceof DcMotor){ | ||
return ((DcMotor) device).getCurrentPosition(); | ||
}else{ | ||
throw new RuntimeException("Cannot get the position of other devices at HardwareSet.class"); | ||
throw new RuntimeException("Cannot get the position of other devices at DeviceMap.class"); | ||
} | ||
} | ||
} |
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
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
28 changes: 7 additions & 21 deletions
28
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Hardwares/basic/Servos.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
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
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
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
26 changes: 0 additions & 26 deletions
26
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/namespace.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/utils/enums/HardwareDevices.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,28 @@ | ||
package org.firstinspires.ftc.teamcode.utils.enums; | ||
|
||
import com.qualcomm.robotcore.hardware.DcMotorEx; | ||
import com.qualcomm.robotcore.hardware.Servo; | ||
|
||
/** | ||
* 仅适用于类型为<code>HardwareDevice</code> 接口下的硬件 | ||
* <p> | ||
* 可以自动登记硬件的名字及其类型 | ||
*/ | ||
public enum HardwareDevices { | ||
LeftFront("leftFront", DcMotorEx.class), | ||
RightFront("eightFront", DcMotorEx.class), | ||
LeftRear("leftRear", DcMotorEx.class), | ||
RightRear("rightRear", DcMotorEx.class), | ||
PlacementArm("rightLift", DcMotorEx.class), | ||
Intake("intake", DcMotorEx.class), | ||
FrontClip("frontClip", Servo.class), | ||
RearClip("rearClip", Servo.class), | ||
SuspensionArm("rack", DcMotorEx.class); | ||
public final String deviceName; | ||
public final Class<?> classType; | ||
|
||
HardwareDevices(String deviceName, Class<?> classType){ | ||
this.deviceName=deviceName; | ||
this.classType=classType; | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/utils/enums/HardwareType.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.