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. |
| 4 | |
| 5 | #include "hal/REVPH.h" |
| 6 | |
| 7 | #include "HALInitializer.h" |
| 8 | #include "HALInternal.h" |
| 9 | #include "PortsInternal.h" |
| 10 | #include "hal/CANAPI.h" |
| 11 | #include "hal/Errors.h" |
| 12 | #include "hal/handles/IndexedHandleResource.h" |
| 13 | #include "mockdata/REVPHDataInternal.h" |
| 14 | |
| 15 | using namespace hal; |
| 16 | |
| 17 | namespace { |
| 18 | struct PCM { |
| 19 | int32_t module; |
| 20 | wpi::mutex lock; |
| 21 | std::string previousAllocation; |
| 22 | }; |
| 23 | } // namespace |
| 24 | |
| 25 | static IndexedHandleResource<HAL_REVPHHandle, PCM, kNumREVPHModules, |
| 26 | HAL_HandleEnum::REVPH>* pcmHandles; |
| 27 | |
| 28 | namespace hal::init { |
| 29 | void InitializeREVPH() { |
| 30 | static IndexedHandleResource<HAL_REVPHHandle, PCM, kNumREVPHModules, |
| 31 | HAL_HandleEnum::REVPH> |
| 32 | pH; |
| 33 | pcmHandles = &pH; |
| 34 | } |
| 35 | } // namespace hal::init |
| 36 | |
| 37 | HAL_REVPHHandle HAL_InitializeREVPH(int32_t module, |
| 38 | const char* allocationLocation, |
| 39 | int32_t* status) { |
| 40 | hal::init::CheckInit(); |
| 41 | |
| 42 | if (module == 0) { |
| 43 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1, |
| 44 | kNumREVPHModules, module); |
| 45 | return HAL_kInvalidHandle; |
| 46 | } |
| 47 | |
| 48 | HAL_REVPHHandle handle; |
| 49 | auto pcm = pcmHandles->Allocate(module, &handle, status); |
| 50 | |
| 51 | if (*status != 0) { |
| 52 | if (pcm) { |
| 53 | hal::SetLastErrorPreviouslyAllocated(status, "REV PH", module, |
| 54 | pcm->previousAllocation); |
| 55 | } else { |
| 56 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1, |
| 57 | kNumREVPHModules, module); |
| 58 | } |
| 59 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
| 60 | } |
| 61 | |
| 62 | pcm->previousAllocation = allocationLocation ? allocationLocation : ""; |
| 63 | pcm->module = module; |
| 64 | |
| 65 | SimREVPHData[module].initialized = true; |
| 66 | // Enable closed loop |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 67 | SimREVPHData[module].compressorConfigType = |
| 68 | HAL_REVPHCompressorConfigType_kDigital; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 69 | |
| 70 | return handle; |
| 71 | } |
| 72 | |
| 73 | void HAL_FreeREVPH(HAL_REVPHHandle handle) { |
| 74 | auto pcm = pcmHandles->Get(handle); |
| 75 | if (pcm == nullptr) { |
| 76 | pcmHandles->Free(handle); |
| 77 | return; |
| 78 | } |
| 79 | SimREVPHData[pcm->module].initialized = false; |
| 80 | pcmHandles->Free(handle); |
| 81 | } |
| 82 | |
| 83 | HAL_Bool HAL_CheckREVPHModuleNumber(int32_t module) { |
| 84 | return module >= 1 && module < kNumREVPDHModules; |
| 85 | } |
| 86 | |
| 87 | HAL_Bool HAL_CheckREVPHSolenoidChannel(int32_t channel) { |
| 88 | return channel < kNumREVPHChannels && channel >= 0; |
| 89 | } |
| 90 | |
| 91 | HAL_Bool HAL_GetREVPHCompressor(HAL_REVPHHandle handle, int32_t* status) { |
| 92 | auto pcm = pcmHandles->Get(handle); |
| 93 | if (pcm == nullptr) { |
| 94 | *status = HAL_HANDLE_ERROR; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | return SimREVPHData[pcm->module].compressorOn; |
| 99 | } |
| 100 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 101 | void HAL_SetREVPHCompressorConfig(HAL_REVPHHandle handle, |
| 102 | const HAL_REVPHCompressorConfig* config, |
| 103 | int32_t* status) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 104 | auto pcm = pcmHandles->Get(handle); |
| 105 | if (pcm == nullptr) { |
| 106 | *status = HAL_HANDLE_ERROR; |
| 107 | return; |
| 108 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 109 | // TODO |
| 110 | // SimREVPHData[pcm->module].compressorConfigType = config. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 111 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 112 | void HAL_SetREVPHClosedLoopControlDisabled(HAL_REVPHHandle handle, |
| 113 | int32_t* status) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 114 | auto pcm = pcmHandles->Get(handle); |
| 115 | if (pcm == nullptr) { |
| 116 | *status = HAL_HANDLE_ERROR; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 117 | return; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 118 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 119 | SimREVPHData[pcm->module].compressorConfigType = |
| 120 | HAL_REVPHCompressorConfigType_kDisabled; |
| 121 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 122 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 123 | void HAL_SetREVPHClosedLoopControlDigital(HAL_REVPHHandle handle, |
| 124 | int32_t* status) { |
| 125 | auto pcm = pcmHandles->Get(handle); |
| 126 | if (pcm == nullptr) { |
| 127 | *status = HAL_HANDLE_ERROR; |
| 128 | return; |
| 129 | } |
| 130 | SimREVPHData[pcm->module].compressorConfigType = |
| 131 | HAL_REVPHCompressorConfigType_kDigital; |
| 132 | } |
| 133 | |
| 134 | void HAL_SetREVPHClosedLoopControlAnalog(HAL_REVPHHandle handle, |
| 135 | double minAnalogVoltage, |
| 136 | double maxAnalogVoltage, |
| 137 | int32_t* status) { |
| 138 | auto pcm = pcmHandles->Get(handle); |
| 139 | if (pcm == nullptr) { |
| 140 | *status = HAL_HANDLE_ERROR; |
| 141 | return; |
| 142 | } |
| 143 | SimREVPHData[pcm->module].compressorConfigType = |
| 144 | HAL_REVPHCompressorConfigType_kAnalog; |
| 145 | } |
| 146 | |
| 147 | void HAL_SetREVPHClosedLoopControlHybrid(HAL_REVPHHandle handle, |
| 148 | double minAnalogVoltage, |
| 149 | double maxAnalogVoltage, |
| 150 | int32_t* status) { |
| 151 | auto pcm = pcmHandles->Get(handle); |
| 152 | if (pcm == nullptr) { |
| 153 | *status = HAL_HANDLE_ERROR; |
| 154 | return; |
| 155 | } |
| 156 | SimREVPHData[pcm->module].compressorConfigType = |
| 157 | HAL_REVPHCompressorConfigType_kHybrid; |
| 158 | } |
| 159 | |
| 160 | HAL_REVPHCompressorConfigType HAL_GetREVPHCompressorConfig( |
| 161 | HAL_REVPHHandle handle, int32_t* status) { |
| 162 | auto pcm = pcmHandles->Get(handle); |
| 163 | if (pcm == nullptr) { |
| 164 | *status = HAL_HANDLE_ERROR; |
| 165 | return HAL_REVPHCompressorConfigType_kDisabled; |
| 166 | } |
| 167 | return SimREVPHData[pcm->module].compressorConfigType; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | HAL_Bool HAL_GetREVPHPressureSwitch(HAL_REVPHHandle handle, int32_t* status) { |
| 171 | auto pcm = pcmHandles->Get(handle); |
| 172 | if (pcm == nullptr) { |
| 173 | *status = HAL_HANDLE_ERROR; |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | return SimREVPHData[pcm->module].pressureSwitch; |
| 178 | } |
| 179 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 180 | double HAL_GetREVPHAnalogVoltage(HAL_REVPHHandle handle, int32_t channel, |
| 181 | int32_t* status) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | double HAL_GetREVPHCompressorCurrent(HAL_REVPHHandle handle, int32_t* status) { |
| 186 | auto pcm = pcmHandles->Get(handle); |
| 187 | if (pcm == nullptr) { |
| 188 | *status = HAL_HANDLE_ERROR; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | return SimREVPHData[pcm->module].compressorCurrent; |
| 193 | } |
| 194 | |
| 195 | int32_t HAL_GetREVPHSolenoids(HAL_REVPHHandle handle, int32_t* status) { |
| 196 | auto pcm = pcmHandles->Get(handle); |
| 197 | if (pcm == nullptr) { |
| 198 | *status = HAL_HANDLE_ERROR; |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | std::scoped_lock lock{pcm->lock}; |
| 203 | auto& data = SimREVPHData[pcm->module].solenoidOutput; |
| 204 | uint8_t ret = 0; |
| 205 | for (int i = 0; i < kNumREVPHChannels; i++) { |
| 206 | ret |= (data[i] << i); |
| 207 | } |
| 208 | return ret; |
| 209 | } |
| 210 | void HAL_SetREVPHSolenoids(HAL_REVPHHandle handle, int32_t mask, int32_t values, |
| 211 | int32_t* status) { |
| 212 | auto pcm = pcmHandles->Get(handle); |
| 213 | if (pcm == nullptr) { |
| 214 | *status = HAL_HANDLE_ERROR; |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | auto& data = SimREVPHData[pcm->module].solenoidOutput; |
| 219 | std::scoped_lock lock{pcm->lock}; |
| 220 | for (int i = 0; i < kNumREVPHChannels; i++) { |
| 221 | auto indexMask = (1 << i); |
| 222 | if ((mask & indexMask) != 0) { |
| 223 | data[i] = (values & indexMask) != 0; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void HAL_FireREVPHOneShot(HAL_REVPHHandle handle, int32_t index, int32_t durMs, |
| 229 | int32_t* status) {} |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 230 | |
| 231 | double HAL_GetREVPHVoltage(HAL_REVPHHandle handle, int32_t* status) { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | double HAL_GetREVPH5VVoltage(HAL_REVPHHandle handle, int32_t* status) { |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | double HAL_GetREVPHSolenoidCurrent(HAL_REVPHHandle handle, int32_t* status) { |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | double HAL_GetREVPHSolenoidVoltage(HAL_REVPHHandle handle, int32_t* status) { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | void HAL_GetREVPHVersion(HAL_REVPHHandle handle, HAL_REVPHVersion* version, |
| 248 | int32_t* status) {} |
| 249 | |
| 250 | void HAL_GetREVPHFaults(HAL_REVPHHandle handle, HAL_REVPHFaults* faults, |
| 251 | int32_t* status) {} |
| 252 | |
| 253 | void HAL_GetREVPHStickyFaults(HAL_REVPHHandle handle, |
| 254 | HAL_REVPHStickyFaults* stickyFaults, |
| 255 | int32_t* status) {} |
| 256 | |
| 257 | void HAL_ClearREVPHStickyFaults(HAL_REVPHHandle handle, int32_t* status) {} |