-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
530fb62
commit 8edf1ab
Showing
4 changed files
with
143 additions
and
7 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
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,87 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj.Notifier; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import java.net.InetAddress; | ||
import java.util.function.Consumer; | ||
|
||
public class RadioPing extends Command { | ||
private Notifier notifier; | ||
|
||
private InetAddress ipAddress; | ||
|
||
private boolean receivedConnection = false; | ||
private int responseCount = 0; | ||
|
||
boolean initialFailed = false; | ||
Consumer<Boolean> onInitialFailed; | ||
|
||
/** Creates a new RadioPing. */ | ||
public RadioPing(Consumer<Boolean> onInitialFailed) { | ||
try { | ||
ipAddress = InetAddress.getByName("10.3.53.1"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
this.onInitialFailed = onInitialFailed; | ||
notifier = new Notifier(this::pingIPAddress); | ||
} | ||
|
||
public RadioPing() { | ||
this((value) -> {}); | ||
} | ||
|
||
private void pingIPAddress() { | ||
try { | ||
if (ipAddress.isReachable(250)) { | ||
synchronized (this) { | ||
receivedConnection = true; | ||
responseCount++; | ||
} | ||
} else { | ||
synchronized (this) { | ||
initialFailed = true; | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean runsWhenDisabled() { | ||
return true; | ||
} | ||
|
||
// Called when the command is initially scheduled. | ||
@Override | ||
public void initialize() { | ||
notifier.startPeriodic(0.500); | ||
} | ||
|
||
// Called every time the scheduler runs while the command is scheduled. | ||
@Override | ||
public void execute() {} | ||
|
||
// Called once the command ends or is interrupted. | ||
@Override | ||
public void end(boolean interrupted) { | ||
notifier.stop(); | ||
onInitialFailed.accept(initialFailed); | ||
} | ||
|
||
// Returns true when the command should end. | ||
@Override | ||
public boolean isFinished() { | ||
if (!initialFailed) { | ||
return receivedConnection; | ||
} else { | ||
return receivedConnection && responseCount >= 1; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/frc/robot/commands/StartupConnectionCheck.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,33 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Commands; | ||
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup; | ||
|
||
// NOTE: Consider using this command inline, rather than writing a subclass. For more | ||
// information, see: | ||
// https://docs.wpilib.org/en/stable/docs/software/commandbased/convenience-features.html | ||
public class StartupConnectionCheck extends SequentialCommandGroup { | ||
private boolean connectionFailed = false; | ||
|
||
@Override | ||
public boolean runsWhenDisabled() { | ||
return true; | ||
} | ||
|
||
/** Creates a new StartupConnectionCheck. */ | ||
public StartupConnectionCheck(Command whilePinging, Command onSuccess, Command onFailed) { | ||
// Add your commands in the addCommands() call, e.g. | ||
// addCommands(new FooCommand(), new BarCommand()); | ||
addCommands( | ||
new RadioPing() | ||
.withTimeout(60.0) | ||
.deadlineWith(whilePinging) | ||
.finallyDo((interrupted) -> connectionFailed = interrupted), | ||
Commands.either(onSuccess, onFailed, () -> !connectionFailed)); | ||
} | ||
} |
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