Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "hal/AnalogGyro.h" |
| 6 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 7 | #include <cmath> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 8 | #include <string> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | #include <thread> |
| 10 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 11 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | |
| 13 | #include "AnalogInternal.h" |
| 14 | #include "HALInitializer.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | #include "HALInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | #include "hal/AnalogAccumulator.h" |
| 17 | #include "hal/AnalogInput.h" |
| 18 | #include "hal/handles/IndexedHandleResource.h" |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | struct AnalogGyro { |
| 23 | HAL_AnalogInputHandle handle; |
| 24 | double voltsPerDegreePerSecond; |
| 25 | double offset; |
| 26 | int32_t center; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | std::string previousAllocation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | static constexpr uint32_t kOversampleBits = 10; |
| 33 | static constexpr uint32_t kAverageBits = 0; |
| 34 | static constexpr double kSamplesPerSecond = 50.0; |
| 35 | static constexpr double kCalibrationSampleTime = 5.0; |
| 36 | static constexpr double kDefaultVoltsPerDegreePerSecond = 0.007; |
| 37 | |
| 38 | using namespace hal; |
| 39 | |
| 40 | static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators, |
| 41 | HAL_HandleEnum::AnalogGyro>* analogGyroHandles; |
| 42 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 43 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | void InitializeAnalogGyro() { |
| 45 | static IndexedHandleResource<HAL_GyroHandle, AnalogGyro, kNumAccumulators, |
| 46 | HAL_HandleEnum::AnalogGyro> |
| 47 | agHandles; |
| 48 | analogGyroHandles = &agHandles; |
| 49 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 50 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | |
| 52 | static void Wait(double seconds) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 53 | if (seconds < 0.0) { |
| 54 | return; |
| 55 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | std::this_thread::sleep_for(std::chrono::duration<double>(seconds)); |
| 57 | } |
| 58 | |
| 59 | extern "C" { |
| 60 | |
| 61 | HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 62 | const char* allocationLocation, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | int32_t* status) { |
| 64 | hal::init::CheckInit(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | // Handle will be type checked by HAL_IsAccumulatorChannel |
| 66 | int16_t channel = getHandleIndex(analogHandle); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | if (!HAL_IsAccumulatorChannel(analogHandle, status)) { |
| 68 | if (*status == 0) { |
| 69 | *status = HAL_INVALID_ACCUMULATOR_CHANNEL; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 70 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro", |
| 71 | 0, kNumAccumulators, channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 72 | } |
| 73 | return HAL_kInvalidHandle; |
| 74 | } |
| 75 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 76 | HAL_GyroHandle handle; |
| 77 | auto gyro = analogGyroHandles->Allocate(channel, &handle, status); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 79 | if (*status != 0) { |
| 80 | if (gyro) { |
| 81 | hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel, |
| 82 | gyro->previousAllocation); |
| 83 | } else { |
| 84 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro", |
| 85 | 0, kNumAccumulators, channel); |
| 86 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 88 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | |
| 90 | // Initialize port structure |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 91 | |
| 92 | gyro->handle = analogHandle; |
| 93 | gyro->voltsPerDegreePerSecond = 0; |
| 94 | gyro->offset = 0; |
| 95 | gyro->center = 0; |
| 96 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 97 | gyro->previousAllocation = allocationLocation ? allocationLocation : ""; |
| 98 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 99 | return handle; |
| 100 | } |
| 101 | |
| 102 | void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 103 | auto gyro = analogGyroHandles->Get(handle); |
| 104 | if (gyro == nullptr) { |
| 105 | *status = HAL_HANDLE_ERROR; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | gyro->voltsPerDegreePerSecond = kDefaultVoltsPerDegreePerSecond; |
| 110 | |
| 111 | HAL_SetAnalogAverageBits(gyro->handle, kAverageBits, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 112 | if (*status != 0) { |
| 113 | return; |
| 114 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 115 | HAL_SetAnalogOversampleBits(gyro->handle, kOversampleBits, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 116 | if (*status != 0) { |
| 117 | return; |
| 118 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | double sampleRate = |
| 120 | kSamplesPerSecond * (1 << (kAverageBits + kOversampleBits)); |
| 121 | HAL_SetAnalogSampleRate(sampleRate, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 122 | if (*status != 0) { |
| 123 | return; |
| 124 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | Wait(0.1); |
| 126 | |
| 127 | HAL_SetAnalogGyroDeadband(handle, 0.0, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 128 | if (*status != 0) { |
| 129 | return; |
| 130 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void HAL_FreeAnalogGyro(HAL_GyroHandle handle) { |
| 134 | analogGyroHandles->Free(handle); |
| 135 | } |
| 136 | |
| 137 | void HAL_SetAnalogGyroParameters(HAL_GyroHandle handle, |
| 138 | double voltsPerDegreePerSecond, double offset, |
| 139 | int32_t center, int32_t* status) { |
| 140 | auto gyro = analogGyroHandles->Get(handle); |
| 141 | if (gyro == nullptr) { |
| 142 | *status = HAL_HANDLE_ERROR; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | gyro->voltsPerDegreePerSecond = voltsPerDegreePerSecond; |
| 147 | gyro->offset = offset; |
| 148 | gyro->center = center; |
| 149 | HAL_SetAccumulatorCenter(gyro->handle, center, status); |
| 150 | } |
| 151 | |
| 152 | void HAL_SetAnalogGyroVoltsPerDegreePerSecond(HAL_GyroHandle handle, |
| 153 | double voltsPerDegreePerSecond, |
| 154 | int32_t* status) { |
| 155 | auto gyro = analogGyroHandles->Get(handle); |
| 156 | if (gyro == nullptr) { |
| 157 | *status = HAL_HANDLE_ERROR; |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | gyro->voltsPerDegreePerSecond = voltsPerDegreePerSecond; |
| 162 | } |
| 163 | |
| 164 | void HAL_ResetAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 165 | auto gyro = analogGyroHandles->Get(handle); |
| 166 | if (gyro == nullptr) { |
| 167 | *status = HAL_HANDLE_ERROR; |
| 168 | return; |
| 169 | } |
| 170 | HAL_ResetAccumulator(gyro->handle, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 171 | if (*status != 0) { |
| 172 | return; |
| 173 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 174 | |
| 175 | const double sampleTime = 1.0 / HAL_GetAnalogSampleRate(status); |
| 176 | const double overSamples = |
| 177 | 1 << HAL_GetAnalogOversampleBits(gyro->handle, status); |
| 178 | const double averageSamples = |
| 179 | 1 << HAL_GetAnalogAverageBits(gyro->handle, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 180 | if (*status != 0) { |
| 181 | return; |
| 182 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 183 | Wait(sampleTime * overSamples * averageSamples); |
| 184 | } |
| 185 | |
| 186 | void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) { |
| 187 | auto gyro = analogGyroHandles->Get(handle); |
| 188 | if (gyro == nullptr) { |
| 189 | *status = HAL_HANDLE_ERROR; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | HAL_InitAccumulator(gyro->handle, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 194 | if (*status != 0) { |
| 195 | return; |
| 196 | } |
| 197 | fmt::print("Calibrating analog gyro for {} seconds.\n", |
| 198 | kCalibrationSampleTime); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | Wait(kCalibrationSampleTime); |
| 200 | |
| 201 | int64_t value; |
| 202 | int64_t count; |
| 203 | HAL_GetAccumulatorOutput(gyro->handle, &value, &count, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 204 | if (*status != 0) { |
| 205 | return; |
| 206 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 207 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 208 | gyro->center = |
| 209 | std::round(static_cast<double>(value) / static_cast<double>(count)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 210 | |
| 211 | gyro->offset = static_cast<double>(value) / static_cast<double>(count) - |
| 212 | static_cast<double>(gyro->center); |
| 213 | HAL_SetAccumulatorCenter(gyro->handle, gyro->center, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 214 | if (*status != 0) { |
| 215 | return; |
| 216 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 217 | HAL_ResetAnalogGyro(handle, status); |
| 218 | } |
| 219 | |
| 220 | void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts, |
| 221 | int32_t* status) { |
| 222 | auto gyro = analogGyroHandles->Get(handle); |
| 223 | if (gyro == nullptr) { |
| 224 | *status = HAL_HANDLE_ERROR; |
| 225 | return; |
| 226 | } |
| 227 | int32_t deadband = static_cast<int32_t>( |
| 228 | volts * 1e9 / HAL_GetAnalogLSBWeight(gyro->handle, status) * |
| 229 | (1 << HAL_GetAnalogOversampleBits(gyro->handle, status))); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 230 | if (*status != 0) { |
| 231 | return; |
| 232 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 233 | HAL_SetAccumulatorDeadband(gyro->handle, deadband, status); |
| 234 | } |
| 235 | |
| 236 | double HAL_GetAnalogGyroAngle(HAL_GyroHandle handle, int32_t* status) { |
| 237 | auto gyro = analogGyroHandles->Get(handle); |
| 238 | if (gyro == nullptr) { |
| 239 | *status = HAL_HANDLE_ERROR; |
| 240 | return 0; |
| 241 | } |
| 242 | int64_t rawValue = 0; |
| 243 | int64_t count = 0; |
| 244 | HAL_GetAccumulatorOutput(gyro->handle, &rawValue, &count, status); |
| 245 | |
| 246 | int64_t value = rawValue - static_cast<int64_t>(static_cast<double>(count) * |
| 247 | gyro->offset); |
| 248 | |
| 249 | double scaledValue = |
| 250 | value * 1e-9 * |
| 251 | static_cast<double>(HAL_GetAnalogLSBWeight(gyro->handle, status)) * |
| 252 | static_cast<double>(1 << HAL_GetAnalogAverageBits(gyro->handle, status)) / |
| 253 | (HAL_GetAnalogSampleRate(status) * gyro->voltsPerDegreePerSecond); |
| 254 | |
| 255 | return scaledValue; |
| 256 | } |
| 257 | |
| 258 | double HAL_GetAnalogGyroRate(HAL_GyroHandle handle, int32_t* status) { |
| 259 | auto gyro = analogGyroHandles->Get(handle); |
| 260 | if (gyro == nullptr) { |
| 261 | *status = HAL_HANDLE_ERROR; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | return (HAL_GetAnalogAverageValue(gyro->handle, status) - |
| 266 | (static_cast<double>(gyro->center) + gyro->offset)) * |
| 267 | 1e-9 * HAL_GetAnalogLSBWeight(gyro->handle, status) / |
| 268 | ((1 << HAL_GetAnalogOversampleBits(gyro->handle, status)) * |
| 269 | gyro->voltsPerDegreePerSecond); |
| 270 | } |
| 271 | |
| 272 | double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) { |
| 273 | auto gyro = analogGyroHandles->Get(handle); |
| 274 | if (gyro == nullptr) { |
| 275 | *status = HAL_HANDLE_ERROR; |
| 276 | return 0; |
| 277 | } |
| 278 | return gyro->offset; |
| 279 | } |
| 280 | |
| 281 | int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) { |
| 282 | auto gyro = analogGyroHandles->Get(handle); |
| 283 | if (gyro == nullptr) { |
| 284 | *status = HAL_HANDLE_ERROR; |
| 285 | return 0; |
| 286 | } |
| 287 | return gyro->center; |
| 288 | } |
| 289 | |
| 290 | } // extern "C" |