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