mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpilib] Add DutyCycleEncoderSim (#2798)
This commit is contained in:
@@ -58,6 +58,8 @@ void DutyCycleEncoder::Init() {
|
||||
|
||||
if (m_simDevice) {
|
||||
m_simPosition = m_simDevice.CreateDouble("Position", false, 0.0);
|
||||
m_simDistancePerRotation =
|
||||
m_simDevice.CreateDouble("DistancePerRotation", false, 1.0);
|
||||
m_simIsConnected = m_simDevice.CreateBoolean("Connected", false, true);
|
||||
} else {
|
||||
m_analogTrigger = std::make_unique<AnalogTrigger>(m_dutyCycle.get());
|
||||
@@ -98,6 +100,7 @@ units::turn_t DutyCycleEncoder::Get() const {
|
||||
|
||||
void DutyCycleEncoder::SetDistancePerRotation(double distancePerRotation) {
|
||||
m_distancePerRotation = distancePerRotation;
|
||||
m_simDistancePerRotation.Set(distancePerRotation);
|
||||
}
|
||||
|
||||
double DutyCycleEncoder::GetDistancePerRotation() const {
|
||||
@@ -129,6 +132,10 @@ void DutyCycleEncoder::SetConnectedFrequencyThreshold(int frequency) {
|
||||
m_frequencyThreshold = frequency;
|
||||
}
|
||||
|
||||
int DutyCycleEncoder::GetSourceChannel() const {
|
||||
return m_dutyCycle->GetSourceChannel();
|
||||
}
|
||||
|
||||
void DutyCycleEncoder::InitSendable(SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("AbsoluteEncoder");
|
||||
builder.AddDoubleProperty(
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "frc/simulation/DutyCycleEncoderSim.h"
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "frc/DutyCycleEncoder.h"
|
||||
#include "frc/simulation/SimDeviceSim.h"
|
||||
|
||||
using namespace frc::sim;
|
||||
|
||||
DutyCycleEncoderSim::DutyCycleEncoderSim(const frc::DutyCycleEncoder& encoder) {
|
||||
wpi::SmallString<128> fullname;
|
||||
wpi::raw_svector_ostream os(fullname);
|
||||
os << "DutyCycleEncoder" << '[' << encoder.GetSourceChannel() << ']';
|
||||
frc::sim::SimDeviceSim deviceSim{fullname.c_str()};
|
||||
m_simPosition = deviceSim.GetDouble("Position");
|
||||
m_simDistancePerRotation = deviceSim.GetDouble("DistancePerRotation");
|
||||
}
|
||||
|
||||
void DutyCycleEncoderSim::Set(units::turn_t turns) {
|
||||
m_simPosition.Set(turns.to<double>());
|
||||
}
|
||||
|
||||
void DutyCycleEncoderSim::SetDistance(double distance) {
|
||||
m_simPosition.Set(distance / m_simDistancePerRotation.Get());
|
||||
}
|
||||
@@ -154,6 +154,13 @@ class DutyCycleEncoder : public ErrorBase,
|
||||
*/
|
||||
double GetDistance() const;
|
||||
|
||||
/**
|
||||
* Get the channel of the source.
|
||||
*
|
||||
* @return the source channel
|
||||
*/
|
||||
int GetSourceChannel() const;
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
@@ -169,6 +176,7 @@ class DutyCycleEncoder : public ErrorBase,
|
||||
|
||||
hal::SimDevice m_simDevice;
|
||||
hal::SimDouble m_simPosition;
|
||||
hal::SimDouble m_simDistancePerRotation;
|
||||
hal::SimBoolean m_simIsConnected;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/angle.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
class DutyCycleEncoder;
|
||||
|
||||
namespace sim {
|
||||
|
||||
/**
|
||||
* Class to control a simulated duty cycle encoder.
|
||||
*/
|
||||
class DutyCycleEncoderSim {
|
||||
public:
|
||||
/**
|
||||
* Constructs from a DutyCycleEncoder object.
|
||||
*
|
||||
* @param dutyCycleEncoder DutyCycleEncoder to simulate
|
||||
*/
|
||||
explicit DutyCycleEncoderSim(const DutyCycleEncoder& encoder);
|
||||
|
||||
/**
|
||||
* Set the position tin turns.
|
||||
*
|
||||
* @param turns The position.
|
||||
*/
|
||||
void Set(units::turn_t turns);
|
||||
|
||||
/**
|
||||
* Set the position.
|
||||
*/
|
||||
void SetDistance(double distance);
|
||||
|
||||
private:
|
||||
hal::SimDouble m_simPosition;
|
||||
hal::SimDouble m_simDistancePerRotation;
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
@@ -32,6 +32,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
protected SimDevice m_simDevice;
|
||||
protected SimDouble m_simPosition;
|
||||
protected SimDouble m_simDistancePerRotation;
|
||||
protected SimBoolean m_simIsConnected;
|
||||
|
||||
/**
|
||||
@@ -72,6 +73,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
if (m_simDevice != null) {
|
||||
m_simPosition = m_simDevice.createDouble("Position", false, 0.0);
|
||||
m_simDistancePerRotation = m_simDevice.createDouble("DistancePerRotation", false, 1.0);
|
||||
m_simIsConnected = m_simDevice.createBoolean("Connected", false, true);
|
||||
} else {
|
||||
m_counter = new Counter();
|
||||
@@ -140,6 +142,10 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
*/
|
||||
public void setDistancePerRotation(double distancePerRotation) {
|
||||
m_distancePerRotation = distancePerRotation;
|
||||
|
||||
if (m_simDistancePerRotation != null) {
|
||||
m_simDistancePerRotation.set(distancePerRotation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,6 +236,15 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel of the source.
|
||||
*
|
||||
* @return the source channel
|
||||
*/
|
||||
public int getSourceChannel() {
|
||||
return m_dutyCycle.getSourceChannel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("AbsoluteEncoder");
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import edu.wpi.first.hal.SimDouble;
|
||||
import edu.wpi.first.wpilibj.DutyCycleEncoder;
|
||||
|
||||
/**
|
||||
* Class to control a simulated duty cycle encoder.
|
||||
*/
|
||||
public class DutyCycleEncoderSim {
|
||||
private final SimDouble m_simPosition;
|
||||
private final SimDouble m_simDistancePerRotation;
|
||||
|
||||
/**
|
||||
* Constructs from an DutyCycleEncoder object.
|
||||
*
|
||||
* @param encoder DutyCycleEncoder to simulate
|
||||
*/
|
||||
public DutyCycleEncoderSim(DutyCycleEncoder encoder) {
|
||||
SimDeviceSim wrappedSimDevice = new SimDeviceSim("DutyCycleEncoder" + "[" + encoder.getSourceChannel() + "]");
|
||||
m_simPosition = wrappedSimDevice.getDouble("Position");
|
||||
m_simDistancePerRotation = wrappedSimDevice.getDouble("DistancePerRotation");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position in turns.
|
||||
*
|
||||
* @param turns The position.
|
||||
*/
|
||||
public void set(double turns) {
|
||||
m_simPosition.set(turns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position.
|
||||
*/
|
||||
public void setDistance(double distance) {
|
||||
m_simPosition.set(distance / m_simDistancePerRotation.get());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user