2017-08-18 21:35:53 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-04-03 08:33:38 -07:00
|
|
|
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
|
2017-08-18 21:35:53 -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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "../PortsInternal.h"
|
|
|
|
|
#include "EncoderDataInternal.h"
|
|
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
2017-12-10 19:38:53 -08:00
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeEncoderData() {
|
|
|
|
|
static EncoderData sed[kNumEncoders];
|
|
|
|
|
::hal::SimEncoderData = sed;
|
|
|
|
|
}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
|
|
|
|
EncoderData* hal::SimEncoderData;
|
2017-08-18 21:35:53 -07:00
|
|
|
void EncoderData::ResetData() {
|
2018-09-03 16:08:07 -07:00
|
|
|
digitalChannelA = 0;
|
2019-10-04 17:29:34 -07:00
|
|
|
digitalChannelB = 0;
|
2018-09-03 16:08:07 -07:00
|
|
|
initialized.Reset(false);
|
2019-10-04 22:56:24 -07:00
|
|
|
simDevice = 0;
|
2018-09-03 16:08:07 -07:00
|
|
|
count.Reset(0);
|
|
|
|
|
period.Reset(std::numeric_limits<double>::max());
|
|
|
|
|
reset.Reset(false);
|
|
|
|
|
maxPeriod.Reset(0);
|
|
|
|
|
direction.Reset(false);
|
|
|
|
|
reverseDirection.Reset(false);
|
|
|
|
|
samplesToAverage.Reset(0);
|
|
|
|
|
distancePerPulse.Reset(1);
|
2018-01-02 17:53:39 -05:00
|
|
|
}
|
|
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
extern "C" {
|
2020-07-04 10:10:43 -07:00
|
|
|
int32_t HALSIM_FindEncoderForChannel(int32_t channel) {
|
|
|
|
|
for (int i = 0; i < kNumEncoders; ++i) {
|
|
|
|
|
if (!SimEncoderData[i].initialized) continue;
|
|
|
|
|
if (SimEncoderData[i].digitalChannelA == channel ||
|
|
|
|
|
SimEncoderData[i].digitalChannelB == channel)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
void HALSIM_ResetEncoderData(int32_t index) {
|
|
|
|
|
SimEncoderData[index].ResetData();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 17:29:34 -07:00
|
|
|
int32_t HALSIM_GetEncoderDigitalChannelA(int32_t index) {
|
2018-09-03 16:08:07 -07:00
|
|
|
return SimEncoderData[index].digitalChannelA;
|
2017-08-18 21:35:53 -07:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 17:29:34 -07:00
|
|
|
int32_t HALSIM_GetEncoderDigitalChannelB(int32_t index) {
|
|
|
|
|
return SimEncoderData[index].digitalChannelB;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 22:56:24 -07:00
|
|
|
HAL_SimDeviceHandle HALSIM_GetEncoderSimDevice(int32_t index) {
|
|
|
|
|
return SimEncoderData[index].simDevice;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
|
|
|
|
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, Encoder##CAPINAME, \
|
|
|
|
|
SimEncoderData, LOWERNAME)
|
2017-08-18 21:35:53 -07:00
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
|
|
|
|
|
DEFINE_CAPI(double, Period, period)
|
|
|
|
|
DEFINE_CAPI(HAL_Bool, Reset, reset)
|
|
|
|
|
DEFINE_CAPI(double, MaxPeriod, maxPeriod)
|
|
|
|
|
DEFINE_CAPI(HAL_Bool, Direction, direction)
|
|
|
|
|
DEFINE_CAPI(HAL_Bool, ReverseDirection, reverseDirection)
|
|
|
|
|
DEFINE_CAPI(int32_t, SamplesToAverage, samplesToAverage)
|
|
|
|
|
DEFINE_CAPI(double, DistancePerPulse, distancePerPulse)
|
2017-08-18 21:35:53 -07:00
|
|
|
|
2020-09-04 20:51:08 -07:00
|
|
|
int32_t HALSIM_RegisterEncoderCountCallback(int32_t index,
|
|
|
|
|
HAL_NotifyCallback callback,
|
|
|
|
|
void* param,
|
|
|
|
|
HAL_Bool initialNotify) {
|
|
|
|
|
return SimEncoderData[index].count.RegisterCallback(callback, param,
|
|
|
|
|
initialNotify);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSIM_CancelEncoderCountCallback(int32_t index, int32_t uid) {
|
|
|
|
|
SimEncoderData[index].count.CancelCallback(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HALSIM_GetEncoderCount(int32_t index) {
|
|
|
|
|
return SimEncoderData[index].count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSIM_SetEncoderCount(int32_t index, int32_t count) {
|
|
|
|
|
SimEncoderData[index].count = count;
|
|
|
|
|
SimEncoderData[index].reset = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 08:33:38 -07:00
|
|
|
void HALSIM_SetEncoderDistance(int32_t index, double distance) {
|
|
|
|
|
auto& simData = SimEncoderData[index];
|
|
|
|
|
simData.count = distance / simData.distancePerPulse;
|
2020-09-04 20:51:08 -07:00
|
|
|
simData.reset = false;
|
2020-04-03 08:33:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HALSIM_GetEncoderDistance(int32_t index) {
|
|
|
|
|
auto& simData = SimEncoderData[index];
|
|
|
|
|
return simData.count * simData.distancePerPulse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSIM_SetEncoderRate(int32_t index, double rate) {
|
|
|
|
|
auto& simData = SimEncoderData[index];
|
|
|
|
|
if (rate == 0) {
|
|
|
|
|
simData.period = std::numeric_limits<double>::infinity();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simData.period = simData.distancePerPulse / rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HALSIM_GetEncoderRate(int32_t index) {
|
|
|
|
|
auto& simData = SimEncoderData[index];
|
|
|
|
|
|
|
|
|
|
return simData.distancePerPulse / simData.period;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
#define REGISTER(NAME) \
|
|
|
|
|
SimEncoderData[index].NAME.RegisterCallback(callback, param, initialNotify)
|
2018-07-12 23:11:26 -04:00
|
|
|
|
2017-10-22 01:45:41 -04:00
|
|
|
void HALSIM_RegisterEncoderAllCallbacks(int32_t index,
|
|
|
|
|
HAL_NotifyCallback callback,
|
|
|
|
|
void* param, HAL_Bool initialNotify) {
|
2018-09-03 16:08:07 -07:00
|
|
|
REGISTER(initialized);
|
|
|
|
|
REGISTER(count);
|
|
|
|
|
REGISTER(period);
|
|
|
|
|
REGISTER(reset);
|
|
|
|
|
REGISTER(maxPeriod);
|
|
|
|
|
REGISTER(direction);
|
|
|
|
|
REGISTER(reverseDirection);
|
|
|
|
|
REGISTER(samplesToAverage);
|
|
|
|
|
REGISTER(distancePerPulse);
|
2017-10-22 01:45:41 -04:00
|
|
|
}
|
2017-10-16 19:56:08 -07:00
|
|
|
} // extern "C"
|