Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <memory> |
| 13 | |
| 14 | #include "HAL/AnalogTrigger.h" |
| 15 | #include "HAL/ChipObject.h" |
| 16 | #include "HAL/Ports.h" |
| 17 | #include "HAL/Types.h" |
| 18 | #include "HAL/handles/DigitalHandleResource.h" |
| 19 | #include "HAL/handles/HandlesInternal.h" |
| 20 | #include "PortsInternal.h" |
| 21 | |
| 22 | namespace hal { |
| 23 | /** |
| 24 | * MXP channels when used as digital output PWM are offset from actual value |
| 25 | */ |
| 26 | constexpr int32_t kMXPDigitalPWMOffset = 6; |
| 27 | |
| 28 | constexpr int32_t kExpectedLoopTiming = 40; |
| 29 | |
| 30 | /** |
| 31 | * kDefaultPwmPeriod is in ms |
| 32 | * |
| 33 | * - 20ms periods (50 Hz) are the "safest" setting in that this works for all |
| 34 | * devices |
| 35 | * - 20ms periods seem to be desirable for Vex Motors |
| 36 | * - 20ms periods are the specified period for HS-322HD servos, but work |
| 37 | * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums |
| 38 | * and get hot; by 5.0ms the hum is nearly continuous |
| 39 | * - 10ms periods work well for Victor 884 |
| 40 | * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed |
| 41 | * controllers. Due to the shipping firmware on the Jaguar, we can't run the |
| 42 | * update period less than 5.05 ms. |
| 43 | * |
| 44 | * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period |
| 45 | * scaling is implemented as an output squelch to get longer periods for old |
| 46 | * devices. |
| 47 | */ |
| 48 | constexpr double kDefaultPwmPeriod = 5.05; |
| 49 | /** |
| 50 | * kDefaultPwmCenter is the PWM range center in ms |
| 51 | */ |
| 52 | constexpr double kDefaultPwmCenter = 1.5; |
| 53 | /** |
| 54 | * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint |
| 55 | */ |
| 56 | constexpr int32_t kDefaultPwmStepsDown = 1000; |
| 57 | constexpr int32_t kPwmDisabled = 0; |
| 58 | |
| 59 | // Create a mutex to protect changes to the DO PWM config |
| 60 | extern priority_recursive_mutex digitalPwmMutex; |
| 61 | |
| 62 | extern std::unique_ptr<tDIO> digitalSystem; |
| 63 | extern std::unique_ptr<tRelay> relaySystem; |
| 64 | extern std::unique_ptr<tPWM> pwmSystem; |
| 65 | extern std::unique_ptr<tSPI> spiSystem; |
| 66 | |
| 67 | struct DigitalPort { |
| 68 | uint8_t channel; |
| 69 | bool configSet = false; |
| 70 | bool eliminateDeadband = false; |
| 71 | int32_t maxPwm = 0; |
| 72 | int32_t deadbandMaxPwm = 0; |
| 73 | int32_t centerPwm = 0; |
| 74 | int32_t deadbandMinPwm = 0; |
| 75 | int32_t minPwm = 0; |
| 76 | }; |
| 77 | |
| 78 | extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort, |
| 79 | kNumDigitalChannels + kNumPWMHeaders> |
| 80 | digitalChannelHandles; |
| 81 | |
| 82 | void initializeDigital(int32_t* status); |
| 83 | bool remapDigitalSource(HAL_Handle digitalSourceHandle, |
| 84 | HAL_AnalogTriggerType analogTriggerType, |
| 85 | uint8_t& channel, uint8_t& module, bool& analogTrigger); |
| 86 | int32_t remapSPIChannel(int32_t channel); |
| 87 | int32_t remapMXPPWMChannel(int32_t channel); |
| 88 | int32_t remapMXPChannel(int32_t channel); |
| 89 | } // namespace hal |