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.simulation; 006 007import edu.wpi.first.wpilibj.DoubleSolenoid; 008import edu.wpi.first.wpilibj.PneumaticsBase; 009import edu.wpi.first.wpilibj.PneumaticsModuleType; 010 011/** Class to control a simulated {@link edu.wpi.first.wpilibj.DoubleSolenoid}. */ 012public class DoubleSolenoidSim { 013 private final PneumaticsBaseSim m_module; 014 private final int m_fwd; 015 private final int m_rev; 016 017 /** 018 * Constructs for a solenoid on the given pneumatics module. 019 * 020 * @param moduleSim the PCM the solenoid is connected to. 021 * @param fwd the forward solenoid channel. 022 * @param rev the reverse solenoid channel. 023 */ 024 public DoubleSolenoidSim(PneumaticsBaseSim moduleSim, int fwd, int rev) { 025 m_module = moduleSim; 026 m_fwd = fwd; 027 m_rev = rev; 028 } 029 030 /** 031 * Constructs for a solenoid on a pneumatics module of the given type and ID. 032 * 033 * @param module the CAN ID of the pneumatics module the solenoid is connected to. 034 * @param moduleType the module type (PH or PCM) 035 * @param fwd the forward solenoid channel. 036 * @param rev the reverse solenoid channel. 037 */ 038 public DoubleSolenoidSim(int module, PneumaticsModuleType moduleType, int fwd, int rev) { 039 this(PneumaticsBaseSim.getForType(module, moduleType), fwd, rev); 040 } 041 042 /** 043 * Constructs for a solenoid on a pneumatics module of the given type and default ID. 044 * 045 * @param moduleType the module type (PH or PCM) 046 * @param fwd the forward solenoid channel. 047 * @param rev the reverse solenoid channel. 048 */ 049 public DoubleSolenoidSim(PneumaticsModuleType moduleType, int fwd, int rev) { 050 this(PneumaticsBase.getDefaultForType(moduleType), moduleType, fwd, rev); 051 } 052 053 /** 054 * Check the value of the double solenoid output. 055 * 056 * @return the output value of the double solenoid. 057 */ 058 public DoubleSolenoid.Value get() { 059 boolean fwdState = m_module.getSolenoidOutput(m_fwd); 060 boolean revState = m_module.getSolenoidOutput(m_rev); 061 if (fwdState && !revState) { 062 return DoubleSolenoid.Value.kForward; 063 } else if (!fwdState && revState) { 064 return DoubleSolenoid.Value.kReverse; 065 } else { 066 return DoubleSolenoid.Value.kOff; 067 } 068 } 069 070 /** 071 * Set the value of the double solenoid output. 072 * 073 * @param value The value to set (Off, Forward, Reverse) 074 */ 075 public void set(final DoubleSolenoid.Value value) { 076 m_module.setSolenoidOutput(m_fwd, value == DoubleSolenoid.Value.kForward); 077 m_module.setSolenoidOutput(m_rev, value == DoubleSolenoid.Value.kReverse); 078 } 079 080 /** 081 * Get the wrapped {@link PneumaticsBaseSim} object. 082 * 083 * @return the wrapped {@link PneumaticsBaseSim} object. 084 */ 085 public PneumaticsBaseSim getModuleSim() { 086 return m_module; 087 } 088}