-
Notifications
You must be signed in to change notification settings - Fork 1
Potentially risky but... first generation laser can failure detection #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rongrrz
wants to merge
1
commit into
main
Choose a base branch
from
Elevator-lasercan-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -91,6 +91,12 @@ public class ElevatorSubsystem extends BaseSetpointSubsystem<Distance> { | |
| public final XDigitalInput bottomSensor; | ||
| public final XLaserCAN distanceSensor; | ||
|
|
||
| public final DoubleProperty laserCanOffsetThreshold; | ||
| private boolean useDistanceSensor = false; | ||
| public int badMotorLaserCanHarmonyCount = 0; | ||
| public int harmonyThreshold = 3; | ||
| private Distance previousLaserCanDistance; | ||
|
|
||
| private final SysIdRoutine sysId; | ||
|
|
||
| private final ComplimentaryFilter sensorFusionFilter; | ||
|
|
@@ -120,7 +126,6 @@ public ElevatorSubsystem(XCANMotorController.XCANMotorControllerFactory motorFac | |
| trimChangeAmount = pf.createPersistentProperty("TrimUpAmount", Inches.of(1)); | ||
| laserCanMaxMeasurementLatency = pf.createPersistentProperty("laserCanMaxMeasurementLatency-S", 0.04); | ||
|
|
||
|
|
||
| //to be tuned | ||
| // based on some initial experiments: | ||
| // Elevator raises 36.375 inches (0.923925 meters) after 42.6535 revolutions | ||
|
|
@@ -142,8 +147,9 @@ public ElevatorSubsystem(XCANMotorController.XCANMotorControllerFactory motorFac | |
| this.motionMagicJerk = pf.createPersistentProperty("motionMagicMaxJerk", 0.1); | ||
|
|
||
| this.motionMagicEnabled = pf.createPersistentProperty("motionMagicEnabled", false); | ||
| pf.setDefaultLevel(PropertyLevel.Important); | ||
|
|
||
| this.laserCanOffsetThreshold = pf.createPersistentProperty("laserCanOffsetThreshold-m", 0.0254); // 1 Inch | ||
| pf.setDefaultLevel(PropertyLevel.Important); | ||
|
|
||
| this.sysId = new SysIdRoutine( | ||
| new SysIdRoutine.Config( | ||
|
|
@@ -184,6 +190,7 @@ public ElevatorSubsystem(XCANMotorController.XCANMotorControllerFactory motorFac | |
|
|
||
| if (contract.isElevatorDistanceSensorReady()) { | ||
| this.distanceSensor = xLaserCANFactory.create(contract.getElevatorDistanceSensor(), this.getPrefix()); | ||
| useDistanceSensor = true; | ||
| registerDataFrameRefreshable(distanceSensor); | ||
| } else { | ||
| this.distanceSensor = null; | ||
|
|
@@ -327,7 +334,7 @@ private Optional<Distance> getCalibratedLaserDistance() { | |
| } | ||
|
|
||
| private Optional<Distance> getRawLaserDistance() { | ||
| if (contract.isElevatorDistanceSensorReady()) { | ||
| if (contract.isElevatorDistanceSensorReady() && useDistanceSensor) { | ||
| var distance = distanceSensor.getDistance(); | ||
| var latency = distanceSensor.getMeasurementLatency(); | ||
| return distance.filter(d -> latency.lt(Seconds.of(laserCanMaxMeasurementLatency.get()))); | ||
|
|
@@ -417,12 +424,16 @@ public void configureMotionMagicConstraints(){ | |
| masterMotor.setTrapezoidalProfileJerk(RadiansPerSecondPerSecond.of(motionMagicJerk.get()).per(Second)); | ||
| } | ||
|
|
||
| public void setUseDistanceSensor(boolean value) { | ||
| useDistanceSensor = value; | ||
| } | ||
|
|
||
| @Override | ||
| public void periodic() { | ||
| if (contract.isElevatorReady()) { | ||
| masterMotor.periodic(); | ||
| } | ||
| if(motionMagicAcceleration.hasChangedSinceLastCheck() || motionMagicJerk.hasChangedSinceLastCheck()){ | ||
| if (motionMagicAcceleration.hasChangedSinceLastCheck() || motionMagicJerk.hasChangedSinceLastCheck()){ | ||
| configureMotionMagicConstraints(); | ||
| } | ||
| //bandage case: isTouchingBottom flashes true for one tick on startup, investigate later? | ||
|
|
@@ -440,7 +451,28 @@ public void periodic() { | |
| aKitLog.record("isElevatorCalibrated", isCalibrated()); | ||
| aKitLog.record("isElevatorMaintainerAtGoal", this.isMaintainerAtGoal()); | ||
| isNotCalibratedAlert.set(!isCalibrated()); | ||
| getRawLaserDistance().ifPresent(d -> aKitLog.record("ElevatorDistanceSensor-m", d.in(Meters))); | ||
| getRawLaserDistance().ifPresent(d -> { | ||
| aKitLog.record("ElevatorDistanceSensor-m", d.in(Meters)); | ||
|
|
||
| if (previousLaserCanDistance == null) { | ||
| previousLaserCanDistance = d; | ||
| } | ||
|
|
||
| // If our laser can distance is dropping above a certain threshold, for three times | ||
| // All while our master motor thinks that it has positive velocity (up I am assuming) then | ||
| // We are in a state of not good stuff and should disable the distance sensor | ||
| if (previousLaserCanDistance.minus(d).in(Meters) > laserCanOffsetThreshold.get() | ||
| && masterMotor.getRawVelocity().baseUnitMagnitude() > 0) { | ||
| if (badMotorLaserCanHarmonyCount < harmonyThreshold) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: suggest renaming to |
||
| badMotorLaserCanHarmonyCount++; | ||
| } else { | ||
| badMotorLaserCanHarmonyCount = 0; | ||
| useDistanceSensor = false; | ||
| } | ||
| } | ||
|
|
||
| previousLaserCanDistance = d; | ||
| }); | ||
| getCalibratedLaserDistance().ifPresent(d -> aKitLog.record("CalibratedElevatorDistanceSensor-m", d.in(Meters))); | ||
| aKitLog.record("CalibratedElevatorMotorSensor-m", getCalibratedMotorDistance().in(Meters)); | ||
| aKitLog.record("MotorOffset-rotations", elevatorMotorPositionOffset.in(Rotations)); | ||
|
|
||
36 changes: 36 additions & 0 deletions
36
src/main/java/competition/subsystems/elevator/commands/SetUseDistanceSensorCommand.java
This file contains hidden or 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,36 @@ | ||
| package competition.subsystems.elevator.commands; | ||
|
|
||
| import competition.subsystems.elevator.ElevatorSubsystem; | ||
| import xbot.common.command.BaseCommand; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| public class SetUseDistanceSensorCommand extends BaseCommand { | ||
|
|
||
| final ElevatorSubsystem elevator; | ||
| private boolean value; | ||
|
|
||
| @Inject | ||
| public SetUseDistanceSensorCommand(ElevatorSubsystem elevator) { | ||
| this.elevator = elevator; | ||
| } | ||
|
|
||
| public void setValue(boolean value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Override | ||
| public void initialize() { | ||
| elevator.setUseDistanceSensor(value); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| // No-code | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFinished() { | ||
| return true; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⭐ for callers, it's nice when the argument is self-documenting (e.g. instead of
value, something likeuseLaserCANSensor)