2018-05-11 12:38:23 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-04-03 08:33:38 -07:00
|
|
|
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
|
2018-05-11 12:38:23 -07:00
|
|
|
/* 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
|
|
|
|
|
|
2020-07-04 10:10:43 -07:00
|
|
|
#include <exception>
|
2018-05-11 12:38:23 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/EncoderData.h>
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
#include "CallbackStore.h"
|
2020-07-04 10:10:43 -07:00
|
|
|
#include "frc/Encoder.h"
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
namespace sim {
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to control a simulated encoder.
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
class EncoderSim {
|
|
|
|
|
public:
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs from an Encoder object.
|
|
|
|
|
*
|
|
|
|
|
* @param encoder Encoder to simulate
|
|
|
|
|
*/
|
|
|
|
|
explicit EncoderSim(const Encoder& encoder)
|
|
|
|
|
: m_index{encoder.GetFPGAIndex()} {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an EncoderSim for a digital input channel. Encoders take two
|
|
|
|
|
* channels, so either one may be specified.
|
|
|
|
|
*
|
|
|
|
|
* @param channel digital input channel
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
* @throws NoSuchElementException if no Encoder is configured for that channel
|
|
|
|
|
*/
|
|
|
|
|
static EncoderSim CreateForChannel(int channel) {
|
|
|
|
|
int index = HALSIM_FindEncoderForChannel(channel);
|
|
|
|
|
if (index < 0) throw std::out_of_range("no encoder found for channel");
|
|
|
|
|
return EncoderSim{index};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an EncoderSim for a simulated index.
|
|
|
|
|
* The index is incremented for each simulated Encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param index simulator index
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
*/
|
|
|
|
|
static EncoderSim CreateForIndex(int index) { return EncoderSim{index}; }
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterInitializedCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderInitializedCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderInitializedCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
bool GetInitialized() const { return HALSIM_GetEncoderInitialized(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetInitialized(bool initialized) {
|
|
|
|
|
HALSIM_SetEncoderInitialized(m_index, initialized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterCountCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderCountCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderCountCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
int GetCount() const { return HALSIM_GetEncoderCount(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetCount(int count) { HALSIM_SetEncoderCount(m_index, count); }
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterPeriodCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderPeriodCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderPeriodCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
double GetPeriod() const { return HALSIM_GetEncoderPeriod(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetPeriod(double period) { HALSIM_SetEncoderPeriod(m_index, period); }
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterResetCallback(NotifyCallback callback,
|
|
|
|
|
bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderResetCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderResetCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
bool GetReset() const { return HALSIM_GetEncoderReset(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetReset(bool reset) { HALSIM_SetEncoderReset(m_index, reset); }
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterMaxPeriodCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderMaxPeriodCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderMaxPeriodCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
double GetMaxPeriod() const { return HALSIM_GetEncoderMaxPeriod(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetMaxPeriod(double maxPeriod) {
|
|
|
|
|
HALSIM_SetEncoderMaxPeriod(m_index, maxPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterDirectionCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderDirectionCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderDirectionCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
bool GetDirection() const { return HALSIM_GetEncoderDirection(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetDirection(bool direction) {
|
|
|
|
|
HALSIM_SetEncoderDirection(m_index, direction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterReverseDirectionCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderReverseDirectionCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderReverseDirectionCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
bool GetReverseDirection() const {
|
2018-05-11 12:38:23 -07:00
|
|
|
return HALSIM_GetEncoderReverseDirection(m_index);
|
|
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetReverseDirection(bool reverseDirection) {
|
|
|
|
|
HALSIM_SetEncoderReverseDirection(m_index, reverseDirection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RegisterSamplesToAverageCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderSamplesToAverageCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderSamplesToAverageCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
2018-05-16 19:45:46 -07:00
|
|
|
return store;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
int GetSamplesToAverage() const {
|
2018-05-11 12:38:23 -07:00
|
|
|
return HALSIM_GetEncoderSamplesToAverage(m_index);
|
|
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void SetSamplesToAverage(int samplesToAverage) {
|
|
|
|
|
HALSIM_SetEncoderSamplesToAverage(m_index, samplesToAverage);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 23:11:26 -04:00
|
|
|
std::unique_ptr<CallbackStore> RegisterDistancePerPulseCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
m_index, -1, callback, &HALSIM_CancelEncoderDistancePerPulseCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterEncoderDistancePerPulseCallback(
|
|
|
|
|
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
|
|
|
|
double GetDistancePerPulse() const {
|
2018-07-12 23:11:26 -04:00
|
|
|
return HALSIM_GetEncoderDistancePerPulse(m_index);
|
|
|
|
|
}
|
2018-08-16 01:17:59 -04:00
|
|
|
|
2018-07-12 23:11:26 -04:00
|
|
|
void SetDistancePerPulse(double distancePerPulse) {
|
|
|
|
|
HALSIM_SetEncoderDistancePerPulse(m_index, distancePerPulse);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
void ResetData() { HALSIM_ResetEncoderData(m_index); }
|
|
|
|
|
|
2020-04-03 08:33:38 -07:00
|
|
|
void SetDistance(double distance) {
|
|
|
|
|
HALSIM_SetEncoderDistance(m_index, distance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double GetDistance() { return HALSIM_GetEncoderDistance(m_index); }
|
|
|
|
|
|
|
|
|
|
void SetRate(double rate) { HALSIM_SetEncoderRate(m_index, rate); }
|
|
|
|
|
|
|
|
|
|
double GetRate() { return HALSIM_GetEncoderRate(m_index); }
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
private:
|
2020-07-04 10:10:43 -07:00
|
|
|
explicit EncoderSim(int index) : m_index{index} {}
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
int m_index;
|
|
|
|
|
};
|
|
|
|
|
} // namespace sim
|
|
|
|
|
} // namespace frc
|