001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.wpilibj; 006 007import edu.wpi.first.hal.AnalogJNI; 008import edu.wpi.first.hal.FRCNetComm.tResourceType; 009import edu.wpi.first.hal.HAL; 010import edu.wpi.first.hal.util.BoundaryException; 011import edu.wpi.first.util.sendable.Sendable; 012import edu.wpi.first.util.sendable.SendableBuilder; 013import edu.wpi.first.util.sendable.SendableRegistry; 014import edu.wpi.first.wpilibj.AnalogTriggerOutput.AnalogTriggerType; 015import java.lang.ref.Reference; 016 017/** Class for creating and configuring Analog Triggers. */ 018public class AnalogTrigger implements Sendable, AutoCloseable { 019 /** Where the analog trigger is attached. */ 020 protected int m_port; 021 022 private AnalogInput m_analogInput; 023 024 private DutyCycle m_dutyCycle; 025 026 private boolean m_ownsAnalog; 027 028 /** 029 * Constructor for an analog trigger given a channel number. 030 * 031 * @param channel the port to use for the analog trigger 032 */ 033 @SuppressWarnings("this-escape") 034 public AnalogTrigger(final int channel) { 035 this(new AnalogInput(channel)); 036 m_ownsAnalog = true; 037 SendableRegistry.addChild(this, m_analogInput); 038 } 039 040 /** 041 * Construct an analog trigger given an analog channel. This should be used in the case of sharing 042 * an analog channel between the trigger and an analog input object. 043 * 044 * @param channel the AnalogInput to use for the analog trigger 045 */ 046 @SuppressWarnings("this-escape") 047 public AnalogTrigger(AnalogInput channel) { 048 m_analogInput = channel; 049 050 m_port = AnalogJNI.initializeAnalogTrigger(channel.m_port); 051 052 int index = getIndex(); 053 054 HAL.report(tResourceType.kResourceType_AnalogTrigger, index + 1); 055 SendableRegistry.addLW(this, "AnalogTrigger", index); 056 } 057 058 /** 059 * Construct an analog trigger given a duty cycle input. 060 * 061 * @param input the DutyCycle to use for the analog trigger 062 */ 063 @SuppressWarnings("this-escape") 064 public AnalogTrigger(DutyCycle input) { 065 m_dutyCycle = input; 066 067 m_port = AnalogJNI.initializeAnalogTriggerDutyCycle(input.m_handle); 068 069 int index = getIndex(); 070 071 HAL.report(tResourceType.kResourceType_AnalogTrigger, index + 1); 072 SendableRegistry.addLW(this, "AnalogTrigger", index); 073 } 074 075 @Override 076 public void close() { 077 SendableRegistry.remove(this); 078 AnalogJNI.cleanAnalogTrigger(m_port); 079 m_port = 0; 080 if (m_ownsAnalog && m_analogInput != null) { 081 m_analogInput.close(); 082 } 083 Reference.reachabilityFence(m_dutyCycle); 084 } 085 086 /** 087 * Set the upper and lower limits of the analog trigger. The limits are given in ADC codes. If 088 * oversampling is used, the units must be scaled appropriately. 089 * 090 * @param lower the lower raw limit 091 * @param upper the upper raw limit 092 */ 093 public void setLimitsRaw(final int lower, final int upper) { 094 if (lower > upper) { 095 throw new BoundaryException("Lower bound is greater than upper"); 096 } 097 AnalogJNI.setAnalogTriggerLimitsRaw(m_port, lower, upper); 098 } 099 100 /** 101 * Set the upper and lower limits of the analog trigger. The limits are given as floating point 102 * values between 0 and 1. 103 * 104 * @param lower the lower duty cycle limit 105 * @param upper the upper duty cycle limit 106 */ 107 public void setLimitsDutyCycle(double lower, double upper) { 108 if (lower > upper) { 109 throw new BoundaryException("Lower bound is greater than upper bound"); 110 } 111 AnalogJNI.setAnalogTriggerLimitsDutyCycle(m_port, lower, upper); 112 } 113 114 /** 115 * Set the upper and lower limits of the analog trigger. The limits are given as floating point 116 * voltage values. 117 * 118 * @param lower the lower voltage limit 119 * @param upper the upper voltage limit 120 */ 121 public void setLimitsVoltage(double lower, double upper) { 122 if (lower > upper) { 123 throw new BoundaryException("Lower bound is greater than upper bound"); 124 } 125 AnalogJNI.setAnalogTriggerLimitsVoltage(m_port, lower, upper); 126 } 127 128 /** 129 * Configure the analog trigger to use the averaged vs. raw values. If the value is true, then the 130 * averaged value is selected for the analog trigger, otherwise the immediate value is used. 131 * 132 * @param useAveragedValue true to use an averaged value, false otherwise 133 */ 134 public void setAveraged(boolean useAveragedValue) { 135 AnalogJNI.setAnalogTriggerAveraged(m_port, useAveragedValue); 136 } 137 138 /** 139 * Configure the analog trigger to use a filtered value. The analog trigger will operate with a 3 140 * point average rejection filter. This is designed to help with 360 degree pot applications for 141 * the period where the pot crosses through zero. 142 * 143 * @param useFilteredValue true to use a filtered value, false otherwise 144 */ 145 public void setFiltered(boolean useFilteredValue) { 146 AnalogJNI.setAnalogTriggerFiltered(m_port, useFilteredValue); 147 } 148 149 /** 150 * Return the index of the analog trigger. This is the FPGA index of this analog trigger instance. 151 * 152 * @return The index of the analog trigger. 153 */ 154 public final int getIndex() { 155 return AnalogJNI.getAnalogTriggerFPGAIndex(m_port); 156 } 157 158 /** 159 * Return the InWindow output of the analog trigger. True if the analog input is between the upper 160 * and lower limits. 161 * 162 * @return The InWindow output of the analog trigger. 163 */ 164 public boolean getInWindow() { 165 return AnalogJNI.getAnalogTriggerInWindow(m_port); 166 } 167 168 /** 169 * Return the TriggerState output of the analog trigger. True if above upper limit. False if below 170 * lower limit. If in Hysteresis, maintain previous state. 171 * 172 * @return The TriggerState output of the analog trigger. 173 */ 174 public boolean getTriggerState() { 175 return AnalogJNI.getAnalogTriggerTriggerState(m_port); 176 } 177 178 /** 179 * Creates an AnalogTriggerOutput object. Gets an output object that can be used for routing. 180 * Caller is responsible for deleting the AnalogTriggerOutput object. 181 * 182 * @param type An enum of the type of output object to create. 183 * @return A pointer to a new AnalogTriggerOutput object. 184 */ 185 public AnalogTriggerOutput createOutput(AnalogTriggerType type) { 186 return new AnalogTriggerOutput(this, type); 187 } 188 189 @Override 190 public void initSendable(SendableBuilder builder) { 191 if (m_ownsAnalog) { 192 m_analogInput.initSendable(builder); 193 } 194 } 195}