2017-08-18 21:35:53 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2016-2018 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/AnalogGyro.h"
|
2017-08-18 21:35:53 -07:00
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#include "AnalogInternal.h"
|
2018-05-13 22:02:47 -07:00
|
|
|
#include "HALInitializer.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/AnalogAccumulator.h"
|
|
|
|
|
#include "hal/AnalogInput.h"
|
|
|
|
|
#include "hal/handles/IndexedHandleResource.h"
|
|
|
|
|
#include "mockdata/AnalogGyroDataInternal.h"
|
2017-08-18 21:35:53 -07:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
struct AnalogGyro {
|
|
|
|
|
HAL_AnalogInputHandle handle;
|
|
|
|
|
uint8_t index;
|
|
|
|
|
};
|
2017-10-16 19:56:08 -07:00
|
|
|
} // namespace
|
2017-08-18 21:35:53 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
|
2017-12-10 19:38:53 -08:00
|
|
|
HAL_HandleEnum::AnalogGyro>* analogGyroHandles;
|
|
|
|
|
|
|
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeAnalogGyro() {
|
|
|
|
|
static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators,
|
|
|
|
|
HAL_HandleEnum::AnalogGyro>
|
|
|
|
|
agH;
|
|
|
|
|
analogGyroHandles = &agH;
|
|
|
|
|
}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
2017-08-18 21:35:53 -07:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
|
|
|
|
|
int32_t* status) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
2017-08-18 21:35:53 -07:00
|
|
|
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
|
|
|
|
|
if (*status == 0) {
|
|
|
|
|
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
|
|
|
|
|
}
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handle known to be correct, so no need to type check
|
|
|
|
|
int16_t channel = getHandleIndex(analogHandle);
|
|
|
|
|
|
2017-12-10 19:38:53 -08:00
|
|
|
auto handle = analogGyroHandles->Allocate(channel, status);
|
2017-08-18 21:35:53 -07:00
|
|
|
|
|
|
|
|
if (*status != 0)
|
|
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
|
|
|
|
|
|
|
|
|
// Initialize port structure
|
2017-12-10 19:38:53 -08:00
|
|
|
auto gyro = analogGyroHandles->Get(handle);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (gyro == nullptr) { // would only error on thread issue
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gyro->handle = analogHandle;
|
|
|
|
|
gyro->index = channel;
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
SimAnalogGyroData[channel].initialized = true;
|
2017-08-18 21:35:53 -07:00
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
|
|
|
|
|
// No op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_FreeAnalogGyro(HAL_GyroHandle handle) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto gyro = analogGyroHandles->Get(handle);
|
|
|
|
|
analogGyroHandles->Free(handle);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (gyro == nullptr) return;
|
2018-09-03 16:08:07 -07:00
|
|
|
SimAnalogGyroData[gyro->index].initialized = false;
|
2017-08-18 21:35:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle,
|
|
|
|
|
double voltsPerDegreePerSecond, double offset,
|
|
|
|
|
int32_t center, int32_t* status) {
|
|
|
|
|
// No op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle,
|
|
|
|
|
double voltsPerDegreePerSecond,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
// No op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ResetAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto gyro = analogGyroHandles->Get(handle);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (gyro == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
SimAnalogGyroData[gyro->index].angle = 0.0;
|
2017-08-18 21:35:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
|
|
|
|
|
// Just do a reset
|
|
|
|
|
HAL_ResetAnalogGyro(handle, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
// No op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetAnalogGyroAngle(HAL_GyroHandle handle, int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto gyro = analogGyroHandles->Get(handle);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (gyro == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
return SimAnalogGyroData[gyro->index].angle;
|
2017-08-18 21:35:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetAnalogGyroRate(HAL_GyroHandle handle, int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto gyro = analogGyroHandles->Get(handle);
|
2017-08-18 21:35:53 -07:00
|
|
|
if (gyro == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
return SimAnalogGyroData[gyro->index].rate;
|
2017-08-18 21:35:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-10-16 19:56:08 -07:00
|
|
|
} // extern "C"
|