Vision Subsystem¶
The Vision Subsystem will house the core of the code. It is always a good idea to have a Vision subsystem that will hold all the getters and setters for the vision on the robot.
VisionSubsystem.java¶
Imports¶
1 2 3 4 5 6 7 | package frc.robot.subsystems; import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import edu.wpi.first.wpilibj2.command.SubsystemBase; |
Line
1is the package setup, and will assign this class to the package.Line
3is the import for networktables; this allows us to talk with the vision Scripts.Line
4is the import for network table entries and allows us to read the data from the table.Line
5is the import for the networktables instance and is used to get the table.Line
6is the import for SmartDashboard, which will be used to put values for user display.Line
7is the import for SubsystemBase, which is required to have this class become a subsystem.
Class¶
9 | public class VisionSubsystem extends SubsystemBase |
Line
9creates the classVisionSubsystemand extends SubsystemBase to have this class be a subsystem.
Objects¶
11 12 13 | private NetworkTableInstance inst = NetworkTableInstance.getDefault(); private NetworkTable table = inst.getTable("Vision"); private NetworkTableEntry data; |
Line
11creates the NetworkTableInstanceLine
12creates the tableLine
13creates the table entry reference
Constructor¶
15 16 17 18 | public VisionSubsystem() { SmartDashboard.putBoolean("Get New Barcode", false); } |
Line
15is the constructor and will create the VisionSubsystem when called.Line
17Will create an entry in the smartdashboard calledGet New Barcodeand sets its default value to false.
Setter¶
20 21 22 23 | public void readBarcode() { table.getEntry("readBarcode").setBoolean(true); } |
Line
20is the setter that is called when a new barcode should be read.Line
22will update the readBarcode flag in networktables to true. This, in turn, will tell the vision scripts to read a new barcode and update the data keys.
Getter¶
25 26 27 28 29 | public void printBarcode() { data = table.getEntry("barcodeData"); SmartDashboard.putString("Barcode Data", data.getString("Nothing was read")); } |
Line
25is the method that will be called to get the current value of the barcodeData entry.Line
27assigns the entry to the data object.Line
28places the string value of data to the dashboard.
Periodic Loop¶
The periodic loop is used to check the current value of the barcodeData entry for every robot loop.
31 32 33 34 35 | @Override public void periodic() { printBarcode(); } |
Line
31is the required Override to tell the compiler to use this periodic method and not the one built into SubsystemBase.Line
32is the periodic method.Line
34will call theprintBarcodemethod every robot loop.
Full Subsystem Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package frc.robot.subsystems; import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import edu.wpi.first.wpilibj2.command.SubsystemBase; public class VisionSubsystem extends SubsystemBase { private NetworkTableInstance inst = NetworkTableInstance.getDefault(); private NetworkTable table = inst.getTable("Vision"); private NetworkTableEntry data; public VisionSubsystem() { SmartDashboard.putBoolean("Get New Barcode", false); } public void readBarcode() { table.getEntry("readBarcode").setBoolean(true); } public void printBarcode() { data = table.getEntry("barcodeData"); SmartDashboard.putString("Barcode Data", data.getString("Nothing was read")); } @Override public void periodic() { printBarcode(); } } |