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 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include "PortsInternal.h" |
| 12 | #include "hal/AnalogTrigger.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | #include "hal/handles/DigitalHandleResource.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
| 15 | namespace hal { |
| 16 | /** |
| 17 | * MXP channels when used as digital output PWM are offset from actual value |
| 18 | */ |
| 19 | constexpr int32_t kMXPDigitalPWMOffset = 6; |
| 20 | |
| 21 | constexpr int32_t kExpectedLoopTiming = 40; |
| 22 | |
| 23 | /** |
| 24 | * kDefaultPwmPeriod is in ms |
| 25 | * |
| 26 | * - 20ms periods (50 Hz) are the "safest" setting in that this works for all |
| 27 | * devices |
| 28 | * - 20ms periods seem to be desirable for Vex Motors |
| 29 | * - 20ms periods are the specified period for HS-322HD servos, but work |
| 30 | * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums |
| 31 | * and get hot; by 5.0ms the hum is nearly continuous |
| 32 | * - 10ms periods work well for Victor 884 |
| 33 | * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed |
| 34 | * controllers. Due to the shipping firmware on the Jaguar, we can't run the |
| 35 | * update period less than 5.05 ms. |
| 36 | * |
| 37 | * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period |
| 38 | * scaling is implemented as an output squelch to get longer periods for old |
| 39 | * devices. |
| 40 | */ |
| 41 | constexpr float kDefaultPwmPeriod = 5.05f; |
| 42 | /** |
| 43 | * kDefaultPwmCenter is the PWM range center in ms |
| 44 | */ |
| 45 | constexpr float kDefaultPwmCenter = 1.5f; |
| 46 | /** |
| 47 | * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint |
| 48 | */ |
| 49 | constexpr int32_t kDefaultPwmStepsDown = 1000; |
| 50 | constexpr int32_t kPwmDisabled = 0; |
| 51 | |
| 52 | struct DigitalPort { |
| 53 | uint8_t channel; |
| 54 | bool configSet = false; |
| 55 | bool eliminateDeadband = false; |
| 56 | int32_t maxPwm = 0; |
| 57 | int32_t deadbandMaxPwm = 0; |
| 58 | int32_t centerPwm = 0; |
| 59 | int32_t deadbandMinPwm = 0; |
| 60 | int32_t minPwm = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 61 | std::string previousAllocation; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort, |
| 65 | kNumDigitalChannels + kNumPWMHeaders>* |
| 66 | digitalChannelHandles; |
| 67 | |
| 68 | /** |
| 69 | * Remap the digital source channel and set the module. |
| 70 | * |
| 71 | * If it's an analog trigger, determine the module from the high order routing |
| 72 | * channel else do normal digital input remapping based on channel number |
| 73 | * (MXP). |
| 74 | */ |
| 75 | bool remapDigitalSource(HAL_Handle digitalSourceHandle, |
| 76 | HAL_AnalogTriggerType analogTriggerType, |
| 77 | uint8_t& channel, uint8_t& module, bool& analogTrigger); |
| 78 | |
| 79 | /** |
| 80 | * Map DIO channel numbers from their physical number (10 to 26) to their |
| 81 | * position in the bit field. |
| 82 | */ |
| 83 | int32_t remapMXPChannel(int32_t channel); |
| 84 | |
| 85 | int32_t remapMXPPWMChannel(int32_t channel); |
| 86 | |
| 87 | int32_t GetDigitalInputChannel(HAL_DigitalHandle handle, int32_t* status); |
| 88 | } // namespace hal |