Advertisement
YWLAElektraBots

Solenoid.class

Jan 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*/
  2. /* Copyright (c) 2008-2018 FIRST. All Rights Reserved.                        */
  3. /* Open Source Software - may be modified and shared by FRC teams. The code   */
  4. /* must be accompanied by the FIRST BSD license file in the root directory of */
  5. /* the project.                                                               */
  6. /*----------------------------------------------------------------------------*/
  7.  
  8. package edu.wpi.first.wpilibj;
  9.  
  10. import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
  11. import edu.wpi.first.wpilibj.hal.HAL;
  12. import edu.wpi.first.wpilibj.hal.SolenoidJNI;
  13. import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
  14.  
  15. /**
  16.  * Solenoid class for running high voltage Digital Output on the PCM.
  17.  *
  18.  * <p>The Solenoid class is typically used for pneumatic solenoids, but could be used for any
  19.  * device within the current spec of the PCM.
  20.  */
  21. public class Solenoid extends SolenoidBase implements Sendable {
  22.   private final int m_channel; // The channel to control.
  23.   private int m_solenoidHandle;
  24.  
  25.   /**
  26.    * Constructor using the default PCM ID (defaults to 0).
  27.    *
  28.    * @param channel The channel on the PCM to control (0..7).
  29.    */
  30.   public Solenoid(final int channel) {
  31.     this(SensorBase.getDefaultSolenoidModule(), channel);
  32.   }
  33.  
  34.   /**
  35.    * Constructor.
  36.    *
  37.    * @param moduleNumber The CAN ID of the PCM the solenoid is attached to.
  38.    * @param channel      The channel on the PCM to control (0..7).
  39.    */
  40.   public Solenoid(final int moduleNumber, final int channel) {
  41.     super(moduleNumber);
  42.     m_channel = channel;
  43.  
  44.     SensorBase.checkSolenoidModule(m_moduleNumber);
  45.     SensorBase.checkSolenoidChannel(m_channel);
  46.  
  47.     int portHandle = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_channel);
  48.     m_solenoidHandle = SolenoidJNI.initializeSolenoidPort(portHandle);
  49.  
  50.     HAL.report(tResourceType.kResourceType_Solenoid, m_channel, m_moduleNumber);
  51.     setName("Solenoid", m_moduleNumber, m_channel);
  52.   }
  53.  
  54.   /**
  55.    * Destructor.
  56.    */
  57.   @Override
  58.   public synchronized void free() {
  59.     super.free();
  60.     SolenoidJNI.freeSolenoidPort(m_solenoidHandle);
  61.     m_solenoidHandle = 0;
  62.   }
  63.  
  64.   /**
  65.    * Set the value of a solenoid.
  66.    *
  67.    * @param on True will turn the solenoid output on. False will turn the solenoid output off.
  68.    */
  69.   public void set(boolean on) {
  70.     SolenoidJNI.setSolenoid(m_solenoidHandle, on);
  71.   }
  72.  
  73.   /**
  74.    * Read the current value of the solenoid.
  75.    *
  76.    * @return True if the solenoid output is on or false if the solenoid output is off.
  77.    */
  78.   public boolean get() {
  79.     return SolenoidJNI.getSolenoid(m_solenoidHandle);
  80.   }
  81.  
  82.   /**
  83.    * Check if solenoid is blacklisted. If a solenoid is shorted, it is added to the blacklist and
  84.    * disabled until power cycle, or until faults are cleared.
  85.    *
  86.    * @return If solenoid is disabled due to short.
  87.    * @see #clearAllPCMStickyFaults()
  88.    */
  89.   public boolean isBlackListed() {
  90.     int value = getPCMSolenoidBlackList() & (1 << m_channel);
  91.     return value != 0;
  92.   }
  93.  
  94.   /**
  95.    * Set the pulse duration in the PCM. This is used in conjunction with
  96.    * the startPulse method to allow the PCM to control the timing of a pulse.
  97.    * The timing can be controlled in 0.01 second increments.
  98.    *
  99.    * @param durationSeconds The duration of the pulse, from 0.01 to 2.55 seconds.
  100.    *
  101.    * @see #startPulse()
  102.    */
  103.   public void setPulseDuration(double durationSeconds) {
  104.     long durationMS = (long) (durationSeconds * 1000);
  105.     SolenoidJNI.setOneShotDuration(m_solenoidHandle, durationMS);
  106.   }
  107.  
  108.   /**
  109.    * Trigger the PCM to generate a pulse of the duration set in
  110.    * setPulseDuration.
  111.    *
  112.    * @see #setPulseDuration(double)
  113.    */
  114.   public void startPulse() {
  115.     SolenoidJNI.fireOneShot(m_solenoidHandle);
  116.   }
  117.  
  118.   @Override
  119.   public void initSendable(SendableBuilder builder) {
  120.     builder.setSmartDashboardType("Solenoid");
  121.     builder.setSafeState(() -> set(false));
  122.     builder.addBooleanProperty("Value", this::get, this::set);
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement