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 | #pragma once |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <memory> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include <string> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | |
| 12 | #include <wpi/mutex.h> |
| 13 | |
| 14 | #include "PortsInternal.h" |
| 15 | #include "hal/AnalogTrigger.h" |
| 16 | #include "hal/ChipObject.h" |
| 17 | #include "hal/Ports.h" |
| 18 | #include "hal/Types.h" |
| 19 | #include "hal/handles/DigitalHandleResource.h" |
| 20 | #include "hal/handles/HandlesInternal.h" |
| 21 | |
| 22 | namespace hal { |
| 23 | |
| 24 | /** |
| 25 | * MXP channels when used as digital output PWM are offset from actual value |
| 26 | */ |
| 27 | constexpr int32_t kMXPDigitalPWMOffset = 6; |
| 28 | |
| 29 | constexpr int32_t kExpectedLoopTiming = 40; |
| 30 | |
| 31 | /** |
| 32 | * kDefaultPwmPeriod is in ms |
| 33 | * |
| 34 | * - 20ms periods (50 Hz) are the "safest" setting in that this works for all |
| 35 | * devices |
| 36 | * - 20ms periods seem to be desirable for Vex Motors |
| 37 | * - 20ms periods are the specified period for HS-322HD servos, but work |
| 38 | * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums |
| 39 | * and get hot; by 5.0ms the hum is nearly continuous |
| 40 | * - 10ms periods work well for Victor 884 |
| 41 | * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed |
| 42 | * controllers. Due to the shipping firmware on the Jaguar, we can't run the |
| 43 | * update period less than 5.05 ms. |
| 44 | * |
| 45 | * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period |
| 46 | * scaling is implemented as an output squelch to get longer periods for old |
| 47 | * devices. |
| 48 | */ |
| 49 | constexpr double kDefaultPwmPeriod = 5.05; |
| 50 | /** |
| 51 | * kDefaultPwmCenter is the PWM range center in ms |
| 52 | */ |
| 53 | constexpr double kDefaultPwmCenter = 1.5; |
| 54 | /** |
| 55 | * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint |
| 56 | */ |
| 57 | constexpr int32_t kDefaultPwmStepsDown = 1000; |
| 58 | constexpr int32_t kPwmDisabled = 0; |
| 59 | |
| 60 | extern std::unique_ptr<tDIO> digitalSystem; |
| 61 | extern std::unique_ptr<tRelay> relaySystem; |
| 62 | extern std::unique_ptr<tPWM> pwmSystem; |
| 63 | extern std::unique_ptr<tSPI> spiSystem; |
| 64 | |
| 65 | struct DigitalPort { |
| 66 | uint8_t channel; |
| 67 | bool configSet = false; |
| 68 | bool eliminateDeadband = false; |
| 69 | int32_t maxPwm = 0; |
| 70 | int32_t deadbandMaxPwm = 0; |
| 71 | int32_t centerPwm = 0; |
| 72 | int32_t deadbandMinPwm = 0; |
| 73 | int32_t minPwm = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 74 | std::string previousAllocation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort, |
| 78 | kNumDigitalChannels + kNumPWMHeaders>* |
| 79 | digitalChannelHandles; |
| 80 | |
| 81 | extern wpi::mutex digitalDIOMutex; |
| 82 | |
| 83 | /** |
| 84 | * Initialize the digital system. |
| 85 | */ |
| 86 | void initializeDigital(int32_t* status); |
| 87 | |
| 88 | /** |
| 89 | * remap the digital source channel and set the module. |
| 90 | * If it's an analog trigger, determine the module from the high order routing |
| 91 | * channel else do normal digital input remapping based on channel number |
| 92 | * (MXP) |
| 93 | */ |
| 94 | bool remapDigitalSource(HAL_Handle digitalSourceHandle, |
| 95 | HAL_AnalogTriggerType analogTriggerType, |
| 96 | uint8_t& channel, uint8_t& module, bool& analogTrigger); |
| 97 | |
| 98 | /** |
| 99 | * Remap the Digital Channel to map to the bitfield channel of the FPGA |
| 100 | */ |
| 101 | constexpr int32_t remapDigitalChannelToBitfieldChannel(int32_t channel) { |
| 102 | // First 10 are headers |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 103 | if (channel < kNumDigitalHeaders) { |
| 104 | return channel; |
| 105 | // 2nd group of 16 are mxp. So if mxp port, add 6, since they start at 10 |
| 106 | } else if (channel < kNumDigitalMXPChannels) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | return channel + 6; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 108 | // Assume SPI, so remove MXP channels |
| 109 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 110 | return channel - kNumDigitalMXPChannels; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 111 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Map DIO channel numbers from their physical number (10 to 26) to their |
| 116 | * position in the bit field. |
| 117 | */ |
| 118 | int32_t remapMXPChannel(int32_t channel); |
| 119 | |
| 120 | int32_t remapMXPPWMChannel(int32_t channel); |
| 121 | |
| 122 | /** |
| 123 | * Map SPI channel numbers from their physical number (27 to 31) to their |
| 124 | * position in the bit field. |
| 125 | */ |
| 126 | int32_t remapSPIChannel(int32_t channel); |
| 127 | |
| 128 | } // namespace hal |