|
| 1 | +package org.hyperonline.hyperlib.vision; |
| 2 | + |
| 3 | +import edu.wpi.first.networktables.*; |
| 4 | +import edu.wpi.first.wpilibj.PIDSource; |
| 5 | +import org.hyperonline.hyperlib.pid.DisplacementPIDSource; |
| 6 | + |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +/** |
| 10 | + * Base class with common functionality used by VisionConnectors. In particular, |
| 11 | + * this provides methods to store, publish, and subscribe to a result, |
| 12 | + * and access it via PID sources. |
| 13 | + * |
| 14 | + * @param <T> VisionResult type of this AbstractVisionConnector |
| 15 | + * @author Chris McGroarty |
| 16 | + */ |
| 17 | +public abstract class AbstractVisionConnector<T extends VisionResult> implements VisionConnector { |
| 18 | + |
| 19 | + protected final NetworkTableEntry m_xError; |
| 20 | + protected final NetworkTableEntry m_yError; |
| 21 | + protected final NetworkTableEntry m_xAbs; |
| 22 | + protected final NetworkTableEntry m_yAbs; |
| 23 | + protected final NetworkTableEntry m_foundTarget; |
| 24 | + protected final NetworkTableEntry m_lastResultTimestamp; |
| 25 | + protected T m_lastResult; |
| 26 | + protected NetworkTable m_table; |
| 27 | + private String m_mainTableName = "hypervision"; |
| 28 | + private String m_subTableName; |
| 29 | + private NetworkTableInstance m_inst; |
| 30 | + private int m_listenerID; |
| 31 | + |
| 32 | + protected AbstractVisionConnector(String subTableName, NetworkTableInstance inst) { |
| 33 | + m_subTableName = Objects.requireNonNull(subTableName); |
| 34 | + |
| 35 | + m_inst = Objects.requireNonNull(inst); |
| 36 | + m_table = m_inst.getTable(getTableName()); |
| 37 | + m_lastResult = getDefaultValue(); |
| 38 | + |
| 39 | + m_xError = m_table.getEntry("xError"); |
| 40 | + m_yError = m_table.getEntry("yError"); |
| 41 | + m_xAbs = m_table.getEntry("xAbsolute"); |
| 42 | + m_yAbs = m_table.getEntry("yAbsolute"); |
| 43 | + m_foundTarget = m_table.getEntry("foundTarget"); |
| 44 | + m_lastResultTimestamp = m_table.getEntry("lastResultTimestamp"); |
| 45 | + } |
| 46 | + |
| 47 | + private String getTableName() { |
| 48 | + return "/" + m_mainTableName + "/" + m_subTableName; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * add listener for the lastResultTimestamp entry in NetworkTables |
| 53 | + * process the next result on an update |
| 54 | + */ |
| 55 | + public void subscribe() { |
| 56 | + m_listenerID = m_table.addEntryListener("lastResultTimestamp", this::next, EntryListenerFlags.kUpdate); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * remove listener for the lastResultTimestamp entry in NetworkTables |
| 61 | + */ |
| 62 | + public void unsubscribe() { |
| 63 | + m_table.removeEntryListener(m_listenerID); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return {T} |
| 68 | + */ |
| 69 | + public abstract T getDefaultValue(); |
| 70 | + |
| 71 | + /** |
| 72 | + * @param result {T} |
| 73 | + */ |
| 74 | + public void updateResult(T result) { |
| 75 | + if (m_lastResult == null) { |
| 76 | + m_lastResult = getDefaultValue(); |
| 77 | + } |
| 78 | + if (!m_lastResult.equals(result)) { |
| 79 | + m_lastResult = (result == null ? getDefaultValue() : result); |
| 80 | + if (m_inst.isConnected()) { |
| 81 | + this.publish(); |
| 82 | + } |
| 83 | + } else { |
| 84 | + System.out.println("NetworkTableInstance(69) not connected"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * publish VisionResult values to corresponding NetworkTable Entries |
| 90 | + * should be overriden and supered by any children of type T extends VisionResult |
| 91 | + */ |
| 92 | + public void publish() { |
| 93 | + m_lastResultTimestamp.setDouble(System.currentTimeMillis()); |
| 94 | + m_xError.setDouble(m_lastResult.xError()); |
| 95 | + m_yError.setDouble(m_lastResult.yError()); |
| 96 | + m_xAbs.setDouble(m_lastResult.xAbsolute()); |
| 97 | + m_yAbs.setDouble(m_lastResult.yAbsolute()); |
| 98 | + m_foundTarget.setBoolean(m_lastResult.foundTarget()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * retrieve a VisionResult from NetworkTable Entries |
| 103 | + * should be overriden and supered by any children of type T extends VisionResult |
| 104 | + * |
| 105 | + * @return {VisionResult} |
| 106 | + */ |
| 107 | + protected VisionResult retrieve() { |
| 108 | + return new VisionResult( |
| 109 | + m_xError.getDouble(0), |
| 110 | + m_yError.getDouble(0), |
| 111 | + m_xAbs.getDouble(0), |
| 112 | + m_yAbs.getDouble(0), |
| 113 | + m_foundTarget.getBoolean(false) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * evaluate and process the results in NetworkTables to my lastResult |
| 119 | + * |
| 120 | + * @param table NetworkTable to get values from |
| 121 | + * @param key Key of the entry that triggered the Listener |
| 122 | + * @param entry Entry that was updated |
| 123 | + * @param value the new Value |
| 124 | + * @param flags Listener flags (created, updated, removed, etc) |
| 125 | + */ |
| 126 | + protected abstract void next(NetworkTable table, String key, NetworkTableEntry entry, |
| 127 | + NetworkTableValue value, int flags); |
| 128 | + |
| 129 | + |
| 130 | + /** |
| 131 | + * @return {T} |
| 132 | + */ |
| 133 | + public T getLastResult() { |
| 134 | + return m_lastResult; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get a PID source which tracks the x-coordinate of the target. |
| 139 | + * |
| 140 | + * @return A PID source tracking the x-coordinate of the target. |
| 141 | + */ |
| 142 | + public PIDSource xPID() { |
| 143 | + return new DisplacementPIDSource() { |
| 144 | + @Override |
| 145 | + public double pidGet() { |
| 146 | + return getLastResult().xError(); |
| 147 | + } |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Get a PID source which tracks the y-coordinate of the target. |
| 153 | + * |
| 154 | + * @return A PID source tracking the y-coordinate of the target. |
| 155 | + */ |
| 156 | + public PIDSource yPID() { |
| 157 | + return new DisplacementPIDSource() { |
| 158 | + @Override |
| 159 | + public double pidGet() { |
| 160 | + return getLastResult().yError(); |
| 161 | + } |
| 162 | + }; |
| 163 | + } |
| 164 | +} |
0 commit comments