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.hal.SimBoolean; 008import edu.wpi.first.hal.SimDouble; 009import edu.wpi.first.wpilibj.DutyCycleEncoder; 010 011/** Class to control a simulated duty cycle encoder. */ 012public class DutyCycleEncoderSim { 013 private final SimDouble m_simPosition; 014 private final SimBoolean m_simIsConnected; 015 016 /** 017 * Constructs from an DutyCycleEncoder object. 018 * 019 * @param encoder DutyCycleEncoder to simulate 020 */ 021 public DutyCycleEncoderSim(DutyCycleEncoder encoder) { 022 this(encoder.getSourceChannel()); 023 } 024 025 /** 026 * Constructs from a digital input channel. 027 * 028 * @param channel digital input channel. 029 */ 030 public DutyCycleEncoderSim(int channel) { 031 SimDeviceSim wrappedSimDevice = new SimDeviceSim("DutyCycle:DutyCycleEncoder", channel); 032 m_simPosition = wrappedSimDevice.getDouble("Position"); 033 m_simIsConnected = wrappedSimDevice.getBoolean("Connected"); 034 } 035 036 /** 037 * Get the position in turns. 038 * 039 * @return The position. 040 */ 041 public double get() { 042 return m_simPosition.get(); 043 } 044 045 /** 046 * Set the position. 047 * 048 * @param value The position. 049 */ 050 public void set(double value) { 051 m_simPosition.set(value); 052 } 053 054 /** 055 * Get if the encoder is connected. 056 * 057 * @return true if the encoder is connected. 058 */ 059 public boolean getConnected() { 060 return m_simIsConnected.get(); 061 } 062 063 /** 064 * Set if the encoder is connected. 065 * 066 * @param isConnected Whether or not the sensor is connected. 067 */ 068 public void setConnected(boolean isConnected) { 069 m_simIsConnected.set(isConnected); 070 } 071}