mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[sim] Add HALSIM accessors for encoder rate and distance (#2467)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2017-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. */
|
||||
@@ -93,6 +93,11 @@ void HALSIM_RegisterEncoderAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
void HALSIM_SetEncoderDistance(int32_t index, double distance);
|
||||
double HALSIM_GetEncoderDistance(int32_t index);
|
||||
void HALSIM_SetEncoderRate(int32_t index, double rate);
|
||||
double HALSIM_GetEncoderRate(int32_t index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2018-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. */
|
||||
@@ -156,6 +156,16 @@ class EncoderSim {
|
||||
|
||||
void ResetData() { HALSIM_ResetEncoderData(m_index); }
|
||||
|
||||
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); }
|
||||
|
||||
private:
|
||||
int m_index;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2018-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. */
|
||||
@@ -413,6 +413,54 @@ Java_edu_wpi_first_hal_sim_mockdata_EncoderDataJNI_setSamplesToAverage
|
||||
HALSIM_SetEncoderSamplesToAverage(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_sim_mockdata_EncoderDataJNI
|
||||
* Method: setDistance
|
||||
* Signature: (ID)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_sim_mockdata_EncoderDataJNI_setDistance
|
||||
(JNIEnv*, jclass, jint index, jdouble value)
|
||||
{
|
||||
HALSIM_SetEncoderDistance(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_sim_mockdata_EncoderDataJNI
|
||||
* Method: getDistance
|
||||
* Signature: (I)D
|
||||
*/
|
||||
JNIEXPORT jdouble JNICALL
|
||||
Java_edu_wpi_first_hal_sim_mockdata_EncoderDataJNI_getDistance
|
||||
(JNIEnv*, jclass, jint index)
|
||||
{
|
||||
return HALSIM_GetEncoderDistance(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_sim_mockdata_EncoderDataJNI
|
||||
* Method: setRate
|
||||
* Signature: (ID)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_sim_mockdata_EncoderDataJNI_setRate
|
||||
(JNIEnv*, jclass, jint index, jdouble value)
|
||||
{
|
||||
HALSIM_SetEncoderRate(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_sim_mockdata_EncoderDataJNI
|
||||
* Method: getRate
|
||||
* Signature: (I)D
|
||||
*/
|
||||
JNIEXPORT jdouble JNICALL
|
||||
Java_edu_wpi_first_hal_sim_mockdata_EncoderDataJNI_getRate
|
||||
(JNIEnv*, jclass, jint index)
|
||||
{
|
||||
return HALSIM_GetEncoderRate(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_sim_mockdata_EncoderDataJNI
|
||||
* Method: resetData
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2017-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. */
|
||||
@@ -66,6 +66,32 @@ DEFINE_CAPI(HAL_Bool, ReverseDirection, reverseDirection)
|
||||
DEFINE_CAPI(int32_t, SamplesToAverage, samplesToAverage)
|
||||
DEFINE_CAPI(double, DistancePerPulse, distancePerPulse)
|
||||
|
||||
void HALSIM_SetEncoderDistance(int32_t index, double distance) {
|
||||
auto& simData = SimEncoderData[index];
|
||||
simData.count = distance / simData.distancePerPulse;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#define REGISTER(NAME) \
|
||||
SimEncoderData[index].NAME.RegisterCallback(callback, param, initialNotify)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user